C.11. Chapter 11

C.11.1. Exercises

C.11.1.1. Suppose you would like to have only one instance of your panel applet running at a time. (Who would want, for example, two pagers in their panel?) Combine KWeather and KUnique into one application that runs only once and displays a "sorry" message if the user tries to start it a second time. See Listings C.35–C.37 for the answers.

C.11.1.1. Suppose you would like to have only one instance of your panel applet running at a time. (Who would want, for example, two pagers in their panel?) Combine KWeather and KUnique into one application that runs only once and displays a "sorry" message if the user tries to start it a second time. See Listings C.35–C.37 for the answers.

   1 
   2  1: #ifndef __KUNIQUEWEATHER_H__
   3  2: #define __KUNIQUEWEATHER_H__
   4  3:
   5  4: #include <kuniqueapp.h>
   6  5: #include <kpanelapplet.h>
   7  6:
   8  7: class KUniqueWeather : public KUniqueApplication, KPanelApplet
   9  8: {
  10  9:  public:
  11 10:   KUniqueWeather (int& argc, char** argv,
  12 11:              const QCString& rAppName = 0, QWidget *parent=0);
  13 12:
  14 13: protected:
  15 14:   void preferences();
  16 15:
  17 16: };
  18 17:
  19 18: #endif
  20 


Example C.36. kuniqueweather.cpp: Class Definition for KUniqueWeather, a Single-Instance Panel Applet

   1 
   2  1: #include <stdio.h>
   3  2:
   4  3: #include <qlabel.h>
   5  4:
   6  5: #include "kuniqueweather.h"
   7  6:
   8  7: KUniqueWeather::KUniqueWeather (int& argc, char** argv,
   9  8:              const QCString& rAppName, QWidget *parent) :
  10  9:   KUniqueApplication (argc, argv, rAppName),
  11 10:   KPanelApplet (parent)
  12 11: {
  13 12:   QLabel *qlabel = new QLabel ("Rainy\n  48F", this);
  14 13:   qlabel->setAlignment (Qt::AlignVCenter);
  15 14:   setMinimumSize (qlabel->sizeHint());
  16 15:
  17 16:   setActions (Preferences);
  18 17:
  19 18:   dock("kweather");
  20 19: }
  21 20:
  22 21: void
  23 22: KUniqueWeather::preferences()
  24 23: {
  25 24:   printf ("Here we let the user configure the panel applet.\n");
  26 25: }
  27 


Example C.37. main.cpp: The main() Function Used to Start KUniqueWeather

   1 
   2  1: #include <kapp.h>
   3  2: #include <kmessagebox.h>
   4  3:
   5  4: #include "kuniqueweather.h"
   6  5:
   7  6: int
   8  7: main (int argc, char *argv[])
   9  8: {
  10  9:   if (!KUniqueWeather::start(argc, argv, "kuniqueweather"))
  11 10:     {
  12 11:       KApplication a (argc, argv, "kuniqueweather");
  13 12:       KMessageBox::sorry (0, "Cannot start more that one instance of "
  14 13:                      "KUniqueWeather.");
  15 14:       exit (0);
  16 15:     }
  17 16:
  18 17:
  19 18:   KUniqueWeather kuniqueweather (argc, argv, "kuniqueweather");
  20 19:
  21 20:   return  kuniqueweather.exec();
  22 21: }
  23