-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogstatewatcher.h
134 lines (119 loc) · 4.45 KB
/
dialogstatewatcher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/************************************************************************
* *
* This source file is part of libkfdialog, a helper library for *
* implementing QtWidgets-based dialogues under KDE Frameworks or *
* standalone. Originally developed as part of Kooka, a KDE *
* scanning/OCR application. *
* *
* The library is free software; you can redistribute and/or *
* modify it under the terms of the GNU General Public License *
* version 2 or (at your option) any later version, as published *
* by the Free Software Foundation and appearing in the file *
* COPYING included in the packaging of this library, or at *
* http://www.gnu.org/licenses/gpl.html *
* *
* Copyright (C) 2016-2021 Jonathan Marten *
* <jjm AT keelhaul DOT me DOT uk> *
* and Kooka authors/contributors *
* *
* Home page: https://github.com/martenjj/libkfdialog *
* *
************************************************************************/
#ifndef DIALOGSTATEWATCHER_H
#define DIALOGSTATEWATCHER_H
#include <qobject.h>
#include "libkfdialog_export.h"
class QDialog;
class QEvent;
class QAbstractButton;
class DialogStateSaver;
/**
* @short Monitor a dialog box to save and restore the size and state.
*
* This class takes care of saving and restoring a dialog's size in the
* application config file. All that is necessary is to create a
* DialogStateWatcher object in the dialog's constructor, passing the
* dialog as a parameter. If the dialog is a subclass of DialogBase
* then a watcher will be created automatically.
*
* The watcher uses a DialogStateSaver to do the actual saving and
* restoring. If required, a custom saver can be subclassed from that
* to save additional information (e.g. the column states of a list view).
*
* @author Jonathan Marten
**/
class LIBKFDIALOG_EXPORT DialogStateWatcher : public QObject
{
Q_OBJECT
public:
/**
* Constructor.
*
* @param pnt the parent dialog
**/
explicit DialogStateWatcher(QDialog *pnt);
/**
* Destructor.
**/
~DialogStateWatcher() override = default;
/**
* Set a state saver for the dialog being watched.
*
* This may be a subclass of a DialogStateSaver, reimplemented in
* order to save special dialog settings (e.g. the column states of
* a list view). If this is not set then a plain DialogStateSaver
* will be created and used internally. If a nullptr state saver is
* set explicitly using this function, then no state restoring or
* saving will be done.
*
* The watcher does not take ownership of the state saver, therefore
* it is the caller's responsibility to delete it when it is no
* longer required.
*
* @param saver the state saver
*
* @note The saver should be set before the dialog is shown for
* the first time.
* @see DialogStateSaver
**/
void setStateSaver(DialogStateSaver *saver);
/**
* Access the state saver used by the watcher.
*
* This may be the default one, or that set by @c setStateSaver().
*
* @return the state saver
**/
DialogStateSaver *stateSaver() const { return (mStateSaver); }
/**
* Sets a button to save the state of the dialog when it is used.
*
* Normally the dialog state will be saved when the parent dialog is accepted.
* This means when any button with the @c QDialogButtonBox::AcceptRole is
* clicked: that is, @c QDialogButtonBox::Ok and some others. Notably, it
* does not include a @c QDialogButtonBox::Close button which is used where
* there is no difference between closing and cancelling. This means that the
* dialog state will not normally be saved when that button is used.
*
* If a button is specified here, the state will be saved when that button is
* used, in addition to any button with the @c QDialogButtonBox::AcceptRole.
* Additional buttons may be specified multiple times, and they will all
* save the state.
*
* @param but The button to activate the saving
*/
void setSaveOnButton(QAbstractButton *but);
protected:
/**
* @reimp
**/
bool eventFilter(QObject *obj, QEvent *ev) override;
private slots:
void restoreConfigInternal();
void saveConfigInternal() const;
private:
QDialog *mParent;
DialogStateSaver *mStateSaver;
bool mHaveOwnSaver;
};
#endif // DIALOGSTATEWATCHER_H