# HG changeset patch # User Torsten # Date 1382548452 -7200 # Node ID 6a2e483125dd4fdfc14592a4e5bfb4b102076c31 # Parent 64ad713b3a644ba4ad957ba26ed0a40d589c5c91 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 diff -r 64ad713b3a64 -r 6a2e483125dd libgui/src/m-editor/file-editor.cc --- 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); + } + } + } } }