[<< Contents] [<< Prev] [Next >>]
We want to implement a number of changes in this part to introduce the concept of edit modes to our application; 'ADD' for adding new stations (already implemented), 'MOVE' for moving existing stations to different locations, and 'DELETE' for deleting stations. However we won't implement the MOVE and DELETE functionality just yet.
To allow the user to switch between the different edit modes we will add a toolbar with three icons (one icon for each edit mode) and we will add three menu options to the edit menu. So the user knows the current edit mode we will display the current edit mode as a text label on the status bar.
To help us code this functionality we will use the KAction class to encapsulate the three user-driven events of entering the three edit modes. We will also be using the Qt signals and slots communication between objects mechanism for the first time.
To implement this functionality we will add a public variable to store the current edit mode, a public constant for the text label id on the status bar, and three slots to receive the signals that the user is selecting an edit mode.
char editMode; // mode, A=ADD, M=MOVE or D=DELETE static const int editModeID = 1; // id of mode on statusbar public slots: void editAdd(); // set mode to add station void editMove(); // set mode to move station void editDel(); // set mode to delete station |
Include three XPM files to give us three icons for our three edit modes.
#include "icon_add.xpm" #include "icon_move.xpm" #include "icon_delete.xpm" |
Include the KAction header file to gives us access to KAction functionality.
#include <kaction.h> |
In the KSimulate constructor we create the three actions, and for each action we specify the text label, the icon to display, a keyboard shortcut, the slot's parent, the slot to execute this action, the action's parent, and an internal name. We then add these three actions to the edit menu and the tool bar. The tool bar, like the main menu bar and the status bar, is created automatically by KMainWindow functionality when we start to use it.
// create actions
KAction *actionAdd = new KAction( "&Add Station", QIconSet(QPixmap(icon_add_xpm)),
ALT+Key_A, this, SLOT(editAdd()), this, "A");
KAction *actionMove = new KAction( "&Move Station", QIconSet(QPixmap(icon_move_xpm)),
ALT+Key_M, this, SLOT(editMove()), this, "M");
KAction *actionDel = new KAction( "&Delete Station", QIconSet(QPixmap(icon_delete_xpm)),
ALT+Key_D, this, SLOT(editDel()), this, "D");
// add actions to Edit menu
actionAdd->plug( menuEdit );
actionMove->plug( menuEdit );
actionDel->plug( menuEdit );
// add actions to tool bar
actionAdd->plug( toolBar() );
actionMove->plug( toolBar() );
actionDel->plug( toolBar() );
|
In the KSimulate constructor we also need to initialise the edit mode to an initial valid mode, disable the MOVE and DELETE actions that are not yet fully implemented, and update the status bar to display the current edit mode. As we are using a fixed width text label on the status bar, the first time it is used we are having to pad the text with sufficient blank spaces to ensure that any subsequent longer labels are fully displayed.
// initialise the edit mode editMode = 'A'; actionMove->setEnabled(false); actionDel->setEnabled(false); statusBar()->insertFixedItem( " ADD ", editModeID, TRUE ); |
Finally add the three slot methods to receive the three action signals. In each slot we simply update the status bar and set the current edit mode public variable.
/******************************** editAdd **********************************/
void KSimulate::editAdd()
{
statusBar()->message("Edit mode set to ADD");
editMode = 'A';
statusBar()->changeItem("ADD", editModeID);
}
/******************************** editMove *********************************/
void KSimulate::editMove()
{
statusBar()->message("Edit mode set to MOVE");
editMode = 'M';
statusBar()->changeItem("MOVE", editModeID);
}
/******************************** editDel **********************************/
void KSimulate::editDel()
{
statusBar()->message("Edit mode set to DELETE");
editMode = 'D';
statusBar()->changeItem("DELETE", editModeID);
}
|
The three icons that will appear on the toolbar and menu options are again 16 pixel by 16 pixel bitmap images (
) that were prepared with the help of GIMP and saved in the XPM format.
These need to be added to our project by using the KDevelop new file functionality (either from the toolbar or File menu), and adding the code below.
/* XPM */
static const char * icon_add_xpm[] = {
"16 16 2 1",
" c None",
". c #000000",
" .. .. .. ",
" .. .. .. ",
" .. .. .. ",
" ...... ",
" .... ",
" .. ",
" .. ",
" .......... ",
" .......... ",
" .. .. ",
" .. .. ",
" .. .. ",
" .. .. ",
" .. .. ",
" .......... ",
" .......... "};
|
/* XPM */
static const char * icon_move_xpm[] = {
"16 16 3 1",
" c None",
". c #000000",
"+ c #0015FF",
" .. .+ .. ",
" .. +++ .. ",
" ..+++++. ",
" ..+++. ",
" .+++ ",
" + +++ + ",
" +++++++++++++ ",
" +++++++++++++++",
" +++++++++++++ ",
" +. +++ ..+ ",
" .. +++ .. ",
" .. +++ .. ",
" .. +++++.. ",
" .. +++ .. ",
" .....+.... ",
" .......... "};
|
/* XPM */
static const char * icon_delete_xpm[] = {
"16 16 3 1",
" c None",
". c #000000",
"+ c #FF0000",
" .. .. .. ",
" .. .. .. ",
" ++ .. .. .. ++ ",
" +++ ...... +++ ",
" +++ .... +++ ",
" +++ .. +++ ",
" +++..+++ ",
" ..++++++.. ",
" ...++++... ",
" .. ++++ .. ",
" ..++++++.. ",
" .+++ +++. ",
" +++ +++ ",
" +++ +++ ",
" +++........+++ ",
" ++..........++ "};
|
The new code will be compiled automatically when you next attempt to re-run the application using KDevelop. Investigate how the application behaves when you comment out either or both of the setEnabled(false) statements in the KSimulate constructor.
[<< Contents] [<< Prev] [Next >>]