Skip to content

Commit

Permalink
Github Issue #92: Added cursor calculation for checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
AngryFender committed Aug 3, 2023
1 parent 7e9c9c6 commit b865274
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
65 changes: 65 additions & 0 deletions CatchTests/MkEdit_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,68 @@ TEST_CASE("MkEdit move cursor to the middle of the characters of 6 word where fi
REQUIRE(currentPositionOfTextCursorInBlock == cursorPosition + symbolLength);
}

TEST_CASE("MkEdit move cursor to the middle of the characters after checkbox", "[MkTextDocument]")
{
MkTextDocument doc;
MkEdit edit;
int symbolLength = 3+3; // '- [' + 'x] '
int cursorPosition = 4; // ☑che
int minusCheckBoxCount = 1; //
int newLinePos = 21;

doc.setPlainText("- [x] check1 \n **new line**");
edit.setDocument(&doc);

QObject::connect(&edit,&MkEdit::cursorPosChanged,
&doc,&MkTextDocument::cursorPosChangedHandle);


QTextCursor cursor = edit.textCursor();
cursor.setPosition(newLinePos);
edit.setTextCursor(cursor);
QString text = edit.toPlainText();
REQUIRE("☑check1 \n **new line**" == text);

cursor.setPosition(cursorPosition);
edit.setTextCursor(cursor);

QString text1 = edit.toPlainText();
REQUIRE("- [x] check1 \n new line" == text1);

int currentPositionOfTextCursorInBlock = edit.textCursor().positionInBlock();
REQUIRE(currentPositionOfTextCursorInBlock == cursorPosition + symbolLength - minusCheckBoxCount);

}

TEST_CASE("MkEdit move cursor to the middle of the 2nd characters after checkbox", "[MkTextDocument]")
{
MkTextDocument doc;
MkEdit edit;
int symbolLength = 3+3+3+3; // '- [' + 'x] ' + '- [' + 'x] '
int cursorPosition = 5; // ☑☑che
int minusCheckBoxCount = 2; // ☑☑
int newLinePos = 27;

doc.setPlainText("- [x] - [x] check1 \n **new line**");
edit.setDocument(&doc);

QObject::connect(&edit,&MkEdit::cursorPosChanged,
&doc,&MkTextDocument::cursorPosChangedHandle);


QTextCursor cursor = edit.textCursor();
cursor.setPosition(newLinePos);
edit.setTextCursor(cursor);
QString text = edit.toPlainText();
REQUIRE("☑☑check1 \n **new line**" == text);

cursor.setPosition(cursorPosition);
edit.setTextCursor(cursor);

QString text1 = edit.toPlainText();
REQUIRE("- [x] - [x] check1 \n new line" == text1);

int currentPositionOfTextCursorInBlock = edit.textCursor().positionInBlock();
REQUIRE(currentPositionOfTextCursorInBlock == cursorPosition + symbolLength - minusCheckBoxCount);
}

2 changes: 0 additions & 2 deletions mkedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,6 @@ void MkEdit::cursorPositionChangedHandle()
selectRange.end = cursor.selectionEnd();
}



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

if(!cursor.hasSelection() && selectRange.isCursorCaculated){
Expand Down
8 changes: 7 additions & 1 deletion mktextdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ void MkTextDocument::showAllFormatSymbolsInTextBlock(QTextBlock &block, FormatDa
for(QString::Iterator cp = textBlock.begin(); cp != textBlock.end(); cp++){
if(*cp == u'' || *cp == u''){
checkMarkPositions.removeAll(blockPos + index);

if(selectRange){
if(index < selectRange->currentposInBlock){
selectRange->currentposInBlock--;
}
}
}

QVector<QPair<int, int>>::iterator it = linkPositions.begin();
Expand All @@ -527,7 +533,7 @@ void MkTextDocument::showAllFormatSymbolsInTextBlock(QTextBlock &block, FormatDa
int symbolLen = (*it)->getSymbol().length();

if(selectRange){
if(cursorPos< selectRange->currentposInBlock){
if(cursorPos <= selectRange->currentposInBlock){
selectRange->currentposInBlock = selectRange->currentposInBlock+symbolLen;
}
}
Expand Down

0 comments on commit b865274

Please sign in to comment.