57bcfece01d029050785a37f4ece6f778ffd6cdf
[quassel.git] / src / uisupport / settingspage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include "uisupport-export.h"
24
25 #include <QWidget>
26
27 class QCheckBox;
28 class QComboBox;
29 class QSpinBox;
30 class FontSelector;
31
32 //! A SettingsPage is a page in the settings dialog.
33 /** The SettingsDlg provides suitable standard buttons, such as Ok, Apply, Cancel, Restore Defaults and Reset.
34  *  Some pages might also be used in standalone dialogs or other containers. A SettingsPage provides suitable
35  *  slots and signals to allow interaction with the container.
36  *
37  *  A derived class needs to keep track of its changed state. Whenever a child widget changes, it needs to be
38  *  compared to its value in permanent storage, and the changed state updated accordingly by calling setChangedState().
39  *  For most standard widgets, SettingsPage can do this automatically if desired. Such a child widget needs to have
40  *  a dynamic property \c settingsKey that maps to the key in the client configuration file. This key is appended
41  *  to settingsKey(), which must be set to a non-null value in a derived class. If the widget's key starts with '/',
42  *  its key is treated as a global path starting from the root, rather than from settingsKey().
43  *  A second dynamic property \c defaultValue can be defined in child widgets as well.
44  *
45  *  For widgets requiring special ways for storing and saving, define the property settingsKey and leave it empty. In this
46  *  case, the methods saveAutoWidgetValue() and loadAutoWidgetValue() will be called with the widget's objectName as parameter.
47  *
48  *  SettingsPage manages loading, saving, setting to default and setting the changed state for all automatic child widgets.
49  *  Derived classes must be sure to call initAutoWidgets() *after* setupUi(); they also need to call the baseclass implementations
50  *  of load(), save() and defaults() (preferably at the end of the derived function, since they call setChangedState(false)).
51  *
52  *  The following widgets can be handled for now:
53  *    - QGroupBox (isChecked())
54  *    - QAbstractButton (isChecked(), e.g. for QCheckBox and QRadioButton)
55  *    - QLineEdit, QTextEdit (text())
56  *    - QComboBox (currentIndex())
57  *    - QSpinBox (value())
58  */
59 class UISUPPORT_EXPORT SettingsPage : public QWidget
60 {
61     Q_OBJECT
62
63 public:
64     SettingsPage(const QString &category, const QString &name, QWidget *parent = nullptr);
65     ~SettingsPage() override {};
66
67     //! The category of this settings page.
68     inline virtual QString category() const { return _category; }
69
70     //! The title of this settings page.
71     inline virtual QString title() const { return _title; }
72
73     //! Whether the settingspage needs a core connection to be selectable
74     /** This is a hint for the settingspage dialog. Do not rely on the settingspage not being
75      *  visible if disconnected, and care about disabling it yourself.
76      */
77     inline virtual bool needsCoreConnection() const { return false; }
78
79     /**
80      * Whether the settingspage should be selectable or not, in a given situation
81      * Used for pages that should only be visible if certain features are available (or not).
82      * @return
83      */
84     inline virtual bool isSelectable() const { return true; }
85
86     //! The key this settings page stores its values under
87     /** This needs to be overriden to enable automatic loading/saving/hasChanged checking of widgets.
88      *  The child widgets' values will be stored in client settings under this key. Every widget that
89      *  should be automatically handled needs to have a \c settingsKey property set, and should also provide
90      *  a \c defaultValue property.
91      *  You can return an empty string (as opposed to a null string) to use the config root as a base, and
92      *  you can override this key for individual widgets by prefixing their SettingsKey with /.
93      */
94     inline virtual QString settingsKey() const { return QString(); }
95
96     //! Derived classes need to define this and return true if they have default settings.
97     /** If this method returns true, the "Restore Defaults" button in the SettingsDlg is
98      *  enabled. You also need to provide an implementation of defaults() then.
99      *
100      * The default implementation returns false.
101        */
102     inline virtual bool hasDefaults() const { return false; }
103
104     //! Check if there are changes in the page, compared to the state saved in permanent storage.
105     inline bool hasChanged() const { return _changed || _autoWidgetsChanged; }
106
107     //! Called immediately before save() is called.
108     /** Derived classes should return false if saving is not possible (e.g. the current settings are invalid).
109      *  \return false, if the SettingsPage cannot be saved in its current state.
110      */
111     inline virtual bool aboutToSave() { return true; }
112
113     //! sets checked state depending on \checked and stores the value for later comparision
114     static void load(QCheckBox *box, bool checked);
115     static bool hasChanged(QCheckBox *box);
116     static void load(QComboBox *box, int index);
117     static bool hasChanged(QComboBox *box);
118     static void load(QSpinBox *box, int value);
119     static bool hasChanged(QSpinBox *box);
120     static void load(FontSelector *box, QFont value);
121     static bool hasChanged(FontSelector *box);
122
123 public slots:
124     //! Save settings to permanent storage.
125     /** This baseclass implementation saves the autoWidgets, so be sure to call it if you use
126      *  this feature in your settingsPage!
127      */
128     virtual void save();
129
130     //! Load settings from permanent storage, overriding any changes the user might have made in the dialog.
131     /** This baseclass implementation loads the autoWidgets, so be sure to call it if you use
132      *  this feature in your settingsPage!
133      */
134     virtual void load();
135
136     //! Restore defaults, overriding any changes the user might have made in the dialog.
137     /** This baseclass implementation loads the defaults of the autoWidgets (if available), so be sure
138      *  to call it if you use this feature in your settingsPage!
139      */
140     virtual void defaults();
141
142 protected slots:
143     //! Calling this slot is equivalent to calling setChangedState(true).
144     inline void changed() { setChangedState(true); }
145
146     //! This should be called whenever the widget state changes from unchanged to change or the other way round.
147     void setChangedState(bool hasChanged = true);
148
149 protected:
150     void initAutoWidgets();
151     virtual QVariant loadAutoWidgetValue(const QString &widgetName);
152     virtual void saveAutoWidgetValue(const QString &widgetName, const QVariant &value);
153
154 signals:
155     //! Emitted whenever the widget state changes.
156     void changed(bool hasChanged);
157
158 private slots:
159     // for auto stuff
160     void autoWidgetHasChanged();
161
162 private:
163     void findAutoWidgets(QObject *parent, QObjectList *widgetList) const;
164     QByteArray autoWidgetPropertyName(QObject *widget) const;
165     QString autoWidgetSettingsKey(QObject *widget) const;
166
167     QString _category, _title;
168     bool _changed, _autoWidgetsChanged;
169     QObjectList _autoWidgets;
170 };