Sunday, December 15, 2013

Edit Box – Recreated Line Replacement

When a program line is changed or inserted, the line should be recreated and the recreated text should replace the line entered in the edit box.  To accomplish this, the program model class will send a signal with the line number of a line that is changed or inserted.  The edit box will receive this signal, retrieve the recreated text for the line, and replace the text of the line with this recreated text.

The new program changed signal was added to the program model class.  The update line routine was modified to send this signal when a line is changed or inserted.  When a line is changed, the actual code of the line may not have changed, for example, if spaces were added or removed, or the case of keywords was changed.  In this case, the program code will not be modified, but this signal still needs to be sent so that the edit box reflects the correct recreated program code.

The new program changed slot routine was added to the edit box class, which starts be retrieving the recreated text for the changed line.  A text cursor is obtained for the edit box and its position is set to the beginning position of the block containing the line.  The cursor is moved to the end of the block keeping the anchor at the beginning, which selects the entire line.  The recreated text is inserted at the cursor and since text is selected, the selected text is replaced.

The program model line text routine was modified to return a null string if the line has an error.  The program changed slot routine does not replace the text if the text is null indicating that the line has an error.  This required the recreator class recreate routine to be modified where the output string is initialized to an empty string (a pair of double quotes) instead of being cleared.  Clearing a string creates a null string, and a null string is not quite the same as an empty string (a null string is empty but an empty string is not null).

When text is replaced in the document using a text cursor, document changed and cursor moved signals are generated from the document.  These signals need to be ignored when the line is being recreated, otherwise an infinite loop occurs because the document change signal updates the program, which generates another program changed signal, an so on.  A flag was added to the edit box and is set before replacing text and cleared afterward.  The document changed and cursor moved slot routines were modified to do nothing if this flag is set.

There are two unresolved issues resulting from these changes.  The first issue occurs during the initial loading of a program.  As a new program is being loaded, each line added to the program should be recreated to the edit box.  However, this cannot occur because until the program has been loaded into the document of the edit box, the text cursor is not valid, so can't be used to replace text.  The second issue occurs when a line is replaced with recreated text; extra undo commands are added to the undo stack.

[commit 4b34bd2dde]