Cleanup
[carla.git] / source / frontend / utils / qsafesettings.cpp
blobe5e06a779d9aee204c7c8c599a851236f5fcb28d
1 /*
2 * Carla plugin host
3 * Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or 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 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "qsafesettings.hpp"
20 //---------------------------------------------------------------------------------------------------------------------
22 bool QSafeSettings::valueBool(const QString& key, const bool defaultValue) const
24 QVariant var(value(key, defaultValue));
26 #if QT_VERSION >= 0x60000
27 if (!var.isNull() && var.convert(QMetaType(QMetaType::Bool)) && var.isValid())
28 #else
29 if (!var.isNull() && var.convert(QVariant::Bool) && var.isValid())
30 #endif
31 return var.toBool();
33 return defaultValue;
36 Qt::CheckState QSafeSettings::valueCheckState(const QString& key, const Qt::CheckState defaultValue) const
38 QVariant var(value(key, defaultValue));
40 #if QT_VERSION >= 0x60000
41 if (!var.isNull() && var.convert(QMetaType(QMetaType::UInt)) && var.isValid())
42 #else
43 if (!var.isNull() && var.convert(QVariant::UInt) && var.isValid())
44 #endif
46 const uint value = var.toUInt();
48 switch (value)
50 case Qt::Unchecked:
51 case Qt::PartiallyChecked:
52 case Qt::Checked:
53 return static_cast<Qt::CheckState>(value);
57 return defaultValue;
60 int QSafeSettings::valueIntPositive(const QString& key, const int defaultValue) const
62 QVariant var(value(key, defaultValue));
64 #if QT_VERSION >= 0x60000
65 if (!var.isNull() && var.convert(QMetaType(QMetaType::Int)) && var.isValid())
66 #else
67 if (!var.isNull() && var.convert(QVariant::Int) && var.isValid())
68 #endif
70 const int value = var.toInt();
71 return value >= 0 ? value : defaultValue;
74 return defaultValue;
77 uint QSafeSettings::valueUInt(const QString& key, const uint defaultValue) const
79 QVariant var(value(key, defaultValue));
81 #if QT_VERSION >= 0x60000
82 if (!var.isNull() && var.convert(QMetaType(QMetaType::UInt)) && var.isValid())
83 #else
84 if (!var.isNull() && var.convert(QVariant::UInt) && var.isValid())
85 #endif
86 return var.toUInt();
88 return defaultValue;
91 double QSafeSettings::valueDouble(const QString& key, const double defaultValue) const
93 QVariant var(value(key, defaultValue));
95 #if QT_VERSION >= 0x60000
96 if (!var.isNull() && var.convert(QMetaType(QMetaType::Double)) && var.isValid())
97 #else
98 if (!var.isNull() && var.convert(QVariant::Double) && var.isValid())
99 #endif
100 return var.toDouble();
102 return defaultValue;
105 QString QSafeSettings::valueString(const QString& key, const QString& defaultValue) const
107 QVariant var(value(key, defaultValue));
109 #if QT_VERSION >= 0x60000
110 if (!var.isNull() && var.convert(QMetaType(QMetaType::QString)) && var.isValid())
111 #else
112 if (!var.isNull() && var.convert(QVariant::String) && var.isValid())
113 #endif
114 return var.toString();
116 return defaultValue;
119 QByteArray QSafeSettings::valueByteArray(const QString& key, const QByteArray defaultValue) const
121 QVariant var(value(key, defaultValue));
123 #if QT_VERSION >= 0x60000
124 if (!var.isNull() && var.convert(QMetaType(QMetaType::QByteArray)) && var.isValid())
125 #else
126 if (!var.isNull() && var.convert(QVariant::ByteArray) && var.isValid())
127 #endif
128 return var.toByteArray();
130 return defaultValue;
133 QStringList QSafeSettings::valueStringList(const QString& key, const QStringList defaultValue) const
135 QVariant var(value(key, defaultValue));
137 #if QT_VERSION >= 0x60000
138 if (!var.isNull() && var.convert(QMetaType(QMetaType::QStringList)) && var.isValid())
139 #else
140 if (!var.isNull() && var.convert(QVariant::StringList) && var.isValid())
141 #endif
142 return var.toStringList();
144 return defaultValue;
147 //---------------------------------------------------------------------------------------------------------------------