From cb6482a3698a83910c8ad90bf484a19655ecc5b9 Mon Sep 17 00:00:00 2001 From: AngryFender Date: Fri, 5 Jan 2024 22:37:02 +0000 Subject: [PATCH] Github Issue #117: Using disconnection and connection of cursor position changed event from being triggered when the logic is actually editing the file. Also improved the cursor position handling logic to being triggered for every character change rather than text block change --- mkedit.cpp | 57 +++++++++++++++++++++++++++------------------- mkedit.h | 6 ++++- mktextdocument.cpp | 7 ------ views_handler.cpp | 16 ------------- views_handler.h | 1 - 5 files changed, 39 insertions(+), 48 deletions(-) diff --git a/mkedit.cpp b/mkedit.cpp index 380f67a..90ece56 100644 --- a/mkedit.cpp +++ b/mkedit.cpp @@ -8,7 +8,7 @@ MkEdit::MkEdit(QWidget *parent):QTextEdit(parent){ fileSaveTimer.setInterval(FILE_SAVE_TIMEOUT); regexUrl.setPattern("(https?|ftp|file)://[\\w\\d._-]+(?:\\.[\\w\\d._-]+)+[\\w\\d._-]*(?:(?:/[\\w\\d._-]+)*/?)?(?:\\?[\\w\\d_=-]+(?:&[\\w\\d_=-]+)*)?(?:#[\\w\\d_-]+)?"); regexFolderFile.setPattern("[a-zA-Z]:[\\\\/](?:[^\\\\/]+[\\\\/])*([^\\\\/]+\\.*)"); - savedBlockNumber= -1; + savedCharacterNumber = -1; this->setContextMenuPolicy(Qt::CustomContextMenu); undoAction.setText("Undo Ctrl+Z"); @@ -44,6 +44,7 @@ MkEdit::MkEdit(QWidget *parent):QTextEdit(parent){ void MkEdit::initialialCursorPosition() { + disconnectSignals(); QTextCursor cursor = this->textCursor(); cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); this->setTextCursor(cursor); @@ -57,6 +58,7 @@ void MkEdit::initialialCursorPosition() selectRange.end = this->textCursor().selectionEnd(); emit cursorPosChanged( textCursor().hasSelection(), savedBlockNumber , getVisibleRect(), &selectRange); + connectSignals(); } void MkEdit::paintEvent(QPaintEvent *e) @@ -124,9 +126,8 @@ void MkEdit::wheelEvent(QWheelEvent *e) { if (e->modifiers() == Qt::ControlModifier) { int zoomDelta = e->angleDelta().y(); - emit connectionDrawTextBlock(false); + disconnectSignals(); emit removeAllMkData(this->textCursor().blockNumber()); - this->blockSignals(true); if (zoomDelta > 0) { if((this->currentFont().pointSizeF())zoomIn(); @@ -134,9 +135,8 @@ void MkEdit::wheelEvent(QWheelEvent *e) if((this->currentFont().pointSizeF())>MINIMUM_FONT_SIZE) this->zoomOut(); } - this->blockSignals(false); emit applyAllMkData( this->textCursor().hasSelection(), this->textCursor().blockNumber(), undoData.selectAll, getVisibleRect()); - emit connectionDrawTextBlock(true); + connectSignals(); this->ensureCursorVisible(); }else{ QTextEdit::wheelEvent(e); @@ -238,7 +238,6 @@ void MkEdit::clearMkEffects() { disconnectSignals(); undoData.scrollValue = this->verticalScrollBar()->sliderPosition(); //this is importantp - emit connectionDrawTextBlock(false); emit removeAllMkData(this->textCursor().blockNumber()); if(!fileSaveTimer.isActive()){ preUndoSetup(); @@ -246,10 +245,17 @@ void MkEdit::clearMkEffects() fileSaveTimer.start(); } +void MkEdit::removeAllMkDataFunc(int blockNumber) +{ + disconnectSignals(); + emit removeAllMkData(blockNumber); + connectSignals(); +} + void MkEdit::applyMkEffects(const bool scroll) { + disconnectSignals(); emit applyAllMkData( this->textCursor().hasSelection(), this->textCursor().blockNumber(), undoData.selectAll, getVisibleRect()); - emit connectionDrawTextBlock(true); if(scroll){ this->verticalScrollBar()->setSliderPosition(undoData.scrollValue); this->ensureCursorVisible(); @@ -260,23 +266,18 @@ void MkEdit::applyMkEffects(const bool scroll) void MkEdit::fileSaveNow() { fileSaveTimer.stop(); - disconnectSignals(); postUndoSetup(); emit fileSave(); applyMkEffects(); - connectSignals(); } void MkEdit::fileSaveWithScroll(const bool scroll) { fileSaveTimer.stop(); - disconnectSignals(); - emit connectionDrawTextBlock(false); - emit removeAllMkData(this->textCursor().blockNumber()); + removeAllMkDataFunc(this->textCursor().blockNumber()); postUndoSetup(); emit fileSave(); applyMkEffects(scroll); - connectSignals(); } bool MkEdit::isMouseOnCheckBox(QMouseEvent *e) @@ -306,8 +307,7 @@ bool MkEdit::isMouseOnCheckBox(QMouseEvent *e) rect.setWidth(width); if(rect.contains(pointer)){ int pos = (*it); - emit connectionDrawTextBlock(false); - emit removeAllMkData(this->textCursor().blockNumber()); + removeAllMkDataFunc(this->textCursor().blockNumber()); preUndoSetup(); applyMkEffects(false); emit pushCheckBox(pos); @@ -328,14 +328,12 @@ bool MkEdit::isMouseOnCheckBox(QMouseEvent *e) rect.setWidth(linkTextWidth); if(rect.contains(pointer)){ int pos = (*it).first; - emit connectionDrawTextBlock(false); - emit removeAllMkData(this->textCursor().blockNumber()); - preUndoSetup(); + removeAllMkDataFunc(this->textCursor().blockNumber()); applyMkEffects(false); + connectSignals(); emit pushLink(pos); fileSaveWithScroll(false); return true; - } } @@ -436,10 +434,10 @@ void MkEdit::insertFromMimeData(const QMimeData *source) { bool isBlock = false; QTextCursor cursor = this->textCursor(); + disconnectSignals(); emit checkIfCursorInBlock(isBlock,cursor); if(!isBlock){ - emit connectionDrawTextBlock(false); emit removeAllMkData(this->textCursor().blockNumber()); preUndoSetup(); @@ -470,7 +468,7 @@ void MkEdit::insertFromMimeData(const QMimeData *source) postUndoSetup(); emit fileSave(); emit applyAllMkData( this->textCursor().hasSelection(), this->textCursor().blockNumber(), undoData.selectAll, getVisibleRect()); - emit connectionDrawTextBlock(true); + connectSignals(); } void MkEdit::mousePressEvent(QMouseEvent *e) @@ -602,10 +600,19 @@ void MkEdit::setKeywordColor(const QColor &color) emit syntaxColorUpdate(syntaxColor); } +void MkEdit::setDocument(QTextDocument *document) +{ + disconnectSignals(); + QTextEdit::setDocument(document); + connectSignals(); +} + void MkEdit::scrollValueUpdateHandle(int value) { int currentBlockNumber = textCursor().blockNumber(); + disconnectSignals(); emit drawTextBlocks(textCursor().hasSelection(), currentBlockNumber,undoData.selectAll, getVisibleRect(), &selectRange); + connectSignals(); } void MkEdit::fileSaveHandle() @@ -632,8 +639,10 @@ void MkEdit::diableMarkdown_internal() void MkEdit::cursorPositionChangedHandle() { int currentBlockNumber = textCursor().blockNumber(); - if(savedBlockNumber != currentBlockNumber){ - savedBlockNumber = currentBlockNumber; + int currentCharacterNumber = textCursor().positionInBlock() + currentBlockNumber; + + if(savedCharacterNumber != currentCharacterNumber){ + savedCharacterNumber = currentCharacterNumber; QTextCursor cursor = this->textCursor(); @@ -648,7 +657,9 @@ void MkEdit::cursorPositionChangedHandle() selectRange.end = cursor.selectionEnd(); } + disconnectSignals(); emit cursorPosChanged( textCursor().hasSelection(), currentBlockNumber, getVisibleRect(), &selectRange); + connectSignals(); if(!cursor.hasSelection() && selectRange.isCursorCaculated){ cursor.setPosition(this->document()->findBlockByNumber(selectRange.currentBlockNo).position()); diff --git a/mkedit.h b/mkedit.h index 2a812e9..f87c18a 100644 --- a/mkedit.h +++ b/mkedit.h @@ -53,6 +53,9 @@ class MkEdit : public QTextEdit void setQuoteColor(const QColor& color); void setKeywordColor(const QColor& color); + void setDocument(QTextDocument *document); + + protected: void insertFromMimeData(const QMimeData *source) override; void mousePressEvent(QMouseEvent *e) override; @@ -64,6 +67,7 @@ class MkEdit : public QTextEdit int widthCodeBlock; QPen penCodeBlock; int savedBlockNumber; + int savedCharacterNumber; UndoData undoData; HighlightColor syntaxColor; QTimer fileSaveTimer; @@ -92,6 +96,7 @@ class MkEdit : public QTextEdit QRect getVisibleRect(); void clearMkEffects(); + void removeAllMkDataFunc(int blockNumber); void applyMkEffects(const bool scroll = true); void fileSaveNow(); void fileSaveWithScroll(const bool scroll = true); @@ -124,7 +129,6 @@ private slots: void smartSelection(int blockNumber, QTextCursor &cursor); void drawTextBlocks(bool hasSelection, int blockNumber, bool showAll, QRect rect, SelectRange *selectRange); - void connectionDrawTextBlock(bool connect); void removeAllMkData(int currentBlockNo); void applyAllMkData(bool hasSelection, int blockNumber, bool showAll, QRect rect); void blockColorChanged(const QColor& color); diff --git a/mktextdocument.cpp b/mktextdocument.cpp index 04713cf..2fe748b 100644 --- a/mktextdocument.cpp +++ b/mktextdocument.cpp @@ -841,15 +841,9 @@ void MkTextDocument::drawTextBlocksHandler(bool hasSelection, int blockNumber, b void MkTextDocument::showMKSymbolsFromSavedBlocks(QRect *rect, int cursorBlockNo) { -// QAbstractTextDocumentLayout* layout = this->documentLayout(); while(!savedBlocks.empty()){ QTextBlock block = savedBlocks.takeFirst(); -// if((optimiseScrolling)&&(rect!=nullptr)&&( layout->blockBoundingRect(block).bottom() < (rect->bottom()+40) && layout->blockBoundingRect(block).top() > (rect->top()-15))){ -// qDebug()<<"optimise container size"<getStatus()!=BlockData::content) { showSymbols(block, CODEBLOCK_SYMBOL); - //showSymbols(block, CODEBLOCK_SYMBOL); } } else{ diff --git a/views_handler.cpp b/views_handler.cpp index b09d834..31b4dc1 100644 --- a/views_handler.cpp +++ b/views_handler.cpp @@ -152,9 +152,6 @@ void ViewsHandler::initConnection() QObject::connect(viewTree, &NavigationView::setVaultPath, this,&ViewsHandler::setVaultPathHandler); - QObject::connect(viewText, &MkEdit::connectionDrawTextBlock, - this,&ViewsHandler::connectionDrawTextBlockHandler); - QObject::connect(viewText, &MkEdit::checkIfCursorInBlock, this, &ViewsHandler::checkIfCursorInBlockHandler); connectDocument(); @@ -543,19 +540,6 @@ void ViewsHandler::setVaultPathHandler() qDebug()<<"New Vault Path "<(cursor.block().userData()); diff --git a/views_handler.h b/views_handler.h index 950b3ef..b264046 100644 --- a/views_handler.h +++ b/views_handler.h @@ -121,7 +121,6 @@ private slots: void showSettingsBtn(); void fileRenamedHandler(const QString& newName, const QString& oldName, const QModelIndex& index); void setVaultPathHandler(); - void connectionDrawTextBlockHandler(bool connection); void checkIfCursorInBlockHandler(bool &isBlock, QTextCursor &cursor); };