Sunday, January 20, 2013

GUI – Edit Actions – Tool Bar

Adding the actions (icons) to the tool bar was simply a matter of dragging the actions from the Action Editor at the bottom of Designer to the tool bar.  Separators were added between the file actions and the cut, copy, paste and delete actions and the undo and redo actions.

While testing the edit actions, it was noticed that the edit box currently accepts formatted text with the paste action (for example, copying an italicized word from a word processor and pasting it into the program).  This will be corrected in the next commit.

[commit 24c21711b9]

GUI – Edit Actions - Enabling

For enabling the undo and redo actions, there are signals from the edit box (derived from the QTextEdit class) when the undo or redo availability changes.  Likewise, there is a signal for when the copy selection availability changes.  The undo, redo, cut, copy and delete actions were set to disabled in Designer.

The edit box's undo and redo signals are connected to the undo and redo set enabled slot respectively.  When an undo or redo is available, this signal is emitted to the action with a true value, which enables the action.  Likewise when an undo or redo is no longer available, this signal is emitted to the action with a false value, which disables the action.

The edit box's copy available signal is connected to the cut, copy and delete set enabled slot.  The copy available simply means that text has been selected, which can then be copied, but also cut or deleted.

[commit d9a3f3b0b7]

GUI – Edit Menu - Actions

The edit box already has a context (right-click) menu with Undo, Redo, Cut, Copy, Paste, Delete and Select All.  Applications customarily also have these same actions on the Edit menu and on the tool bar.  These actions were created in Designer after adding the Edit menu and adding each of these edit actions.  Once the actions were created, each was edited to add an icon, shortcut key and status tip.

The slot functions for each action was implemented in the MainWindow class with the names on_actionName_triggered so that these would automatically be connected to the actions.  The implementation of most of these functions was a single line that calls the appropriate member function of the EditBox instance (undo, redo, cut, copy, and paste).  The delete and select all actions required slightly different handling.

For delete, there is no function to delete the currently selected text in the QTextEdit class that EditBox is derived from.  The QTextCursor member of QTextEdit, which controls the cursor and selection of QTextEdit, must be used.  The textCursor() access function is used to obtain the text cursor of EditBox.  For deleting the current selected, the removeSelectText() function of QTextCursor is called:
m_editBox->textCursor().removeSelectedText();
The select all action required a little more handling.  The QTextCursor has a way of selecting text by using the select() function with an argument to specify what to select, with QTextCursor::Document being the option for selecting all of the text.  However, the select() function can't be called directly using the textCursor() access function like with delete because the selection doesn't get back to the document inside the edit box.  Instead, the text cursor needs to be obtained, the selection made and the text cursor put back with the setTextCursor() access function:
QTextCursor textCursor = m_editBox->textCursor();
textCursor.select(QTextCursor::Document);
m_editBox->setTextCursor(textCursor);
Additional functionality is required to enable and disable these actions when appropriate.  For example, cut and copy should only be enabled if text is currently selected.  The context menu already works like this.  This functionality will be the subject of the next commit.

[commit d8c2933481]

GUI – Recent Files List – Status Tips

I realized that no status tips were given to the open recent menu actions.  Therefore a status tip was added for the Clear Recent List action in Designer.  Sub-menu actions can not be given status tips (they can be assigned, but they don't show up when hovering over the sub-menu because the sub-menu is opened).

Status tips were also assigned to each of the open recent file actions added to the sub-menu with a string that simply says "Open" followed by the full path of the file name.

The code was also changed to use the QstringList::at() function to access the files in the file list member variable instead of using the [] operator.  The reason is that the at() function is more efficient because the [] operator returns a modifiable reference to the item (an lvalue), where as the at() function just returns a reference (an rvalue).

[commit 858bbe1e28]

GUI – Current Directory

While the application is running, it will remember the directory of the last file loaded so when the open file dialog is displayed again, that directory will be the starting directory.  This application's current working directory is updated by the open file dialog after a file is selected.  A "." for the current directory is used as the starting directory passed to the open file dialog box, so when the application is restarted, the current directory starts back to where the application was started from.  The application should remember the last directory when restarted.

To have the application remember the last directory, a new current directory member variable was added to the MainWindow class.  After a new file is loaded from the Open menu action, the directory of the file path is saved in this variable.  This variable is then saved as part of the application settings and restored when the application is started.

As was done for the restore and save settings for the RecentFiles source file, the names of the settings were put into constant character strings so that the strings are not specified in two different places (the restore and save functions) to prevent the possibility of a mistake in the name between the two functions, which would cause a setting to not be restored properly.

[commit 04c28a8252]