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