Showing posts with label EditBox. Show all posts
Showing posts with label EditBox. Show all posts

Friday, January 10, 2014

Edit Box – New Program Correction

A problem was found with the New program command.  The problem occurs when clearing (New) the loaded program.  After clearing the program, as soon as a character is entered, a crash occurred.  The problem occurred because while the document in the edit box was cleared, several of the other variables were not, so the edit box tried to remove all the lines from the attached program unit that had already been removed.  The extra selections list, line count, modified line number, and modified line is new flag variables also needed to be reset.

[commit 96557e5562]

Sunday, January 5, 2014

Edit Box – Error Handling During Startup

During startup when the program is loaded into the edit box, any errors detected in the program were saved in a member variable until the cursor became valid.  A valid cursor is needed in order to generated the extra selections used to highlight the errors.  The error list changed routine was then called, which would process the saved errors and clear the saved errors list.

The mechanism was modified where any errors are ignored during startup.  When the cursor becomes valid, the errors are retrieved from the attached program unit and the extra selections are generated.  The saved error list member variable was no longer needed and was removed.  Access functions were added to the program model to obtain the error count and an error item by index.

[commit 10724e4aab]

Edit Box – Line Wrapping Problem

While working on the next set of changes, an unrelated problem was discovered with error highlighting.  The problem occurred when there were long lines that wrapped onto the next line.  The problem was first noticed when a line with an error was modified and error was shifted to the wrong place.  The problem also occurred in other situations, including moving to an error that was in the wrapped portion of a line, or when a recreated line was after a wrapped line.

The problem was identified to be the use of the findBlockByLineNumber() function, a member of the QTextDocument class that holds the document of the edit box (in the QPlainTextEdit base class).  This function was being used to get the text block for a given line number, which was then used to gets its position within the document to set the position of the cursor.

However, this function was not working as expected.  What it actually does is return the text block of the actual physical line specified, which takes into account wrapped lines where each part of a wrapped line counts as separate lines.  The findBlockByNumber() function does work as needed and does not consider wrapped lines.  This latter function was already being used in two places, but not in three others.

On the surface, these two functions appear to do the same thing for a plain text document.  The only difference in the help documentation was in the argument, one taking a line number and the other taking a block number.  It was incorrectly assumed that line number meant the same thing as block number.  It is true that each line is a block in a plain text document, but a line number actually represents the physical line on the screen.  Once the latter function was used throughout, there were no problems.

[commit bf7fb54a79]

Saturday, January 4, 2014

Main Window – Multiple Edit Boxes

The application will eventually support multiple edit boxes where the subroutines and functions can be opened at the same time in additional edit boxes.  The main routine or any of the subroutines or functions could also be opened in multiple edit boxes.  This support will be added later, but some preparation work can be done and this will be useful in the reorganization of the program and edit box classes.

Currently there is a list of actions that are built and given to the edit box instance for its context menu.  This context menu will only be assigned to the edit box that is currently active and only one edit box will be active at a given time (the one that has focus).  When the focus changes to a different edit box, the context menu needs to be removed from the edit box losing focus and added to the one gaining focus.  To make this easier, the list of actions are built and stored into a new member variable.  This list will be assigned to an edit box instance as needed.

In addition to setting the actions (context menu) of an edit box, the various signals from the edit box needs to be connected to the various actions in the context menu (for enabling and disabling the actions) and to the status bar update slot (for the cursor changed signal).  These items need to be done when an edit box instance becomes active.  For the edit box losing focus, the context menu (actions) needs to be removed and the signals disconnected.

This code was put into a new edit box set active routine that takes an edit box instance as an argument.  Preliminary code was added for first disconnecting the signals and removing the actions of the edit box losing focus.  This code was commented since there is currently only one edit box instance.

[commit 5b8741768c]

Sunday, December 29, 2013

Program – New Action

The next addition to the program class is to handle the New program action.  Previously, the clear function of the edit box was called.  This function is part of the QPlainTextEdit base class that the edit box class is derived from.  When this function was called, it generated a document changed signal, which the edit box processed and ended up clearing the program unit that was attached to the edit box.  However, the program class should have this responsibility since there may not be an attached edit box.

The new action routine in the main window class was modified to call the new clear function in the program class.  This new clear function calls a new clear function of the program unit member (a program model instance).  Eventually, all of the program units of the program (for subroutines and functions) will be deleted with only the program unit for the main routine being cleared.

The new program model class clear function clears the line information list, program code vector, error list and all of the dictionaries.  This required new clear functions be added to the dictionary and information dictionary classes.  A new clear function was also added to the abstract information class, which was implemented for the constant number and string information classes.  Finally, the clear function sends the new program cleared signal, which is connected to the edit box clear funtion.

The clear function was reimplemented in the edit box class.  This was necessary so that a flag could be set before calling the clear function of the QPlainTextEdit base class.  This flag indicates to the document changed slot of the edit box routine that the signal should be ignored as it didn't originate by editing the program.  The recreating line flag, that is used to ignore document changes when lines are replaced with their recreated text, was used for this purpose.  This flag was renamed to the more appropriate ignore change.

A minor problem was also corrected in the main window class.  Previously, before a program was saved, the main window instance called the capture modified line routine of the edit box to make sure that any changes made to the current line are saved.  This normally takes place when the cursor is moved from the line being changed.  This check also needs to take place before the check to see if the program should be saved before it is cleared by the new action.  This functionality was put into the new program capture edit changes routine since it is called from two different places.

[commit 8d6e5703c0]

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]

Saturday, December 14, 2013

Edit Box – Lines Changed Signal

The edit box was using the lines changed signal to notify the program model instance when lines changed in the document that included the starting line number, the number of lines deleted and the number of lines inserted and the text of the lines.  The program model update slot routine was connected to and processed this signal.  Since the edit box can access the program model directly, this signal is not needed.  The tester class is already accessing this routine directly.

The lines changed signal was removed and the two emits of this signal were changed to calling the update routine directly.  The update routine was changed from a public slot to a normal public member function in the program model class.  A few comments were added and word 'slot' was added to the comments of all the slot functions so that these would be easier to identify.

[commit c864bdde07]

Program – Error List Handling

The program model class was keeping a list of any errors detected when lines are translated.  After a group of line changes were processed, if the list of errors changed, the entire error list was sent to the edit box via a signal.  The edit box stored this list of errors and used it to generate its extra selection list, which is used to highlight the errors.  Since the edit box now has access to the program model, it was no longer necessary for the edit box to keep a copy of this list.

So that the edit box can keep its extra selection list up to date, the program model was modified to send signals for when an error item has been inserted, changed, or removed.  The connected edit box slots will update the extra selection list accordingly.  When the program model is done updating the error list, it send a signal that the error list has changed.  The connected edit box slot sets the extra selections to the base QPlainTextEdit class from updated extra selection list.  The edit box no longer scans through the error list to generate its extra selection list.

The functionality for finding the next or previous error from the current cursor location was moved from the edit box class to the program model class.  The current line number and column are passed to these routines, which are set to the next error line number and column upon returning along with a flag of whether the end or beginning of the program was passed so that a message can be issued.

A new routine was added to the program model class to handle when the current line is edited and contains an error.  The error is shifted if the edit takes place before the error, or deleted if the edit takes place within the error.  If the error list changes, the appropriate signals are sent to the edit box.  This routine was not made a slot since the edit box can call it directly.

During the initial loading of the program, the text cursor in the edit box is not valid.  Any errors in the program are sent as inserted error signals.  Since the text cursor is not valid, the extra selections cannot be created (each extra selection contains a format and a cursor, which needs to be set to a valid cursor).  The errors are temporarily saved in a list.  After the text cursor  becomes valid, extra selections are created from this list (which is then cleared).

The error list class used by the program model to hold the list of errors previously kept track of the first and last index affected by changes to the list.  These indexes were used by the edit box to maintain its extra selection list.  With the new direct change signals, these indexes are no longer needed, so the error list was changed to having a simple changed flag.  The error list changed signal is only sent when this flag is set.  The has changed access function was modified to clear this flag after it is read so a separate reset function is not needed.

Finally, the main window class status bar update slot was modified to receive the error message by an argument in the signal instead of it retrieving the message for the current line from the edit box, which retrieved the message from its copy of the error list.  The routine sending this signal in the edit box class was modified to send the error message, which is retrieved from the program model via its list of errors.

[commit 19c5f06d0c]

Tuesday, December 10, 2013

Edit Box – Program Unit Access

The edit class box needs access to the program model, specifically to the program unit currently opened in the instance of an edit box.  Eventually when subroutines and functions are implemented, any one of them could will be opened with their own edit box.  There could be several edit box instances opened at any given time.  Right now there is just a single program unit, the main routine, which will be opened in a single edit box instance.

To allow an edit box instance access to its program unit, a new pointer to a program unit instance (program model class) was added to the constructor of the edit box class.  This pointer is stored in a new program unit pointer member variable.  The connection of the line changes signal (from the edit box class), and the error list changed signal (from the program unit) are now made in the constructor of the edit box instead of the constructor of the main window class.

The constructor of the main window class was modified to create the program model first (the program unit for the main routine) before creating the edit box instance, which now requires the pointer to program unit instance that it will be editing.

[commit aa2125609a]

Saturday, December 7, 2013

Program (Recreator) and GUI Integration

The recreator is fully integrated with the program model such that program lines can be converted back into text.  When lines are entered into the program, the lines need to be recreated back to text and put into the edit box (the GUI), specifically into the text document of the edit box.  Like the temporary program view (being used for debugging) is the viewer of the data held by the program model, the edit box is the viewer of the data contained in the document.  The edit box also allows editing, so it is more than just a viewer.

The document of the edit box is really just the text representation of the program.  The program model holds the actual data of the program.  Ideally, the program model would be the document of the edit box and it would convert text to program code and back while editing.  However, Qt does not have an abstract text document class from which a document sub-class could be built that would hold its data in another form like program code.  The QTextDocument class is meant for text.

Alternatively, a new viewer could be designed that would allow all the text editing features (cut, copy, paste, undo, redo, etc.) like the QPlainTextEdit class that the edit box class is based on.  Designing one would be quite an effort.  Therefore, the edit box will the viewer for two data models at the same time, the text document (to allow text editing) and the program model (for holding the program code).  The program model will be the master of the data, with the text document being updated as the program changes.

This implies that the edit box either own the program model with the program code or at least have easy access to it like via a pointer.  The later approach will be used since the main window class will ultimately be the owner of the program.  Eventually there will be a list of program models, one for the main routine and several for the subroutines and functions of the program.  There will only be associated edit box instances when the main routine, subroutines or functions are open for editing.

Since the edit box will now have access to the program model, signals from the program model (for program changes) do not need to contain actual data.  For instance, when a program line has changed, its recreated text is needed to update the text document.  The signal could contain both the line number and text (already recreated).  Looking at the edit box to document interface, when the document changes, only the position, number of characters removed and inserted are contained in the signal.  The edit box must obtain the actual text changes by querying the document.  So, when the program changes, only the line number will be sent and the edit box will request the recreated text from the program model.

Friday, June 14, 2013

Modifying A Line With An Error

When a line is modified that contains an error, the highlighted error can be shifted around or changed in size.  This can make the error message no longer applicable, so the highlighted error should be removed.  Upon the cursor leaving the line, the line will be rechecked for errors.  A check for this situation was added to the document change slot of the edit box class when a single line is changed.  No action was taken for single line changes.

When a single line is modified, the errors lists is searched its line number.  If the line contains an error, but the modification occurred after the error, no further action is taken.  If the cursor and the error are at the end of the line, or the cursor is within the error, the error is removed from the error list.  If the cursor is before the error, the column of the error is moved by the net change (number of characters added less the number removed).

The update errors slot is called to update the display.  A cursor changed signal is also emitted so that the error message on the status line is removed when the error is removed.  This didn't always occur because the cursor is not always moved when the document is changed (for example, when characters are deleted).  New move error column functions were added to the error list and error item classes to support the shifting of errors.

[commit 98d7b2c0b4]

Thursday, June 13, 2013

Error Action Enabling/Disabling

Now with actions to move the cursor to the next or previous error, these actions should only be enabled when there are actually errors.  To accomplish this, an errors available signal was added to the edit box class.  The update errors slot was modified to determine when the errors available condition changes by comparing the before and after error list emptiness status.  Several problems were found and corrected while testing the new signal.

In the update errors slot, when there were no errors in the incoming errors list, a Qt error occurred because bad indexes were used in accessing the errors list.  This was due to using the start and end change indexes when they were not valid.  A check was added for when the incoming error list has no errors to prevent this.

The second problem occurred when there were no errors and the next or previous error key shortcuts were used.  This was due because the Ctrl+. (period) and Ctrl+, (comma) were not intercepted since the errors actions were disabled, causing these keys to be treated as a regular period and comma, which was inserted into the program.  Checks were added to the key press event handler to catch and ignore the period and comma keys when the Ctrl modifier was active.

Finally, during testing, an unrelated problem was found with the detection of changed lines when comparing RPN lists that contained errors.  Previously this comparison (equality operator function) compared the error column and length and reported equality if they were the same.  This caused a problem were sometimes the error was not displayed correctly in the modified line (or not at all).  The equality function was modified to report no match if either RPN list has an error.

[commit 352667221f] [commit 15084235a2]

Saturday, May 18, 2013

Move To Error Actions

It would be convenient to be able to move the cursor the next (or previous) error.  This was accomplished be adding two additional actions, one for moving the cursor to the next error and the other to move the cursor to the previous error.  Each action was given an arrow icon, which were modified to have a red hue.  Both were also assigned key shortcuts, which were Ctrl+. (period) for next and Ctrl+, (comma) for previous.  These keys were chosen because they also contain the greater than and less than keys, which are like arrows.

The actions are automatically connected to associated triggered functions in the MainWindow class, which call new functions in the EditBox class to perform the action.  These functions first find the error for the current line.  If the current line does not have an error, the find returns the index of next error or will return in index one beyond the end of the error list.  The functions then move to the next or previous error depending whether the cursor is already sitting at the error found.  If beyond the last or first error, a question box is displayed informing this and asks whether to move to the first or last error in the program.  If there is only one error, then no question box is displayed.

There is some sort of issue when running on Linux (Mint 13 KDE) using the static QMessageBox::question() function to display the question asking whether to wrap around and to the first or last error - an X Error message is output.  This may have something to do with another problem where there is suppose to be a question mark icon on this message box, but the information icon is displayed instead.  These problems do not occur on Windows and also do not occur on Linux if the application is launched with the -style Plastique option.

For now the next and previous error tool bar buttons and edit menu items are always enabled.  Next these will be disabled when there are no errors.

[commit f086ce50b8]

Sunday, May 12, 2013

Error Highlighting – Error Messages

To display error messages in the status bar area at the bottom of the application window required a few minor changes.  Two labels were added to the status bar of the main window, one for the current line number and column, and the other for the error message.  A signal was added to the edit box, which is emitted when the cursor positioned has changed.

A new function was added to the main window class for creating the status bar: adding the labels to the status bar and connecting the signal cursor changed signal from the edit box to a new update status bar slot in main window.  New member label pointers were added so that the labels are easily accessible when it's time to update their text.  New access functions were added to the edit box class to return the current line number, column and message for the line (if there is one) so the update slot can get the information for the status bar labels.

The new message access function of the edit box searches for the line number in the error list.  A new find index function was added to the error list class, which returns a -1 if the line number does not have an error.  The existing find function is called, which only returns an index regardless if the line has an error, so a check is made if the index returned is the line being search for.  If the line does not have an error, a blank message string is returned.

There were a couple of issues getting the error messages to be displayed in the status bar correctly.  Click Continue... for details of these issues and how they were resolved.

[commit c38cd02bd4]

Saturday, May 11, 2013

Error Highlighting – Program Load Fix

Displaying the error messages on the status bar at the bottom of the window has proven to be difficult.  While working on getting these changes to work, a problem was discovered when loading a program with errors - the errors were not display just like the problem when the application first starts.

The mechanism added to correct that initial start up problem did not work for this case because the cursor valid flag, which is initially reset, is set the first time the cursor is moved.  When a program is loaded, the text of the program loaded in set in the edit box's document using the setPlainText() function.  Apparently while the text is being set, the cursor is again invalid preventing the errors from being highlighted.

The cursor valid flag mechanism was utilized to correct this problem.  The setPlainText() function of the base QPlainTextEdit class was reimplemented in the edit box class, which resets the cursor valid flag and calls the base class function.  Once the text is set, a cursor changed signal gets emitted and the cursor valid flag is once again set triggering the errors to be updated.

[commit 58b0f23680]

Saturday, May 4, 2013

Errors List Corrections

While testing the error highlighting, some problems were found.  The first one exhibited itself as the last error was not correctly updated when it changed.  The problem was that a new error was being incorrectly inserted each time an error was changed.  This occurred in the set error routine in the program model because it did not return after replacing an error and it proceeded to also insert an error.

The set error routine was cleaned up where the first part now specifically handles changes (not inserts) and returns after an error is removed (the line no longer has an error), or an error is replaced (the line still has an error).  The rest of the routine handles inserting a new error and adjusting the rest of the error indexes and line numbers (for line inserts).  The remove error routine contained an else clause for exiting when called for an insert, but since this no longer occurs, the else clause was removed.

The second problem was in the edit box update errors slot.  The problem occurred when two or more lines with an error were removed and all the errors were changed as a result (due to the line numbers changing).  The problem is that the ending change index is set to the last index upon the first removed error, but after the second error is removed, the ending change index is not beyond the end of the error list.  This caused the routine to run past the end of the errors list causing a segmentation fault.  This was corrected by limiting the number of errors changed to one less than the size of the errors list.

[commit 30e7d98a1d] [commit 65893e51f9]

Friday, May 3, 2013

Error Highlighting – Initial Start Up Fix

The problem with the errors not being highlighted when the program is first loaded was not caused due to the edit box not being drawn yet as first thought.  The problem was identified to be that the text cursor was not valid initially.  When the extra selection list was set up, each enter had an invalid cursor, so nothing was highlighted.  The seemed to be no way to initialize the text cursor in the edit box constructor.

To compensate for this problem, a mechanism was implemented where the updated errors list is saved and not acted upon unless the text cursor is valid.  A new errors list member was added to the edit box class to hold the saved errors list.  The errors list needs to be saved anyway because it will be needed to access the error messages.

A new cursor valid flag was also added to the edit box class, which is initialized to false.  In the cursor moved slot, the text cursor will be valid and this event always occurs during startup.  So in this slot, if the cursor valid flag is not set, it is set to true and then the update errors slot is called to set the extra selections list.

[commit 89cfa532a8]

Thursday, May 2, 2013

Error Highlighting – Extra Selection List

To highlight the errors sent from the program model, a new extra selections list member was added to the edit box class.  The update errors slot first determines the number of errors that have been inserted or removed by taking the difference between the size of the new errors list and the size of the current extra selections list.  The number of errors that changes is determined by taking the difference between the end and start change indexes plus one, then subtracting the number of errors inserted (which may be zero).

Starting at the start change index for the number of errors that changed (which may also be zero), the extra selection is replaced with the corresponding error item from the errors list.  Continuing for the number of errors inserted, the new error items from the error list are inserted into the extra selections list.  Finally for the number of errors removed, the extra selections are removed from the list.  (There will never be both insertions and removals.)

A new support function was added for converting an error item to an extra selection.  If the length of the error is negative, this indicates an alternate column (which occurs in the case where there is a syntax error in the exponent of a floating point number, where the column is the beginning of the number).  For this case, the syntax error will be highlighted and not the number for a length of one character.

The format of the extra selection is set to red background.  The block for the line of the error item is retrieved from the document to get its position.  The cursor of the extra selection is set to the text cursor of the document and positioned to the error (position of the line plus the column of the error.  The cursor is moved to the end of the error with the keep anchor option, which highlights the error with the selected format.

Once the extra selections list has been updated for the change in the errors list, the edit box extra selections are set, which causes the errors to highlight.  However, there is a problem.  If a program contains errors when it is loaded, the errors are not initially highlighted.  I think this is because the edit box hasn't been drawn yet at the time the first errors changed signal is emitted.

[commit 24ae7f0467]

Friday, April 26, 2013

Program Model – Error List Changes

The program model is now keeping a list of errors in the program.  This list will be used to create the extra selections in the edit box to highlight the errors in the program.  A signal was added to the program model to emit the error list when it changes and a slot was added to the edit box to receive the error list.  For now, this slot just outputs the error list with indicators of the start and end errors that were changed.

The error list class was modified to capture the start and end indexes of the changes to a list when the program model receives a lines changed signal.  New start and end change indexes were added to the error list class along with access functions - one for resetting these indexes called at the start of the program model update slot, one for checking if there was a change to the error list called at the end of the update slot (to determine if the error list change signal should be emitted), and two to access each index called by the edit box to update the error highlighting.

To catch when errors in the list are modified, the insert, remove and replace functions in the error list class were overloaded.  Each calls the QList base function and then calls a new set change index private function, which checks if the index being modified is lower or higher then the current start and end change indexes.  New increment and decrement line number functions were also implemented, which call the error item functions and then the set change index function.

The new update errors slot function for the edit box class receives the error list change signal.  For now, the function just outputs the error list with indicators on the errors that changed, which was used for debugging.

[commit 666b15b531]

Thursday, April 25, 2013

Another Undo/Redo Problem

Now that the program model is keeping a list of errors in the program, the next incremental change is to report changes to this list to the edit box so that the errors can be highlighted.  While testing these changes, another problem was found with the detection of lines changes when using redo.

The problem occurs when the current line is modified (specifically from a redo) and another redo causes a change to occur in another part of the program.  The line that was modified does not get reported.  This can also occur with an undo, but only when going up and down through the undo/redo stack.  The line is not being reported because for undo and redo, the document change signal occurs before the cursor moved signal (where a modified line gets reported).

A check was added towards the beginning of the document change slot after gathering information about the change but before acting upon it.  If there is a modified line and the modified line is not at the line of the change, then the modified line is captured before processing the current change.

A complication occurs when the modified line is after the line of the change.  When reporting the modified line, the capture routine needs to report the line number contained in the modified line variable, however, when retrieving the text of this line, since the document has already been modified by the undo or redo, the number of the line is not necessarily the same if the undo or redo inserted or deleted lines.

Fortunately, the offset of where the actual program line is (after the undo or redo change) is the same as the net line count change caused by the undo or redo.  The capture modified line routine was modified to take an optional offset.  The net line count change is passed if the modified line is after the line of the change, otherwise, no offset is needed if the modified line is before the change.

[commit 0049ab8883]