diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index 0a8a137f6a..264ae75434 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -3,7 +3,7 @@ #include -CS::Editor::Editor() +CS::Editor::Editor() : mViewManager (mDocumentManager) { } diff --git a/apps/opencs/view/doc/view.cpp b/apps/opencs/view/doc/view.cpp index 2bad69badb..51078d4481 100644 --- a/apps/opencs/view/doc/view.cpp +++ b/apps/opencs/view/doc/view.cpp @@ -1,8 +1,31 @@ #include "view.hpp" -CSVDoc::View::View (CSMDoc::Document *document) : mDocument (document) +#include + +#include + +#include "viewmanager.hpp" + +void CSVDoc::View::closeEvent (QCloseEvent *event) +{ + if (!mViewManager.closeRequest (this)) + event->ignore(); +} + +CSVDoc::View::View (ViewManager& viewManager, CSMDoc::Document *document) +: mViewManager (viewManager), mDocument (document) { resize (200, 200); setWindowTitle ("New Document"); +} + +const CSMDoc::Document *CSVDoc::View::getDocument() const +{ + return mDocument; +} + +CSMDoc::Document *CSVDoc::View::getDocument() +{ + return mDocument; } \ No newline at end of file diff --git a/apps/opencs/view/doc/view.hpp b/apps/opencs/view/doc/view.hpp index 797eef0835..b12bb7e41a 100644 --- a/apps/opencs/view/doc/view.hpp +++ b/apps/opencs/view/doc/view.hpp @@ -10,20 +10,31 @@ namespace CSMDoc namespace CSVDoc { + class ViewManager; + class View : public QWidget { Q_OBJECT + ViewManager& mViewManager; CSMDoc::Document *mDocument; // not implemented View (const View&); View& operator= (const View&); + private: + + void closeEvent (QCloseEvent *event); + public: - View (CSMDoc::Document *document); + View (ViewManager& viewManager, CSMDoc::Document *document); ///< The ownership of \a document is not transferred to *this. + + const CSMDoc::Document *getDocument() const; + + CSMDoc::Document *getDocument(); }; } diff --git a/apps/opencs/view/doc/viewmanager.cpp b/apps/opencs/view/doc/viewmanager.cpp index 59b79be47b..da73213f71 100644 --- a/apps/opencs/view/doc/viewmanager.cpp +++ b/apps/opencs/view/doc/viewmanager.cpp @@ -1,9 +1,12 @@ #include "viewmanager.hpp" +#include "../../model/doc/documentmanager.hpp" + #include "view.hpp" -CSVDoc::ViewManager::ViewManager() +CSVDoc::ViewManager::ViewManager (CSMDoc::DocumentManager& documentManager) +: mDocumentManager (documentManager) { } @@ -12,15 +15,49 @@ CSVDoc::ViewManager::~ViewManager() { for (std::vector::iterator iter (mViews.begin()); iter!=mViews.end(); ++iter) delete *iter; + + for (std::vector::iterator iter (mClosed.begin()); iter!=mClosed.end(); ++iter) + delete *iter; } CSVDoc::View *CSVDoc::ViewManager::addView (CSMDoc::Document *document) { - View *view = new View (document); + View *view = new View (*this, document); mViews.push_back (view); view->show(); return view; +} + +int CSVDoc::ViewManager::countViews (const CSMDoc::Document *document) const +{ + int count = 0; + + for (std::vector::const_iterator iter (mViews.begin()); iter!=mViews.end(); ++iter) + if ((*iter)->getDocument()==document) + ++count; + + return count; +} + +bool CSVDoc::ViewManager::closeRequest (View *view) +{ + std::vector::iterator iter = std::find (mViews.begin(), mViews.end(), view); + + if (iter!=mViews.end()) + { + bool last = countViews (view->getDocument())<=1; + + /// \todo check if document has not been saved -> return false and start close dialogue + + mViews.erase (iter); + mClosed.push_back (view); + + if (last) + mDocumentManager.removeDocument (view->getDocument()); + } + + return true; } \ No newline at end of file diff --git a/apps/opencs/view/doc/viewmanager.hpp b/apps/opencs/view/doc/viewmanager.hpp index dc6a07bce7..6901590ed8 100644 --- a/apps/opencs/view/doc/viewmanager.hpp +++ b/apps/opencs/view/doc/viewmanager.hpp @@ -6,6 +6,7 @@ namespace CSMDoc { class Document; + class DocumentManager; } namespace CSVDoc @@ -14,7 +15,9 @@ namespace CSVDoc class ViewManager { + CSMDoc::DocumentManager& mDocumentManager; std::vector mViews; + std::vector mClosed; // not implemented ViewManager (const ViewManager&); @@ -22,13 +25,18 @@ namespace CSVDoc public: - ViewManager(); + ViewManager (CSMDoc::DocumentManager& documentManager); ~ViewManager(); View *addView (CSMDoc::Document *document); ///< The ownership of the returned view is not transferred. + int countViews (const CSMDoc::Document *document) const; + ///< Return number of views for \a document. + + bool closeRequest (View *view); + };