Skip to content
This repository has been archived by the owner on May 30, 2019. It is now read-only.

Commit

Permalink
Handle close/quit window/application, save before.
Browse files Browse the repository at this point in the history
- CloseRequest controls whether just the current window or all windows
  are to be closed
- SaveStrategy defines how to deal with a modified document before close
  • Loading branch information
till213 committed Feb 9, 2011
1 parent e8de7da commit 35e1b24
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 106 deletions.
13 changes: 12 additions & 1 deletion src/Kernel/src/DocumentInfo.h
Expand Up @@ -28,10 +28,21 @@ class ScreenieScene;

struct DocumentInfo
{
/*!
* Defines how to deal with unsaved documents before closing the window.
*/
enum SaveStrategy
{
Discard, /*!< Discard the modifications, close the document immediatelly */
Save, /*!< Save the modifications before closing the document */
Ask /*!< Ask the user whether to discard or save the modifications before closing the document */
};

int id;
QMainWindow *mainWindow;
ScreenieScene *screenieScene;
QString windowTitle;
int id;
SaveStrategy saveStrategy;
};

#endif // DOCUMENTINFO_H
60 changes: 50 additions & 10 deletions src/Kernel/src/DocumentManager.cpp
Expand Up @@ -50,10 +50,12 @@ class DocumentManagerPrivate
QSignalMapper windowMapper;
static DocumentManager *instance;
static int nextWindowId;
static DocumentManager::CloseRequest closeRequest;
};

DocumentManager *DocumentManagerPrivate::instance = 0;
int DocumentManagerPrivate::nextWindowId = 0;
DocumentManager::CloseRequest DocumentManagerPrivate::closeRequest = DocumentManager::CloseCurrent;

// public

Expand Down Expand Up @@ -82,6 +84,7 @@ void DocumentManager::add(DocumentInfo *documentInfo)
// update object name ("window ID")
mainWindow->setObjectName(mainWindow->objectName() + QString::number(d->nextWindowId));
mainWindow->installEventFilter(this);
/*!\todo This gets messy: make DocumentInfo a proper class. */
documentInfo->id = d->nextWindowId;
++d->nextWindowId;
documentInfo->windowTitle = tr("New %1", "New document title + ID").arg(d->nextWindowId);
Expand All @@ -95,26 +98,26 @@ void DocumentManager::add(DocumentInfo *documentInfo)
emit changed();
}

void DocumentManager::setWindowTitle(const QString &windowTitle, const QMainWindow &mainWindow)
QString DocumentManager::getWindowTitle(const QMainWindow &mainWindow) const
{
QString result;
DocumentInfo *documentInfo = getDocumentInfo(mainWindow);
if (documentInfo != 0) {
documentInfo->windowTitle = windowTitle;
QAction *action = getWindowAction(documentInfo->id);
if (action != 0) {
action->setText(windowTitle);
}
result = documentInfo->windowTitle;
}
return result;
}

QString DocumentManager::getWindowTitle(const QMainWindow &mainWindow) const
void DocumentManager::setWindowTitle(const QString &windowTitle, const QMainWindow &mainWindow)
{
QString result;
DocumentInfo *documentInfo = getDocumentInfo(mainWindow);
if (documentInfo != 0) {
result = documentInfo->windowTitle;
documentInfo->windowTitle = windowTitle;
QAction *action = getWindowAction(documentInfo->id);
if (action != 0) {
action->setText(windowTitle);
}
}
return result;
}

QActionGroup &DocumentManager::getActionGroup() const
Expand All @@ -138,6 +141,43 @@ int DocumentManager::getModifiedCount() const
return result;
}

DocumentInfo::SaveStrategy DocumentManager::getSaveStrategy(const QMainWindow &mainWindow) const
{
DocumentInfo::SaveStrategy result;
const DocumentInfo *documentInfo = getDocumentInfo(mainWindow);
if (documentInfo != 0) {
result = documentInfo->saveStrategy;
} else {
result = DocumentInfo::Discard;
}
return result;
}

void DocumentManager::setSaveStrategy(const QMainWindow &mainWindow, DocumentInfo::SaveStrategy saveStrategy)
{
DocumentInfo *documentInfo = getDocumentInfo(mainWindow);
if (documentInfo != 0) {
documentInfo->saveStrategy = saveStrategy;
}
}

void DocumentManager::setSaveStrategyForAll(DocumentInfo::SaveStrategy saveStrategy)
{
foreach (DocumentInfo *documentInfo, d->documentInfos) {
documentInfo->saveStrategy = saveStrategy;
}
}

DocumentManager::CloseRequest DocumentManager::getCloseRequest()
{
return DocumentManagerPrivate::closeRequest;
}

void DocumentManager::setCloseRequest(CloseRequest closeRequest)
{
DocumentManagerPrivate::closeRequest = closeRequest;
}

bool DocumentManager::eventFilter(QObject *object, QEvent *event)
{
bool result;
Expand Down
16 changes: 14 additions & 2 deletions src/Kernel/src/DocumentManager.h
Expand Up @@ -30,8 +30,8 @@ class QEvent;
class QMainWindow;

#include "KernelLib.h"
#include "DocumentInfo.h"

struct DocumentInfo;
class DocumentManagerPrivate;

/*!
Expand All @@ -41,6 +41,12 @@ class DocumentManager : public QObject
{
Q_OBJECT
public:
enum CloseRequest
{
CloseCurrent,
Quit
};

KERNEL_API static DocumentManager &getInstance();
KERNEL_API static void destroyInstance();

Expand All @@ -55,14 +61,20 @@ class DocumentManager : public QObject
* \sa #changed()
*/
KERNEL_API void add(DocumentInfo *documentInfo);
KERNEL_API void setWindowTitle(const QString &windowTitle, const QMainWindow &mainWindow);
KERNEL_API QString getWindowTitle(const QMainWindow &mainWindow) const;
KERNEL_API void setWindowTitle(const QString &windowTitle, const QMainWindow &mainWindow);
KERNEL_API QActionGroup &getActionGroup() const;
KERNEL_API int count() const;
KERNEL_API int getModifiedCount() const;

KERNEL_API DocumentInfo::SaveStrategy getSaveStrategy(const QMainWindow &mainWindow) const;
KERNEL_API void setSaveStrategy(const QMainWindow &mainWindow, DocumentInfo::SaveStrategy saveStrategy);
KERNEL_API void setSaveStrategyForAll(DocumentInfo::SaveStrategy saveStrategy);

virtual bool eventFilter(QObject *object, QEvent *event);

KERNEL_API static CloseRequest getCloseRequest();
KERNEL_API static void setCloseRequest(CloseRequest closeRequest);
signals:
void changed();

Expand Down
2 changes: 2 additions & 0 deletions src/Kernel/src/PropertyDialogFactory.cpp
Expand Up @@ -82,6 +82,8 @@ QDialog *PropertyDialogFactory::createDialog(ScreenieModelInterface &screenieMod
this, SLOT(handlePropertyDialogDestroyed()));
result->setAttribute(Qt::WA_DeleteOnClose);
result->setWindowIcon(QIcon(":/img/application-icon.png"));
/*!\todo Bad design! The factory should not delete the previous dialog! That "logic" belongs
e.g. into the QGraphicsScene/View instance */
if (PropertyDialogFactoryPrivate::lastDialog != 0) {
delete PropertyDialogFactoryPrivate::lastDialog;
}
Expand Down

0 comments on commit 35e1b24

Please sign in to comment.