mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 18:36:45 +02:00
Introduced SettingHandle
New class that serves as an accesor to a single setting
This commit is contained in:
parent
d15d8e0ad6
commit
6cf00041f8
2 changed files with 82 additions and 4 deletions
|
@ -9,5 +9,16 @@
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <QSettings>
|
||||||
|
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
|
|
||||||
|
QVariant getFromSettings(QString key, QVariant defaultValue) {
|
||||||
|
return QSettings().value(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setInSettings(QString key, QVariant value) {
|
||||||
|
QSettings().setValue(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,13 +12,80 @@
|
||||||
#ifndef hifi_Settings_h
|
#ifndef hifi_Settings_h
|
||||||
#define hifi_Settings_h
|
#define hifi_Settings_h
|
||||||
|
|
||||||
#include <QSettings>
|
#include <QString>
|
||||||
|
#include <QVariant>
|
||||||
|
|
||||||
// Just a simple wrapper for now
|
template <typename T>
|
||||||
class Settings : public QSettings {
|
class SettingHandle {
|
||||||
|
public:
|
||||||
|
SettingHandle(const QString& key, const T& defaultValue);
|
||||||
|
|
||||||
|
const T& get() const; // Returns setting value, returns its default value if not found
|
||||||
|
const T& get(const T& other) const; // Returns setting value, returns other if not found
|
||||||
|
const T& getDefault() const;
|
||||||
|
|
||||||
|
void set(const T& value) const;
|
||||||
|
void reset() const;
|
||||||
|
|
||||||
|
static const QVariant::Type type;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const QString _key;
|
||||||
|
const QVariant _defaultValue;
|
||||||
|
|
||||||
|
friend QVariant getFromSettings(QString key, QVariant defaultValue);
|
||||||
|
friend void setInSettings(QString key, QVariant value);
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace SettingsKey {
|
// Specialize template here for types used in Setting
|
||||||
|
template<typename T> const QVariant::Type SettingHandle<T>::type = QVariant::Invalid;
|
||||||
|
template<> const QVariant::Type SettingHandle<int>::type = QVariant::Int;
|
||||||
|
template<> const QVariant::Type SettingHandle<bool>::type = QVariant::Bool;
|
||||||
|
template<> const QVariant::Type SettingHandle<float>::type = QVariant::Double;
|
||||||
|
template<> const QVariant::Type SettingHandle<double>::type = QVariant::Double;
|
||||||
|
template<> const QVariant::Type SettingHandle<QString>::type = QVariant::String;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
SettingHandle<T>::SettingHandle(const QString& key, const T& defaultValue) : _key(key), _defaultValue(defaultValue) {
|
||||||
|
// Will fire if template not specialized for that type below
|
||||||
|
Q_STATIC_ASSERT_X(SettingHandle<T>::type != QVariant::Invalid, "SettingHandle: Invalid type");
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
const T& SettingHandle<T>::get() const {
|
||||||
|
QVariant variant = getFromSettings(_key, _defaultValue);
|
||||||
|
if (variant.type() == type) {
|
||||||
|
return variant.value<T>();
|
||||||
|
}
|
||||||
|
return _defaultValue.value<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
const T& SettingHandle<T>::get(const T& other) const {
|
||||||
|
QVariant variant = getFromSettings(_key, _defaultValue);
|
||||||
|
if (variant.type() == type) {
|
||||||
|
return variant.value<T>();
|
||||||
|
}
|
||||||
|
return other;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> inline
|
||||||
|
const T& SettingHandle<T>::getDefault() const {
|
||||||
|
return _defaultValue.value<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> inline
|
||||||
|
void SettingHandle<T>::set(const T& value) const {
|
||||||
|
setInSettings(_key, QVariant(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> inline
|
||||||
|
void SettingHandle<T>::reset() const {
|
||||||
|
setInSettings(_key, _defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put applicationwide settings here
|
||||||
|
namespace SettingHandles {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue