C.5. Chapter 5

C.5.1. Exercises

C.5.1.1. Use KStatusBar::insertWidget() to insert the KDE widget of your choice into the statusbar. Is the widget appropriate for the statusbar? What information does it convey to the user? (See Listings C.12–C.14.)
C.5.1.2. Create a document-centric application that has QMultiLineEdit as its client area. Be sure to use KMenuBar, KToolBar, and KStatusBar. Include New and Quit on the File menu and New on the toolbar. Put the line number into the statusbar. (You will need to refer to the Qt documentation for QMultiLineEdit for this exercise.) See Listings C.15–C.17.

C.5.1.1. Use KStatusBar::insertWidget() to insert the KDE widget of your choice into the statusbar. Is the widget appropriate for the statusbar? What information does it convey to the user? (See Listings C.12–C.14.)

   1 
   2 #ifndef __KSTATWIDGET_H__
   3 #define __KSTATWIDGET_H__
   4 
   5 class KProgress;
   6 class QTimer;
   7 
   8 #include <ktmainwindow.h>
   9 
  10 /**
  11  * KStatWidget
  12  * Put a progress bar on the statusbar.
  13  **/
  14 class KStatWidget : public KTMainWindow
  15 {
  16  Q_OBJECT
  17 
  18  public:
  19   KStatWidget (const char *name=0);
  20 
  21   public slots:
  22   /**
  23    * Advance the progress bar.
  24    **/
  25    void slotTimeout ();
  26 
  27  private:
  28   KProgress *kprogress;
  29   QTimer *qtimer;
  30 };
  31 
  32 #endif
  33 


Example C.13. kstatwidget.cpp: Class Definition for KStatWidget

   1 
   2 #include <qlabel.h>
   3 #include <qpainter.h>
   4 #include <qtimer.h>
   5 
   6 #include <kprogress.h>
   7 
   8 #include "kstatwidget.moc"
   9 
  10 KStatWidget::KStatWidget (const char *name=0) :
  11   KTMainWindow (name)
  12 {
  13   kprogress = new KProgress (0, 100, 0,
  14                             KProgress::Horizontal,
  15                             statusBar());
  16 
  17   statusBar()->insertItem ("Progress: ",1);
  18 
  19   //This widget is stretched to fit the window.
  20   statusBar()->insertWidget (kprogress, 1, 2);
  21   QTimer *qtimer = new QTimer;
  22   connect ( qtimer, SIGNAL (timeout()),
  23             this, SLOT (slotTimeout()) );
  24   qtimer->start (500);
  25 
  26   QLabel *qlabel = new QLabel (this);
  27   setView (qlabel);
  28 }
  29 
  30 void
  31 KStatWidget::slotTimeout()
  32 {
  33   kprogress->advance (10);
  34 }
  35 


Example C.14. main.cpp: main() Function, Which Can Be Used to Try KStatWidget

   1 
   2 #include <kapp.h>
   3 
   4 #include "kstatwidget.h"
   5 
   6 int main (int argc, char *argv[])
   7 {
   8   KApplication *kapplication = new KApplication (argc, argv, "kstatwidgettest");
   9 KStatWidget *kstatwidget = new KStatWidget (0);
  10 
  11   kapplication->setMainWidget (kstatwidget);
  12 
  13   kstatwidget->show();
  14   kapplication->exec();
  15 }
  16 

C.5.1.2. Create a document-centric application that has QMultiLineEdit as its client area. Be sure to use KMenuBar, KToolBar, and KStatusBar. Include New and Quit on the File menu and New on the toolbar. Put the line number into the statusbar. (You will need to refer to the Qt documentation for QMultiLineEdit for this exercise.) See Listings C.15–C.17.

   1 
   2 #ifndef __KEDITOR_H__
   3 #define __KEDITOR_H__
   4 
   5 #include <ktmainwindow.h>
   6 
   7 class QMultiLineEdit;
   8 
   9 class KEditor : public KTMainWindow
  10 {
  11  Q_OBJECT
  12  public:
  13   KEditor (const char *name=0);
  14 
  15  protected slots:
  16    /**
  17     * Update the line number field in the statusbar.
  18     **/
  19    void slotUpdateStatusBar ();
  20 
  21  private:
  22   QMultiLineEdit *qmle;
  23 };
  24 
  25 #endif
  26 


Example C.16. keditor.cpp: Class Definition for KEditor

   1 
   2 #include <qmultilineedit.h>
   3 
   4 #include <kapp.h>
   5 #include <kiconloader.h>
   6 #include <kmenubar.h>
   7 #include <kstdaction.h>
   8 #include <kaction.h>
   9 
  10 #include "keditor.moc"
  11 
  12 //Status Bar id
  13 const int SBLineNumber = 2;
  14 
  15 KEditor::KEditor (const char *name) : KTMainWindow (name)
  16 {
  17   qmle = new QMultiLineEdit (this);
  18 
  19   KStdAction::openNew (qmle, SLOT (clear()), actionCollection());
  20   KStdAction::quit (kapp, SLOT (closeAllWindows()), actionCollection());
  21 
  22   createGUI();
  23 
  24   statusBar()->insertItem ("Line", 1);
  25   statusBar()->insertItem ("0000", SBLineNumber);
  26   slotUpdateStatusBar();
  27 
  28   connect ( qmle, SIGNAL (textChanged()),
  29             this, SLOT (slotUpdateStatusBar()) );
  30 
  31 
  32   setView (qmle);
  33 }
  34 
  35 void
  36 KEditor::slotUpdateStatusBar ()
  37 {
  38   QString linenumber;
  39   int line, col;
  40 
  41   qmle->getCursorPosition (&line, &col);
  42   linenumber.sprintf ("%4d", line);
  43 
  44   statusBar()->changeItem (linenumber, SBLineNumber);
  45 }
  46 


Example C.17. main.cpp: main() Function, Which Can Be Used to Test KEditor

   1 
   2 #include <kapp.h>
   3 
   4 #include "keditor.h"
   5 
   6 int
   7 main (int argc, char *argv[])
   8 {
   9   KApplication kapplication  (argc, argv, "keditor");
  10   KEditor *keditor = new KEditor (0);
  11 
  12   kapplication.setMainWidget (keditor);
  13 
  14   keditor->show();
  15   return kapplication.exec();
  16 }
  17