# HG changeset patch # User Jacob Dawid # Date 1311194671 -7200 # Node ID 7b0a721d0ae870107002cf21541accae27683add # Parent 2426dd43c593aac1c6c606736ee8f6a98be34eec# Parent 8c143d6d0330dfb5688d44f477756bcf6ca93df3 Merge with bitbucket. diff -r 2426dd43c593 -r 7b0a721d0ae8 gui/src/FileEditorMdiSubWindow.cpp --- a/gui/src/FileEditorMdiSubWindow.cpp Wed Jul 20 22:44:02 2011 +0200 +++ b/gui/src/FileEditorMdiSubWindow.cpp Wed Jul 20 22:44:31 2011 +0200 @@ -34,9 +34,21 @@ FileEditorMdiSubWindow::~FileEditorMdiSubWindow () { - while (checkFileModified ("Close File")) +} + +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) { - } // don't close if something went wrong while saving the file + event->ignore(); + } + else + { + event->accept(); + } } void @@ -59,45 +71,50 @@ m_fileName = fileName; setWindowTitle (fileName); m_statusBar->showMessage (tr ("File loaded."), 2000); - m_editor->setModified (false); + m_editor->setModified (false); // loaded file is not modified yet } void FileEditorMdiSubWindow::newFile () { - if (checkFileModified ("Open New File")) - { - return; // something went wrong while saving the old file - } - m_fileName = ""; - setWindowTitle (m_fileName); - m_editor->setText (""); - m_editor->setModified (false); + if (checkFileModified ("Open New File")==QMessageBox::Cancel) + { + return; // existing file not saved and creating new file canceled by user + } + m_fileName = ""; + setWindowTitle (m_fileName); + m_editor->setText (""); + m_editor->setModified (false); // new file is not modified yet } int FileEditorMdiSubWindow::checkFileModified (QString msg) { + int decision = QMessageBox::Yes; if (m_editor->isModified ()) { - int decision = QMessageBox::question (this, - msg, - "Do you want to save the current file?", - QMessageBox::Yes, - QMessageBox::No); + // file is modified but not saved, aks user what to do + decision = QMessageBox::question (this, + msg, + tr ("Do you want to save the current file\n%1 ?"). + arg (m_fileName), + QMessageBox::Cancel, + QMessageBox::No, + QMessageBox::Yes); if (decision == QMessageBox::Yes) - { - 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 error - return (1); - } - } + { + 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); + } + } } - return (0); + return (decision); } void @@ -125,6 +142,7 @@ out << m_editor->text (); QApplication::restoreOverrideCursor (); m_statusBar->showMessage (tr ("File saved"), 2000); + m_editor->setModified (false); // files is save -> not modified } void diff -r 2426dd43c593 -r 7b0a721d0ae8 gui/src/FileEditorMdiSubWindow.h --- a/gui/src/FileEditorMdiSubWindow.h Wed Jul 20 22:44:02 2011 +0200 +++ b/gui/src/FileEditorMdiSubWindow.h Wed Jul 20 22:44:31 2011 +0200 @@ -22,6 +22,7 @@ #include #include #include +#include #include // Not available in the Debian repos yet! // #include @@ -34,7 +35,9 @@ ~FileEditorMdiSubWindow (); void loadFile (QString fileName); - public slots:void newFile (); + public slots: + + void newFile (); void saveFile (); void saveFileAs (); @@ -45,6 +48,10 @@ void showToolTipRedo (); void registerModified (bool modified); + +protected: + void closeEvent(QCloseEvent *event); + private: int checkFileModified (QString msg); void construct ();