1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-24 09:39:51 +00:00

Fix to allow application closing from multiple views and a single view.

This commit is contained in:
graffy76 2013-03-12 06:28:13 -05:00
parent 96b62940b3
commit 50abb22162
7 changed files with 76 additions and 52 deletions

View File

@ -2,7 +2,7 @@
#include "document.hpp" #include "document.hpp"
#include <cassert> #include <cassert>
#include <QDebug>
void CSMDoc::Document::load (const std::vector<boost::filesystem::path>::const_iterator& begin, void CSMDoc::Document::load (const std::vector<boost::filesystem::path>::const_iterator& begin,
const std::vector<boost::filesystem::path>::const_iterator& end, bool lastAsModified) const std::vector<boost::filesystem::path>::const_iterator& end, bool lastAsModified)
{ {
@ -237,6 +237,11 @@ CSMDoc::Document::Document (const std::vector<boost::filesystem::path>& files, b
connect (&mSaveTimer, SIGNAL(timeout()), this, SLOT (saving())); connect (&mSaveTimer, SIGNAL(timeout()), this, SLOT (saving()));
} }
CSMDoc::Document::~Document()
{
qDebug() << "document destroyed";
}
QUndoStack& CSMDoc::Document::getUndoStack() QUndoStack& CSMDoc::Document::getUndoStack()
{ {
return mUndoStack; return mUndoStack;

View File

@ -63,6 +63,7 @@ namespace CSMDoc
public: public:
Document (const std::vector<boost::filesystem::path>& files, bool new_); Document (const std::vector<boost::filesystem::path>& files, bool new_);
~Document();
QUndoStack& getUndoStack(); QUndoStack& getUndoStack();

View File

@ -46,7 +46,8 @@ void CSVDoc::View::setupFileMenu()
file->addAction(close); file->addAction(close);
QAction *exit = new QAction (tr ("&Exit"), this); QAction *exit = new QAction (tr ("&Exit"), this);
connect (exit, SIGNAL (triggered()), QApplication::instance(), SLOT (closeAllWindows())); connect (exit, SIGNAL (triggered()), this, SLOT (exit()));
connect (this, SIGNAL(exitApplicationRequest(CSVDoc::View *)), &mViewManager, SLOT(exitApplication(CSVDoc::View *)));
file->addAction(exit); file->addAction(exit);
} }
@ -131,8 +132,6 @@ CSVDoc::View::View (ViewManager& viewManager, CSMDoc::Document *document, int to
: mViewManager (viewManager), mDocument (document), mViewIndex (totalViews-1), : mViewManager (viewManager), mDocument (document), mViewIndex (totalViews-1),
mViewTotal (totalViews) mViewTotal (totalViews)
{ {
// setDockOptions (QMainWindow::AllowNestedDocks);
resize (300, 300); /// \todo get default size from settings and set reasonable minimal size resize (300, 300); /// \todo get default size from settings and set reasonable minimal size
mSubViewWindow.setDockOptions (QMainWindow::AllowNestedDocks); mSubViewWindow.setDockOptions (QMainWindow::AllowNestedDocks);
@ -255,3 +254,8 @@ CSVDoc::Operations *CSVDoc::View::getOperations() const
{ {
return mOperations; return mOperations;
} }
void CSVDoc::View::exit()
{
emit exitApplicationRequest (this);
}

View File

@ -66,6 +66,8 @@ namespace CSVDoc
void updateActions(); void updateActions();
void exitApplication();
public: public:
View (ViewManager& viewManager, CSMDoc::Document *document, int totalViews); View (ViewManager& viewManager, CSMDoc::Document *document, int totalViews);
@ -92,7 +94,7 @@ namespace CSVDoc
void loadDocumentRequest(); void loadDocumentRequest();
void closeAllViews (View *); void exitApplicationRequest (CSVDoc::View *view);
public slots: public slots:
@ -106,6 +108,8 @@ namespace CSVDoc
void save(); void save();
void exit();
void verify(); void verify();
void addGlobalsSubView(); void addGlobalsSubView();

View File

@ -36,7 +36,7 @@ void CSVDoc::ViewManager::updateIndices()
} }
CSVDoc::ViewManager::ViewManager (CSMDoc::DocumentManager& documentManager) CSVDoc::ViewManager::ViewManager (CSMDoc::DocumentManager& documentManager)
: mDocumentManager (documentManager), mCloseMeOnSaveStateChange(0), mUserWarned(false) : mDocumentManager (documentManager), mExitOnSaveStateChange(false), mUserWarned(false)
{ {
mDelegateFactories = new CSVWorld::CommandDelegateFactoryCollection; mDelegateFactories = new CSVWorld::CommandDelegateFactoryCollection;
@ -45,16 +45,12 @@ CSVDoc::ViewManager::ViewManager (CSMDoc::DocumentManager& documentManager)
mDelegateFactories->add (CSMWorld::ColumnBase::Display_GlobalVarType, mDelegateFactories->add (CSMWorld::ColumnBase::Display_GlobalVarType,
new CSVWorld::VarTypeDelegateFactory (ESM::VT_Short, ESM::VT_Long, ESM::VT_Float)); new CSVWorld::VarTypeDelegateFactory (ESM::VT_Short, ESM::VT_Long, ESM::VT_Float));
connect (this, SIGNAL (exitApplication()), QApplication::instance(), SLOT (closeAllWindows()));
} }
CSVDoc::ViewManager::~ViewManager() CSVDoc::ViewManager::~ViewManager()
{ {
delete mDelegateFactories; delete mDelegateFactories;
//not needed due to deletion in ViewManager::closeRequest?
for (std::vector<View *>::iterator iter (mViews.begin()); iter!=mViews.end(); ++iter) for (std::vector<View *>::iterator iter (mViews.begin()); iter!=mViews.end(); ++iter)
delete *iter; delete *iter;
} }
@ -112,32 +108,40 @@ bool CSVDoc::ViewManager::closeRequest (View *view)
CSMDoc::Document *document = view->getDocument(); CSMDoc::Document *document = view->getDocument();
//notify user of saving in progress if (last)
if ( (document->getState() & CSMDoc::State_Saving) ) continueWithClose = notifySaveOnClose (view);
continueWithClose = showSaveInProgressMessageBox (iter); else
//notify user of unsaved changes and process response
else if ( document->getState() & CSMDoc::State_Modified)
continueWithClose = showModifiedDocumentMessageBox (iter);
if (continueWithClose)
{ {
(*iter)->deleteLater(); (*iter)->deleteLater();
mViews.erase (iter); mViews.erase (iter);
if (last) updateIndices();
mDocumentManager.removeDocument (document);
else
updateIndices();
} }
} }
return continueWithClose; return continueWithClose;
} }
bool CSVDoc::ViewManager::showModifiedDocumentMessageBox (std::vector<View *>::iterator viewIter) bool CSVDoc::ViewManager::notifySaveOnClose (CSVDoc::View *view)
{
bool result = true;
CSMDoc::Document *document = view->getDocument();
//notify user of saving in progress
if ( (document->getState() & CSMDoc::State_Saving) )
result = showSaveInProgressMessageBox (view);
//notify user of unsaved changes and process response
else if ( document->getState() & CSMDoc::State_Modified)
result = showModifiedDocumentMessageBox (view);
return result;
}
bool CSVDoc::ViewManager::showModifiedDocumentMessageBox (CSVDoc::View *view)
{ {
QMessageBox messageBox; QMessageBox messageBox;
CSMDoc::Document *document = view->getDocument();
messageBox.setText ("The document has been modified."); messageBox.setText ("The document has been modified.");
messageBox.setInformativeText ("Do you want to save your changes?"); messageBox.setInformativeText ("Do you want to save your changes?");
@ -147,31 +151,31 @@ bool CSVDoc::ViewManager::showModifiedDocumentMessageBox (std::vector<View *>::i
bool retVal = true; bool retVal = true;
connect (this, SIGNAL (closeMessageBox()), &messageBox, SLOT (close())); connect (this, SIGNAL (closeMessageBox()), &messageBox, SLOT (close()));
connect ((*viewIter)->getDocument(), SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *)));
connect (document, SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *)));
mUserWarned = true; mUserWarned = true;
int response = messageBox.exec(); int response = messageBox.exec();
mUserWarned = false; mUserWarned = false;
switch (response) switch (response)
{ {
case QMessageBox::Save: case QMessageBox::Save:
(*viewIter)->getDocument()->save(); document->save();
mCloseMeOnSaveStateChange = viewIter; mExitOnSaveStateChange = true;
retVal = false; retVal = false;
break; break;
case QMessageBox::Discard: case QMessageBox::Discard:
disconnect ((*viewIter)->getDocument(), SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *))); disconnect (document, SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *)));
break; break;
case QMessageBox::Cancel: case QMessageBox::Cancel:
//disconnect to prevent unintended view closures //disconnect to prevent unintended view closures
disconnect ((*viewIter)->getDocument(), SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *))); disconnect (document, SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *)));
retVal = false; retVal = false;
break; break;
@ -183,9 +187,10 @@ bool CSVDoc::ViewManager::showModifiedDocumentMessageBox (std::vector<View *>::i
return retVal; return retVal;
} }
bool CSVDoc::ViewManager::showSaveInProgressMessageBox (std::vector<View *>::iterator viewIter) bool CSVDoc::ViewManager::showSaveInProgressMessageBox (CSVDoc::View *view)
{ {
QMessageBox messageBox; QMessageBox messageBox;
CSMDoc::Document *document = view->getDocument();
messageBox.setText ("The document is currently being saved."); messageBox.setText ("The document is currently being saved.");
messageBox.setInformativeText("Do you want to close now and abort saving, or wait until saving has completed?"); messageBox.setInformativeText("Do you want to close now and abort saving, or wait until saving has completed?");
@ -199,38 +204,37 @@ bool CSVDoc::ViewManager::showSaveInProgressMessageBox (std::vector<View *>::ite
bool retVal = true; bool retVal = true;
//Connections shut down message box if operation ends before user makes a decision. //Connections shut down message box if operation ends before user makes a decision.
connect ((*viewIter)->getDocument(), SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *))); connect (document, SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *)));
connect (this, SIGNAL (closeMessageBox()), &messageBox, SLOT (close())); connect (this, SIGNAL (closeMessageBox()), &messageBox, SLOT (close()));
//set / clear the user warned flag to indicate whether or not the message box is currently active. //set / clear the user warned flag to indicate whether or not the message box is currently active.
mUserWarned = true; mUserWarned = true;
messageBox.exec(); messageBox.exec();
mUserWarned = false; mUserWarned = false;
//if closed by the warning handler, defaults to the RejectRole button (closeButton) //if closed by the warning handler, defaults to the RejectRole button (closeButton)
if (messageBox.clickedButton() == waitButton) if (messageBox.clickedButton() == waitButton)
{ {
//save the View iterator for shutdown after the save operation ends //save the View iterator for shutdown after the save operation ends
mCloseMeOnSaveStateChange = viewIter; mExitOnSaveStateChange = true;
retVal = false; retVal = false;
} }
else if (messageBox.clickedButton() == closeButton) else if (messageBox.clickedButton() == closeButton)
{ {
//disconnect to avoid segmentation fault //disconnect to avoid segmentation fault
disconnect ((*viewIter)->getDocument(), SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *))); disconnect (document, SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *)));
(*viewIter)->abortOperation(CSMDoc::State_Saving);
mCloseMeOnSaveStateChange = mViews.end(); view->abortOperation(CSMDoc::State_Saving);
mExitOnSaveStateChange = true;
} }
else if (messageBox.clickedButton() == cancelButton) else if (messageBox.clickedButton() == cancelButton)
{ {
//abort shutdown, allow save to complete //abort shutdown, allow save to complete
//disconnection to prevent unintended view closures //disconnection to prevent unintended view closures
mCloseMeOnSaveStateChange = mViews.end(); mExitOnSaveStateChange = false;
disconnect ((*viewIter)->getDocument(), SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *))); disconnect (document, SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (onExitWarningHandler(int, CSMDoc::Document *)));
retVal = false; retVal = false;
} }
@ -262,10 +266,13 @@ void CSVDoc::ViewManager::onExitWarningHandler (int state, CSMDoc::Document *doc
//otherwise, the user has closed the message box before the save operation ended. //otherwise, the user has closed the message box before the save operation ended.
//exit the application //exit the application
else if (mCloseMeOnSaveStateChange!=mViews.end()) else if (mExitOnSaveStateChange)
{ QApplication::instance()->exit();
emit exitApplication();
mCloseMeOnSaveStateChange = mViews.end();
}
} }
} }
void CSVDoc::ViewManager::exitApplication (CSVDoc::View *view)
{
if (notifySaveOnClose (view))
QApplication::instance()->exit();
}

View File

@ -27,7 +27,7 @@ namespace CSVDoc
CSMDoc::DocumentManager& mDocumentManager; CSMDoc::DocumentManager& mDocumentManager;
std::vector<View *> mViews; std::vector<View *> mViews;
CSVWorld::CommandDelegateFactoryCollection *mDelegateFactories; CSVWorld::CommandDelegateFactoryCollection *mDelegateFactories;
std::vector<View *>::iterator mCloseMeOnSaveStateChange; bool mExitOnSaveStateChange;
bool mUserWarned; bool mUserWarned;
// not implemented // not implemented
@ -35,8 +35,9 @@ namespace CSVDoc
ViewManager& operator= (const ViewManager&); ViewManager& operator= (const ViewManager&);
void updateIndices(); void updateIndices();
bool showModifiedDocumentMessageBox (std::vector<View*>::iterator view); bool notifySaveOnClose (View *view = 0);
bool showSaveInProgressMessageBox (std::vector<View*>::iterator view); bool showModifiedDocumentMessageBox (View *view);
bool showSaveInProgressMessageBox (View *view);
public: public:
@ -60,7 +61,9 @@ namespace CSVDoc
void closeMessageBox(); void closeMessageBox();
void exitApplication(); public slots:
void exitApplication (CSVDoc::View *view);
private slots: private slots:

View File

@ -1,6 +1,6 @@
#include <QDebug>
#include <QFileInfo> #include <QFileInfo>
#include <QDir> #include <QDir>
#include <QDebug>
#include <stdexcept> #include <stdexcept>