C.2. Chapter 2

C.2.1. Exercises

C.2.1.1. Referring to the KDE class documentation for KToolBar, modify KSimpleApp to include a line editor on the toolbar.
C.2.1.2. Modify KSimpleApp to put a QMultiLineEdit widget in the content area instead of a QLabel. Replace all the references to the Reposition Text function with a function that clears the widget. You will need to refer to the Qt class documentation for QMultiLineEdit.

C.2.1.1. Referring to the KDE class documentation for KToolBar, modify KSimpleApp to include a line editor on the toolbar.

ksimpleapp.h and ksimpleapp.cpp were modified. You may use main.cpp, given in Listing 2.4, to create a complete application. Listings C.1 and C.2 display these modifications.


Example C.1. Modified ksimpleapp.h

   1 
   2  1: #include <ktmainwindow.h>
   3  2:
   4  3: /**
   5  4:  * This is a simple KDE application.
   6  5:  * @author David Sweet <dsweet@kde.org>
   7  6:  **/
   8  7:
   9  8: class QLabel;
  10  9:
  11 10: class KSimpleApp : public KTMainWindow
  12 11: {
  13 12:  Q_OBJECT
  14 13:
  15 14:  public:
  16 15:   /**
  17 16:    * Create the widget.
  18 17:    **/
  19 18:   KSimpleApp (const char *name=0);
  20 19:
  21 20:   public slots:
  22 21:     /**
  23 22:      * Reposition the text in the context area. The user will
  24 23:      *  cycle through: left, center, and right.
  25 24:      **/
  26 25:     void slotRepositionText();
  27 26:
  28 27:   /**
  29 28:    * Close the window (thus quitting the application).
  30 29:    **/
  31 30:   void slotClose();
  32 31:
  33 32:   /**
  34 33:    * Chapter 2, Exercise 1
  35 34:    * Respond to an Enter keypress.
  36 35:    **/
  37 36:   void slotEnterPressed();
  38 37:
  39 38:  private:
  40 39:   QLabel *text;
  41 40:   int alignment [3], indexalignment;
  42 41: };
  43 


Example C.2. Modified ksimpleapp.cpp

   1 
   2  1: #include <qlabel.h>
   3  2:
   4  3: #include <kstdaccel.h>
   5  4: #include <kiconloader.h>
   6  5: #include <kmenubar.h>
   7  6: #include <kapp.h>
   8  7:
   9  8: #include "ksimpleapp.moc"
  10  9:
  11 10: KSimpleApp::KSimpleApp (const char *name) :
  12 11:   KTMainWindow (name)
  13 12: {
  14 13:   KStdAccel keys;
  15 14:
  16 15:   QPopupMenu *filemenu = new QPopupMenu;
  17 16:   filemenu->insertItem (BarIcon ("idea"), "&Reposition Text",
  18 17:                    this, SLOT (slotRepositionText()),
  19 18:                    CTRL+Key_R);
  20 19:   filemenu->insertSeparator();
  21 20:   filemenu->insertItem ("&Quit", this, SLOT (slotClose()), keys.quit());
  22 21:
  23 22:   menuBar()->insertItem ("&File", filemenu);
  24 23:
  25 24:   const int buttonid = 1;
  26 25:   toolBar()->insertButton ( BarIcon("idea"), buttonid,
  27 26:                        SIGNAL(clicked()), this,
  28 27:                        SLOT (slotRepositionText()), true,
  29 28:                        "Reposition text" );
  30 29:
  31 30:   //Chapter 2, Exercise 1
  32 31:   const int linedid = 2;
  33 32:   toolBar()->insertLined ("Initial text", linedid,
  34 33:                      SIGNAL(returnPressed()), this,
  35 34:                      SLOT (slotEnterPressed()), true);
  36 35:
  37 36:   statusBar()->message ("Ready!");
  38 37:
  39 38:   text = new QLabel ("Hello!", this);
  40 39:   text->setBackgroundColor (Qt::white);
  41 40:   alignment [0] = QLabel::AlignLeft | QLabel::AlignVCenter;
  42 41:   alignment [1] = QLabel::AlignHCenter | QLabel::AlignVCenter;
  43 42:   alignment [2] = QLabel::AlignRight | QLabel::AlignVCenter;
  44 43:   indexalignment = 0;
  45 44:
  46 45:   text->setAlignment (alignment [indexalignment]);
  47 46:   setView (text);
  48 47:
  49 48: }
  50 49:
  51 50: //Chapter 2, Exercise 1
  52 51: void
  53 52: KSimpleApp::slotEnterPressed()
  54 53: {
  55 54:   //You would process the Enter keypress here.
  56 55: }
  57 56:
  58 57: void
  59 58: KSimpleApp::slotRepositionText ()
  60 59: {
  61 60:   indexalignment = (indexalignment+1)%3;
  62 61:   text->setAlignment (alignment[indexalignment]);
  63 62:
  64 63:   statusBar()->message ("Repositioned text in content area", 1000);
  65 64: }
  66 65:
  67 66: void
  68 67: KSimpleApp::slotClose()
  69 68: {
  70 69:   close();
  71 70: }
  72 

C.2.1.2. Modify KSimpleApp to put a QMultiLineEdit widget in the content area instead of a QLabel. Replace all the references to the Reposition Text function with a function that clears the widget. You will need to refer to the Qt class documentation for QMultiLineEdit.

ksimpleapp.h and ksimpleapp.cpp were modified. You may use main.cpp, given in Listing 2.4, to create a complete application.

Modifications to Listings C.3 and C.4 are marked in comments.


Example C.3. Modified ksimpleapp.h

   1 
   2  1: #include <ktmainwindow.h>
   3  2:
   4  3: /**
   5  4:  * This is a simple KDE application.
   6  5:  * @author David Sweet <dsweet@kde.org>
   7  6:  **/
   8  7:
   9  8: class QLabel;
  10  9: class QMultiLineEdit;
  11 10:
  12 11: class KSimpleApp : public KTMainWindow
  13 12: {
  14 13:  Q_OBJECT
  15 14:
  16 15:  public:
  17 16:   /**
  18 17:    * Create the widget.
  19 18:    **/
  20 19:   KSimpleApp (const char *name=0);
  21 20:
  22 21:   public slots:
  23 22:     /**
  24 23:      * Chapter 2, Exercise 2
  25 24:      * Clear the text in the content area.
  26 25:      **/
  27 26:     void slotClearText();
  28 27:
  29 28:   /**
  30 29:    * Close the window (thus quitting the application).
  31 30:    **/
  32 31:   void slotClose();
  33 32:
  34 33:  private:
  35 34:   QLabel *text;
  36 35:   int alignment [3], indexalignment;
  37 36:   //Chapter 2, Exercise 2
  38 37:   QMultiLineEdit *editor;
  39 38: };
  40 


Example C.4. Modified ksimpleapp.cpp

   1 
   2  1: #include <qlabel.h>
   3  2: #include <qmultilineedit.h>
   4  3:
   5  4: #include <kstdaccel.h>
   6  5: #include <kiconloader.h>
   7  6: #include <kmenubar.h>
   8  7: #include <kapp.h>
   9  8:
  10  9: #include "ksimpleapp.moc"
  11 10:
  12 11: KSimpleApp::KSimpleApp (const char *name) :
  13 12:   KTMainWindow (name)
  14 13: {
  15 14:   KStdAccel keys;
  16 15:
  17 16:   QPopupMenu *filemenu = new QPopupMenu;
  18 17:   //Chapter 2, Exercise 2
  19 18:   filemenu->insertItem (BarIcon ("idea"), "&Clear text",
  20 19:                    this, SLOT (slotClearText()),
  21 20:                    CTRL+Key_C);
  22 21:   filemenu->insertSeparator();
  23 22:   filemenu->insertItem ("&Quit", this, SLOT (slotClose()), keys.quit());
  24 23:
  25 24:   menuBar()->insertItem ("&File", filemenu);
  26 25:
  27 26:   const int buttonid = 1;
  28 27:   //Chapter 2, Exercise 2
  29 28:   toolBar()->insertButton ( BarIcon("idea"), buttonid,
  30 29:                        SIGNAL(clicked()), this,
  31 30:                        SLOT (slotClearText()), true,
  32 31:                        "Clear text" );
  33 32:
  34 33:
  35 34:   statusBar()->message ("Ready!");
  36 35:
  37 36:
  38 37:   //Chapter 2, Exercise 2
  39 38:   editor = new QMultiLineEdit (this);
  40 39:   editor->setText ("Initial text.");
  41 40:
  42 41:   setView (editor);
  43 42:
  44 43: }
  45 44:
  46 45: void
  47 46: KSimpleApp::slotClearText ()
  48 47: {
  49 48:   //Chapter 2, Exercise 2
  50 49:   editor->setText ("");
  51 50:   statusBar()->message ("Cleared text in content area", 1000);
  52 51: }
  53 52:
  54 53: void
  55 54: KSimpleApp::slotClose()
  56 55: {
  57 56:   close();
  58 57: }
  59