Skip to content

Commit

Permalink
Github Issue #91: Improvement in the behaviour of the selection remai…
Browse files Browse the repository at this point in the history
…ning consistent when scrolling is performed. TextBlock selections are now constant. Formatted texts needs to be improved
  • Loading branch information
AngryFender committed Jan 7, 2024
1 parent 4b214be commit e5ca9dc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
33 changes: 25 additions & 8 deletions mkedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,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) emit undoStackUndoSignal(); undoData.undoRedoSkip = true; fileSaveNow(); return;
case Qt::Key_Y: if( event->modifiers() == Qt::CTRL) emit undoStackRedoSignal(); undoData.undoRedoSkip = true; fileSaveNow(); return;
case Qt::Key_Z: if( event->modifiers() == Qt::CTRL) emit undoStackUndoSignal(); undoData.undoRedoSkip = true; fileSaveNow(); scrollValueUpdateHandle(undoData.scrollValue);return;
case Qt::Key_Y: if( event->modifiers() == Qt::CTRL) emit undoStackRedoSignal(); undoData.undoRedoSkip = true; fileSaveNow(); ;return;

default: break;
}
Expand Down Expand Up @@ -647,29 +647,46 @@ void MkEdit::cursorPositionChangedHandle()
QTextCursor cursor = this->textCursor();

if(!cursor.hasSelection()){
selectRange.hasSelection = false;
selectRange.start = -1;
selectRange.end = -1;
selectRange.currentposInBlock = cursor.positionInBlock();
selectRange.currentBlockPos = cursor.block().position();
selectRange.isCursorCaculated = false;
selectRange.selectionFirstStartPos = NO_SELECTION_POS;
}else{
selectRange.hasSelection = true;
selectRange.start = cursor.selectionStart();
selectRange.end = cursor.selectionEnd();
if(NO_SELECTION_POS == selectRange.selectionFirstStartPos){
selectRange.selectionFirstStartPos = cursor.selectionStart();
selectRange.selectionFirstStartBlock = cursor.blockNumber();
selectRange.selectionFirstStartPosInBlock = cursor.positionInBlock();
}
}

disconnectSignals();
emit cursorPosChanged( textCursor().hasSelection(), currentBlockNumber, getVisibleRect(), &selectRange);
connectSignals();

//insert cursor inbetween the formatted words since after symbols are inserted the positions are shifted
if(!cursor.hasSelection() && selectRange.isCursorCaculated){
cursor.setPosition(this->document()->findBlockByNumber(selectRange.currentBlockNo).position());
cursor.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor);
cursor.setPosition(this->document()->findBlockByNumber(selectRange.currentBlockNo).position()+selectRange.currentposInBlock);
this->setTextCursor(cursor);
}

for(int rep = 0; rep < selectRange.currentposInBlock; rep++){
cursor.movePosition(QTextCursor::NextCharacter,QTextCursor::MoveAnchor);
//make sure selection works regardless of the formatting used
if(selectRange.hasSelection){
if(currentCharacterNumber > selectRange.selectionFirstStartPos){
// cursor.setPosition( this->document()->findBlockByNumber(selectRange.selectionFirstStartBlock).position() + selectRange.selectionFirstStartPosInBlock);
cursor.setPosition(selectRange.selectionFirstStartPos);
cursor.setPosition(currentCharacterNumber,QTextCursor::KeepAnchor);
}else{
cursor.clearSelection();
cursor.setPosition( this->document()->findBlockByNumber(selectRange.selectionFirstStartBlock).position() + selectRange.selectionFirstStartPosInBlock + 1);
cursor.setPosition(currentCharacterNumber,QTextCursor::KeepAnchor);
}
this->setTextCursor(cursor);
}
//emit cursorUpdate(this->textCursor().blockNumber(), this->textCursor().positionInBlock());
connectSignals();
}
}
1 change: 0 additions & 1 deletion mkedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class MkEdit : public QTextEdit
QColor codeBlockColor;
int widthCodeBlock;
QPen penCodeBlock;
int savedBlockNumber;
int savedCharacterNumber;
UndoData undoData;
HighlightColor syntaxColor;
Expand Down
4 changes: 4 additions & 0 deletions mktextdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ class FormatCollection{
struct SelectRange{
int start = NO_SELECTION_POS;
int end = NO_SELECTION_POS;
bool hasSelection = false;
int currentposInBlock = NO_SELECTION_POS;
int currentBlockPos = NO_SELECTION_POS;
int currentBlockNo = NO_SELECTION_POS;
int blockStart = NO_SELECTION_POS;
int posInBlockStart = NO_SELECTION_POS;
int blockEnd = NO_SELECTION_POS;
int posInBlockEnd = NO_SELECTION_POS;
int selectionFirstStartPos = NO_SELECTION_POS;
int selectionFirstStartBlock = NO_SELECTION_POS;
int selectionFirstStartPosInBlock = NO_SELECTION_POS;
bool isCursorCaculated = false;
};

Expand Down

0 comments on commit e5ca9dc

Please sign in to comment.