File indexing completed on 2024-04-28 05:41:40

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 QCachegrind
0011  */
0012 
0013 #include "qcgconfig.h"
0014 
0015 #include <QSettings>
0016 
0017 #include "tracedata.h"
0018 #include "traceitemview.h"
0019 
0020 //
0021 // QCGConfigGroup
0022 //
0023 
0024 QCGConfigGroup::QCGConfigGroup(QSettings* settings, QString prefix,
0025                                bool readOnly)
0026 {
0027     _settings = settings;
0028     _prefix = prefix;
0029     _readOnly = readOnly;
0030 }
0031 
0032 QCGConfigGroup::~QCGConfigGroup()
0033 {}
0034 
0035 void QCGConfigGroup::setValue(const QString& key, const QVariant& value,
0036                               const QVariant& defaultValue)
0037 {
0038     if ((_settings == nullptr) || _readOnly) return;
0039 
0040     QString fullKey = QStringLiteral("%1/%2").arg(_prefix).arg(key);
0041     if (value == defaultValue)
0042         _settings->remove(fullKey);
0043     else
0044         _settings->setValue(fullKey, value);
0045 }
0046 
0047 QVariant QCGConfigGroup::value(const QString& key,
0048                                const QVariant& defaultValue) const
0049 {
0050     if (_settings == nullptr) return defaultValue;
0051 
0052     QString fullKey = QStringLiteral("%1/%2").arg(_prefix).arg(key);
0053     return _settings->value(fullKey, defaultValue);
0054 }
0055 
0056 
0057 
0058 //
0059 // QCGConfigStorage
0060 //
0061 
0062 QCGConfigStorage::QCGConfigStorage()
0063 {
0064     _settings = new QSettings;
0065 }
0066 
0067 QCGConfigStorage::~QCGConfigStorage()
0068 {
0069     delete _settings;
0070 }
0071 
0072 ConfigGroup* QCGConfigStorage::getGroup(const QString& group,
0073                                         const QString& optSuffix)
0074 {
0075     // for writing
0076     if (optSuffix.isEmpty())
0077         return new QCGConfigGroup(_settings, group, false);
0078 
0079     // for reading
0080     QStringList gList = _settings->childGroups();
0081     if (gList.contains(group+optSuffix))
0082         return new QCGConfigGroup(_settings, group + optSuffix, true);
0083     else if (gList.contains(group))
0084         return new QCGConfigGroup(_settings, group, true);
0085 
0086     // requested group does not exist, return only default values
0087     return new QCGConfigGroup(nullptr, QString(), true);
0088 }