Saturday, March 16, 2013

Program – Custom Data Model

Up to now only the simple predefined string list model has been used to hold the program.  The next task was to start the implementation of a custom data model that will eventually hold the program code that will be run.  To get he program model started, it will just hold a list of strings representing the lines of the program.

The new ProgramModel class is based on the QAbstractListModel as it only needs to hold a list of program lines.  This will be changed as needed since the program model will eventually need to be able to hold multiple lists of program lines, one for the main routine and several for the subroutines and functions of the program.

Now that the initial program model has been implemented (click Continue... for details), using the program view, all the various edit change operations and scenarios will now be tested to make sure all the possible changes are covered and working properly.  This is necessary to make sure the edit box keeps the program model properly informed of program changes.  Then the process of connecting to the parser and translator will begin.

[commit b543f74b6d]

Program Model Implementation

For a custom data model, several virtual functions from the base class must be re-implemented.  Since the program model will be read-only (from the GUI viewpoint), only the data() function, that will be used by the program view for reading the data values (program lines) from the model to display to the screen, and the rowCount() function, for returning the number of lines in the program, were needed.  These functions simply access the string list member variable of the program model.

The program model also has an update() slot function for receiving program line changes from the edit box.  The functionality of this slot is essentially the same as the program change slot previously in the main window (which was removed) except instead of accessing the string list model, it accesses the string list member directly.  However, some additional functionality was needed.

Program Model Update Slot

When lines of the program model are changed, a signal needs to be emitted that data has changed.  The program view widget uses this signal to update the display (it calls the data function to get the data).  To avoid unnecessary screen updates (and eventually unnecessary line re-compiles), a change detection check was added.

When rows (lines) are removed from the model, the beginRemoveRows() function from the base class must be called with the range of rows that are about to be deleted.  The lines are then removed from the string list member.  Once complete, the endRemoveRows() function must be called.  These additional functions keep the program view widget informed so that the display can be updated accordingly.

Similarly when row are inserted into the model, the beginInsertRows() and endInsertRows() functions are called before and after the new lines are inserted.

Finally, if the number of lines in the program has changed, a signal is emitted with the new line count.  This signal is used to keep the program view widget informed if the size of the line number area has changed (in other words, the number of spaces needed to hold the line numbers).

Program Line Delegate

The line number width update function was changed to a slot and is now connected to the new line count changed signal from the program model.  This function determines the width of the line number area based on the number of digits required for the new line count.  Previously, when this width changed a signal was emitted indicating that the program view needed to be redrawn.  This was changed to update the program view rectangle directly.

These changes required that the program line delegate contain a pointer to the program view widget, so a new member variable was added.  The constructor was updated where the font metrics argument was replaced with a pointer to the program view widget.  This pointer is used to get font metrics information (specifically for the pixel width of a digit).

The base class of the program line delegate was changed from the QItemDelegate class to QStyledItemDelegate class because this is the default delegate.  This delegate also respects and uses the current style sheet.  Style sheets are a way for the look of an application to be customized.  By default, Qt applications look like the host operating system.  Style sheets are not currently used here.

Main Window

The MainWindow class constructor makes all the needed signal-slot connections: the edit box lines changed signal to the program model update slot, and the program model line count changed signal to the program line delegate line number width update slot.  The program changed and program view update slots functions were removed along this the program line count member variable.

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.)