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"
31 #include "uisettings.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 foreach(QObject *widget, _autoWidgets) {
117 if (widget->inherits("ColorButton"))
118 connect(widget, SIGNAL(colorChanged(QColor)), SLOT(autoWidgetHasChanged()));
119 else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
120 connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged()));
121 else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
122 connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged()));
123 else if (widget->inherits("QComboBox"))
124 connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged()));
125 else if (widget->inherits("QSpinBox"))
126 connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged()));
127 else if (widget->inherits("FontSelector"))
128 connect(widget, SIGNAL(fontChanged(QFont)), SLOT(autoWidgetHasChanged()));
130 qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
135 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const
137 foreach(QObject *child, parent->children()) {
138 if (child->property("settingsKey").isValid())
139 autoList->append(child);
140 findAutoWidgets(child, autoList);
145 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const
148 if (widget->inherits("ColorButton"))
150 else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
152 else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
154 else if (widget->inherits("QComboBox"))
155 prop = "currentIndex";
156 else if (widget->inherits("QSpinBox"))
158 else if (widget->inherits("FontSelector"))
159 prop = "selectedFont";
161 qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
167 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const
169 QString key = widget->property("settingsKey").toString();
172 if (key.startsWith('/'))
175 key.prepend(settingsKey() + '/');
180 void SettingsPage::autoWidgetHasChanged()
182 bool changed_ = false;
183 foreach(QObject *widget, _autoWidgets) {
184 QVariant curValue = widget->property(autoWidgetPropertyName(widget));
185 if (!curValue.isValid())
186 qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
188 if (curValue != widget->property("storedValue")) {
194 if (changed_ != _autoWidgetsChanged) {
195 bool old = hasChanged();
196 _autoWidgetsChanged = changed_;
197 if (hasChanged() != old)
198 emit changed(hasChanged());
203 void SettingsPage::load()
206 foreach(QObject *widget, _autoWidgets) {
207 QString key = autoWidgetSettingsKey(widget);
210 val = loadAutoWidgetValue(widget->objectName());
212 val = s.value(key, QVariant());
214 val = widget->property("defaultValue");
215 widget->setProperty(autoWidgetPropertyName(widget), val);
216 widget->setProperty("storedValue", val);
218 bool old = hasChanged();
219 _autoWidgetsChanged = _changed = false;
220 if (hasChanged() != old)
221 emit changed(hasChanged());
225 void SettingsPage::save()
228 foreach(QObject *widget, _autoWidgets) {
229 QString key = autoWidgetSettingsKey(widget);
230 QVariant val = widget->property(autoWidgetPropertyName(widget));
231 widget->setProperty("storedValue", val);
233 saveAutoWidgetValue(widget->objectName(), val);
235 s.setValue(key, val);
237 bool old = hasChanged();
238 _autoWidgetsChanged = _changed = false;
239 if (hasChanged() != old)
240 emit changed(hasChanged());
244 void SettingsPage::defaults()
246 foreach(QObject *widget, _autoWidgets) {
247 QVariant val = widget->property("defaultValue");
248 widget->setProperty(autoWidgetPropertyName(widget), val);
250 autoWidgetHasChanged();
254 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName)
256 qWarning() << "Could not load value for SettingsPage widget" << widgetName;
261 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &)
263 qWarning() << "Could not save value for SettingsPage widget" << widgetName;