Monday, February 25, 2013

Detecting Line Changes – New Idea

After some additional research, I discovered what may be a better and easier way to detect line changes (including deleted and inserted line).  The contents change signal that is emitted containing the number of characters removed and added also contains the position in the document where the change occurred.

Since all document changes cause this signal, catching line changes from this signal should be sufficient for all of the commands that change the document including the undo and redo commands.  And it may not be necessary to consider the what text was selected before the change or what the text being inserted contains.

This position of this signal can be used to determine where the cursor was before the change occurred and the line number for this position can easily be obtained and compared to the current cursor line number to determine the number of lines affected by the change.  Along with this position and the current cursor position, the net change in the number of lines in document can be used to determine how many lines may have been deleted or inserted by the command.

While doing initial testing this new scheme, it was noticed that when the file is loaded upon startup (or from loading a file), that two of these contents change signals are emitted along with the general document changed signal (used to catch when a line is modified).  These signals should be ignored.

To accomplish this, the setPlainText() was reimplemented in the EditBox class, which simply sets the ignore change flag, calls the base QPlainTextEdit::setPlainText() function and then clears the flag.  It was also necessary to add a check of this flag in the document changed slot for receiving the contents change signal.

[commit 01d1156c64]