test: Add build system support and a main function for unit tests
[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 #include <utility>
28
29 #include "fontselector.h"
30 #include "uisettings.h"
31 #include "widgethelpers.h"
32
33 SettingsPage::SettingsPage(QString category, QString title, QWidget *parent)
34     : QWidget(parent),
35     _category(std::move(category)),
36     _title(std::move(title)),
37     _changed(false),
38     _autoWidgetsChanged(false)
39 {
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     if (!connectToWidgetsChangedSignals(_autoWidgets, this, &SettingsPage::autoWidgetHasChanged)) {
117         qWarning() << "SettingsPage::initAutoWidgets(): Unsupported auto widget type(s)!";
118     }
119 }
120
121
122 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const
123 {
124     foreach(QObject *child, parent->children()) {
125         if (child->property("settingsKey").isValid())
126             autoList->append(child);
127         findAutoWidgets(child, autoList);
128     }
129 }
130
131
132 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const
133 {
134     QByteArray prop;
135     if (widget->inherits("ColorButton"))
136         prop = "color";
137     else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
138         prop = "checked";
139     else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
140         prop = "text";
141     else if (widget->inherits("QComboBox"))
142         prop = "currentIndex";
143     else if (widget->inherits("QSpinBox"))
144         prop = "value";
145     else if (widget->inherits("FontSelector"))
146         prop = "selectedFont";
147     else
148         qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
149
150     return prop;
151 }
152
153
154 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const
155 {
156     QString key = widget->property("settingsKey").toString();
157     if (key.isEmpty())
158         return QString("");
159     if (key.startsWith('/'))
160         key.remove(0, 1);
161     else
162         key.prepend(settingsKey() + '/');
163     return key;
164 }
165
166
167 void SettingsPage::autoWidgetHasChanged()
168 {
169     bool changed_ = false;
170     foreach(QObject *widget, _autoWidgets) {
171         QVariant curValue = widget->property(autoWidgetPropertyName(widget));
172         if (!curValue.isValid())
173             qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
174
175         if (curValue != widget->property("storedValue")) {
176             changed_ = true;
177             break;
178         }
179     }
180
181     if (changed_ != _autoWidgetsChanged) {
182         bool old = hasChanged();
183         _autoWidgetsChanged = changed_;
184         if (hasChanged() != old)
185             emit changed(hasChanged());
186     }
187 }
188
189
190 void SettingsPage::load()
191 {
192     UiSettings s("");
193     foreach(QObject *widget, _autoWidgets) {
194         QString key = autoWidgetSettingsKey(widget);
195         QVariant val;
196         if (key.isEmpty())
197             val = loadAutoWidgetValue(widget->objectName());
198         else
199             val = s.value(key, QVariant());
200         if (!val.isValid())
201             val = widget->property("defaultValue");
202         widget->setProperty(autoWidgetPropertyName(widget), val);
203         widget->setProperty("storedValue", val);
204     }
205     bool old = hasChanged();
206     _autoWidgetsChanged = _changed = false;
207     if (hasChanged() != old)
208         emit changed(hasChanged());
209 }
210
211
212 void SettingsPage::save()
213 {
214     UiSettings s("");
215     foreach(QObject *widget, _autoWidgets) {
216         QString key = autoWidgetSettingsKey(widget);
217         QVariant val = widget->property(autoWidgetPropertyName(widget));
218         widget->setProperty("storedValue", val);
219         if (key.isEmpty())
220             saveAutoWidgetValue(widget->objectName(), val);
221         else
222             s.setValue(key, val);
223     }
224     bool old = hasChanged();
225     _autoWidgetsChanged = _changed = false;
226     if (hasChanged() != old)
227         emit changed(hasChanged());
228 }
229
230
231 void SettingsPage::defaults()
232 {
233     foreach(QObject *widget, _autoWidgets) {
234         QVariant val = widget->property("defaultValue");
235         widget->setProperty(autoWidgetPropertyName(widget), val);
236     }
237     autoWidgetHasChanged();
238 }
239
240
241 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName)
242 {
243     qWarning() << "Could not load value for SettingsPage widget" << widgetName;
244     return QVariant();
245 }
246
247
248 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &)
249 {
250     qWarning() << "Could not save value for SettingsPage widget" << widgetName;
251 }