13 #ifndef hifi_SettingHandle_h
14 #define hifi_SettingHandle_h
16 #include <type_traits>
18 #include <QtCore/QStack>
19 #include <QtCore/QString>
20 #include <QtCore/QVariant>
21 #include <QtCore/QReadWriteLock>
22 #include <QtCore/QSharedPointer>
23 #include <QtCore/QDebug>
24 #include <QLoggingCategory>
26 #include <glm/glm.hpp>
27 #include <glm/gtc/quaternion.hpp>
29 #include "SettingInterface.h"
32 Q_DECLARE_LOGGING_CATEGORY(settings_handle)
41 extern const QString SETTINGS_FULL_PRIVATE_GROUP_NAME;
62 Group(
const QString &groupName = QString()) {
66 QString name()
const {
return _name; }
68 bool isArray()
const {
return _arraySize != -1; }
70 void setIndex(
int i) {
71 if ( _arraySize < i+1) {
78 int index()
const {
return _arrayIndex; }
79 int size()
const {
return _arraySize; }
80 void setSize(
int sz) { _arraySize = sz; }
89 static const QString firstRun;
92 QString fileName()
const;
94 void remove(
const QString& key);
100 QStringList allKeys()
const;
101 bool contains(
const QString& key)
const;
102 int beginReadArray(
const QString & prefix);
103 void beginWriteArray(
const QString& prefix,
int size = -1);
105 void setArrayIndex(
int i);
107 void beginGroup(
const QString& prefix);
110 void setValue(
const QString& name,
const QVariant& value);
111 QVariant value(
const QString& name,
const QVariant& defaultValue = QVariant())
const;
113 void getFloatValueIfValid(
const QString& name,
float& floatValue);
114 void getBoolValue(
const QString& name,
bool& boolValue);
116 void setVec3Value(
const QString& name,
const glm::vec3& vecValue);
117 void getVec3ValueIfValid(
const QString& name, glm::vec3& vecValue);
119 void setQuatValue(
const QString& name,
const glm::quat& quatValue);
120 void getQuatValueIfValid(
const QString& name, glm::quat& quatValue);
125 QString getGroupPrefix()
const;
126 QString getPath(
const QString &value)
const;
128 QSharedPointer<Setting::Manager> _manager;
129 QStack<Group> _groups;
130 QString _groupPrefix;
143 template <
typename T>
152 Handle(
const QString& key) : Interface(key) {}
159 Handle(
const QStringList& path) : Interface(path.join(
"/")) {}
167 Handle(
const QString& key,
const T& defaultValue) : Interface(key), _defaultValue(defaultValue) {}
175 Handle(
const QStringList& path,
const T& defaultValue) :
Handle(path.join(
"/"), defaultValue) {}
228 return Deprecated(path.join(
"/"), defaultValue);
241 return get(_defaultValue);
250 T
get(
const T& other)
const {
252 return (_isSet) ? _value : other;
272 return _defaultValue;
290 void set(
const T& value) {
295 if ((!_isSet && (value != _defaultValue)) || _value != value) {
320 virtual void setVariant(
const QVariant& variant)
override;
321 virtual QVariant getVariant()
override {
return QVariant::fromValue(
get()); }
327 qCInfo(settings_handle).nospace() <<
"[DEPRECATION NOTICE] " << _key <<
"(" <<
get() <<
") has been deprecated, and has no effect";
332 _isDeprecated =
true;
336 const T _defaultValue;
337 bool _isDeprecated{
false };
340 template <
typename T>
341 void Handle<T>::setVariant(
const QVariant& variant) {
342 if (variant.canConvert<T>() || std::is_same<T, QVariant>::value) {
343 set(variant.value<T>());
Handle to a setting of type T.
Definition: SettingHandle.h:144
static Handle Deprecated(const QString &key)
Construct a handle to a deprecated setting.
Definition: SettingHandle.h:185
static Handle Deprecated(const QStringList &path, const T &defaultValue)
Construct a handle to a deprecated setting with a default value.
Definition: SettingHandle.h:227
void reset()
Sets the value to the default.
Definition: SettingHandle.h:279
Handle(const QString &key)
Construct handle to a setting.
Definition: SettingHandle.h:152
T get(const T &other) const
Returns the value of the setting, or 'other' if not found.
Definition: SettingHandle.h:250
Handle(const QStringList &path, const T &defaultValue)
Construct a handle to a setting with a default value.
Definition: SettingHandle.h:175
bool isSet() const
Returns whether the setting is set to a value.
Definition: SettingHandle.h:261
T get() const
Returns the value of the setting, or the default value if not found.
Definition: SettingHandle.h:240
void set(const T &value)
Set the setting to the specified value.
Definition: SettingHandle.h:290
const T & getDefault() const
Returns the default value for this setting.
Definition: SettingHandle.h:271
Handle(const QStringList &path)
Construct a handle to a setting.
Definition: SettingHandle.h:159
Handle(const QString &key, const T &defaultValue)
Construct handle to a setting with a default value.
Definition: SettingHandle.h:167
static Handle Deprecated(const QString &key, const T &defaultValue)
Construct a handle to a deprecated setting with a default value.
Definition: SettingHandle.h:212
static Handle Deprecated(const QStringList &path)
Construct a handle to a deprecated setting.
Definition: SettingHandle.h:199
void remove()
Remove the value from the setting.
Definition: SettingHandle.h:311
QSettings analog.
Definition: SettingHandle.h:56