Fix QTextEdit/QQuickTextEdit undo bug - Part #2
If a user selected the text "foo" and typed "bar", upon pressing undo, the text would change to "b". This is incorrect and does not match the functionality of QLineEdit or the default behaviours of Windows/OSX/Ubuntu. This was fixed by a change made to always merge two sequential inserts if they are not part of the same block. Previously the selection delete and the "b" were part of one edit block and "ar" was part of another. With this change, the selection delete and "bar" are part of the same edit block. Unit test changes are part of a separate review (Part #1) since they required changes in qtdeclarative. [ChangeLog][QtGui][Important Behavior Changes] Fixed QTextEdit to match undo functionality of QLineEdit to group two sequential inserts into one undo action. Task-number: QTBUG-38825 Change-Id: I76bf30e331e3526277c3e0ade58cf95b611fc117 Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>bb10
parent
6691df5336
commit
36ecf2c025
|
|
@ -1077,8 +1077,9 @@ void QTextDocumentPrivate::appendUndoItem(const QTextUndoCommand &c)
|
|||
QTextUndoCommand &last = undoStack[undoState - 1];
|
||||
|
||||
if ( (last.block_part && c.block_part && !last.block_end) // part of the same block => can merge
|
||||
|| (!c.block_part && !last.block_part)) { // two single undo items => can merge
|
||||
|
||||
|| (!c.block_part && !last.block_part) // two single undo items => can merge
|
||||
|| (c.command == QTextUndoCommand::Inserted && last.command == c.command && (last.block_part && !c.block_part))) {
|
||||
// two sequential inserts that are not part of the same block => can merge
|
||||
if (last.tryMerge(c))
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2899,15 +2899,28 @@ void tst_QTextDocument::testUndoBlocks()
|
|||
doc->undo();
|
||||
QCOMPARE(doc->toPlainText(), QString(""));
|
||||
|
||||
cursor.insertText("town");
|
||||
cursor.beginEditBlock(); // Edit block 1 - Deletion/Insertion
|
||||
cursor.setPosition(0, QTextCursor::KeepAnchor);
|
||||
cursor.insertText("r");
|
||||
cursor.endEditBlock();
|
||||
cursor.insertText("est"); // Merged into edit block 1
|
||||
QCOMPARE(doc->toPlainText(), QString("rest"));
|
||||
doc->undo();
|
||||
QCOMPARE(doc->toPlainText(), QString("town"));
|
||||
doc->undo();
|
||||
QCOMPARE(doc->toPlainText(), QString(""));
|
||||
|
||||
// This case would not happen in practice. If the user typed out this text, it would all be part of one
|
||||
// edit block. This would cause the undo to clear all text. But for the purpose of testing the beginEditBlock
|
||||
// and endEditBlock calls with respect to qtextdocument this is tested.
|
||||
cursor.insertText("quod");
|
||||
cursor.beginEditBlock();
|
||||
cursor.beginEditBlock(); // Edit block 1 - Insertion
|
||||
cursor.insertText(" erat");
|
||||
cursor.endEditBlock();
|
||||
cursor.insertText(" demonstrandum");
|
||||
cursor.insertText(" demonstrandum"); // Merged into edit block 1
|
||||
QCOMPARE(doc->toPlainText(), QString("quod erat demonstrandum"));
|
||||
doc->undo();
|
||||
QCOMPARE(doc->toPlainText(), QString("quod erat"));
|
||||
doc->undo();
|
||||
QCOMPARE(doc->toPlainText(), QString("quod"));
|
||||
doc->undo();
|
||||
QCOMPARE(doc->toPlainText(), QString(""));
|
||||
|
|
|
|||
Loading…
Reference in New Issue