diff --git a/apps/opencs/model/doc/documentmanager.cpp b/apps/opencs/model/doc/documentmanager.cpp index d44da15c5..fe6aaef27 100644 --- a/apps/opencs/model/doc/documentmanager.cpp +++ b/apps/opencs/model/doc/documentmanager.cpp @@ -59,7 +59,7 @@ void CSMDoc::DocumentManager::addDocument (const std::vector::iterator iter = std::find (mDocuments.begin(), mDocuments.end(), document); @@ -86,6 +86,8 @@ void CSMDoc::DocumentManager::documentLoaded (Document *document) void CSMDoc::DocumentManager::documentNotLoaded (Document *document, const std::string& error) { -// emit loadingStopped (document, false, error); - removeDocument (document); + emit loadingStopped (document, false, error); + + if (error.empty()) // do not remove the document yet, if we have an error + removeDocument (document); } \ No newline at end of file diff --git a/apps/opencs/model/doc/documentmanager.hpp b/apps/opencs/model/doc/documentmanager.hpp index d834d85d4..de4a5e94b 100644 --- a/apps/opencs/model/doc/documentmanager.hpp +++ b/apps/opencs/model/doc/documentmanager.hpp @@ -43,9 +43,6 @@ namespace CSMDoc ///< \param new_ Do not load the last content file in \a files and instead create in an /// appropriate way. - void removeDocument (Document *document); - ///< Emits the lastDocumentDeleted signal, if applicable. - void setResourceDir (const boost::filesystem::path& parResDir); private: @@ -61,6 +58,11 @@ namespace CSMDoc ///< Document load has been interrupted either because of a call to abortLoading /// or a problem during loading). In the former case error will be an empty string. + public slots: + + void removeDocument (CSMDoc::Document *document); + ///< Emits the lastDocumentDeleted signal, if applicable. + signals: void documentAdded (CSMDoc::Document *document); diff --git a/apps/opencs/view/doc/loader.cpp b/apps/opencs/view/doc/loader.cpp index 3b5890479..1b2ca8ad5 100644 --- a/apps/opencs/view/doc/loader.cpp +++ b/apps/opencs/view/doc/loader.cpp @@ -13,11 +13,11 @@ void CSVDoc::LoadingDocument::closeEvent (QCloseEvent *event) { event->ignore(); - emit cancel (mDocument); + cancel(); } CSVDoc::LoadingDocument::LoadingDocument (CSMDoc::Document *document) -: mDocument (document) +: mDocument (document), mAborted (false) { setWindowTitle (("Opening " + document->getSavePath().filename().string()).c_str()); @@ -52,10 +52,16 @@ CSVDoc::LoadingDocument::LoadingDocument (CSMDoc::Document *document) mRecordProgress->setTextVisible (true); mRecordProgress->setValue (0); - QDialogButtonBox *buttonBox = new QDialogButtonBox (QDialogButtonBox::Cancel, Qt::Horizontal, - this); + // error message + mError = new QLabel (this); + mError->setWordWrap (true); - layout->addWidget (buttonBox); + layout->addWidget (mError); + + // buttons + mButtons = new QDialogButtonBox (QDialogButtonBox::Cancel, Qt::Horizontal, this); + + layout->addWidget (mButtons); setLayout (layout); @@ -63,7 +69,7 @@ CSVDoc::LoadingDocument::LoadingDocument (CSMDoc::Document *document) show(); - connect (buttonBox, SIGNAL (rejected()), this, SLOT (cancel())); + connect (mButtons, SIGNAL (rejected()), this, SLOT (cancel())); } void CSVDoc::LoadingDocument::nextStage (const std::string& name, int steps) @@ -73,10 +79,9 @@ void CSVDoc::LoadingDocument::nextStage (const std::string& name, int steps) mFileProgress->setValue (mFileProgress->value()+1); mRecordProgress->setValue (0); - mRecordProgress->setMaximum (steps); + mRecordProgress->setMaximum (steps>0 ? steps : 1); } - void CSVDoc::LoadingDocument::nextRecord() { int value = mRecordProgress->value()+1; @@ -85,9 +90,22 @@ void CSVDoc::LoadingDocument::nextRecord() mRecordProgress->setValue (value); } +void CSVDoc::LoadingDocument::abort (const std::string& error) +{ + mAborted = true; + mError->setText (QString::fromUtf8 (("Loading failed: " + error).c_str())); + mButtons->setStandardButtons (QDialogButtonBox::Close); +} + void CSVDoc::LoadingDocument::cancel() { - emit cancel (mDocument); + if (!mAborted) + emit cancel (mDocument); + else + { + emit close (mDocument); + deleteLater(); + } } @@ -107,21 +125,32 @@ void CSVDoc::Loader::add (CSMDoc::Document *document) connect (loading, SIGNAL (cancel (CSMDoc::Document *)), this, SIGNAL (cancel (CSMDoc::Document *))); + connect (loading, SIGNAL (close (CSMDoc::Document *)), + this, SIGNAL (close (CSMDoc::Document *))); } void CSVDoc::Loader::loadingStopped (CSMDoc::Document *document, bool completed, const std::string& error) { + std::map::iterator iter = mDocuments.begin(); + + for (; iter!=mDocuments.end(); ++iter) + if (iter->first==document) + break; + + if (iter==mDocuments.end()) + return; + if (completed || error.empty()) { - for (std::map::iterator iter (mDocuments.begin()); - iter!=mDocuments.end(); ++iter) - if (iter->first==document) - { - delete iter->second; - mDocuments.erase (iter); - break; - } + delete iter->second; + mDocuments.erase (iter); + } + else if (!completed && !error.empty()) + { + iter->second->abort (error); + // Leave the window open for now (wait for the user to close it) + mDocuments.erase (iter); } } diff --git a/apps/opencs/view/doc/loader.hpp b/apps/opencs/view/doc/loader.hpp index d1c740011..ece071755 100644 --- a/apps/opencs/view/doc/loader.hpp +++ b/apps/opencs/view/doc/loader.hpp @@ -9,6 +9,7 @@ class QLabel; class QProgressBar; +class QDialogButtonBox; namespace CSMDoc { @@ -25,6 +26,9 @@ namespace CSVDoc QLabel *mFile; QProgressBar *mFileProgress; QProgressBar *mRecordProgress; + bool mAborted; + QDialogButtonBox *mButtons; + QLabel *mError; private: @@ -38,6 +42,8 @@ namespace CSVDoc void nextRecord(); + void abort (const std::string& error); + private slots: void cancel(); @@ -45,6 +51,10 @@ namespace CSVDoc signals: void cancel (CSMDoc::Document *document); + ///< Stop loading process. + + void close (CSMDoc::Document *document); + ///< Close stopped loading process. }; class Loader : public QObject @@ -63,6 +73,8 @@ namespace CSVDoc void cancel (CSMDoc::Document *document); + void close (CSMDoc::Document *document); + public slots: void add (CSMDoc::Document *document); diff --git a/apps/opencs/view/doc/viewmanager.cpp b/apps/opencs/view/doc/viewmanager.cpp index b8971a296..816eff791 100644 --- a/apps/opencs/view/doc/viewmanager.cpp +++ b/apps/opencs/view/doc/viewmanager.cpp @@ -103,6 +103,10 @@ CSVDoc::ViewManager::ViewManager (CSMDoc::DocumentManager& documentManager) connect ( &mLoader, SIGNAL (cancel (CSMDoc::Document *)), &mDocumentManager, SIGNAL (cancelLoading (CSMDoc::Document *))); + + connect ( + &mLoader, SIGNAL (close (CSMDoc::Document *)), + &mDocumentManager, SLOT (removeDocument (CSMDoc::Document *))); } CSVDoc::ViewManager::~ViewManager()