d773d7208e819079f469b2881bda18d0b5e80c5d
[quassel.git] / src / uisupport / settingspage.cpp
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 #include "settingspage.h"
22
23 #include <QCheckBox>
24 #include <QComboBox>
25 #include <QSpinBox>
26 #include <QVariant>
27
28 #include "fontselector.h"
29
30 #include "uisettings.h"
31
32 SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
33     : QWidget(parent),
34     _category(category),
35     _title(title),
36     _changed(false),
37     _autoWidgetsChanged(false)
38 {
39     setAttribute(Qt::WA_DeleteOnClose);
40 }
41
42
43 void SettingsPage::setChangedState(bool hasChanged_)
44 {
45     if (hasChanged_ != _changed) {
46         bool old = hasChanged();
47         _changed = hasChanged_;
48         if (hasChanged() != old)
49             emit changed(hasChanged());
50     }
51 }
52
53
54 void SettingsPage::load(QCheckBox *box, bool checked)
55 {
56     box->setProperty("storedValue", checked);
57     box->setChecked(checked);
58 }
59
60
61 bool SettingsPage::hasChanged(QCheckBox *box)
62 {
63     return box->property("storedValue").toBool() != box->isChecked();
64 }
65
66
67 void SettingsPage::load(QComboBox *box, int index)
68 {
69     box->setProperty("storedValue", index);
70     box->setCurrentIndex(index);
71 }
72
73
74 bool SettingsPage::hasChanged(QComboBox *box)
75 {
76     return box->property("storedValue").toInt() != box->currentIndex();
77 }
78
79
80 void SettingsPage::load(QSpinBox *box, int value)
81 {
82     box->setProperty("storedValue", value);
83     box->setValue(value);
84 }
85
86
87 bool SettingsPage::hasChanged(QSpinBox *box)
88 {
89     return box->property("storedValue").toInt() != box->value();
90 }
91
92
93 void SettingsPage::load(FontSelector *box, QFont value)
94 {
95     box->setProperty("storedValue", value);
96     box->setSelectedFont(value);
97 }
98
99
100 bool SettingsPage::hasChanged(FontSelector *box)
101 {
102     return box->property("storedValue").value<QFont>() != box->selectedFont();
103 }
104
105
106 /*** Auto child widget handling ***/
107
108 void SettingsPage::initAutoWidgets()
109 {
110     _autoWidgets.clear();
111
112     // find all descendants that should be considered auto widgets
113     // we need to climb the QObject tree recursively
114     findAutoWidgets(this, &_autoWidgets);
115
116     foreach(QObject *widget, _autoWidgets) {
117         if (widget->inherits("ColorButton"))
118             connect(widget, SIGNAL(colorChanged(QColor)), SLOT(autoWidgetHasChanged()));
119         else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
120             connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged()));
121         else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
122             connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged()));
123         else if (widget->inherits("QComboBox"))
124             connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged()));
125         else if (widget->inherits("QSpinBox"))
126             connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged()));
127         else if (widget->inherits("FontSelector"))
128             connect(widget, SIGNAL(fontChanged(QFont)), SLOT(autoWidgetHasChanged()));
129         else
130             qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
131     }
132 }
133
134
135 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const
136 {
137     foreach(QObject *child, parent->children()) {
138         if (child->property("settingsKey").isValid())
139             autoList->append(child);
140         findAutoWidgets(child, autoList);
141     }
142 }
143
144
145 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const
146 {
147     QByteArray prop;
148     if (widget->inherits("ColorButton"))
149         prop = "color";
150     else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
151         prop = "checked";
152     else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
153         prop = "text";
154     else if (widget->inherits("QComboBox"))
155         prop = "currentIndex";
156     else if (widget->inherits("QSpinBox"))
157         prop = "value";
158     else if (widget->inherits("FontSelector"))
159         prop = "selectedFont";
160     else
161         qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
162
163     return prop;
164 }
165
166
167 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const
168 {
169     QString key = widget->property("settingsKey").toString();
170     if (key.isEmpty())
171         return QString("");
172     if (key.startsWith('/'))
173         key.remove(0, 1);
174     else
175         key.prepend(settingsKey() + '/');
176     return key;
177 }
178
179
180 void SettingsPage::autoWidgetHasChanged()
181 {
182     bool changed_ = false;
183     foreach(QObject *widget, _autoWidgets) {
184         QVariant curValue = widget->property(autoWidgetPropertyName(widget));
185         if (!curValue.isValid())
186             qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
187
188         if (curValue != widget->property("storedValue")) {
189             changed_ = true;
190             break;
191         }
192     }
193
194     if (changed_ != _autoWidgetsChanged) {
195         bool old = hasChanged();
196         _autoWidgetsChanged = changed_;
197         if (hasChanged() != old)
198             emit changed(hasChanged());
199     }
200 }
201
202
203 void SettingsPage::load()
204 {
205     UiSettings s("");
206     foreach(QObject *widget, _autoWidgets) {
207         QString key = autoWidgetSettingsKey(widget);
208         QVariant val;
209         if (key.isEmpty())
210             val = loadAutoWidgetValue(widget->objectName());
211         else
212             val = s.value(key, QVariant());
213         if (!val.isValid())
214             val = widget->property("defaultValue");
215         widget->setProperty(autoWidgetPropertyName(widget), val);
216         widget->setProperty("storedValue", val);
217     }
218     bool old = hasChanged();
219     _autoWidgetsChanged = _changed = false;
220     if (hasChanged() != old)
221         emit changed(hasChanged());
222 }
223
224
225 void SettingsPage::save()
226 {
227     UiSettings s("");
228     foreach(QObject *widget, _autoWidgets) {
229         QString key = autoWidgetSettingsKey(widget);
230         QVariant val = widget->property(autoWidgetPropertyName(widget));
231         widget->setProperty("storedValue", val);
232         if (key.isEmpty())
233             saveAutoWidgetValue(widget->objectName(), val);
234         else
235             s.setValue(key, val);
236     }
237     bool old = hasChanged();
238     _autoWidgetsChanged = _changed = false;
239     if (hasChanged() != old)
240         emit changed(hasChanged());
241 }
242
243
244 void SettingsPage::defaults()
245 {
246     foreach(QObject *widget, _autoWidgets) {
247         QVariant val = widget->property("defaultValue");
248         widget->setProperty(autoWidgetPropertyName(widget), val);
249     }
250     autoWidgetHasChanged();
251 }
252
253
254 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName)
255 {
256     qWarning() << "Could not load value for SettingsPage widget" << widgetName;
257     return QVariant();
258 }
259
260
261 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &)
262 {
263     qWarning() << "Could not save value for SettingsPage widget" << widgetName;
264 }