1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "settingspage.h"
28 #include "uisettings.h"
30 SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
35 _autoWidgetsChanged(false)
40 void SettingsPage::setChangedState(bool hasChanged_)
42 if (hasChanged_ != _changed) {
43 bool old = hasChanged();
44 _changed = hasChanged_;
45 if (hasChanged() != old)
46 emit changed(hasChanged());
51 void SettingsPage::load(QCheckBox *box, bool checked)
53 box->setProperty("storedValue", checked);
54 box->setChecked(checked);
58 bool SettingsPage::hasChanged(QCheckBox *box)
60 return box->property("storedValue").toBool() != box->isChecked();
64 void SettingsPage::load(QComboBox *box, int index)
66 box->setProperty("storedValue", index);
67 box->setCurrentIndex(index);
71 bool SettingsPage::hasChanged(QComboBox *box)
73 return box->property("storedValue").toInt() != box->currentIndex();
77 void SettingsPage::load(QSpinBox *box, int value)
79 box->setProperty("storedValue", value);
84 bool SettingsPage::hasChanged(QSpinBox *box)
86 return box->property("storedValue").toInt() != box->value();
91 /*** Auto child widget handling ***/
93 void SettingsPage::initAutoWidgets()
97 // find all descendants that should be considered auto widgets
98 // we need to climb the QObject tree recursively
99 findAutoWidgets(this, &_autoWidgets);
101 foreach(QObject *widget, _autoWidgets) {
102 if (widget->inherits("ColorButton"))
103 connect(widget, SIGNAL(colorChanged(QColor)), SLOT(autoWidgetHasChanged()));
104 else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
105 connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged()));
106 else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
107 connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged()));
108 else if (widget->inherits("QComboBox"))
109 connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged()));
110 else if (widget->inherits("QSpinBox"))
111 connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged()));
112 else if (widget->inherits("FontSelector"))
113 connect(widget, SIGNAL(fontChanged(QFont)), SLOT(autoWidgetHasChanged()));
115 qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
120 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const
122 foreach(QObject *child, parent->children()) {
123 if (child->property("settingsKey").isValid())
124 autoList->append(child);
125 findAutoWidgets(child, autoList);
130 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const
133 if (widget->inherits("ColorButton"))
135 else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
137 else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
139 else if (widget->inherits("QComboBox"))
140 prop = "currentIndex";
141 else if (widget->inherits("QSpinBox"))
143 else if (widget->inherits("FontSelector"))
144 prop = "selectedFont";
146 qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
152 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const
154 QString key = widget->property("settingsKey").toString();
157 if (key.startsWith('/'))
160 key.prepend(settingsKey() + '/');
165 void SettingsPage::autoWidgetHasChanged()
167 bool changed_ = false;
168 foreach(QObject *widget, _autoWidgets) {
169 QVariant curValue = widget->property(autoWidgetPropertyName(widget));
170 if (!curValue.isValid())
171 qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
173 if (curValue != widget->property("storedValue")) {
179 if (changed_ != _autoWidgetsChanged) {
180 bool old = hasChanged();
181 _autoWidgetsChanged = changed_;
182 if (hasChanged() != old)
183 emit changed(hasChanged());
188 void SettingsPage::load()
191 foreach(QObject *widget, _autoWidgets) {
192 QString key = autoWidgetSettingsKey(widget);
195 val = loadAutoWidgetValue(widget->objectName());
197 val = s.value(key, QVariant());
199 val = widget->property("defaultValue");
200 widget->setProperty(autoWidgetPropertyName(widget), val);
201 widget->setProperty("storedValue", val);
203 bool old = hasChanged();
204 _autoWidgetsChanged = _changed = false;
205 if (hasChanged() != old)
206 emit changed(hasChanged());
210 void SettingsPage::save()
213 foreach(QObject *widget, _autoWidgets) {
214 QString key = autoWidgetSettingsKey(widget);
215 QVariant val = widget->property(autoWidgetPropertyName(widget));
216 widget->setProperty("storedValue", val);
218 saveAutoWidgetValue(widget->objectName(), val);
220 s.setValue(key, val);
222 bool old = hasChanged();
223 _autoWidgetsChanged = _changed = false;
224 if (hasChanged() != old)
225 emit changed(hasChanged());
229 void SettingsPage::defaults()
231 foreach(QObject *widget, _autoWidgets) {
232 QVariant val = widget->property("defaultValue");
233 widget->setProperty(autoWidgetPropertyName(widget), val);
235 autoWidgetHasChanged();
239 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName)
241 qWarning() << "Could not load value for SettingsPage widget" << widgetName;
246 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &)
248 qWarning() << "Could not save value for SettingsPage widget" << widgetName;