Friday, March 8, 2013

Detecting Changes With Undo/Redo

Curiously, when the document's contents change signal is received for an undo or redo operation, the cursor has not been moved yet, so the current cursor position cannot be used to determine the number of lines changed from its line number and whether the cursor is now at the end of the line.

The beginning of the document changed slot function was modified to get the current text cursor and set its position to the position of the change.  The line number of this position and whether this position is at the beginning of the line is obtained.  This cursor is then moved to the end of the change by moving the cursor right by the number of characters added by the change.  This method also works for all of the other types of document changes.  Several of the local variables in the function were renamed to make the code clearer.

The reimplemented undo and redo functions are no longer deleted and were removed along with the checks for the undo and redo key sequences in the key press event handler.  It is also no longer necessary to keep track of the undo and redo operations on the current modified line, so the undo added slot was removed along with the modified line count and undo active variables.  This mechanism was originally implemented to keep track of changes on the current line so that unnecessary line change signals were not emitted, but now the receiver of this signal is will be responsible for detecting actual line changes (see post on March 3).

[commit a9db922c4d]

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)