changeset 13657:347dfbea2c8a

modified editor files can be saved when main window is closed
author ttl <ttl@justmail.de>
date Sun, 28 Aug 2011 00:03:28 +0200
parents 1347d498959c
children d5b84316610d
files gui/src/FileEditorMdiSubWindow.cpp gui/src/FileEditorMdiSubWindow.h gui/src/MainWindow.cpp gui/src/MainWindow.h
diffstat 4 files changed, 44 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/gui/src/FileEditorMdiSubWindow.cpp	Sat Aug 27 00:17:11 2011 +0000
+++ b/gui/src/FileEditorMdiSubWindow.cpp	Sun Aug 28 00:03:28 2011 +0200
@@ -17,7 +17,6 @@
  */
 
 #include "FileEditorMdiSubWindow.h"
-#include "MainWindow.h"
 #include <QVBoxLayout>
 #include <QApplication>
 #include <QFile>
@@ -40,15 +39,18 @@
 void
 FileEditorMdiSubWindow::closeEvent(QCloseEvent *event)
 {
-  // ignore close event if file is not saved and user cancels closing this window
-  // TODO: This does not work if the main window is closed!
-  if (checkFileModified ("Close File")==QMessageBox::Cancel)
+  if ( m_mainWindow->isCloseApplication() )
     {
-      event->ignore();
+      // close wohle application: save file or not if modified
+      checkFileModified ("Close Octave GUI",0); // no cancel possible
     }
   else
     {
-      event->accept();
+      // ignore close event if file is not saved and user cancels closing this window
+      if (checkFileModified ("Close File",QMessageBox::Cancel)==QMessageBox::Cancel)
+        event->ignore();
+      else
+        event->accept();
     }
 }
 
@@ -89,7 +91,7 @@
 void
 FileEditorMdiSubWindow::openFile ()
 {
-    if (checkFileModified ("Open File")==QMessageBox::Cancel)
+    if (checkFileModified ("Open File",QMessageBox::Cancel)==QMessageBox::Cancel)
       {
         return; // existing file not saved and opening another file canceled by user
       }
@@ -133,7 +135,7 @@
 void
 FileEditorMdiSubWindow::newFile ()
 {
-    if (checkFileModified ("Open New File")==QMessageBox::Cancel)
+    if (checkFileModified ("Create New File",QMessageBox::Cancel)==QMessageBox::Cancel)
       {
         return; // existing file not saved and creating new file canceled by user
       }
@@ -144,29 +146,31 @@
 }
 
 int
-FileEditorMdiSubWindow::checkFileModified (QString msg)
+FileEditorMdiSubWindow::checkFileModified (QString msg, int cancelButton)
 {
   int decision = QMessageBox::Yes;
   if (m_editor->isModified ())
     {
       // file is modified but not saved, aks user what to do
-      decision = QMessageBox::question (this,
+      decision = QMessageBox::warning (this,
                                         msg,
-                                        tr ("Do you want to save the current file\n%1 ?").
+                                        tr ("The file %1\n"
+                                            "has been modified. Do you want to save the changes?").
                                           arg (m_fileName),
-                                        QMessageBox::Cancel,
-                                        QMessageBox::No,
-                                        QMessageBox::Yes);
-
-      if (decision == QMessageBox::Yes)
+                                        QMessageBox::Save, QMessageBox::Discard, cancelButton );
+      if (decision == QMessageBox::Save)
         {
           saveFile ();
           if (m_editor->isModified ())
             {
               // If the user attempted to save the file, but it's still
               // modified, then probably something went wrong, so return cancel
-              // for cancel this operation
-              return (QMessageBox::Cancel);
+              // for cancel this operation or try to save files as if cancel not
+              // possible
+              if ( cancelButton )
+                return (QMessageBox::Cancel);
+              else
+                saveFileAs ();
             }
         }
     }
@@ -331,11 +335,13 @@
 // function for setting the already existing lexer from MainWindow
 void
 FileEditorMdiSubWindow::initEditor (TerminalEmulation* terminalEmulation,
-                                    LexerOctaveGui* lexer)
+                                    LexerOctaveGui* lexer,
+                                    MainWindow* mainWindow)
 {
   m_editor->setLexer(lexer);
   m_terminalEmulation = terminalEmulation; // for sending commands to octave
                        // TODO: make a global commandOctave function?
+  m_mainWindow = mainWindow;  // get the MainWindow for chekcing state at subwindow close
 }
 
 // TODO: Do we still need tool tips in the status bar? Tool tips are now
--- a/gui/src/FileEditorMdiSubWindow.h	Sat Aug 27 00:17:11 2011 +0000
+++ b/gui/src/FileEditorMdiSubWindow.h	Sun Aug 28 00:03:28 2011 +0200
@@ -19,6 +19,7 @@
 #ifndef FILEEDITORMDISUBWINDOW_H
 #define FILEEDITORMDISUBWINDOW_H
 
+#include "MainWindow.h"
 #include "TerminalEmulation.h"
 #include <QMdiSubWindow>
 #include <QToolBar>
@@ -45,7 +46,9 @@
   FileEditorMdiSubWindow (QWidget * parent = 0);
   ~FileEditorMdiSubWindow ();
   void loadFile (QString fileName);
-  void initEditor (TerminalEmulation *terminalEmulation, LexerOctaveGui *lexer);
+  void initEditor (TerminalEmulation *terminalEmulation,
+                   LexerOctaveGui *lexer,
+                   MainWindow *mainWindow);
 
 public slots:
 
@@ -67,7 +70,7 @@
   void closeEvent(QCloseEvent *event);
 
 private:
-  int checkFileModified (QString msg);
+  int checkFileModified (QString msg, int cancelButton);
   void construct ();
   void doCommentSelectedText (bool comment);
   QMenuBar *m_menuBar;
@@ -78,6 +81,7 @@
   TerminalEmulation* m_terminalEmulation;
   QAction* m_copyAction;
   QAction* m_cutAction;
+  MainWindow* m_mainWindow;
   int m_markerBookmark;
   bool m_modified;
 
--- a/gui/src/MainWindow.cpp	Sat Aug 27 00:17:11 2011 +0000
+++ b/gui/src/MainWindow.cpp	Sun Aug 28 00:03:28 2011 +0200
@@ -96,7 +96,7 @@
          }
        m_lexerAPI->prepare();           // prepare API info ... this make take some time
     }
-  subWindow->initEditor(m_terminalView->terminalEmulation(), m_lexer);   // init necessary informations for editor
+  subWindow->initEditor(m_terminalView->terminalEmulation(), m_lexer, this);   // init necessary informations for editor
 
   if ( fileName.isEmpty() )
     subWindow->newFile ();
@@ -237,8 +237,10 @@
 {
   reportStatusMessage (tr ("Saving data and shutting down."));
   writeSettings ();
-
+  m_closeApplication = true;  // inform editor window that whole application is closed
   OctaveLink::instance ()->terminateOctave();
+  m_centralMdiArea->closeAllSubWindows();   // send close events to subwindows
+                                            // (editor files can be saved!)
   QMainWindow::closeEvent (closeEvent);
 }
 
@@ -264,6 +266,7 @@
 void
 MainWindow::construct ()
 {
+  m_closeApplication = false;   // flag for editor files when closed
   setWindowIcon (ResourceManager::instance ()->icon (ResourceManager::Octave));
 
   // Initialize MDI area.
--- a/gui/src/MainWindow.h	Sat Aug 27 00:17:11 2011 +0000
+++ b/gui/src/MainWindow.h	Sun Aug 28 00:03:28 2011 +0200
@@ -79,6 +79,11 @@
   {
     return m_filesDockWidget;
   }
+  bool isCloseApplication ()
+  {
+    return m_closeApplication;
+  }
+
 signals:
   void settingsChanged ();
 
@@ -132,6 +137,9 @@
   // Toolbars.
   QStatusBar *m_statusBar;
 
+  // Flag for closing whole application
+  bool m_closeApplication;
+
 };
 
 #endif // MAINWINDOW_H