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())
29 if (!var
.isNull() && var
.convert(QVariant::Bool
) && var
.isValid())
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())
43 if (!var
.isNull() && var
.convert(QVariant::UInt
) && var
.isValid())
46 const uint value
= var
.toUInt();
51 case Qt::PartiallyChecked
:
53 return static_cast<Qt::CheckState
>(value
);
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())
67 if (!var
.isNull() && var
.convert(QVariant::Int
) && var
.isValid())
70 const int value
= var
.toInt();
71 return value
>= 0 ? value
: 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())
84 if (!var
.isNull() && var
.convert(QVariant::UInt
) && var
.isValid())
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())
98 if (!var
.isNull() && var
.convert(QVariant::Double
) && var
.isValid())
100 return var
.toDouble();
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())
112 if (!var
.isNull() && var
.convert(QVariant::String
) && var
.isValid())
114 return var
.toString();
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())
126 if (!var
.isNull() && var
.convert(QVariant::ByteArray
) && var
.isValid())
128 return var
.toByteArray();
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())
140 if (!var
.isNull() && var
.convert(QVariant::StringList
) && var
.isValid())
142 return var
.toStringList();
147 //---------------------------------------------------------------------------------------------------------------------