File indexing completed on 2024-05-12 17:10:51
0001 /* 0002 * configmanager.h - Emerald window decoration for KDE 0003 * 0004 * Copyright (c) 2010 Christoph Feck <christoph@maxiom.de> 0005 * 0006 * This program is free software; you can redistribute it and/or modify 0007 * it under the terms of the GNU General Public License as published by 0008 * the Free Software Foundation; either version 2 of the License, or 0009 * (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program; if not, write to the Free Software 0018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 0019 * 0020 */ 0021 0022 #ifndef CONFIGMANAGER_H 0023 #define CONFIGMANAGER_H 1 0024 0025 #include <KConfigGroup> 0026 #include <QMetaProperty> 0027 0028 namespace Smaragd 0029 { 0030 0031 class ConfigManager 0032 { 0033 public: 0034 ConfigManager() 0035 { 0036 /* */ 0037 } 0038 0039 ~ConfigManager() 0040 { 0041 /* */ 0042 } 0043 0044 public: 0045 void addWidgets(QWidget *parent) 0046 { 0047 typedef QList<QWidget *> WidgetList; 0048 WidgetList widgets = parent->findChildren<QWidget *>(QRegExp(QLatin1String("^cm_"))); 0049 for (WidgetList::const_iterator i = widgets.constBegin(); i != widgets.constEnd(); ++i) { 0050 QWidget *widget = *i; 0051 items.append(Item(widget)); 0052 } 0053 } 0054 0055 void connectConfigChanged(QObject *receiver, const char slot[]) const 0056 { 0057 for (ItemList::const_iterator item = items.constBegin(); item != items.constEnd(); ++item) { 0058 item->connectValueChanged(receiver, slot); 0059 } 0060 } 0061 0062 void save(KConfigGroup &configGroup) const 0063 { 0064 for (ItemList::const_iterator item = items.constBegin(); item != items.constEnd(); ++item) { 0065 QVariant value = item->value(); 0066 if (!value.isNull()) { 0067 if (value == item->configDefault) { 0068 configGroup.deleteEntry(item->configLabel()); 0069 } else { 0070 configGroup.writeEntry(item->configLabel(), value); 0071 } 0072 } 0073 } 0074 } 0075 0076 void load(const KConfigGroup &configGroup) const 0077 { 0078 for (ItemList::const_iterator item = items.constBegin(); item != items.constEnd(); ++item) { 0079 item->setValue(((Item) *item).configSaved = configGroup.readEntry(item->configLabel(), item->configDefault.isNull() ? QString() : item->configDefault)); 0080 } 0081 } 0082 0083 bool hasChanged() const 0084 { 0085 for (ItemList::const_iterator item = items.constBegin(); item != items.constEnd(); ++item) { 0086 if (item->configSaved != item->value()) { 0087 return true; 0088 } 0089 } 0090 return false; 0091 } 0092 0093 void defaults() const 0094 { 0095 for (ItemList::const_iterator item = items.constBegin(); item != items.constEnd(); ++item) { 0096 item->setValue(item->configDefault); 0097 } 0098 } 0099 0100 private: 0101 class Item 0102 { 0103 public: 0104 Item(QWidget *widget) 0105 : configWidget(widget) 0106 , configDefault(value()) 0107 { 0108 /* */ 0109 } 0110 0111 public: 0112 inline void setValue(const QVariant &value) const; 0113 inline QVariant value() const; 0114 inline void connectValueChanged(QObject *receiver, const char slot[]) const; 0115 inline QString configLabel() const; 0116 0117 public: 0118 QWidget *configWidget; 0119 QVariant configDefault; 0120 QVariant configSaved; 0121 }; 0122 0123 private: 0124 typedef QList<Item> ItemList; 0125 0126 private: 0127 ItemList items; 0128 }; 0129 0130 0131 void ConfigManager::Item::setValue(const QVariant &value) const 0132 { 0133 QMetaProperty property = configWidget->metaObject()->userProperty(); 0134 property.write(configWidget, value); 0135 } 0136 0137 QVariant ConfigManager::Item::value() const 0138 { 0139 QMetaProperty property = configWidget->metaObject()->userProperty(); 0140 return property.read(configWidget); 0141 } 0142 0143 void ConfigManager::Item::connectValueChanged(QObject *receiver, const char slot[]) const 0144 { 0145 QMetaProperty property = configWidget->metaObject()->userProperty(); 0146 if (property.hasNotifySignal()) { 0147 QMetaMethod method = property.notifySignal(); 0148 QByteArray signalSignature(method.methodSignature()); 0149 signalSignature.prepend(QSIGNAL_CODE + '0'); 0150 QObject::connect(configWidget, signalSignature.constData(), receiver, slot); 0151 } 0152 } 0153 0154 QString ConfigManager::Item::configLabel() const 0155 { 0156 QString cmName = configWidget->objectName().mid(3); 0157 return cmName; 0158 } 0159 0160 }; // namespace Smaragd 0161 0162 #endif 0163