The document (QTextDocument) of the QTextEdit class provides two signals when it has changed, one when any change is made and one with the specific change made by position and number of characters removed or added. Neither of these are sufficient as one has no detail and the other too much detail. What is needed is when a line has been modified and when the cursor has moved away from this line. It is not necessary to compile the line as each character is entered or deleted.
To accomplish this, a new member variable was added to EditBox that will hold the line number of the last line that was modified and will be initialized to ‑1 to indicate no line has been modified. The document's contents changed signal is connected to a new document changed slot in EditBox. Using this variable and slot, the steps for catching line changes are:
- In the new document changed slot, the current line number that the cursor is at is recorded in the new line modified member variable.
- If there is a request to move the cursor from the current line, the contents of the current line is obtained (which will eventually be compiled).
- The line modified variable will be reset to ‑1.
- The cursor is then moved.
There was a minor problem that needed to be corrected. The line modified variable was initialized to ‑1 in the constructor. When a program was loaded, this triggered the contents of the document to changed and this signal was emitted causing it to record line 0 with a change. To correct this, when the document modified flag is reset, the line modified variable was also reset, so a new EditBox function was added to do both of these operations.
Eventually upon loading a program, the entire program will need to be compiled line-by-line. Now that the basic line detection mechanism is working, the detection of additional cursor movements can be implemented.
[commit 2e3e29c6fb]
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.)