changeset 17736:6a2e483125dd

editor asks if a new file should be created when called from edit (bug #40351) * file-editor.cc(request_open_file): new dialog box asking the user if the a non existing file has to be created or not
author Torsten <ttl@justmail.de>
date Wed, 23 Oct 2013 19:14:12 +0200
parents 64ad713b3a64
children d3bb7f1e3971
files libgui/src/m-editor/file-editor.cc
diffstat 1 files changed, 52 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor.cc	Sat Apr 06 13:56:21 2013 +0200
+++ b/libgui/src/m-editor/file-editor.cc	Wed Oct 23 19:14:12 2013 +0200
@@ -295,17 +295,59 @@
               else
                 {
                   delete fileEditorTab;
-                  // Create a NonModal message about error.
-                  QMessageBox *msgBox
-                    = new QMessageBox (QMessageBox::Critical,
-                                       tr ("Octave Editor"),
-                                       tr ("Could not open file %1 for read:\n%2.").
-                                       arg (openFileName).arg (result),
-                                       QMessageBox::Ok, this);
+
+                  if (QFile::exists (openFileName))
+                    {
+                      // File not readable: create a NonModal message about error.
+                      QMessageBox *msgBox
+                        = new QMessageBox (QMessageBox::Critical,
+                                   tr ("Octave Editor"),
+                                   tr ("Could not open file\n%1\nfor read: %2.").
+                                   arg (openFileName).arg (result),
+                                   QMessageBox::Ok, this);
+
+                      msgBox->setWindowModality (Qt::NonModal);
+                      msgBox->setAttribute (Qt::WA_DeleteOnClose);
+                      msgBox->show ();
+                    }
+                  else
+                    {
+                      // File does not exist
+                      QMessageBox *msgBox
+                        = new QMessageBox (QMessageBox::Question,
+                           tr ("Octave Editor"),
+                           tr ("File\n%1\ndoes not exist. "
+                               "Do you want to create it?").arg (openFileName),
+                           QMessageBox::Yes | QMessageBox::No, this);
 
-                  msgBox->setWindowModality (Qt::NonModal);
-                  msgBox->setAttribute (Qt::WA_DeleteOnClose);
-                  msgBox->show ();
+                      // msgBox->setWindowModality (Qt::Modal);
+                      msgBox->setAttribute (Qt::WA_DeleteOnClose);
+                      int answer = msgBox->exec ();
+
+                      if (answer == QMessageBox::Yes)
+                        {
+                          // create the file and call the editor again
+                          QFile file (openFileName);
+                          if (!file.open (QIODevice::WriteOnly))
+                            {
+                              // error opening the file
+                              msgBox = new QMessageBox (QMessageBox::Critical,
+                                   tr ("Octave Editor"),
+                                   tr ("Could not open file\n%1\nfor write: %2.").
+                                   arg (openFileName).arg (file.errorString ()),
+                                   QMessageBox::Ok, this);
+
+                              msgBox->setWindowModality (Qt::NonModal);
+                              msgBox->setAttribute (Qt::WA_DeleteOnClose);
+                              msgBox->show ();
+                            }
+                          else
+                            {
+                              file.close ();
+                              request_open_file (openFileName);
+                            }
+                        }
+                    }
                 }
             }