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