C.6. Chapter 6

C.6.1. Exercises

C.6.1.1. Improve the program you wrote for Exercise 2 from Chapter 5. Create a full-featured Edit menu (with Copy, Paste, and Cut, Undo, and Redo), and support file saving and opening, with KIO::NetAccess. (See Listing C.18.)
C.6.1.2. Use KRun to execute a program (and tell the user of its completion). Store the text of the KLineEdit for the sake of session management. (See Listing C.19.)

C.6.1.1. Improve the program you wrote for Exercise 2 from Chapter 5. Create a full-featured Edit menu (with Copy, Paste, and Cut, Undo, and Redo), and support file saving and opening, with KIO::NetAccess. (See Listing C.18.)

   1 
   2 keditor.h: Class Declaration for KEditor
   3 #ifndef __KEDITOR_H__
   4 #define __KEDITOR_H__
   5 
   6 #include <ktmainwindow.h>
   7 #include <kurl.h>
   8 
   9 class QMultiLineEdit;
  10 
  11 class KEditor : public KTMainWindow
  12 {
  13  Q_OBJECT
  14  public:
  15   KEditor (const char *name=0);
  16 
  17  protected slots:
  18   /**
  19    * Update the line number field in the statusbar.
  20    **/
  21   void slotUpdateStatusBar ();
  22   /**
  23    * Open the "Save As" dialog.
  24    **/
  25   void slotSaveAs();
  26   /**
  27    * Save the file.
  28    **/
  29   void slotSave();
  30   /**
  31    * Open the "Open" dialog.
  32    **/
  33   void slotOpen();
  34 
  35  private:
  36   QMultiLineEdit *qmle;
  37   KURL url;
  38   QString file;
  39 };
  40 
  41 #endif
  42 keditor.cpp: Class Definition for KEditor,
  43 #include <qmultilineedit.h>
  44 
  45 #include <kapp.h>
  46 #include <kiconloader.h>
  47 #include <kmenubar.h>
  48 #include <kstdaction.h>
  49 #include <kaction.h>
  50 
  51 #include <netaccess.h>
  52 #include <ktempfile.h>
  53 
  54 #include "keditor.moc"
  55 
  56 //Status Bar id
  57 const int SBLineNumber = 2;
  58 
  59 KEditor::KEditor (const char *name) : KTMainWindow (name)
  60 {
  61   qmle = new QMultiLineEdit (this);
  62 
  63   KStdAction::openNew (qmle, SLOT (clear()), actionCollection());
  64   KStdAction::quit (kapp, SLOT (closeAllWindows()), actionCollection());
  65   KStdAction::copy (qmle, SLOT (copy()), actionCollection());
  66   KStdAction::cut (qmle, SLOT (cut()), actionCollection());
  67   KStdAction::paste (qmle, SLOT (paste()), actionCollection());
  68   KStdAction::undo (qmle, SLOT (undo()), actionCollection());
  69   KStdAction::redo (qmle, SLOT (redo()), actionCollection());
  70 
  71   KStdAction::open(this, SLOT(slotOpen()), actionCollection());
  72   KStdAction::save(this, SLOT(slotSave()), actionCollection());
  73   KStdAction::saveAs(this, SLOT(slotSaveAs()), actionCollection());
  74 
  75   createGUI();
  76 
  77   statusBar()->insertItem ("Line", 1);
  78   statusBar()->insertItem ("0000", SBLineNumber);
  79   slotUpdateStatusBar();
  80   connect ( qmle, SIGNAL (textChanged()),
  81         this, SLOT (slotUpdateStatusBar()) );
  82 
  83 
  84   setView (qmle);
  85 }
  86 
  87 void
  88 KEditor::slotUpdateStatusBar ()
  89 {
  90   QString linenumber;
  91   int line, col;
  92 
  93   qmle->getCursorPosition (&line, &col);
  94   linenumber.sprintf ("%4d", line);
  95 
  96   statusBar()->changeItem (linenumber, SBLineNumber);
  97 }
  98 
  99 void
 100 KEditor::slotSaveAs()
 101 {
 102   url=KFileDialog::getSaveUrl(0,
 103         "*.txt|Text Files (*.txt)",this)
 104   file=url.path();
 105 
 106   if (!file.isLocalPath())
 107   {
 108      KTempFile temp;
 109      file=temp.name();
 110 
 111      slotSave();
 112      temp.unlink();
 113      return;
 114   }
 115   slotSave();
 116 }
 117 
 118 void
 119 KEditor::slotSave()
 120 {
 121   if (url.isEmpty() || file.isEmpty())
 122     slotSaveAs(), return;
 123   QFile f(file);
 124 
 125   if (!f.open(IO_WriteOnly | IO_Truncate))
 126     KNotifyClient::event("cannotopenfile"), return;
 127 
 128   QTextStream t( &f );
 129   t << qmle->text();
 130 
 131   f.close();
 132   qmle->setEdited(false);
 133 }
 134 
 135 void
 136 KEditor::slotOpen()
 137 {
 138   if ( qmle->edited() )
 139   {
 140     int result=KMessageBox::questionYesNo(this,
 141       i18n("You already have a file open! Would you like
 142            "to save the currently "
 143            "opened file and open another?"),
 144       i18n("Continue?"));
 145 
 146     if (result==KMessageBox::Yes)
 147       slotSave();
 148     else
 149       return;
 150   }
 151 
 152   url=KFileDialog::getOpenURL(0,
 153         "*.txt|Text Files (*.txt)", this);
 154 
 155   if (!KIO::NetAccess::download(url, file))
 156     KNotifyClient::event("cannotopenfile"), return;
 157 
 158   QFile f(file);
 159   if (!f.open(IO_ReadOnly))
 160     KNotifyClient::event("cannotopenfile"), return;
 161 
 162   QTextStream t( &f );
 163   QString text(t.read());
 164   qmle->clear();
 165   qmle->setText(text);
 166   f.close();
 167 }
 168 
 169 // main.cpp: main() which can be used to test KEditor
 170 #include <kapp.h>
 171 
 172 #include "keditor.h"
 173 
 174 int
 175 main (int argc, char *argv[])
 176 {
 177   KApplication kapplication  (argc, argv, "keditor");
 178   KEditor *keditor = new KEditor (0);
 179 
 180   kapplication.setMainWidget (keditor);
 181 
 182   keditor->show();
 183   return kapplication.exec();
 184 }
 185 

C.6.1.2. Use KRun to execute a program (and tell the user of its completion). Store the text of the KLineEdit for the sake of session management. (See Listing C.19.)

   1 
   2 kjogger.h: Class Declaration for KJogger
   3 #ifndef __KJOGGER_H__
   4 #define __KJOGGER_H__
   5 
   6 #include <ktmainwindow.h>
   7 #include <klineedit.h>
   8 #include <kprocess.h>
   9 #include <kconfig.h>
  10 
  11 class JogView;
  12 
  13 class KJogger : public KTMainWindow
  14 {
  15  Q_OBJECT
  16  public:
  17   KJogger (const char *name=0);
  18 
  19  protected:
  20   void saveProperties(KConfig* config);
  21   void readProperties(KConfig* config);
  22 
  23  private:
  24   JogView *view;
  25 
  26 };
  27 
  28 class JogView : public KLineEdit
  29 {
  30  Q_OBJECT
  31  public:
  32   JogView(QWidget *parent);
  33 
  34  protected slots:
  35   /**
  36    * Run the program.
  37    **/
  38   void slotRun();
  39   /**
  40    * Enable the KLineEdit that we are.
  41    **/
  42   void slotEnable(KProcess*);
  43  private:
  44   KProcess proc;
  45 };
  46 
  47 #endif
  48 
  49 kjogger.cpp: Class Definition for KJogger,
  50 
  51 #include <kapp.h>
  52 #include <kmenubar.h>
  53 #include <kstdaction.h>
  54 #include <kaction.h>
  55 
  56 #include "kjogger.moc"
  57 
  58 
  59 KJogger::KJogger (const char *name) : KTMainWindow (name)
  60 {
  61   KStdAction::quit (kapp, SLOT (closeAllWindows()),
  62     actionCollection());
  63   createGUI();
  64   view=new JogView(this);
  65 
  66   setView (view);
  67 
  68 }
  69 
  70 void
  71 KJogger::saveProperties(KConfig* config)
  72 {
  73   config->writeEntry("program",view->text());
  74 }
  75 
  76 void
  77 KJogger::readProperties(KConfig* config)
  78 {
  79   view->setText(config->readEntry("program",""));
  80 }
  81 
  82 JogView::JogView (QWidget *parent) : KLineEdit(parent)
  83 {
  84   connect(this, SIGNAL(returnPressed()), SLOT (slotRun()) );
  85   connect(&proc, SIGNAL (processExited(KProcess*)), SLOT (slotEnable(KProcess*)) );
  86 }
  87 
  88 void
  89 JogView::slotRun()
  90 {
  91   setEnabled(false);
  92   proc.clearArguments();
  93   proc << text();
  94 
  95   proc.start();
  96 }
  97 
  98 void
  99 JogView::slotEnable(KProcess*)
 100 {
 101   setEnabled(true);
 102 }
 103 
 104 
 105 // A main() function required to test this program.
 106 #include "kjogger.h"
 107 #include <kapp.h>
 108 #include <dcopclient.h>
 109 
 110 int main(int argc, char **argv)
 111 {
 112   KApplication app(argc, argv, "kjogger");
 113   app.dcopClient()->registerAs(app.name());
 114 
 115   if (app.isRestored())
 116      RESTORE(KJogger)
 117   else
 118   {
 119      KJogger *widget = new KJogger;
 120      widget->show();
 121   }
 122 
 123   return app.exec();
 124 }
 125