Skip to content

Commit

Permalink
Github Issue #117: Using disconnection and connection of cursor posit…
Browse files Browse the repository at this point in the history
…ion 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
  • Loading branch information
AngryFender committed Jan 5, 2024
1 parent dab963c commit cb6482a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 48 deletions.
57 changes: 34 additions & 23 deletions mkedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -124,19 +126,17 @@ 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())<MAXIMUM_FONT_SIZE)
this->zoomIn();
} else {
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);
Expand Down Expand Up @@ -238,18 +238,24 @@ void MkEdit::clearMkEffects()
{
disconnectSignals();
undoData.scrollValue = this->verticalScrollBar()->sliderPosition(); //this is importantp
emit connectionDrawTextBlock(false);
emit removeAllMkData(this->textCursor().blockNumber());
if(!fileSaveTimer.isActive()){
preUndoSetup();
}
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();
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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;

}
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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();

Expand All @@ -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());
Expand Down
6 changes: 5 additions & 1 deletion mkedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -64,6 +67,7 @@ class MkEdit : public QTextEdit
int widthCodeBlock;
QPen penCodeBlock;
int savedBlockNumber;
int savedCharacterNumber;
UndoData undoData;
HighlightColor syntaxColor;
QTimer fileSaveTimer;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions mktextdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"<<savedBlocks.count();
// continue;
// }

QTextBlockUserData* data =block.userData();
if(data == nullptr){
block = block.next();
Expand All @@ -862,7 +856,6 @@ void MkTextDocument::showMKSymbolsFromSavedBlocks(QRect *rect, int cursorBlockNo
if(blockData->getStatus()!=BlockData::content)
{
showSymbols(block, CODEBLOCK_SYMBOL);
//showSymbols(block, CODEBLOCK_SYMBOL);
}
}
else{
Expand Down
16 changes: 0 additions & 16 deletions views_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -543,19 +540,6 @@ void ViewsHandler::setVaultPathHandler()
qDebug()<<"New Vault Path "<<newPath;
}

void ViewsHandler::connectionDrawTextBlockHandler(bool connection)
{
if(connection) {

QObject::connect(viewText,&MkEdit::drawTextBlocks,
currentDocument.data(),&MkTextDocument::drawTextBlocksHandler);

}else{
QObject::disconnect(viewText,&MkEdit::drawTextBlocks,
currentDocument.data(),&MkTextDocument::drawTextBlocksHandler);
}
}

void ViewsHandler::checkIfCursorInBlockHandler(bool &isBlock, QTextCursor &cursor)
{
BlockData* blockData = dynamic_cast<BlockData*>(cursor.block().userData());
Expand Down
1 change: 0 additions & 1 deletion views_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down

0 comments on commit cb6482a

Please sign in to comment.