# HG changeset patch # User ttl # Date 1311278781 -7200 # Node ID 8e87f3ba3bebd4cdea33a9aa315a5490b2380fa9 # Parent 8b116446a904b5d591fa4a3329ec67aed6382966 gui-editor: enabled 'Save File As' diff --git a/gui/src/FileEditorMdiSubWindow.cpp b/gui/src/FileEditorMdiSubWindow.cpp --- a/gui/src/FileEditorMdiSubWindow.cpp +++ b/gui/src/FileEditorMdiSubWindow.cpp @@ -81,7 +81,7 @@ { return; // existing file not saved and creating new file canceled by user } - m_fileName = ""; + m_fileName = UNNAMED_FILE; setWindowTitle (m_fileName); m_editor->setText (""); m_editor->setModified (false); // new file is not modified yet @@ -120,40 +120,72 @@ void FileEditorMdiSubWindow::saveFile () { - if (m_fileName.isEmpty ()) + saveFile(m_fileName); +} + +void +FileEditorMdiSubWindow::saveFile (QString fileName) +{ + // it is a new file with the name "" -> call saveFielAs + if (fileName==UNNAMED_FILE) { - m_fileName = - QFileDialog::getSaveFileName (this, "Save File", m_fileName); - if (m_fileName.isEmpty ()) - return; + saveFileAs(); + return; } - QFile file (m_fileName); + // check for a valid file name to save the contents + QString saveFileName; + if (fileName.isEmpty ()) + { + saveFileName = QFileDialog::getSaveFileName (this, "Save File", fileName); + if (saveFileName.isEmpty ()) + return; + } + else + { + saveFileName = fileName; + } + + // open the file + QFile file (saveFileName); if (!file.open (QFile::WriteOnly)) { QMessageBox::warning (this, tr ("File Editor"), tr ("Cannot write file %1:\n%2."). - arg (m_fileName).arg (file.errorString ())); + arg (saveFileName).arg (file.errorString ())); return; } + // save the contents into the file QTextStream out (&file); QApplication::setOverrideCursor (Qt::WaitCursor); out << m_editor->text (); QApplication::restoreOverrideCursor (); - m_statusBar->showMessage (tr ("File saved"), 2000); + m_fileName = saveFileName; // save file name for later use + setWindowTitle(m_fileName); // set the window title to actual file name + m_statusBar->showMessage (tr ("File %1 saved").arg(m_fileName), 2000); m_editor->setModified (false); // files is save -> not modified } void FileEditorMdiSubWindow::saveFileAs () -{ /* - QString saveFileName = QFileDialog::getSaveFileName(this, "Save File", m_fileName); - if(saveFileName.isEmpty()) - return; - - QFile file(saveFileName); - file.open(QFile::WriteOnly); +{ + QString saveDir(m_fileName); + if (saveDir==UNNAMED_FILE) + saveDir = QDir::homePath(); + QString saveFileName = QFileDialog::getSaveFileName( + this, "Save File", saveDir,SAVE_FILE_FILTER); + if(saveFileName.isEmpty()) + return; + saveFile(saveFileName); +/* QFile file(saveFileName); + if (!file.open (QFile::WriteOnly)) + { + QMessageBox::warning (this, tr ("File Editor"), + tr ("Cannot write file %1:\n%2."). + arg (m_fileName).arg (file.errorString ())); + return; + } if(file.write(m_simpleEditor->toPlainText().toLocal8Bit()) == -1) { QMessageBox::warning(this, diff --git a/gui/src/FileEditorMdiSubWindow.h b/gui/src/FileEditorMdiSubWindow.h --- a/gui/src/FileEditorMdiSubWindow.h +++ b/gui/src/FileEditorMdiSubWindow.h @@ -28,6 +28,9 @@ // #include #include +const char UNNAMED_FILE[] = ""; +const char SAVE_FILE_FILTER[] = "Octave Files *.m(*.m);;All Files *.*(*.*)"; + class FileEditorMdiSubWindow:public QMdiSubWindow { Q_OBJECT public: @@ -39,6 +42,7 @@ void newFile (); void saveFile (); + void saveFile (QString fileName); void saveFileAs (); void showToolTipNew ();