There are quite a few situations to handle with detecting changed, deleted or inserted lines when selection text is involved. When there is selected text, it needs to determined if the selection is on one line or several lines. If text is being pastes on top of a selection (replacing it), it needs to determined if the pasted text is one line or several lines. Each of these situations will be handled one at a time to make this more manageable.
The first situation to be handled is when text has been selected and either a character is typed (the selected text is replaced with the character), or a delete command key is entered (delete or backspace). After the key has been entered, if there was a selection before the key is processed, it needs to determine how many lines may have been deleted and report these lines. The line the cursor is left on is set as the modified line, which will be reported when the cursor leaves the line.
To capture the selection before the key is processed (it won't be there after the key is processed), the particulars of the selection need to be saved, namely, whether there was a selection, and the starting and ending line of the selection. Because of implicit sharing, which the QTextCursor class supports, a copy of it before processing the key can not be made because the copy will be updated when the key is processed (the selection goes away).
To simplify the process of saving the selection particulars, a new Selection class was implemented with members for the start and end positions and the start and end line numbers of the selection. Because the document is changed when the key is processed, the start and end positions can't be used afterward except to check if there was a selection (the positions are not the same). This class has functions to set these variables from the text cursor (called before a key is processed), check if the selection is empty, get the number of lines in the selection (ending line minus starting line plus one), and accessors for the starting and ending line numbers.
The code needs to know if something was actually deleted by the key command before checking if lines were deleted (the command may have just moved the selection). There is a signal from the document like the general document changed signal, but includes the position of the change along with the number of characters removed and added. This signal was connected to a new slot function to capture and store the number of characters removed and added.
A new function was implemented to capture any lines that may have been deleted when there was a selection. This function checks if there was a selection before the operation, and there were characters removed. If the selection was more than one line, then the lines starting with the selection's starting line for the number of lines minus one are reported as being deleted. The number of characters removed and added are reset to zero so further operations don't erroneously report more lines being deleted.
If the selection, was on one line, no lines need to be reported. The current line modified is already being set when the document changed signal was received, so only lines below the cursor line need to be reported as being deleted. When the cursor leaves this line, the line will be reported as being changed.
[commit 1c0c27c744]
Saturday, February 2, 2013
Subscribe to:
Post Comments (Atom)
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.)