More switches to new Settings

This commit is contained in:
Atlante45 2015-01-20 00:30:29 -08:00
parent cdb0c22207
commit 6a00dcbf0a
19 changed files with 152 additions and 134 deletions

View file

@ -1921,10 +1921,8 @@ Headers DomainServer::setupCookieHeadersFromProfileReply(QNetworkReply* profileR
_cookieSessionHash.insert(cookieUUID, sessionData);
// persist the cookie to settings file so we can get it back on DS relaunch
QSettings localSettings;
localSettings.beginGroup(DS_SETTINGS_SESSIONS_GROUP);
QVariant sessionVariant = QVariant::fromValue(sessionData);
localSettings.setValue(cookieUUID.toString(), QVariant::fromValue(sessionData));
QStringList path = QStringList() << DS_SETTINGS_SESSIONS_GROUP << cookieUUID.toString();
SettingHandles::SettingHandle<QVariant>(path).set(QVariant::fromValue(sessionData));
// setup expiry for cookie to 1 month from today
QDateTime cookieExpiry = QDateTime::currentDateTimeUtc().addMonths(1);

View file

@ -38,7 +38,6 @@
#include <QObject>
#include <QWheelEvent>
#include <QScreen>
#include <QSettings>
#include <QShortcut>
#include <QSystemTrayIcon>
#include <QTimer>
@ -146,6 +145,12 @@ const QString SKIP_FILENAME = QStandardPaths::writableLocation(QStandardPaths::D
const QString DEFAULT_SCRIPTS_JS_URL = "http://s3.amazonaws.com/hifi-public/scripts/defaultScripts.js";
namespace SettingHandles {
const SettingHandle<bool> firstRun("firstRun", true);
const SettingHandle<QString> lastScriptLocation("LastScriptLocation");
const SettingHandle<QString> scriptsLocation("scriptsLocation");
}
void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
QString logMessage = LogHandler::getInstance().printMessage((LogMsgType) type, context, message);
@ -428,21 +433,19 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) :
connect(this, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()));
// check first run...
Settings settings;
QString firstRunKey = "firstRun";
QVariant firstRunValue = settings.value(firstRunKey, true);
if (firstRunValue.isValid() && firstRunValue.toBool()) {
bool firstRun = SettingHandles::firstRun.get();
if (firstRun) {
qDebug() << "This is a first run...";
// clear the scripts, and set out script to our default scripts
clearScriptsBeforeRunning();
loadScript(DEFAULT_SCRIPTS_JS_URL);
settings.setValue(firstRunKey, false);
SettingHandles::firstRun.set(false);
} else {
// do this as late as possible so that all required subsystems are initialized
loadScripts();
_previousScriptLocation = settings.value("LastScriptLocation", "").toString();
_previousScriptLocation = SettingHandles::lastScriptLocation.get();
}
_trayIcon->show();
@ -2968,7 +2971,7 @@ void Application::renderRearViewMirror(const QRect& region, bool billboard) {
_mirrorCamera.setPosition(_myAvatar->getPosition() +
_myAvatar->getOrientation() * glm::vec3(0.0f, 0.0f, -1.0f) * BILLBOARD_DISTANCE * _myAvatar->getScale());
} else if (_rearMirrorTools->getZoomLevel() == BODY) {
} else if (SettingHandles::rearViewZoomLevel.get() == BODY) {
_mirrorCamera.setFieldOfView(MIRROR_FIELD_OF_VIEW); // degrees
_mirrorCamera.setPosition(_myAvatar->getChestPosition() +
_myAvatar->getOrientation() * glm::vec3(0.0f, 0.0f, -1.0f) * MIRROR_REARVIEW_BODY_DISTANCE * _myAvatar->getScale());
@ -3365,10 +3368,12 @@ void Application::packetSent(quint64 length) {
_bandwidthMeter.outputStream(BandwidthMeter::OCTREE).updateValue(length);
}
const QString SETTINGS_KEY = "Settings";
void Application::loadScripts() {
// loads all saved scripts
Settings settings;
int size = settings.beginReadArray("Settings");
int size = settings.beginReadArray(SETTINGS_KEY);
for (int i = 0; i < size; ++i){
settings.setArrayIndex(i);
QString string = settings.value("script").toString();
@ -3381,7 +3386,7 @@ void Application::loadScripts() {
void Application::clearScriptsBeforeRunning() {
// clears all scripts from the settings
Settings().remove("Settings");
SettingHandles::SettingHandle<QVariant>(SETTINGS_KEY).remove();
}
void Application::saveScripts() {
@ -3392,7 +3397,7 @@ void Application::saveScripts() {
// Saves all currently running user-loaded scripts
Settings settings;
settings.beginWriteArray("Settings");
settings.beginWriteArray(SETTINGS_KEY);
int i = 0;
for (auto it = runningScripts.begin(); it != runningScripts.end(); ++it) {
if (getScriptEngine(*it)->isUserLoaded()) {
@ -3708,7 +3713,7 @@ QString Application::getPreviousScriptLocation() {
void Application::setPreviousScriptLocation(const QString& previousScriptLocation) {
_previousScriptLocation = previousScriptLocation;
Settings().setValue("LastScriptLocation", _previousScriptLocation);
SettingHandles::lastScriptLocation.set(_previousScriptLocation);
}
void Application::loadDialog() {
@ -3744,11 +3749,11 @@ void Application::loadScriptURLDialog() {
}
QString Application::getScriptsLocation() const {
return Settings().value("scriptsLocation", QString()).toString();
return SettingHandles::scriptsLocation.get();
}
void Application::setScriptsLocation(const QString& scriptsLocation) {
Settings().setValue("scriptsLocation", scriptsLocation);
SettingHandles::scriptsLocation.set(scriptsLocation);
emit scriptLocationChanged(scriptsLocation);
}

View file

@ -24,37 +24,20 @@
#include "Menu.h"
#include "Util.h"
namespace SettingHandles {
const SettingHandle<QRect> windowGeometry("WindowGeometry", qApp->desktop()->availableGeometry());
}
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
}
void MainWindow::restoreGeometry() {
QRect available = qApp->desktop()->availableGeometry();
Settings settings;
settings.beginGroup("Window");
int x = (int)loadSetting(&settings, "x", 0);
int y = (int)loadSetting(&settings, "y", 0);
move(x, y);
int width = (int)loadSetting(&settings, "width", available.width());
int height = (int)loadSetting(&settings, "height", available.height());
resize(width, height);
settings.endGroup();
setGeometry(SettingHandles::windowGeometry.get(qApp->desktop()->availableGeometry()));
}
void MainWindow::saveGeometry() {
Settings settings;
settings.beginGroup("Window");
settings.setValue("width", rect().width());
settings.setValue("height", rect().height());
settings.setValue("x", pos().x());
settings.setValue("y", pos().y());
settings.endGroup();
SettingHandles::windowGeometry.set(geometry());
}
void MainWindow::moveEvent(QMoveEvent* event) {

View file

@ -60,8 +60,6 @@ static const QString BLENDSHAPE_FIELD = "bs";
static const QString S3_URL = "http://public.highfidelity.io";
static const QString MODEL_URL = "/api/v1/models";
static const QString SETTING_NAME = "LastModelUploadLocation";
static const unsigned long long MAX_SIZE = 50 * 1024 * BYTES_PER_MEGABYTES; // 50 GB (Virtually remove limit)
static const int MAX_TEXTURE_SIZE = 1024;
static const int TIMEOUT = 1000;
@ -70,6 +68,11 @@ static const int MAX_CHECK = 30;
static const int QCOMPRESS_HEADER_POSITION = 0;
static const int QCOMPRESS_HEADER_SIZE = 4;
namespace SettingHandles {
const SettingHandle<QString> lastModelUploadLocation("LastModelUploadLocation",
QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
}
void ModelUploader::uploadModel(ModelType modelType) {
ModelUploader* uploader = new ModelUploader(modelType);
QThread* thread = new QThread();
@ -114,8 +117,7 @@ ModelUploader::~ModelUploader() {
bool ModelUploader::zip() {
// File Dialog
Settings settings;
QString lastLocation = settings.value(SETTING_NAME).toString();
QString lastLocation = SettingHandles::lastModelUploadLocation.get();
if (lastLocation.isEmpty()) {
lastLocation = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
@ -132,7 +134,7 @@ bool ModelUploader::zip() {
// If the user canceled we return.
return false;
}
settings.setValue(SETTING_NAME, filename);
SettingHandles::lastModelUploadLocation.set(filename);
// First we check the FST file (if any)
QFile* fst;

View file

@ -238,14 +238,6 @@ void runTimingTests() {
qDebug("vec3 assign and dot() usecs: %f, last result:%f", elapsedUsecs / (float) numTests, result);
}
float loadSetting(QSettings* settings, const char* name, float defaultValue) {
float value = settings->value(name, defaultValue).toFloat();
if (glm::isnan(value)) {
value = defaultValue;
}
return value;
}
bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection,
const glm::vec3& sphereCenter, float sphereRadius, float& distance) {
glm::vec3 relativeOrigin = rayStarting - sphereCenter;

View file

@ -31,8 +31,6 @@ void renderBevelCornersRect(int x, int y, int width, int height, int bevelDistan
void runTimingTests();
float loadSetting(QSettings* settings, const char* name, float defaultValue);
bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection,
const glm::vec3& sphereCenter, float sphereRadius, float& distance);

View file

@ -115,8 +115,8 @@ glm::quat Avatar::getWorldAlignedOrientation () const {
}
float Avatar::getLODDistance() const {
return DependencyManager::get<LODManager>()->getAvatarLODDistanceMultiplier() *
glm::distance(Application::getInstance()->getCamera()->getPosition(), _position) / _scale;
return SettingHandles::avatarLODDistanceMultiplier.get() *
glm::distance(qApp->getCamera()->getPosition(), _position) / _scale;
}
void Avatar::simulate(float deltaTime) {

View file

@ -20,7 +20,7 @@ SettingsScriptingInterface* SettingsScriptingInterface::getInstance() {
}
QVariant SettingsScriptingInterface::getValue(const QString& setting) {
QVariant value = Settings().value(setting);
QVariant value = SettingHandles::SettingHandle<QVariant>(setting).get();
if (!value.isValid()) {
value = "";
}
@ -28,7 +28,7 @@ QVariant SettingsScriptingInterface::getValue(const QString& setting) {
}
QVariant SettingsScriptingInterface::getValue(const QString& setting, const QVariant& defaultValue) {
QVariant value = Settings().value(setting, defaultValue);
QVariant value = SettingHandles::SettingHandle<QVariant>(setting, defaultValue).get();
if (!value.isValid()) {
value = "";
}
@ -36,5 +36,5 @@ QVariant SettingsScriptingInterface::getValue(const QString& setting, const QVar
}
void SettingsScriptingInterface::setValue(const QString& setting, const QVariant& value) {
Settings().setValue(setting, value);
SettingHandles::SettingHandle<QVariant>(setting).set(value);
}

View file

@ -26,6 +26,10 @@
#include "Application.h"
#include "MainWindow.h"
namespace SettingHandles {
const SettingHandle<QString> animationDirectory("animation_directory", QString());
}
AnimationsDialog::AnimationsDialog(QWidget* parent) :
QDialog(parent) {
@ -159,13 +163,12 @@ AnimationPanel::AnimationPanel(AnimationsDialog* dialog, const AnimationHandlePo
}
void AnimationPanel::chooseURL() {
Settings settings;
QString directory = settings.value("animation_directory").toString();
QString directory = SettingHandles::animationDirectory.get();
QString filename = QFileDialog::getOpenFileName(this, "Choose Animation", directory, "Animation files (*.fbx)");
if (filename.isEmpty()) {
return;
}
settings.setValue("animation_directory", QFileInfo(filename).path());
SettingHandles::animationDirectory.set(QFileInfo(filename).path());
_url->setText(QUrl::fromLocalFile(filename).toString());
emit _url->returnPressed();
}

View file

@ -42,6 +42,10 @@ const QRegularExpression regexHifiLinks("([#@]\\S+)");
const QString mentionSoundsPath("/mention-sounds/");
const QString mentionRegex("@(\\b%1\\b)");
namespace SettingHandles {
const SettingHandle<QDateTime> usernameMentionTimestamp("MentionTimestamp", QDateTime());
}
ChatWindow::ChatWindow(QWidget* parent) :
QWidget(parent, Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint |
Qt::WindowCloseButtonHint),
@ -378,10 +382,9 @@ void ChatWindow::messageReceived(const QXmppMessage& message) {
if (message.body().contains(usernameMention)) {
// Don't show messages already seen in icon tray at start-up.
Settings settings;
bool showMessage = settings.value("usernameMentionTimestamp").toDateTime() < _lastMessageStamp;
bool showMessage = SettingHandles::usernameMentionTimestamp.get() < _lastMessageStamp;
if (showMessage) {
settings.setValue("usernameMentionTimestamp", _lastMessageStamp);
SettingHandles::usernameMentionTimestamp.set(_lastMessageStamp);
}
if (isHidden() && showMessage) {

View file

@ -20,8 +20,11 @@
#include "InfoView.h"
#define SETTINGS_VERSION_KEY "info-version"
#define MAX_DIALOG_HEIGHT_RATIO 0.9
static const float MAX_DIALOG_HEIGHT_RATIO = 0.9f;
namespace SettingHandles {
const SettingHandle<QString> infoVersion("info-version", QString());
}
InfoView::InfoView(bool forced, QString path) :
_forced(forced)
@ -49,15 +52,13 @@ bool InfoView::shouldShow() {
return true;
}
Settings settings;
QString lastVersion = settings.value(SETTINGS_VERSION_KEY).toString();
QString lastVersion = SettingHandles::infoVersion.get();
QWebElement versionTag = page()->mainFrame()->findFirstElement("#version");
QString version = versionTag.attribute("value");
if (version != QString::null && (lastVersion == QString::null || lastVersion != version)) {
settings.setValue(SETTINGS_VERSION_KEY, version);
SettingHandles::infoVersion.set(version);
shouldShow = true;
} else {
shouldShow = false;

View file

@ -14,16 +14,12 @@
#include <QMouseEvent>
#include <PathUtils.h>
#include <Settings.h>
#include <SharedUtil.h>
#include "Application.h"
#include "RearMirrorTools.h"
#include "Util.h"
const char SETTINGS_GROUP_NAME[] = "Rear View Tools";
const char ZOOM_LEVEL_SETTINGS[] = "ZoomLevel";
const int ICON_SIZE = 24;
const int ICON_PADDING = 5;
@ -56,8 +52,9 @@ void RearMirrorTools::render(bool fullScreen) {
if (_windowed) {
displayIcon(_bounds, _closeIconRect, _closeTextureId);
displayIcon(_bounds, _headZoomIconRect, _zoomHeadTextureId, getZoomLevel() == HEAD);
displayIcon(_bounds, _bodyZoomIconRect, _zoomBodyTextureId, getZoomLevel() == BODY);
ZoomLevel zoomLevel = (ZoomLevel)SettingHandles::rearViewZoomLevel.get();
displayIcon(_bounds, _headZoomIconRect, _zoomHeadTextureId, zoomLevel == HEAD);
displayIcon(_bounds, _bodyZoomIconRect, _zoomBodyTextureId, zoomLevel == BODY);
}
}
}
@ -71,12 +68,12 @@ bool RearMirrorTools::mousePressEvent(int x, int y) {
}
if (_headZoomIconRect.contains(x, y)) {
setZoomLevel(HEAD);
SettingHandles::rearViewZoomLevel.set(HEAD);
return true;
}
if (_bodyZoomIconRect.contains(x, y)) {
setZoomLevel(BODY);
SettingHandles::rearViewZoomLevel.set(BODY);
return true;
}
@ -97,22 +94,6 @@ bool RearMirrorTools::mousePressEvent(int x, int y) {
return false;
}
ZoomLevel RearMirrorTools::getZoomLevel() {
Settings settings;
settings.beginGroup(SETTINGS_GROUP_NAME);
ZoomLevel zoomLevel = (ZoomLevel)settings.value(ZOOM_LEVEL_SETTINGS, HEAD).toInt();
settings.endGroup();
return zoomLevel;
}
void RearMirrorTools::setZoomLevel(ZoomLevel zoomLevel) {
Settings settings;
settings.beginGroup(SETTINGS_GROUP_NAME);
settings.setValue(ZOOM_LEVEL_SETTINGS, zoomLevel);
settings.endGroup();
}
void RearMirrorTools::displayIcon(QRect bounds, QRect iconBounds, GLuint textureId, bool selected) {
glMatrixMode(GL_PROJECTION);

View file

@ -16,20 +16,26 @@
#include <QGLWidget>
#include <Settings.h>
enum ZoomLevel {
HEAD,
BODY
HEAD = 0,
BODY = 1
};
namespace SettingHandles {
const char SETTINGS_GROUP_NAME[] = "Rear View Tools";
const char ZOOM_LEVEL_SETTINGS[] = "ZoomLevel";
const SettingHandle<int> rearViewZoomLevel(QStringList() << SETTINGS_GROUP_NAME << ZOOM_LEVEL_SETTINGS,
ZoomLevel::HEAD);
}
class RearMirrorTools : public QObject {
Q_OBJECT
public:
RearMirrorTools(QGLWidget* parent, QRect& bounds);
void render(bool fullScreen);
bool mousePressEvent(int x, int y);
ZoomLevel getZoomLevel();
void setZoomLevel(ZoomLevel zoomLevel);
signals:
void closeView();

View file

@ -485,6 +485,10 @@ void QColorEditor::selectColor() {
}
}
namespace SettingHandles {
const SettingHandle<QStringList> editorURLs("editorURLs");
}
QUrlEditor::QUrlEditor(QWidget* parent) :
QComboBox(parent) {
@ -492,7 +496,7 @@ QUrlEditor::QUrlEditor(QWidget* parent) :
setInsertPolicy(InsertAtTop);
// populate initial URL list from settings
addItems(Settings().value("editorURLs").toStringList());
addItems(SettingHandles::editorURLs.get());
connect(this, SIGNAL(activated(const QString&)), SLOT(updateURL(const QString&)));
connect(model(), SIGNAL(rowsInserted(const QModelIndex&,int,int)), SLOT(updateSettings()));
@ -512,7 +516,7 @@ void QUrlEditor::updateSettings() {
for (int i = 0, size = qMin(MAX_STORED_URLS, count()); i < size; i++) {
urls.append(itemText(i));
}
Settings().setValue("editorURLs", urls);
SettingHandles::editorURLs.set(urls);
}
BaseVec3Editor::BaseVec3Editor(QWidget* parent) : QWidget(parent) {

View file

@ -58,6 +58,10 @@ static QItemEditorCreatorBase* heightfieldColorEditorCreator = createHeightfield
const float DEFAULT_PLACEMENT_GRANULARITY = 0.01f;
const float DEFAULT_VOXELIZATION_GRANULARITY = powf(2.0f, -3.0f);
namespace SettingHandles {
const SettingHandle<QString> heightfieldDir("heightDir", QString());
}
Spanner::Spanner() :
_renderer(NULL),
_placementGranularity(DEFAULT_PLACEMENT_GRANULARITY),
@ -610,13 +614,13 @@ static int getHeightfieldSize(int size) {
}
void HeightfieldHeightEditor::select() {
Settings settings;
QString result = QFileDialog::getOpenFileName(this, "Select Height Image", settings.value("heightDir").toString(),
"Images (*.png *.jpg *.bmp *.raw *.mdr)");
QString result = QFileDialog::getOpenFileName(this, "Select Height Image",
SettingHandles::heightfieldDir.get(),
"Images (*.png *.jpg *.bmp *.raw *.mdr)");
if (result.isNull()) {
return;
}
settings.setValue("heightDir", QFileInfo(result).path());
SettingHandles::heightfieldDir.set(QFileInfo(result).path());
const quint16 CONVERSION_OFFSET = 1;
QString lowerResult = result.toLower();
bool isMDR = lowerResult.endsWith(".mdr");
@ -920,13 +924,13 @@ void HeightfieldColorEditor::setColor(const HeightfieldColorPointer& color) {
}
void HeightfieldColorEditor::select() {
Settings settings;
QString result = QFileDialog::getOpenFileName(this, "Select Color Image", settings.value("heightDir").toString(),
QString result = QFileDialog::getOpenFileName(this, "Select Color Image",
SettingHandles::heightfieldDir.get(),
"Images (*.png *.jpg *.bmp)");
if (result.isNull()) {
return;
}
settings.setValue("heightDir", QFileInfo(result).path());
SettingHandles::heightfieldDir.get(QFileInfo(result).path());
QImage image;
if (!image.load(result)) {
QMessageBox::warning(this, "Invalid Image", "The selected image could not be read.");

View file

@ -90,11 +90,9 @@ void AccountManager::logout() {
connect(&_accountInfo, &DataServerAccountInfo::balanceChanged, this, &AccountManager::accountInfoBalanceChanged);
if (_shouldPersistToSettingsFile) {
Settings settings;
settings.beginGroup(ACCOUNTS_GROUP);
QString keyURLString(_authURL.toString().replace("//", DOUBLE_SLASH_SUBSTITUTE));
settings.remove(keyURLString);
QStringList path = QStringList() << ACCOUNTS_GROUP << keyURLString;
SettingHandles::SettingHandle<DataServerAccountInfo>(path).remove();
qDebug() << "Removed account info for" << _authURL << "from in-memory accounts and .ini file";
} else {
@ -135,7 +133,7 @@ void AccountManager::setAuthURL(const QUrl& authURL) {
foreach(const QString& key, settings.allKeys()) {
// take a key copy to perform the double slash replacement
QString keyCopy(key);
QUrl keyURL(keyCopy.replace("slashslash", "//"));
QUrl keyURL(keyCopy.replace(DOUBLE_SLASH_SUBSTITUTE, "//"));
if (keyURL == _authURL) {
// pull out the stored access token and store it in memory
@ -324,10 +322,9 @@ void AccountManager::passErrorToCallback(QNetworkReply* requestReply) {
void AccountManager::persistAccountToSettings() {
if (_shouldPersistToSettingsFile) {
// store this access token into the local settings
Settings localSettings;
localSettings.beginGroup(ACCOUNTS_GROUP);
localSettings.setValue(_authURL.toString().replace("//", DOUBLE_SLASH_SUBSTITUTE),
QVariant::fromValue(_accountInfo));
QString keyURLString(_authURL.toString().replace("//", DOUBLE_SLASH_SUBSTITUTE));
QStringList path = QStringList() << ACCOUNTS_GROUP << keyURLString;
SettingHandles::SettingHandle<QVariant>(path).set(QVariant::fromValue(_accountInfo));
}
}

View file

@ -24,6 +24,14 @@
#include "AddressManager.h"
const QString ADDRESS_MANAGER_SETTINGS_GROUP = "AddressManager";
const QString SETTINGS_CURRENT_ADDRESS_KEY = "address";
namespace SettingHandles {
const SettingHandle<QUrl> currentAddress(QStringList() << ADDRESS_MANAGER_SETTINGS_GROUP
<< "address",
QUrl());
}
AddressManager::AddressManager() :
_rootPlaceName(),
_rootPlaceID(),
@ -47,25 +55,16 @@ const QUrl AddressManager::currentAddress() const {
return hifiURL;
}
const QString ADDRESS_MANAGER_SETTINGS_GROUP = "AddressManager";
const QString SETTINGS_CURRENT_ADDRESS_KEY = "address";
void AddressManager::loadSettings(const QString& lookupString) {
if (lookupString.isEmpty()) {
Settings settings;
settings.beginGroup(ADDRESS_MANAGER_SETTINGS_GROUP);
handleLookupString(settings.value(SETTINGS_CURRENT_ADDRESS_KEY).toString());
handleLookupString(SettingHandles::currentAddress.get().toString());
} else {
handleLookupString(lookupString);
}
}
void AddressManager::storeCurrentAddress() {
Settings settings;
settings.beginGroup(ADDRESS_MANAGER_SETTINGS_GROUP);
settings.setValue(SETTINGS_CURRENT_ADDRESS_KEY, currentAddress());
settings.endGroup();
SettingHandles::currentAddress.set(currentAddress());
}
const QString AddressManager::currentPath(bool withOrientation) const {

View file

@ -9,6 +9,7 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <QSettings>
#include "Settings.h"
@ -21,5 +22,9 @@ QVariant SettingsBridge::getFromSettings(const QString& key, const QVariant& def
void SettingsBridge::setInSettings(const QString& key, const QVariant& value) {
QSettings().setValue(key, value);
}
void SettingsBridge::removeFromSettings(const QString& key) {
QSettings().remove(key);
}
}

View file

@ -17,16 +17,28 @@
#include <QVariant>
// TODO: remove
#include <glm/glm.hpp>
class Settings : public QSettings {
};
float loadSetting(QSettings* settings, const char* name, float defaultValue) {
float value = settings->value(name, defaultValue).toFloat();
if (glm::isnan(value)) {
value = defaultValue;
}
return value;
}
////
namespace SettingHandles {
template <typename T>
class SettingHandle {
public:
SettingHandle(const QString& key);
SettingHandle(const QStringList& path);
SettingHandle(const QString& key, const T& defaultValue);
SettingHandle(const QStringList& path, const T& defaultValue);
T get() const; // Returns setting value, returns its default value if not found
T get(const T& other) const; // Returns setting value, returns other if not found
@ -35,6 +47,8 @@ public:
void set(const T& value) const;
void reset() const;
void remove() const;
private:
const QString _key;
const QVariant _defaultValue;
@ -44,13 +58,31 @@ class SettingsBridge {
private:
static QVariant getFromSettings(const QString& key, const QVariant& defaultValue);
static void setInSettings(const QString& key, const QVariant& value);
static void removeFromSettings(const QString& key);
template<typename T>
friend class SettingHandle;
};
template <typename T>
SettingHandle<T>::SettingHandle(const QString& key, const T& defaultValue) : _key(key), _defaultValue(defaultValue) {
SettingHandle<T>::SettingHandle(const QString& key) : _key(key) {
}
template <typename T>
SettingHandle<T>::SettingHandle(const QStringList& path) : _key(path.join("/")) {
}
template <typename T>
SettingHandle<T>::SettingHandle(const QString& key, const T& defaultValue) :
_key(key),
_defaultValue(defaultValue) {
}
template <typename T>
SettingHandle<T>::SettingHandle(const QStringList& path, const T& defaultValue) :
_key(path.join("/")),
_defaultValue(defaultValue) {
}
template <typename T>
@ -83,7 +115,12 @@ void SettingHandle<T>::set(const T& value) const {
template <typename T> inline
void SettingHandle<T>::reset() const {
setInSettings(_key, _defaultValue);
SettingsBridge::setInSettings(_key, _defaultValue);
}
template <typename T> inline
void SettingHandle<T>::remove() const {
SettingsBridge::removeFromSettings(_key);
}
}