1 /***************************************************************************
2 * Copyright (C) 2005-2020 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"
30 #include "fontselector.h"
31 #include "uisettings.h"
32 #include "widgethelpers.h"
34 SettingsPage::SettingsPage(QString category, QString title, QWidget* parent)
36 , _category(std::move(category))
37 , _title(std::move(title))
39 , _autoWidgetsChanged(false)
42 void SettingsPage::setChangedState(bool hasChanged_)
44 if (hasChanged_ != _changed) {
45 bool old = hasChanged();
46 _changed = hasChanged_;
47 if (hasChanged() != old)
48 emit changed(hasChanged());
52 void SettingsPage::load(QCheckBox* box, bool checked)
54 box->setProperty("storedValue", checked);
55 box->setChecked(checked);
58 bool SettingsPage::hasChanged(QCheckBox* box)
60 return box->property("storedValue").toBool() != box->isChecked();
63 void SettingsPage::load(QComboBox* box, int index)
65 box->setProperty("storedValue", index);
66 box->setCurrentIndex(index);
69 bool SettingsPage::hasChanged(QComboBox* box)
71 return box->property("storedValue").toInt() != box->currentIndex();
74 void SettingsPage::load(QSpinBox* box, int value)
76 box->setProperty("storedValue", value);
80 bool SettingsPage::hasChanged(QSpinBox* box)
82 return box->property("storedValue").toInt() != box->value();
85 void SettingsPage::load(FontSelector* box, QFont value)
87 box->setProperty("storedValue", value);
88 box->setSelectedFont(value);
91 bool SettingsPage::hasChanged(FontSelector* box)
93 return box->property("storedValue").value<QFont>() != box->selectedFont();
96 /*** Auto child widget handling ***/
98 void SettingsPage::initAutoWidgets()
100 _autoWidgets.clear();
102 // find all descendants that should be considered auto widgets
103 // we need to climb the QObject tree recursively
104 findAutoWidgets(this, &_autoWidgets);
106 if (!connectToWidgetsChangedSignals(_autoWidgets, this, &SettingsPage::autoWidgetHasChanged)) {
107 qWarning() << "SettingsPage::initAutoWidgets(): Unsupported auto widget type(s)!";
111 void SettingsPage::findAutoWidgets(QObject* parent, QObjectList* autoList) const
113 foreach (QObject* child, parent->children()) {
114 if (child->property("settingsKey").isValid())
115 autoList->append(child);
116 findAutoWidgets(child, autoList);
120 QByteArray SettingsPage::autoWidgetPropertyName(QObject* widget) const
123 if (widget->inherits("ColorButton"))
125 else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
127 else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
129 else if (widget->inherits("QComboBox"))
130 prop = "currentIndex";
131 else if (widget->inherits("QSpinBox"))
133 else if (widget->inherits("FontSelector"))
134 prop = "selectedFont";
136 qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
141 QString SettingsPage::autoWidgetSettingsKey(QObject* widget) const
143 QString key = widget->property("settingsKey").toString();
146 if (key.startsWith('/'))
149 key.prepend(settingsKey() + '/');
153 void SettingsPage::autoWidgetHasChanged()
155 bool changed_ = false;
156 foreach (QObject* widget, _autoWidgets) {
157 QVariant curValue = widget->property(autoWidgetPropertyName(widget));
158 if (!curValue.isValid())
159 qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
161 if (curValue != widget->property("storedValue")) {
167 if (changed_ != _autoWidgetsChanged) {
168 bool old = hasChanged();
169 _autoWidgetsChanged = changed_;
170 if (hasChanged() != old)
171 emit changed(hasChanged());
175 void SettingsPage::load()
178 foreach (QObject* widget, _autoWidgets) {
179 QString key = autoWidgetSettingsKey(widget);
182 val = loadAutoWidgetValue(widget->objectName());
184 val = s.value(key, QVariant());
186 val = widget->property("defaultValue");
187 widget->setProperty(autoWidgetPropertyName(widget), val);
188 widget->setProperty("storedValue", val);
190 bool old = hasChanged();
191 _autoWidgetsChanged = _changed = false;
192 if (hasChanged() != old)
193 emit changed(hasChanged());
196 void SettingsPage::save()
199 foreach (QObject* widget, _autoWidgets) {
200 QString key = autoWidgetSettingsKey(widget);
201 QVariant val = widget->property(autoWidgetPropertyName(widget));
202 widget->setProperty("storedValue", val);
204 saveAutoWidgetValue(widget->objectName(), val);
206 s.setValue(key, val);
208 bool old = hasChanged();
209 _autoWidgetsChanged = _changed = false;
210 if (hasChanged() != old)
211 emit changed(hasChanged());
214 void SettingsPage::defaults()
216 foreach (QObject* widget, _autoWidgets) {
217 QVariant val = widget->property("defaultValue");
218 widget->setProperty(autoWidgetPropertyName(widget), val);
220 autoWidgetHasChanged();
223 QVariant SettingsPage::loadAutoWidgetValue(const QString& widgetName)
225 qWarning() << "Could not load value for SettingsPage widget" << widgetName;
229 void SettingsPage::saveAutoWidgetValue(const QString& widgetName, const QVariant&)
231 qWarning() << "Could not save value for SettingsPage widget" << widgetName;