From c74ba9cc9cadc0ebd7a63b157da5960a749afe7b Mon Sep 17 00:00:00 2001 From: AngryFender Date: Mon, 1 Jan 2024 19:57:11 +0000 Subject: [PATCH] Github Issue #100: Undo stack is now saved as a property of the MkTextDocument. So each document now has its own independent undo stack --- CMakeLists.txt | 2 -- CatchTests/CMakeLists.txt | 1 - editcommand.cpp | 43 ----------------------------- editcommand.h | 46 ------------------------------- mkedit.cpp | 22 ++------------- mkedit.h | 11 ++++---- mktextdocument.cpp | 58 ++++++++++++++++++++++++++++++++++++++- mktextdocument.h | 47 +++++++++++++++++++++++++++++-- views_handler.cpp | 25 +++++++++++++---- 9 files changed, 129 insertions(+), 126 deletions(-) delete mode 100644 editcommand.cpp delete mode 100644 editcommand.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f489d6a..85bb709 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,8 +31,6 @@ set(PROJECT_SOURCES blockdata.cpp linedata.h linedata.cpp - editcommand.h - editcommand.cpp theme.h navigationview.h navigationview.cpp diff --git a/CatchTests/CMakeLists.txt b/CatchTests/CMakeLists.txt index 377eeb7..158f5f9 100644 --- a/CatchTests/CMakeLists.txt +++ b/CatchTests/CMakeLists.txt @@ -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 diff --git a/editcommand.cpp b/editcommand.cpp deleted file mode 100644 index ed19f7c..0000000 --- a/editcommand.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "editcommand.h" - -EditCommand::EditCommand(UndoData &data) -{ - this->view = data.view; - this->doc = dynamic_cast(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); - } -} diff --git a/editcommand.h b/editcommand.h deleted file mode 100644 index 03a7ca6..0000000 --- a/editcommand.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef EDITCOMMAND_H -#define EDITCOMMAND_H - -#include -#include -#include -#include -#include - -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 // EDITCOMMAND_H diff --git a/mkedit.cpp b/mkedit.cpp index a10478d..f29f569 100644 --- a/mkedit.cpp +++ b/mkedit.cpp @@ -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){ @@ -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; } @@ -225,7 +224,7 @@ void MkEdit::postUndoSetup() if(!undoData.undoRedoSkip){ EditCommand *edit = new EditCommand(undoData); - undoStack.push(edit); + emit undoStackPushSignal(edit) ; } } @@ -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(); diff --git a/mkedit.h b/mkedit.h index b8d736d..f471648 100644 --- a/mkedit.h +++ b/mkedit.h @@ -12,8 +12,6 @@ #include #include #include -#include -#include #include #define FILE_SAVE_TIMEOUT 300 @@ -67,7 +65,6 @@ class MkEdit : public QTextEdit QPen penCodeBlock; int savedBlockNumber; int savedCharacterNumber; - QUndoStack undoStack; UndoData undoData; HighlightColor syntaxColor; QTimer fileSaveTimer; @@ -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: @@ -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 diff --git a/mktextdocument.cpp b/mktextdocument.cpp index 090ea3e..419fd95 100644 --- a/mktextdocument.cpp +++ b/mktextdocument.cpp @@ -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) @@ -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() { @@ -1177,3 +1191,45 @@ QString MkTextDocument::numberListGetNextNumber(const QString &text) } return ""; } + +EditCommand::EditCommand(UndoData &data) +{ + this->view = data.view; + this->doc = dynamic_cast(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); + } +} diff --git a/mktextdocument.h b/mktextdocument.h index fdc9e2b..f534dce 100644 --- a/mktextdocument.h +++ b/mktextdocument.h @@ -16,6 +16,9 @@ #include #include #include +#include +#include +#include class FormatCollection{ @@ -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; @@ -156,6 +163,8 @@ public slots: QRegularExpression regexNumbering; QQueue savedBlocks; + QUndoStack undoStack; + QVector checkMarkPositions; QVector> linkPositions; @@ -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 diff --git a/views_handler.cpp b/views_handler.cpp index 70b9ffc..0ffd1cd 100644 --- a/views_handler.cpp +++ b/views_handler.cpp @@ -193,9 +193,6 @@ void ViewsHandler::connectDocument() QObject::connect(viewText,&MkEdit::setMarkdownStatus, currentDocument.data(),&MkTextDocument::setMarkdownHandle); - QObject::connect(currentDocument.data(),&MkTextDocument::clearUndoStack, - viewText,&MkEdit::clearUndoStackHandle); - QObject::connect(viewText,&MkEdit::drawTextBlocks, currentDocument.data(),&MkTextDocument::drawTextBlocksHandler); @@ -211,6 +208,15 @@ void ViewsHandler::connectDocument() QObject::connect(viewText,&MkEdit::cursorUpdate, currentDocument.data(),&MkTextDocument::cursorUpdateHandle); + QObject::connect(viewText,&MkEdit::undoStackPushSignal, + currentDocument.data(),&MkTextDocument::undoStackPush); + + QObject::connect(viewText,&MkEdit::undoStackUndoSignal, + currentDocument.data(),&MkTextDocument::undoStackUndo); + + QObject::connect(viewText,&MkEdit::undoStackRedoSignal, + currentDocument.data(),&MkTextDocument::undoStackRedo); + } void ViewsHandler::disconnectDocument() @@ -245,9 +251,6 @@ void ViewsHandler::disconnectDocument() QObject::disconnect(viewText,&MkEdit::setMarkdownStatus, currentDocument.data(),&MkTextDocument::setMarkdownHandle); - QObject::disconnect(currentDocument.data(),&MkTextDocument::clearUndoStack, - viewText,&MkEdit::clearUndoStackHandle); - QObject::disconnect(viewText,&MkEdit::drawTextBlocks, currentDocument.data(),&MkTextDocument::drawTextBlocksHandler); @@ -263,6 +266,16 @@ void ViewsHandler::disconnectDocument() QObject::disconnect(viewText,&MkEdit::cursorUpdate, currentDocument.data(),&MkTextDocument::cursorUpdateHandle); + QObject::disconnect(viewText,&MkEdit::undoStackPushSignal, + currentDocument.data(),&MkTextDocument::undoStackPush); + + QObject::disconnect(viewText,&MkEdit::undoStackUndoSignal, + currentDocument.data(),&MkTextDocument::undoStackUndo); + + QObject::disconnect(viewText,&MkEdit::undoStackRedoSignal, + currentDocument.data(),&MkTextDocument::undoStackRedo); + + } QString ViewsHandler::getFileContent(QFile& file)