File indexing completed on 2024-04-21 05:41:34
0001 /* 0002 This file is part of KCachegrind. 0003 0004 SPDX-FileCopyrightText: 2002-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de> 0005 0006 SPDX-License-Identifier: GPL-2.0-only 0007 */ 0008 0009 /* 0010 * Configuration for KCachegrind 0011 */ 0012 0013 #include "kdeconfig.h" 0014 0015 #include <KConfig> 0016 #include <KConfigGroup> 0017 0018 #include "tracedata.h" 0019 #include "traceitemview.h" 0020 #include "ui_configdlgbase.h" 0021 0022 // 0023 // KDEConfigGroup 0024 // 0025 0026 KDEConfigGroup::KDEConfigGroup(KConfigGroup* group, bool readOnly) 0027 { 0028 _kgroup = group; 0029 _readOnly = readOnly; 0030 } 0031 0032 KDEConfigGroup::~KDEConfigGroup() 0033 { 0034 delete _kgroup; 0035 } 0036 0037 void KDEConfigGroup::setValue(const QString& key, const QVariant& value, 0038 const QVariant& defaultValue) 0039 { 0040 if ((_kgroup == nullptr) || _readOnly) return; 0041 0042 if (value == defaultValue) { 0043 _kgroup->deleteEntry(key); 0044 return; 0045 } 0046 0047 switch(value.type()) { 0048 case QVariant::Bool: 0049 _kgroup->writeEntry(key, value.toBool()); 0050 break; 0051 case QVariant::Int: 0052 _kgroup->writeEntry(key, value.toInt()); 0053 break; 0054 case QVariant::Double: 0055 _kgroup->writeEntry(key, value.toDouble()); 0056 break; 0057 case QVariant::String: 0058 _kgroup->writeEntry(key, value.toString()); 0059 break; 0060 case QVariant::StringList: 0061 _kgroup->writeEntry(key, value.toStringList()); 0062 break; 0063 case QVariant::Color: 0064 _kgroup->writeEntry(key, value.value<QColor>()); 0065 break; 0066 default: 0067 qFatal("KDEConfigGroup::setValue - QVariant type %s not supported", 0068 value.typeName()); 0069 } 0070 } 0071 0072 QVariant KDEConfigGroup::value(const QString& key, 0073 const QVariant& defaultValue) const 0074 { 0075 if (_kgroup == nullptr) return defaultValue; 0076 0077 switch(defaultValue.type()) { 0078 case QVariant::Bool: 0079 return QVariant(_kgroup->readEntry(key, 0080 defaultValue.toBool())); 0081 case QVariant::Int: 0082 return QVariant(_kgroup->readEntry(key, 0083 defaultValue.toInt())); 0084 case QVariant::Double: 0085 return QVariant(_kgroup->readEntry(key, 0086 defaultValue.toDouble())); 0087 case QVariant::String: 0088 return QVariant(_kgroup->readEntry(key, 0089 defaultValue.toString())); 0090 case QVariant::StringList: 0091 return QVariant(_kgroup->readEntry(key, 0092 defaultValue.toStringList())); 0093 case QVariant::Color: 0094 return QVariant(_kgroup->readEntry(key, 0095 defaultValue.value<QColor>())); 0096 default: 0097 qFatal("KDEConfigGroup::value - QVariant type %s not supported", 0098 defaultValue.typeName()); 0099 } 0100 return defaultValue; 0101 } 0102 0103 0104 0105 // 0106 // KDEConfigStorage 0107 // 0108 0109 KDEConfigStorage::KDEConfigStorage(KConfig* kconfig) 0110 { 0111 _kconfig = kconfig; 0112 } 0113 0114 ConfigGroup* KDEConfigStorage::getGroup(const QString& group, 0115 const QString& optSuffix) 0116 { 0117 KConfigGroup* g; 0118 bool readOnly; 0119 0120 if (!optSuffix.isEmpty()) { 0121 readOnly = true; 0122 QStringList gList = _kconfig->groupList(); 0123 if (gList.contains(group+optSuffix)) 0124 g = new KConfigGroup(_kconfig, group+optSuffix); 0125 else if (gList.contains(group)) 0126 g = new KConfigGroup(_kconfig, group); 0127 else 0128 g = nullptr; 0129 } 0130 else { 0131 readOnly = false; 0132 g = new KConfigGroup(_kconfig, group); 0133 } 0134 0135 return new KDEConfigGroup(g, readOnly); 0136 }