2012-11-22 12:30:02 +00:00
|
|
|
|
|
|
|
#include "viewmanager.hpp"
|
|
|
|
|
2012-11-22 14:09:04 +00:00
|
|
|
#include <map>
|
|
|
|
|
2012-11-22 13:10:23 +00:00
|
|
|
#include "../../model/doc/documentmanager.hpp"
|
2012-11-22 22:42:17 +00:00
|
|
|
#include "../../model/doc/document.hpp"
|
2012-11-22 13:10:23 +00:00
|
|
|
|
2013-02-10 16:21:25 +00:00
|
|
|
#include "../world/util.hpp"
|
2013-02-17 16:27:25 +00:00
|
|
|
#include "../world/enumdelegate.hpp"
|
|
|
|
#include "../world/vartypedelegate.hpp"
|
2013-02-10 16:21:25 +00:00
|
|
|
|
2012-11-22 12:30:02 +00:00
|
|
|
#include "view.hpp"
|
|
|
|
|
2013-03-02 13:57:41 +00:00
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
|
2012-11-22 14:09:04 +00:00
|
|
|
void CSVDoc::ViewManager::updateIndices()
|
|
|
|
{
|
|
|
|
std::map<CSMDoc::Document *, std::pair<int, int> > documents;
|
|
|
|
|
|
|
|
for (std::vector<View *>::const_iterator iter (mViews.begin()); iter!=mViews.end(); ++iter)
|
|
|
|
{
|
|
|
|
std::map<CSMDoc::Document *, std::pair<int, int> >::iterator document = documents.find ((*iter)->getDocument());
|
|
|
|
|
|
|
|
if (document==documents.end())
|
|
|
|
document =
|
|
|
|
documents.insert (
|
|
|
|
std::make_pair ((*iter)->getDocument(), std::make_pair (0, countViews ((*iter)->getDocument())))).
|
|
|
|
first;
|
|
|
|
|
|
|
|
(*iter)->setIndex (document->second.first++, document->second.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-22 13:10:23 +00:00
|
|
|
CSVDoc::ViewManager::ViewManager (CSMDoc::DocumentManager& documentManager)
|
2013-03-02 13:57:41 +00:00
|
|
|
: mDocumentManager (documentManager), mCloseMeOnSaveStateChange(0)
|
2012-11-22 12:30:02 +00:00
|
|
|
{
|
2013-02-10 16:21:25 +00:00
|
|
|
mDelegateFactories = new CSVWorld::CommandDelegateFactoryCollection;
|
2013-02-17 16:27:25 +00:00
|
|
|
|
|
|
|
mDelegateFactories->add (CSMWorld::ColumnBase::Display_VarType,
|
|
|
|
new CSVWorld::VarTypeDelegateFactory (ESM::VT_None, ESM::VT_String, ESM::VT_Int, ESM::VT_Float));
|
2012-11-22 12:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CSVDoc::ViewManager::~ViewManager()
|
|
|
|
{
|
2013-02-10 16:21:25 +00:00
|
|
|
delete mDelegateFactories;
|
|
|
|
|
2012-11-22 12:30:02 +00:00
|
|
|
for (std::vector<View *>::iterator iter (mViews.begin()); iter!=mViews.end(); ++iter)
|
|
|
|
delete *iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSVDoc::View *CSVDoc::ViewManager::addView (CSMDoc::Document *document)
|
|
|
|
{
|
2012-11-22 22:42:17 +00:00
|
|
|
if (countViews (document)==0)
|
|
|
|
{
|
|
|
|
// new document
|
|
|
|
connect (document, SIGNAL (stateChanged (int, CSMDoc::Document *)),
|
|
|
|
this, SLOT (documentStateChanged (int, CSMDoc::Document *)));
|
2012-11-23 09:25:34 +00:00
|
|
|
|
2012-11-23 11:20:35 +00:00
|
|
|
connect (document, SIGNAL (progress (int, int, int, int, CSMDoc::Document *)),
|
|
|
|
this, SLOT (progress (int, int, int, int, CSMDoc::Document *)));
|
2012-11-22 22:42:17 +00:00
|
|
|
}
|
|
|
|
|
2013-03-02 13:57:41 +00:00
|
|
|
// QMainWindow *mainWindow = new QMainWindow;
|
2013-02-23 03:53:32 +00:00
|
|
|
|
2013-03-02 13:57:41 +00:00
|
|
|
View *view = new View (*this, document, countViews (document)+1); //, mainWindow);
|
2012-11-22 12:30:02 +00:00
|
|
|
|
|
|
|
mViews.push_back (view);
|
|
|
|
|
|
|
|
view->show();
|
|
|
|
|
2012-11-23 13:05:49 +00:00
|
|
|
connect (view, SIGNAL (newDocumentRequest ()), this, SIGNAL (newDocumentRequest()));
|
2013-02-02 15:14:58 +00:00
|
|
|
connect (view, SIGNAL (loadDocumentRequest ()), this, SIGNAL (loadDocumentRequest()));
|
2012-11-23 13:05:49 +00:00
|
|
|
|
2012-11-22 14:09:04 +00:00
|
|
|
updateIndices();
|
|
|
|
|
2012-11-22 12:30:02 +00:00
|
|
|
return view;
|
2012-11-22 13:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CSVDoc::ViewManager::countViews (const CSMDoc::Document *document) const
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for (std::vector<View *>::const_iterator iter (mViews.begin()); iter!=mViews.end(); ++iter)
|
|
|
|
if ((*iter)->getDocument()==document)
|
|
|
|
++count;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSVDoc::ViewManager::closeRequest (View *view)
|
|
|
|
{
|
|
|
|
std::vector<View *>::iterator iter = std::find (mViews.begin(), mViews.end(), view);
|
|
|
|
|
2013-03-02 13:57:41 +00:00
|
|
|
bool continueWithClose = true;
|
|
|
|
|
2012-11-22 13:10:23 +00:00
|
|
|
if (iter!=mViews.end())
|
|
|
|
{
|
|
|
|
bool last = countViews (view->getDocument())<=1;
|
|
|
|
|
2012-11-23 09:25:34 +00:00
|
|
|
/// \todo check if save is in progress -> warn user about possible data loss
|
2012-11-22 13:10:23 +00:00
|
|
|
/// \todo check if document has not been saved -> return false and start close dialogue
|
|
|
|
|
2013-03-02 13:57:41 +00:00
|
|
|
CSMDoc::Document *document = view->getDocument();
|
|
|
|
|
|
|
|
//notify user of saving in progress
|
|
|
|
if ( document->getState() & CSMDoc::State_Saving )
|
|
|
|
continueWithClose = showSaveInProgressMessageBox (view);
|
2013-03-02 18:49:26 +00:00
|
|
|
else
|
|
|
|
//notify user of unsaved changes and process response
|
|
|
|
if ( document->getState() & CSMDoc::State_Modified)
|
|
|
|
continueWithClose = showModifiedDocumentMessageBox (view);
|
2013-03-02 13:57:41 +00:00
|
|
|
|
|
|
|
if (continueWithClose)
|
|
|
|
{
|
|
|
|
mViews.erase (iter);
|
|
|
|
|
|
|
|
if (last)
|
|
|
|
mDocumentManager.removeDocument (document);
|
|
|
|
else
|
|
|
|
updateIndices();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return continueWithClose;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSVDoc::ViewManager::showModifiedDocumentMessageBox (View* view)
|
|
|
|
{
|
|
|
|
QMessageBox messageBox;
|
|
|
|
|
|
|
|
messageBox.setText ("The document has been modified.");
|
|
|
|
messageBox.setInformativeText ("Do you want to save your changes?");
|
|
|
|
messageBox.setStandardButtons (QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
|
|
|
|
messageBox.setDefaultButton (QMessageBox::Save);
|
|
|
|
|
|
|
|
bool retVal = true;
|
|
|
|
|
|
|
|
switch (messageBox.exec())
|
|
|
|
{
|
|
|
|
case QMessageBox::Save:
|
|
|
|
view->getDocument()->save();
|
2013-03-02 18:49:26 +00:00
|
|
|
|
|
|
|
retVal = !(view->getDocument()->getState() & CSMDoc::State_Saving);
|
|
|
|
|
|
|
|
if (!retVal)
|
|
|
|
mCloseMeOnSaveStateChange = view;
|
|
|
|
else
|
|
|
|
mCloseMeOnSaveStateChange = 0;
|
2013-03-02 13:57:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QMessageBox::Discard:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QMessageBox::Cancel:
|
|
|
|
retVal = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
2012-11-22 13:10:23 +00:00
|
|
|
|
2013-03-02 13:57:41 +00:00
|
|
|
bool CSVDoc::ViewManager::showSaveInProgressMessageBox (View* view)
|
|
|
|
{
|
|
|
|
QMessageBox messageBox;
|
|
|
|
|
|
|
|
messageBox.setText ("The document is currently being saved.");
|
|
|
|
messageBox.setInformativeText("Do you want to abort the save?");
|
|
|
|
messageBox.setStandardButtons (QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
|
|
|
|
|
2013-03-02 15:22:44 +00:00
|
|
|
bool retVal = true;
|
2013-03-02 13:57:41 +00:00
|
|
|
|
|
|
|
switch (messageBox.exec())
|
|
|
|
{
|
2013-03-02 15:22:44 +00:00
|
|
|
case QMessageBox::Yes: //immediate shutdown
|
|
|
|
mCloseMeOnSaveStateChange = 0;
|
2013-03-02 13:57:41 +00:00
|
|
|
view->abortOperation(CSMDoc::State_Saving);
|
|
|
|
break;
|
|
|
|
|
2013-03-02 15:22:44 +00:00
|
|
|
case QMessageBox::No: //shutdown after save completes
|
2013-03-02 18:49:26 +00:00
|
|
|
|
|
|
|
//return true (continue with close) if the save operation ended before the
|
|
|
|
//user clicked "No"
|
|
|
|
retVal = !(view->getDocument()->getState() & CSMDoc::State_Saving);
|
|
|
|
|
|
|
|
if (!retVal)
|
2013-03-02 15:22:44 +00:00
|
|
|
mCloseMeOnSaveStateChange = view;
|
2013-03-02 18:49:26 +00:00
|
|
|
else
|
|
|
|
mCloseMeOnSaveStateChange = 0;
|
|
|
|
|
2013-03-02 13:57:41 +00:00
|
|
|
break;
|
|
|
|
|
2013-03-02 15:22:44 +00:00
|
|
|
case QMessageBox::Cancel: //abort shutdown, allow save to complete
|
|
|
|
mCloseMeOnSaveStateChange = 0;
|
2013-03-02 13:57:41 +00:00
|
|
|
retVal = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2012-11-22 13:10:23 +00:00
|
|
|
}
|
|
|
|
|
2013-03-02 13:57:41 +00:00
|
|
|
return retVal;
|
2012-11-22 22:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSVDoc::ViewManager::documentStateChanged (int state, CSMDoc::Document *document)
|
|
|
|
{
|
|
|
|
for (std::vector<View *>::const_iterator iter (mViews.begin()); iter!=mViews.end(); ++iter)
|
|
|
|
if ((*iter)->getDocument()==document)
|
|
|
|
(*iter)->updateDocumentState();
|
2013-03-02 13:57:41 +00:00
|
|
|
|
2013-03-02 15:22:44 +00:00
|
|
|
//mechanism to shutdown main window after saving operation completes
|
2013-03-02 13:57:41 +00:00
|
|
|
if (mCloseMeOnSaveStateChange && (mPreviousDocumentState & CSMDoc::State_Saving))
|
2013-03-02 18:49:26 +00:00
|
|
|
if (mCloseMeOnSaveStateChange->close())
|
|
|
|
mCloseMeOnSaveStateChange = 0;
|
2013-03-02 15:22:44 +00:00
|
|
|
|
2013-03-02 13:57:41 +00:00
|
|
|
mPreviousDocumentState = state;
|
2012-11-22 23:36:01 +00:00
|
|
|
}
|
2012-11-23 09:25:34 +00:00
|
|
|
|
2012-11-23 11:20:35 +00:00
|
|
|
void CSVDoc::ViewManager::progress (int current, int max, int type, int threads, CSMDoc::Document *document)
|
2012-11-23 09:25:34 +00:00
|
|
|
{
|
|
|
|
for (std::vector<View *>::const_iterator iter (mViews.begin()); iter!=mViews.end(); ++iter)
|
|
|
|
if ((*iter)->getDocument()==document)
|
2012-11-23 11:20:35 +00:00
|
|
|
(*iter)->updateProgress (current, max, type, threads);
|
2013-02-23 03:53:32 +00:00
|
|
|
}
|