KDE programming tutorial using KDevelop

Part 11: New and Quit


[<< Contents] [<< Prev] [Next >>]

Adding two new File menu options

In this part we will add two more File menu options, one to start a new simulation and another to quit the application. A button to start a new simulation will also be added to the tool bar. To help the user prevent losing work, both the options will offer the user the chance to save their current simulation data before proceeding.

Updating the code

ksimulate.h

In the KSimulate class, we will implement the virtual "queryClose" function which is called when a request to close the main application window is made.

protected:
  bool queryClose();                           // check user happy to quit

And add the public slot to receive the signal that the user wants to start a new simulation.

  bool fileNew();                              // start new simulation

ksimulate.cpp

Include the header file to give access to the KMessageBox functionality.

#include <kmessagebox.h>

In the constructor, create two new actions to encapulsate the New and Quit actions using standard action definitions provided by KStdAction. The "New" action is associated with our new public slot, but the "Quit" action is associated with the inherited QWidget "close" slot.

  KAction *actionNew  = KStdAction::openNew(this, SLOT(fileNew()), 0);
  KAction *actionQuit = KStdAction::quit(this, SLOT(close()), 0);

Add the New and Quit actions to the File menu around the already existing menu entries, and include a separator to split the Quit option of from the others.

  actionNew->plug( menuFile );
  actionOpen->plug( menuFile );
  actionSave->plug( menuFile );
  menuFile->insertSeparator();
  actionQuit->plug( menuFile );

And add the New action to the toolbar.

  actionNew->plug( toolBar() );

Now we can add the code for the "queryClose" functionality. If there are no stations we can quit immediately. Otherwise using a KMessageBox we give the user the chance to save their simulation data before the application closes.

/******************************* queryClose *********************************/

bool KSimulate::queryClose()
{
  // no need to do anything if no stations
  QCanvasItemList  list = canvasView->canvas()->allItems();
  if (list.isEmpty()) return true;

  // check if user wants to save before quitting
  while (true)
    switch (KMessageBox::warningYesNoCancel(this,
        "Do you want to save before you quit?",
        QString::null, QString("&Save"), QString("&Quit")) )
    {
      case KMessageBox::Yes:  // "Save"
        // if save not successful ask again
        if (!fileSave()) break;

      case KMessageBox::No:   // "Quit"
        return true;

      default:                // "Cancel"
        return false;
    }
}

Similarly add the code for the start a new simulation action slot. If there are no stations we are done already. Otherwise using a KMessageBox we give the user the chance to save their simulation data before the deleting the stations and updating the canvas.

/******************************** fileNew **********************************/

bool KSimulate::fileNew()
{
  // no need to do anything if no stations
  QCanvasItemList  list = canvasView->canvas()->allItems();
  if (list.isEmpty()) return true;

  // check if user wants to save before starting new simulation
  while (true)
    switch (KMessageBox::warningYesNoCancel(this,
        "Do you want to save before you start a new simulation?",
        QString::null, QString("&Save"), QString("&New")) )
    {
      case KMessageBox::Yes:  // "Save"
        // if save not successful ask again
        if (!fileSave()) break;

      case KMessageBox::No:   // "New"
        // delete every station
        for( QCanvasItemList::iterator it = list.begin(); it != list.end(); ++it )
          delete *it;
        canvasView->canvas()->update();
        statusBar()->message("New simulation started");
        return true;

      default:                // "Cancel"
        return false;
    }
}

Compile and run

The new code will be compiled automatically when you next attempt to re-run the application using KDevelop. Investigate how the application behaves.


[<< Contents] [<< Prev] [Next >>]


Last updated 05-Jan-2005