Skip to content

Commit

Permalink
Github Issue #100: Undo stack is now saved as a property of the MkTex…
Browse files Browse the repository at this point in the history
…tDocument. So each document now has its own independent undo stack
  • Loading branch information
AngryFender committed Jan 1, 2024
1 parent c71f104 commit c74ba9c
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 126 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ set(PROJECT_SOURCES
blockdata.cpp
linedata.h
linedata.cpp
editcommand.h
editcommand.cpp
theme.h
navigationview.h
navigationview.cpp
Expand Down
1 change: 0 additions & 1 deletion CatchTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ add_executable(${PROJECT_NAME}
${CMAKE_SOURCE_DIR}/linedata.cpp
${CMAKE_SOURCE_DIR}/blockdata.cpp
${CMAKE_SOURCE_DIR}/formatdata.cpp
${CMAKE_SOURCE_DIR}/editcommand.cpp
${CMAKE_SOURCE_DIR}/mktextdocument.cpp
${CMAKE_SOURCE_DIR}/recentfilesdialog.cpp

Expand Down
43 changes: 0 additions & 43 deletions editcommand.cpp

This file was deleted.

46 changes: 0 additions & 46 deletions editcommand.h

This file was deleted.

22 changes: 3 additions & 19 deletions mkedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ void MkEdit::keyPressEvent(QKeyEvent *event)
case Qt::Key_Left:
case Qt::Key_Down:
case Qt::Key_Alt: QTextEdit::keyPressEvent(event);return;
case Qt::Key_V:
case Qt::Key_C: if( event->modifiers() == Qt::CTRL) {QTextEdit::keyPressEvent(event);return;}break;
case Qt::Key_S: if( event->modifiers() == Qt::CTRL) {smartSelectionSetup(); return;}break;
case Qt::Key_Tab: if( event->modifiers() == Qt::NoModifier){
Expand All @@ -173,8 +172,8 @@ void MkEdit::keyPressEvent(QKeyEvent *event)
case Qt::Key_Space: fileSaveNow(); return;
case Qt::Key_QuoteLeft: quoteLeftKey(); fileSaveNow(); return;
case Qt::Key_D: if( event->modifiers() == Qt::CTRL) emit duplicateLine(this->textCursor().blockNumber());; fileSaveNow(); return;
case Qt::Key_Z: if( event->modifiers() == Qt::CTRL) undo(); undoData.undoRedoSkip = true; fileSaveNow(); return;
case Qt::Key_Y: if( event->modifiers() == Qt::CTRL) redo(); undoData.undoRedoSkip = true; fileSaveNow(); return;
case Qt::Key_Z: if( event->modifiers() == Qt::CTRL) emit undoStackUndoSignal(); undoData.undoRedoSkip = true; fileSaveNow(); return;
case Qt::Key_Y: if( event->modifiers() == Qt::CTRL) emit undoStackRedoSignal(); undoData.undoRedoSkip = true; fileSaveNow(); return;

default: break;
}
Expand Down Expand Up @@ -225,7 +224,7 @@ void MkEdit::postUndoSetup()

if(!undoData.undoRedoSkip){
EditCommand *edit = new EditCommand(undoData);
undoStack.push(edit);
emit undoStackPushSignal(edit) ;
}
}

Expand Down Expand Up @@ -604,21 +603,6 @@ void MkEdit::setKeywordColor(const QColor &color)
emit syntaxColorUpdate(syntaxColor);
}

void MkEdit::undo()
{
undoStack.undo();
}

void MkEdit::redo()
{
undoStack.redo();
}

void MkEdit::clearUndoStackHandle()
{
undoStack.clear();
}

void MkEdit::scrollValueUpdateHandle(int value)
{
int currentBlockNumber = textCursor().blockNumber();
Expand Down
11 changes: 5 additions & 6 deletions mkedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <QTimer>
#include <mktextdocument.h>
#include <QScrollBar>
#include <QUndoStack>
#include <editcommand.h>
#include <theme.h>

#define FILE_SAVE_TIMEOUT 300
Expand Down Expand Up @@ -67,7 +65,6 @@ class MkEdit : public QTextEdit
QPen penCodeBlock;
int savedBlockNumber;
int savedCharacterNumber;
QUndoStack undoStack;
UndoData undoData;
HighlightColor syntaxColor;
QTimer fileSaveTimer;
Expand Down Expand Up @@ -111,9 +108,6 @@ class MkEdit : public QTextEdit
void deleteContextMenu();
void selectBlock();
void cursorPositionChangedHandle();
void undo();
void redo();
void clearUndoStackHandle();
void scrollValueUpdateHandle(int value);

private slots:
Expand Down Expand Up @@ -150,6 +144,11 @@ private slots:
void setMarkdownStatus(bool state, QRect rect);
void cursorUpdate(const int blockNo, const int characterPos);
void checkIfCursorInBlock(bool &isBlock, QTextCursor &cursor);

void undoStackPushSignal(QUndoCommand *);
void undoStackUndoSignal();
void undoStackRedoSignal();
void undoStackClear();
};

#endif // MKEDIT_H
58 changes: 57 additions & 1 deletion mktextdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void MkTextDocument::setPlainText(const QString &text)

QTextDocument::setPlainText(text);
identifyUserData(false);
emit clearUndoStack();
undoStack.clear();
}

void MkTextDocument::setUndoRedoText(const QString &text)
Expand Down Expand Up @@ -1118,6 +1118,20 @@ void MkTextDocument::cursorUpdateHandle(const int blockNo, const int characterNo
this->characterNo = characterNo;
}

void MkTextDocument::undoStackPush(QUndoCommand *edit)
{
undoStack.push(edit);
}

void MkTextDocument::undoStackUndo()
{
undoStack.undo();
}

void MkTextDocument::undoStackRedo()
{
undoStack.redo();
}

void MkTextDocument::resetFormatLocation()
{
Expand Down Expand Up @@ -1177,3 +1191,45 @@ QString MkTextDocument::numberListGetNextNumber(const QString &text)
}
return "";
}

EditCommand::EditCommand(UndoData &data)
{
this->view = data.view;
this->doc = dynamic_cast<MkTextDocument*>(data.doc);
this->text = data.text;
this->cursorPos = data.cursorPos;
this->scrollValue = data.scrollValue;

this->oldText = data.oldText;
this->oldCursorPos = data.oldCursorPos;
this->oldStartSelection = data.oldStartSelection;
this->oldEndSelection = data.oldEndSelection;
isConstructorRedo = true;

}

void EditCommand::undo()
{
doc->setUndoRedoText(oldText);
QTextCursor textCursor = view->textCursor();
if(oldCursorPos == oldStartSelection){
textCursor.setPosition(oldEndSelection);
textCursor.setPosition(oldCursorPos,QTextCursor::KeepAnchor);
}else{
textCursor.setPosition(oldStartSelection);
textCursor.setPosition(oldEndSelection,QTextCursor::KeepAnchor);
}
view->setTextCursor(textCursor);
}

void EditCommand::redo()
{
if(isConstructorRedo){
isConstructorRedo = false;
}else{
doc->setUndoRedoText(text);
QTextCursor textCursor = view->textCursor();
textCursor.setPosition(cursorPos);
view->setTextCursor(textCursor);
}
}
47 changes: 45 additions & 2 deletions mktextdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include <QAbstractTextDocumentLayout>
#include <QQueue>
#include <QMutex>
#include <QTextEdit>
#include <QUndoCommand>
#include <QUndoStack>


class FormatCollection{
Expand Down Expand Up @@ -113,6 +116,10 @@ public slots:
void setMarkdownHandle(bool state, QRect rect);
void cursorUpdateHandle(const int blockNo, const int characterNo);

void undoStackPush(QUndoCommand *edit);
void undoStackUndo();
void undoStackRedo();

private:
struct CheckingBlock{
QTextBlock start;
Expand Down Expand Up @@ -156,6 +163,8 @@ public slots:
QRegularExpression regexNumbering;
QQueue<QTextBlock> savedBlocks;

QUndoStack undoStack;

QVector<int> checkMarkPositions;
QVector<QPair<int, int>> linkPositions;

Expand Down Expand Up @@ -200,10 +209,44 @@ public slots:
QString numberListGetNextNumber(const QString &text);

void hideMKSymbolsFromDrawingRect(QRect rect,bool hasSelection, int blockNumber, bool showAll,SelectRange * const editSelectRange, const bool clearPushCheckBoxData = true);
signals:
void clearUndoStack();

};

struct UndoData{
QTextEdit *view;
QTextDocument *doc;
QString text;
int cursorPos;
QString oldText;
int oldCursorPos;
int oldStartSelection;
int oldEndSelection;
bool undoRedoSkip;
bool selectAll;
int scrollValue;
};

class EditCommand : public QUndoCommand
{
public:
EditCommand(UndoData &data);

void undo() override;
void redo() override;

private:
QTextEdit *view;
MkTextDocument *doc;
QString text;
int cursorPos;
int scrollValue;

QString oldText;
int oldCursorPos;
int oldStartSelection;
int oldEndSelection;
bool isConstructorRedo;
};


#endif // MKTEXTDOCUMENT_H
Loading

0 comments on commit c74ba9c

Please sign in to comment.