summaryrefslogtreecommitdiffstats
path: root/src/gui/text/qtextdocument_p.cpp
diff options
context:
space:
mode:
authormae <qt-info@nokia.com>2010-06-04 11:39:07 +0200
committermae <qt-info@nokia.com>2010-06-08 11:32:55 +0200
commita46e97c429077022bc7f426a3adec588643abc57 (patch)
tree5053261acd1567c5ed32d04a40038b6634ba8848 /src/gui/text/qtextdocument_p.cpp
parent3e707f559f6f26ab7a5d5623f8e5af9ea9cc317e (diff)
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 'src/gui/text/qtextdocument_p.cpp')
-rw-r--r--src/gui/text/qtextdocument_p.cpp19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/gui/text/qtextdocument_p.cpp b/src/gui/text/qtextdocument_p.cpp
index e2bca04d8c..f3cd48157f 100644
--- a/src/gui/text/qtextdocument_p.cpp
+++ b/src/gui/text/qtextdocument_p.cpp
@@ -192,6 +192,7 @@ QTextDocumentPrivate::QTextDocumentPrivate()
initialBlockCharFormatIndex(-1) // set correctly later in init()
{
editBlock = 0;
+ editBlockCursorPosition = -1;
docChangeFrom = -1;
undoState = 0;
@@ -967,6 +968,10 @@ int QTextDocumentPrivate::undoRedo(bool undo)
editPos = -1;
break;
}
+ case QTextUndoCommand::CursorMoved:
+ editPos = c.pos;
+ editLength = 0;
+ break;
case QTextUndoCommand::Custom:
resetBlockRevision = -1; // ## TODO
if (undo)
@@ -1046,6 +1051,18 @@ void QTextDocumentPrivate::appendUndoItem(const QTextUndoCommand &c)
if (undoState < undoStack.size())
clearUndoRedoStacks(QTextDocument::RedoStack);
+ if (editBlock != 0 && editBlockCursorPosition >= 0) { // we had a beginEditBlock() with a cursor position
+ if (c.pos != (quint32) editBlockCursorPosition) { // and that cursor position is different from the command
+ // generate a CursorMoved undo item
+ QT_INIT_TEXTUNDOCOMMAND(cc, QTextUndoCommand::CursorMoved, true, QTextUndoCommand::MoveCursor,
+ 0, 0, editBlockCursorPosition, 0, 0);
+ undoStack.append(cc);
+ undoState++;
+ editBlockCursorPosition = -1;
+ }
+ }
+
+
if (!undoStack.isEmpty() && modified) {
QTextUndoCommand &last = undoStack[undoState - 1];
@@ -1167,6 +1184,8 @@ void QTextDocumentPrivate::endEditBlock()
}
}
+ editBlockCursorPosition = -1;
+
finishEdit();
}