implemented view/document closing

pull/16/head
Marc Zinnschlag 12 years ago
parent 9834bb3ad5
commit 758371d7e4

@ -3,7 +3,7 @@
#include <QtGui/QApplication>
CS::Editor::Editor()
CS::Editor::Editor() : mViewManager (mDocumentManager)
{
}

@ -1,8 +1,31 @@
#include "view.hpp"
CSVDoc::View::View (CSMDoc::Document *document) : mDocument (document)
#include <iostream>
#include <QCloseEvent>
#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;
}

@ -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();
};
}

@ -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<View *>::iterator iter (mViews.begin()); iter!=mViews.end(); ++iter)
delete *iter;
for (std::vector<View *>::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<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);
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;
}

@ -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<View *> mViews;
std::vector<View *> 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);
};

Loading…
Cancel
Save