1 /***************************************************************************
2 * Copyright (C) 2005-2015 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();
90 /*** Auto child widget handling ***/
92 void SettingsPage::initAutoWidgets()
96 // find all descendants that should be considered auto widgets
97 // we need to climb the QObject tree recursively
98 findAutoWidgets(this, &_autoWidgets);
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()));
114 qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
119 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const
121 foreach(QObject *child, parent->children()) {
122 if (child->property("settingsKey").isValid())
123 autoList->append(child);
124 findAutoWidgets(child, autoList);
129 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const
132 if (widget->inherits("ColorButton"))
134 else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
136 else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
138 else if (widget->inherits("QComboBox"))
139 prop = "currentIndex";
140 else if (widget->inherits("QSpinBox"))
142 else if (widget->inherits("FontSelector"))
143 prop = "selectedFont";
145 qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
151 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const
153 QString key = widget->property("settingsKey").toString();
156 if (key.startsWith('/'))
159 key.prepend(settingsKey() + '/');
164 void SettingsPage::autoWidgetHasChanged()
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";
172 if (curValue != widget->property("storedValue")) {
178 if (changed_ != _autoWidgetsChanged) {
179 bool old = hasChanged();
180 _autoWidgetsChanged = changed_;
181 if (hasChanged() != old)
182 emit changed(hasChanged());
187 void SettingsPage::load()
190 foreach(QObject *widget, _autoWidgets) {
191 QString key = autoWidgetSettingsKey(widget);
194 val = loadAutoWidgetValue(widget->objectName());
196 val = s.value(key, QVariant());
198 val = widget->property("defaultValue");
199 widget->setProperty(autoWidgetPropertyName(widget), val);
200 widget->setProperty("storedValue", val);
202 bool old = hasChanged();
203 _autoWidgetsChanged = _changed = false;
204 if (hasChanged() != old)
205 emit changed(hasChanged());
209 void SettingsPage::save()
212 foreach(QObject *widget, _autoWidgets) {
213 QString key = autoWidgetSettingsKey(widget);
214 QVariant val = widget->property(autoWidgetPropertyName(widget));
215 widget->setProperty("storedValue", val);
217 saveAutoWidgetValue(widget->objectName(), val);
219 s.setValue(key, val);
221 bool old = hasChanged();
222 _autoWidgetsChanged = _changed = false;
223 if (hasChanged() != old)
224 emit changed(hasChanged());
228 void SettingsPage::defaults()
230 foreach(QObject *widget, _autoWidgets) {
231 QVariant val = widget->property("defaultValue");
232 widget->setProperty(autoWidgetPropertyName(widget), val);
234 autoWidgetHasChanged();
238 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName)
240 qWarning() << "Could not load value for SettingsPage widget" << widgetName;
245 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &)
247 qWarning() << "Could not save value for SettingsPage widget" << widgetName;