LJ Archive

Listing 2. editor.cpp

  1 #include "editor.h"
  2
  3 #include <qtextedit.h>
  4 #include <qmessagebox.h>
  5 #include <qfiledialog.h>
  6 #include <qfile.h>
  7 #include <qstatusbar.h>
  8 #include <qstylesheet.h>
  9 #include <qapplication.h>
 10
 11 /*
 12  *  Constructs an Editor which is a child of
 13  *  "parent", with the name "name" and widget
 14  *  flags set to "f".
 15  */
 16 Editor::Editor( QWidget* parent,
 17                 const char* name, WFlags fl )
 18     : ljeditor( parent, name, fl )
 19 {
 20     editField = ljeditor::TextEdit;
 21     fileName = "";
 22 }
 23
 24 /*
 25  * Destroys the object and frees any allocated
 26  * resources.
 27  */
 28 Editor::~Editor()
 29 {
 30     // no need to delete child widgets,
 31     // Qt does it all for us
 32 }
 33
 34 /*
 35  * Opens a new editor window.
 36  */
 37 void Editor::fileNew()
 38 {
 39     Editor * editor = new Editor;
 40     editor->show();
 41 }
 42

...

154 /*
155  * A window close event occurs
156  */
157 void Editor::closeEvent( QCloseEvent * close )
158 {
159     if ( saveAndContinue(
160         tr( "Closing aborted" ) ) )
161         close->accept();
162     else
163         close->ignore();
164 }


166 /*
167  * Ask user to save the old file or to cancel
168  * the operation.
169  */
170 bool Editor::saveAndContinue(
171     QString abortMessage )
172 {
173     if ( ! fileName.isEmpty() ||
174         ! editField->text().isEmpty() ){
175   switch( QMessageBox::information( this,
176             fileName,
177             QString( tr( "Save %1?" ) ).arg(
178                 fileName ),
179             tr( "Yes" ), tr( "No" ),
180             tr( "Cancel" ) ) ){
181         case 0: if ( ! fileName.isEmpty() )
182                     fileSave();
183                 else
184                     fileSaveAs();
185                 return TRUE;
186                 break;
187         case 1: statusBar()->message(
188                     QString(
189                         tr( "%1 not saved" )
190                            ).arg(fileName),
191                     2000 );
192                 return TRUE;
193                 break;
194         case 2: statusBar()->message(
195                     abortMessage, 2000 );
196                 return FALSE;
197                 break;
198         default: return FALSE;
199         }
200     } else {
201   return TRUE;
202     }
203 }
LJ Archive