track document modification state and display it in the top level window title bar

pull/16/head
Marc Zinnschlag 12 years ago
parent 8e546ebd30
commit d7c63d4c74

@ -1,9 +1,27 @@
#include "document.hpp"
CSMDoc::Document::Document() {}
CSMDoc::Document::Document()
{
connect (&mUndoStack, SIGNAL (cleanChanged (bool)), this, SLOT (modificationStateChanged (bool)));
}
QUndoStack& CSMDoc::Document::getUndoStack()
{
return mUndoStack;
}
int CSMDoc::Document::getState() const
{
int state = 0;
if (!mUndoStack.isClean())
state |= State_Modified;
return state;
}
void CSMDoc::Document::modificationStateChanged (bool clean)
{
emit stateChanged (getState(), this);
}

@ -2,11 +2,21 @@
#define CSM_DOC_DOCUMENT_H
#include <QUndoStack>
#include <QObject>
namespace CSMDoc
{
class Document
class Document : public QObject
{
Q_OBJECT
public:
enum State
{
State_Modified = 1
};
QUndoStack mUndoStack;
// not implemented
@ -18,6 +28,16 @@ namespace CSMDoc
Document();
QUndoStack& getUndoStack();
int getState() const;
signals:
void stateChanged (int state, CSMDoc::Document *document);
private slots:
void modificationStateChanged (bool clean);
};
}

@ -55,6 +55,9 @@ void CSVDoc::View::updateTitle()
stream << "New Document ";
if (mDocument->getState() & CSMDoc::Document::State_Modified)
stream << " *";
if (mViewTotal>1)
stream << " [" << (mViewIndex+1) << "/" << mViewTotal << "]";
@ -89,6 +92,11 @@ void CSVDoc::View::setIndex (int viewIndex, int totalViews)
updateTitle();
}
void CSVDoc::View::updateDocumentState()
{
updateTitle();
}
void CSVDoc::View::newView()
{
mViewManager.addView (mDocument);

@ -50,6 +50,8 @@ namespace CSVDoc
void setIndex (int viewIndex, int totalViews);
void updateDocumentState();
private slots:
void newView();

@ -4,6 +4,7 @@
#include <map>
#include "../../model/doc/documentmanager.hpp"
#include "../../model/doc/document.hpp"
#include "view.hpp"
@ -42,6 +43,13 @@ CSVDoc::ViewManager::~ViewManager()
CSVDoc::View *CSVDoc::ViewManager::addView (CSMDoc::Document *document)
{
if (countViews (document)==0)
{
// new document
connect (document, SIGNAL (stateChanged (int, CSMDoc::Document *)),
this, SLOT (documentStateChanged (int, CSMDoc::Document *)));
}
View *view = new View (*this, document, countViews (document)+1);
mViews.push_back (view);
@ -84,4 +92,11 @@ bool CSVDoc::ViewManager::closeRequest (View *view)
}
return true;
}
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();
}

@ -3,6 +3,8 @@
#include <vector>
#include <QObject>
namespace CSMDoc
{
class Document;
@ -13,8 +15,10 @@ namespace CSVDoc
{
class View;
class ViewManager
class ViewManager : public QObject
{
Q_OBJECT
CSMDoc::DocumentManager& mDocumentManager;
std::vector<View *> mViews;
std::vector<View *> mClosed;
@ -29,7 +33,7 @@ namespace CSVDoc
ViewManager (CSMDoc::DocumentManager& documentManager);
~ViewManager();
virtual ~ViewManager();
View *addView (CSMDoc::Document *document);
///< The ownership of the returned view is not transferred.
@ -39,8 +43,9 @@ namespace CSVDoc
bool closeRequest (View *view);
private slots:
void documentStateChanged (int state, CSMDoc::Document *document);
};
}

Loading…
Cancel
Save