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 "fontselector.h"
30 #include "uisettings.h"
32 SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
37 _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());
53 void SettingsPage::load(QCheckBox *box, bool checked)
55 box->setProperty("storedValue", checked);
56 box->setChecked(checked);
60 bool SettingsPage::hasChanged(QCheckBox *box)
62 return box->property("storedValue").toBool() != box->isChecked();
66 void SettingsPage::load(QComboBox *box, int index)
68 box->setProperty("storedValue", index);
69 box->setCurrentIndex(index);
73 bool SettingsPage::hasChanged(QComboBox *box)
75 return box->property("storedValue").toInt() != box->currentIndex();
79 void SettingsPage::load(QSpinBox *box, int value)
81 box->setProperty("storedValue", value);
86 bool SettingsPage::hasChanged(QSpinBox *box)
88 return box->property("storedValue").toInt() != box->value();
92 void SettingsPage::load(FontSelector *box, QFont value)
94 box->setProperty("storedValue", value);
95 box->setSelectedFont(value);
99 bool SettingsPage::hasChanged(FontSelector *box)
101 return box->property("storedValue").value<QFont>() != box->selectedFont();
105 /*** Auto child widget handling ***/
107 void SettingsPage::initAutoWidgets()
109 _autoWidgets.clear();
111 // find all descendants that should be considered auto widgets
112 // we need to climb the QObject tree recursively
113 findAutoWidgets(this, &_autoWidgets);
115 foreach(QObject *widget, _autoWidgets) {
116 if (widget->inherits("ColorButton"))
117 connect(widget, SIGNAL(colorChanged(QColor)), SLOT(autoWidgetHasChanged()));
118 else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
119 connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged()));
120 else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
121 connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged()));
122 else if (widget->inherits("QComboBox"))
123 connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged()));
124 else if (widget->inherits("QSpinBox"))
125 connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged()));
126 else if (widget->inherits("FontSelector"))
127 connect(widget, SIGNAL(fontChanged(QFont)), SLOT(autoWidgetHasChanged()));
129 qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
134 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const
136 foreach(QObject *child, parent->children()) {
137 if (child->property("settingsKey").isValid())
138 autoList->append(child);
139 findAutoWidgets(child, autoList);
144 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const
147 if (widget->inherits("ColorButton"))
149 else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
151 else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
153 else if (widget->inherits("QComboBox"))
154 prop = "currentIndex";
155 else if (widget->inherits("QSpinBox"))
157 else if (widget->inherits("FontSelector"))
158 prop = "selectedFont";
160 qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
166 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const
168 QString key = widget->property("settingsKey").toString();
171 if (key.startsWith('/'))
174 key.prepend(settingsKey() + '/');
179 void SettingsPage::autoWidgetHasChanged()
181 bool changed_ = false;
182 foreach(QObject *widget, _autoWidgets) {
183 QVariant curValue = widget->property(autoWidgetPropertyName(widget));
184 if (!curValue.isValid())
185 qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
187 if (curValue != widget->property("storedValue")) {
193 if (changed_ != _autoWidgetsChanged) {
194 bool old = hasChanged();
195 _autoWidgetsChanged = changed_;
196 if (hasChanged() != old)
197 emit changed(hasChanged());
202 void SettingsPage::load()
205 foreach(QObject *widget, _autoWidgets) {
206 QString key = autoWidgetSettingsKey(widget);
209 val = loadAutoWidgetValue(widget->objectName());
211 val = s.value(key, QVariant());
213 val = widget->property("defaultValue");
214 widget->setProperty(autoWidgetPropertyName(widget), val);
215 widget->setProperty("storedValue", val);
217 bool old = hasChanged();
218 _autoWidgetsChanged = _changed = false;
219 if (hasChanged() != old)
220 emit changed(hasChanged());
224 void SettingsPage::save()
227 foreach(QObject *widget, _autoWidgets) {
228 QString key = autoWidgetSettingsKey(widget);
229 QVariant val = widget->property(autoWidgetPropertyName(widget));
230 widget->setProperty("storedValue", val);
232 saveAutoWidgetValue(widget->objectName(), val);
234 s.setValue(key, val);
236 bool old = hasChanged();
237 _autoWidgetsChanged = _changed = false;
238 if (hasChanged() != old)
239 emit changed(hasChanged());
243 void SettingsPage::defaults()
245 foreach(QObject *widget, _autoWidgets) {
246 QVariant val = widget->property("defaultValue");
247 widget->setProperty(autoWidgetPropertyName(widget), val);
249 autoWidgetHasChanged();
253 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName)
255 qWarning() << "Could not load value for SettingsPage widget" << widgetName;
260 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &)
262 qWarning() << "Could not save value for SettingsPage widget" << widgetName;