1 /***************************************************************************
2 * Copyright (C) 2005-09 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) any later version. *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "settingspage.h"
28 #include "uisettings.h"
30 SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
35 _autoWidgetsChanged(false)
39 void SettingsPage::setChangedState(bool hasChanged_) {
40 if(hasChanged_ != _changed) {
41 bool old = hasChanged();
42 _changed = hasChanged_;
43 if(hasChanged() != old)
44 emit changed(hasChanged());
48 void SettingsPage::load(QCheckBox *box, bool checked) {
49 box->setProperty("StoredValue", checked);
50 box->setChecked(checked);
53 bool SettingsPage::hasChanged(QCheckBox *box) {
54 return box->property("StoredValue").toBool() != box->isChecked();
58 void SettingsPage::load(QComboBox *box, int index) {
59 box->setProperty("StoredValue", index);
60 box->setCurrentIndex(index);
63 bool SettingsPage::hasChanged(QComboBox *box) {
64 return box->property("StoredValue").toInt() != box->currentIndex();
67 void SettingsPage::load(QSpinBox *box, int value) {
68 box->setProperty("StoredValue", value);
72 bool SettingsPage::hasChanged(QSpinBox *box) {
73 return box->property("StoredValue").toInt() != box->value();
76 /*** Auto child widget handling ***/
78 void SettingsPage::initAutoWidgets() {
81 // find all descendants that should be considered auto widgets
82 // we need to climb the QObject tree recursively
83 findAutoWidgets(this, &_autoWidgets);
85 foreach(QObject *widget, _autoWidgets) {
86 if(widget->inherits("ColorButton"))
87 connect(widget, SIGNAL(colorChanged(QColor)), SLOT(autoWidgetHasChanged()));
88 else if(widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
89 connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged()));
90 else if(widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
91 connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged()));
92 else if(widget->inherits("QComboBox"))
93 connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged()));
94 else if(widget->inherits("QSpinBox"))
95 connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged()));
96 else if(widget->inherits("FontSelector"))
97 connect(widget, SIGNAL(fontChanged(QFont)), SLOT(autoWidgetHasChanged()));
99 qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
103 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const {
104 foreach(QObject *child, parent->children()) {
105 if(child->property("settingsKey").isValid())
106 autoList->append(child);
107 findAutoWidgets(child, autoList);
111 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const {
113 if(widget->inherits("ColorButton"))
115 else if(widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
117 else if(widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
119 else if(widget->inherits("QComboBox"))
120 prop = "currentIndex";
121 else if(widget->inherits("QSpinBox"))
123 else if(widget->inherits("FontSelector"))
124 prop = "selectedFont";
126 qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
131 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const {
132 QString key = widget->property("settingsKey").toString();
135 if(key.startsWith('/'))
138 key.prepend(settingsKey() + '/');
142 void SettingsPage::autoWidgetHasChanged() {
143 bool changed_ = false;
144 foreach(QObject *widget, _autoWidgets) {
145 QVariant curValue = widget->property(autoWidgetPropertyName(widget));
146 if(!curValue.isValid())
147 qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
149 if(curValue != widget->property("storedValue")) {
155 if(changed_ != _autoWidgetsChanged) {
156 bool old = hasChanged();
157 _autoWidgetsChanged = changed_;
158 if(hasChanged() != old)
159 emit changed(hasChanged());
163 void SettingsPage::load() {
165 foreach(QObject *widget, _autoWidgets) {
166 QString key = autoWidgetSettingsKey(widget);
169 val = loadAutoWidgetValue(widget->objectName());
171 val = s.value(key, QVariant());
173 val = widget->property("defaultValue");
174 widget->setProperty(autoWidgetPropertyName(widget), val);
175 widget->setProperty("storedValue", val);
177 bool old = hasChanged();
178 _autoWidgetsChanged = _changed = false;
179 if(hasChanged() != old)
180 emit changed(hasChanged());
183 void SettingsPage::save() {
185 foreach(QObject *widget, _autoWidgets) {
186 QString key = autoWidgetSettingsKey(widget);
187 QVariant val = widget->property(autoWidgetPropertyName(widget));
188 widget->setProperty("storedValue", val);
190 saveAutoWidgetValue(widget->objectName(), val);
192 s.setValue(key, val);
194 bool old = hasChanged();
195 _autoWidgetsChanged = _changed = false;
196 if(hasChanged() != old)
197 emit changed(hasChanged());
200 void SettingsPage::defaults() {
201 foreach(QObject *widget, _autoWidgets) {
202 QVariant val = widget->property("defaultValue");
203 widget->setProperty(autoWidgetPropertyName(widget), val);
205 autoWidgetHasChanged();
208 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName) {
209 qWarning() << "Could not load value for SettingsPage widget" << widgetName;
213 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &) {
214 qWarning() << "Could not save value for SettingsPage widget" << widgetName;