Sunday, December 30, 2012

GUI – Status Bar

The status bar on the bottom of the window was already automatically created when the MainWindow class was created from the new file wizard and is already being used for the menu item status tips.  Eventually program information like current line and column will be displayed on the status line along with error messages.  But for now there are a few places where messages are needed for the file operation functions, specifically when the program has been loaded or saved successfully.

[commit ea1b7585bb]

GUI – File Operations - Functions

A couple of helper functions were implemented to support the file operation functions.  The first is a function check if it is okay to continue, which checks if the file has been modified and if it has, ask if the file should be saved or the operation canceled.  This check is needed on the new, open and close program functions.

The second helper function sets a new member variable that holds the current program file path and sets the window title to the base file name of the current program file path.  The window title is set to the string "<file name>[*] ‑ IBCP" where the [*] is a placeholder for whether the file has been modified.  Qt handles this in a platform specific way.  Qt handles this in a platform specific way.  Generally on Windows and Linux, it  simple puts an asterisk after the file name, but on MAC, a dot is put in the close bubble.  If the program file path is blank, the program name is set to the "Untitled" string.

In the main window constructor, a connection was added to the edit box document's modification changed signal to the main windows set window modified slot.  Whenever the document is modified, this signal causes the file modified indicator to appear.

Two support functions were also implemented that load and save the program into or from memory, though right now they simply read and write a text file to or from the edit box document as plain text.  Eventually when a program is loaded, it will need to be parsed, translated, encoded and stored in memory.  Details of the main file operation functions can be found by clicking Continue...

[commit ea1b7585bb]

Actions/Menus – Using Designer

I discovered a much better and easier way to create actions and menus for the application without having to write all the lines of code by using Designer within QtCreator.  The UI form for the main window is opened in Designer by double-clicking on mainwindow.ui from the project files area.

Main menus are added by double-clicking on the "Type Here" text at the top of the edit area and entering the text.  Hot keys are entered by preceding the desired character with an '&' ampersand character.  The menus can be repositioned by dragging them as desired.

Menu items are added is a similar way, by selecting the main menu and again double-clicking the "Type Here" text.  Separators are added by double-clicking the "Add Separator" text.  Status tips are entered in the statusTip field on the lower-right side properties after selecting the desired action from the upper-right side object tree list.  Shortcut keys can be entered in the shortcut field in the properties area or can be entered using the Action Editor tab at the bottom of Designer.

Designer automatically names the actions with the name "actionXxx" where Xxx is the text entered for the menu item.  The under-bar character is used for spaces, so these were removed to follow the camel casing naming convention.  Qt will make connections between actions and functions automatically if the functions are named correctly.  For the menu item actions, the functions are named on_actionXxx_triggered() where actionXxx is the name of the action and triggered is the name of the signal to connect.  All of the dummy program functions were renamed to this form so that the needed connections are made automatically.

However, this does not work for connecting the actionExit triggered signal to the MainWindow::close() function, since this does not follow the automatic naming convention.  This was accomplished by using Signal & Slots Editor tab at the bottom of Designer.  A new signal/slot is added by clicking the large plus icon.  For this new entry, the Sender was set to actionExit, the Signal was set to triggered(), the Receiver was set to MainWindow, and the Slot was set to close().

The actionAboutQt action could not be setup in Designer since there is no way to connect to the application's aboutQt() function using the Signal & Slots Editor because the application object is not an available Receiver.  This was instead accomplished by adding a new on_actionAboutQt_triggered() function to MainWindow, which simply calls qApp‑>aboutQt().

Now that all the actions and menus are in the UI form for MainWindow, the action enumeration, action pointers and menu pointers were removed from the class definition.  The pointers are now contained in the Ui::MainWindow class that is automatically generated from the mainwindow.ui form file by the UIC (Qt User-Interface Compiler), which MainWindow contains a member pointer to.  Since the setupUi() function, called in the constructor, now creates the actions and sets up the menus, the createActions() and createMenus() functions are no longer needed and were removed.

[commit 46737de13d]

GUI – File Operations - Actions

To start, the EditBox will be made to handle simple text files.  Once the base GUI functionality is implemented, the process of implementing a full program editor will commence .  The first file operations to implement are new, open, save, and save as.

To create an action, several lines of code are needed, plus there needs to be a QAction pointer added to the class definition for each.  The program is going to end up with a lot of actions, so a more streamlined way of handling actions was needed.

In the MainWindow class definition, an enumeration for all actions was added.  Values will be added to this enumeration for each new action implemented.  The enumeration ends with a size of value, which is used to declare an array of QAction pointers.

In the createActions() function, a simple action information structure was added containing the action enumeration value, the name, the shortcut key sequence and the status tip string of the action.  An array of these action information structures is declared and initialized.  The size of enumeration value in the action value is used to indicate the end of the array.

A loop was added to process each element in this array, creating the QAction instance, setting its shortcut key sequence and status tip.  Not all actions will have a shortcut key sequence, so for these, the key sequence is initialized to an unknown value and no key sequence will not be set for these.

Finally all the actions are connected to their associated functions.  Unfortunately, the connection call could not be included in the loop because of the Qt SIGNAL and SLOT macros and the inability to declare a function pointer that could be added to the information array.  Plus, not all connection calls are identical (the close function returns a boolean instead of nothing and the About Qt action is connected to an application function, not a MainWindow function).  For now only dummy file operation functions were added - these will be implemented next.

[commit 8958bacc2e]