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