summaryrefslogtreecommitdiffstats
path: root/tests/auto/qtextcursor
diff options
context:
space:
mode:
authormae <qt-info@nokia.com>2010-06-04 09:39:07 (GMT)
committermae <qt-info@nokia.com>2010-06-08 09:32:55 (GMT)
commita46e97c429077022bc7f426a3adec588643abc57 (patch)
tree5053261acd1567c5ed32d04a40038b6634ba8848 /tests/auto/qtextcursor
parent3e707f559f6f26ab7a5d5623f8e5af9ea9cc317e (diff)
downloadQt-a46e97c429077022bc7f426a3adec588643abc57.zip
Qt-a46e97c429077022bc7f426a3adec588643abc57.tar.gz
Qt-a46e97c429077022bc7f426a3adec588643abc57.tar.bz2
Cursor positioning in QTextDocument after undo()
QTextDocument had no way of storing the cursor position from which a document change was initiated in the undo stack. That means that any undo operation would reposition the text cursor to the text position where the actual change happened. This works in many cases, but not always. In Qt Creator we have standard IDE shortcuts like e.g. Ctrl+Return for InsertLineBelowCurrentLine, which insert a newline at the end of the current line, not at the cursor position. Using undo there resulted in a surprisingly wrong cursor position. The problem becomes worse with more advanced refactoring and productivity tools (like snippets). The patch creates a synthetic CursorMoved undo item with the position which was current at the time of calling QTextCursor::beginEditBlock(), but only if necesary, i.e. only in those cases where the cursor would be positioned wrongly. Reviewed-by: Roberto Raggi
Diffstat (limited to 'tests/auto/qtextcursor')
-rw-r--r--tests/auto/qtextcursor/tst_qtextcursor.cpp18
1 files changed, 17 insertions, 1 deletions
diff --git a/tests/auto/qtextcursor/tst_qtextcursor.cpp b/tests/auto/qtextcursor/tst_qtextcursor.cpp
index 99babac..41835bb 100644
--- a/tests/auto/qtextcursor/tst_qtextcursor.cpp
+++ b/tests/auto/qtextcursor/tst_qtextcursor.cpp
@@ -151,6 +151,7 @@ private slots:
void cursorPositionWithBlockUndoAndRedo();
void cursorPositionWithBlockUndoAndRedo2();
+ void cursorPositionWithBlockUndoAndRedo3();
private:
int blockCount();
@@ -1756,9 +1757,9 @@ void tst_QTextCursor::adjustCursorsOnInsert()
void tst_QTextCursor::cursorPositionWithBlockUndoAndRedo()
{
cursor.insertText("AAAABBBBCCCCDDDD");
- cursor.beginEditBlock();
cursor.setPosition(12);
int cursorPositionBefore = cursor.position();
+ cursor.beginEditBlock();
cursor.insertText("*");
cursor.setPosition(8);
cursor.insertText("*");
@@ -1814,5 +1815,20 @@ void tst_QTextCursor::cursorPositionWithBlockUndoAndRedo2()
QCOMPARE(cursor.position(), cursorPositionBefore);
}
+void tst_QTextCursor::cursorPositionWithBlockUndoAndRedo3()
+{
+ // verify that it's the position of the beginEditBlock that counts, and not the last edit position
+ cursor.insertText("AAAABBBB");
+ int cursorPositionBefore = cursor.position();
+ cursor.beginEditBlock();
+ cursor.setPosition(4);
+ QVERIFY(cursor.position() != cursorPositionBefore);
+ cursor.insertText("*");
+ cursor.endEditBlock();
+ QCOMPARE(cursor.position(), 5);
+ doc->undo(&cursor);
+ QCOMPARE(cursor.position(), cursorPositionBefore);
+}
+
QTEST_MAIN(tst_QTextCursor)
#include "tst_qtextcursor.moc"