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"
29 #include "fontselector.h"
30 #include "uisettings.h"
31 #include "widgethelpers.h"
33 SettingsPage::SettingsPage(QString category, QString title, QWidget *parent)
35 _category(std::move(category)),
36 _title(std::move(title)),
38 _autoWidgetsChanged(false)
43 void SettingsPage::setChangedState(bool hasChanged_)
45 if (hasChanged_ != _changed) {
46 bool old = hasChanged();
47 _changed = hasChanged_;
48 if (hasChanged() != old)
49 emit changed(hasChanged());
54 void SettingsPage::load(QCheckBox *box, bool checked)
56 box->setProperty("storedValue", checked);
57 box->setChecked(checked);
61 bool SettingsPage::hasChanged(QCheckBox *box)
63 return box->property("storedValue").toBool() != box->isChecked();
67 void SettingsPage::load(QComboBox *box, int index)
69 box->setProperty("storedValue", index);
70 box->setCurrentIndex(index);
74 bool SettingsPage::hasChanged(QComboBox *box)
76 return box->property("storedValue").toInt() != box->currentIndex();
80 void SettingsPage::load(QSpinBox *box, int value)
82 box->setProperty("storedValue", value);
87 bool SettingsPage::hasChanged(QSpinBox *box)
89 return box->property("storedValue").toInt() != box->value();
93 void SettingsPage::load(FontSelector *box, QFont value)
95 box->setProperty("storedValue", value);
96 box->setSelectedFont(value);
100 bool SettingsPage::hasChanged(FontSelector *box)
102 return box->property("storedValue").value<QFont>() != box->selectedFont();
106 /*** Auto child widget handling ***/
108 void SettingsPage::initAutoWidgets()
110 _autoWidgets.clear();
112 // find all descendants that should be considered auto widgets
113 // we need to climb the QObject tree recursively
114 findAutoWidgets(this, &_autoWidgets);
116 if (!connectToWidgetsChangedSignals(_autoWidgets, this, &SettingsPage::autoWidgetHasChanged)) {
117 qWarning() << "SettingsPage::initAutoWidgets(): Unsupported auto widget type(s)!";
122 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const
124 foreach(QObject *child, parent->children()) {
125 if (child->property("settingsKey").isValid())
126 autoList->append(child);
127 findAutoWidgets(child, autoList);
132 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const
135 if (widget->inherits("ColorButton"))
137 else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
139 else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
141 else if (widget->inherits("QComboBox"))
142 prop = "currentIndex";
143 else if (widget->inherits("QSpinBox"))
145 else if (widget->inherits("FontSelector"))
146 prop = "selectedFont";
148 qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
154 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const
156 QString key = widget->property("settingsKey").toString();
159 if (key.startsWith('/'))
162 key.prepend(settingsKey() + '/');
167 void SettingsPage::autoWidgetHasChanged()
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";
175 if (curValue != widget->property("storedValue")) {
181 if (changed_ != _autoWidgetsChanged) {
182 bool old = hasChanged();
183 _autoWidgetsChanged = changed_;
184 if (hasChanged() != old)
185 emit changed(hasChanged());
190 void SettingsPage::load()
193 foreach(QObject *widget, _autoWidgets) {
194 QString key = autoWidgetSettingsKey(widget);
197 val = loadAutoWidgetValue(widget->objectName());
199 val = s.value(key, QVariant());
201 val = widget->property("defaultValue");
202 widget->setProperty(autoWidgetPropertyName(widget), val);
203 widget->setProperty("storedValue", val);
205 bool old = hasChanged();
206 _autoWidgetsChanged = _changed = false;
207 if (hasChanged() != old)
208 emit changed(hasChanged());
212 void SettingsPage::save()
215 foreach(QObject *widget, _autoWidgets) {
216 QString key = autoWidgetSettingsKey(widget);
217 QVariant val = widget->property(autoWidgetPropertyName(widget));
218 widget->setProperty("storedValue", val);
220 saveAutoWidgetValue(widget->objectName(), val);
222 s.setValue(key, val);
224 bool old = hasChanged();
225 _autoWidgetsChanged = _changed = false;
226 if (hasChanged() != old)
227 emit changed(hasChanged());
231 void SettingsPage::defaults()
233 foreach(QObject *widget, _autoWidgets) {
234 QVariant val = widget->property("defaultValue");
235 widget->setProperty(autoWidgetPropertyName(widget), val);
237 autoWidgetHasChanged();
241 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName)
243 qWarning() << "Could not load value for SettingsPage widget" << widgetName;
248 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &)
250 qWarning() << "Could not save value for SettingsPage widget" << widgetName;