summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--facetracknoir/ui.cpp27
-rw-r--r--opentrack/mappings.hpp29
-rw-r--r--opentrack/options.hpp65
-rw-r--r--qfunctionconfigurator/qfunctionconfigurator.cpp9
4 files changed, 61 insertions, 69 deletions
diff --git a/facetracknoir/ui.cpp b/facetracknoir/ui.cpp
index 895f605c..d1b8ed43 100644
--- a/facetracknoir/ui.cpp
+++ b/facetracknoir/ui.cpp
@@ -23,6 +23,7 @@
*********************************************************************************/
#include "ui.h"
#include "opentrack/tracker.h"
+#include "opentrack/options.hpp"
#include <QFileDialog>
#ifndef _WIN32
@@ -31,8 +32,6 @@
# include <windows.h>
#endif
-static constexpr const char* _group = "opentrack-2.3";
-
MainWindow::MainWindow() :
pose_update_timer(this),
kbd_quit(QKeySequence("Ctrl+Q"), this),
@@ -106,8 +105,8 @@ void MainWindow::open() {
if (! fileName.isEmpty() ) {
{
- QSettings settings(_group);
- settings.setValue ("SettingsFile", QFileInfo(fileName).absoluteFilePath());
+ QSettings settings(group::org);
+ settings.setValue (group::filename_key, QFileInfo(fileName).absoluteFilePath());
}
fill_profile_combobox();
load_settings();
@@ -127,8 +126,7 @@ void MainWindow::save() {
save_mappings();
#if defined(__unix) || defined(__linux)
- QSettings settings(_group);
- const QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString();
+ const QString currentFile = group::ini_pathname();
QByteArray bytes = QFile::encodeName(currentFile);
const char* filename_as_asciiz = bytes.constData();
@@ -141,9 +139,8 @@ void MainWindow::save() {
void MainWindow::saveAs()
{
- QSettings settings(_group);
- QString oldFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString();
-
+ QString oldFile = group::ini_pathname();
+ QSettings settings(group::org);
QString fileName = QFileDialog::getSaveFileName(this, tr("Save file"),
oldFile,
tr("Settings file (*.ini);;All Files (*)"));
@@ -161,7 +158,7 @@ void MainWindow::saveAs()
oldFileFile.copy( fileName );
}
- settings.setValue ("SettingsFile", fileName);
+ settings.setValue (group::filename_key, fileName);
save();
}
@@ -181,9 +178,7 @@ extern "C" volatile const char* opentrack_version;
void MainWindow::fill_profile_combobox()
{
- QSettings settings(_group);
- QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath()
- + "/settings/default.ini" ).toString();
+ QString currentFile = group::ini_pathname();
qDebug() << "Config file now" << currentFile;
QFileInfo pathInfo ( currentFile );
setWindowTitle(QString( const_cast<const char*>(opentrack_version) + QStringLiteral(" :: ")) + pathInfo.fileName());
@@ -392,10 +387,10 @@ void MainWindow::exit() {
void MainWindow::profileSelected(int index)
{
- QSettings settings(_group);
- QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString();
+ QSettings settings(group::org);
+ QString currentFile = group::ini_pathname();
QFileInfo pathInfo ( currentFile );
- settings.setValue ("SettingsFile", pathInfo.absolutePath() + "/" + ui.iconcomboProfile->itemText(index));
+ settings.setValue (group::filename_key, pathInfo.absolutePath() + "/" + ui.iconcomboProfile->itemText(index));
load_settings();
}
diff --git a/opentrack/mappings.hpp b/opentrack/mappings.hpp
index 1a64fcd9..c58b5863 100644
--- a/opentrack/mappings.hpp
+++ b/opentrack/mappings.hpp
@@ -6,8 +6,6 @@ using namespace options;
#include "../qfunctionconfigurator/functionconfig.h"
#include "main-settings.hpp"
-static constexpr const char* settings_group = "opentrack-2.3";
-
class Mapping {
public:
Mapping(QString primary,
@@ -20,12 +18,9 @@ public:
name1(primary),
name2(secondary)
{
- // XXX TODO move all this qsettings boilerplate into a single header -sh 20141004
- QSettings settings(settings_group);
- QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString();
- QSettings iniFile(currentFile, QSettings::IniFormat);
- curve.loadSettings(iniFile, primary);
- curveAlt.loadSettings(iniFile, secondary);
+ mem<QSettings> iniFile = group::ini_file();
+ curve.loadSettings(*iniFile, primary);
+ curveAlt.loadSettings(*iniFile, secondary);
}
Map curve;
Map curveAlt;
@@ -53,29 +48,25 @@ public:
void load_mappings()
{
- QSettings settings(settings_group);
- QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString();
- QSettings iniFile( currentFile, QSettings::IniFormat );
+ mem<QSettings> iniFile = group::ini_file();
for (int i = 0; i < 6; i++)
{
- axes[i].curve.loadSettings(iniFile, axes[i].name1);
- axes[i].curveAlt.loadSettings(iniFile, axes[i].name2);
+ axes[i].curve.loadSettings(*iniFile, axes[i].name1);
+ axes[i].curveAlt.loadSettings(*iniFile, axes[i].name2);
}
}
void save_mappings()
{
- QSettings settings(settings_group);
- QString currentFile = settings.value("SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini").toString();
- QSettings iniFile(currentFile, QSettings::IniFormat);
+ mem<QSettings> iniFile = group::ini_file();
for (int i = 0; i < 6; i++)
{
- axes[i].curve.saveSettings(iniFile, axes[i].name1);
- axes[i].curveAlt.saveSettings(iniFile, axes[i].name2);
+ axes[i].curve.saveSettings(*iniFile, axes[i].name1);
+ axes[i].curveAlt.saveSettings(*iniFile, axes[i].name2);
}
}
-
+
void invalidate_unsaved()
{
for (int i = 0; i < 6; i++)
diff --git a/opentrack/options.hpp b/opentrack/options.hpp
index af2e94d0..f7614b9c 100644
--- a/opentrack/options.hpp
+++ b/opentrack/options.hpp
@@ -39,7 +39,7 @@ namespace options {
template<typename k, typename v>
using map = std::map<k, v>;
using std::string;
-
+
template<typename t>
// don't elide usages of the function, qvariant default implicit
// conversion results in nonsensical runtime behavior -sh
@@ -80,12 +80,6 @@ namespace options {
private:
map<string, QVariant> kvs;
string name;
- static const QString ini_pathname()
- {
- QSettings settings(group::org);
- return settings.value("SettingsFile",
- QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString();
- }
public:
group(const string& name) : name(name)
{
@@ -101,7 +95,7 @@ namespace options {
conf.endGroup();
}
static constexpr const char* org = "opentrack-2.3";
-
+
void save()
{
QSettings s(ini_pathname(), QSettings::IniFormat);
@@ -114,22 +108,35 @@ namespace options {
}
s.endGroup();
}
-
+
template<typename t>
t get(const string& k)
{
return qcruft_to_t<t>(kvs[k]);
}
-
+
void put(const string& s, const QVariant& d)
{
kvs[s] = d;
}
-
+
bool contains(const string& s)
{
return kvs.count(s) != 0;
}
+
+ static constexpr const char* filename_key = "settings-file";
+ static constexpr const char* default_path = "/settings/default.ini";
+
+ static const QString ini_pathname()
+ {
+ QSettings settings(group::org);
+ return settings.value(filename_key, QCoreApplication::applicationDirPath() + default_path).toString();
+ }
+ static const mem<QSettings> ini_file()
+ {
+ return std::make_shared<QSettings>(ini_pathname(), QSettings::IniFormat);
+ }
};
class impl_bundle : public QObject {
@@ -150,20 +157,20 @@ namespace options {
modified(false)
{
}
-
+
string name() { return group_name; }
-
+
void reload() {
QMutexLocker l(&mtx);
saved = group(group_name);
transient = saved;
modified = false;
}
-
+
bool store_kv(const string& name, const QVariant& datum)
{
QMutexLocker l(&mtx);
-
+
auto old = transient.get<QVariant>(name);
if (!transient.contains(name) || datum != old)
{
@@ -197,9 +204,9 @@ namespace options {
return modified;
}
};
-
+
class opt_bundle;
-
+
namespace
{
template<typename k, typename v, typename cnt = int>
@@ -213,42 +220,42 @@ namespace options {
map<k, tt> implsgl_data;
public:
opt_singleton() : implsgl_mtx(QMutex::Recursive) {}
-
+
static opt_singleton<k, v>& datum()
{
static auto ret = std::make_shared<opt_singleton<k, v>>();
return *ret;
}
-
+
pbundle bundle(const k& key)
{
QMutexLocker l(&implsgl_mtx);
-
+
if (implsgl_data.count(key) != 0)
return std::get<1>(implsgl_data[key]);
-
+
auto shr = std::make_shared<v>(key);
implsgl_data[key] = tt(cnt(1), shr);
return shr;
}
-
+
void bundle_decf(const k& key)
{
QMutexLocker l(&implsgl_mtx);
-
+
if (--std::get<0>(implsgl_data[key]) == 0)
implsgl_data.erase(key);
}
-
+
~opt_singleton() { implsgl_data.clear(); }
};
-
+
using pbundle = std::shared_ptr<opt_bundle>;
using t_fact = opt_singleton<string, opt_bundle>;
}
-
+
static inline t_fact::pbundle bundle(const string name) { return t_fact::datum().bundle(name); }
-
+
class opt_bundle : public impl_bundle
{
public:
@@ -257,7 +264,7 @@ namespace options {
{
qDebug() << "bundle +" << QString::fromStdString(group_name);
}
-
+
~opt_bundle()
{
qDebug() << "bundle -" << QString::fromStdString(group_name);
@@ -293,7 +300,7 @@ namespace options {
DEFINE_SLOT(QString)
DEFINE_SLOT(bool)
};
-
+
static inline string string_from_qstring(const QString& datum)
{
auto tmp = datum.toUtf8();
diff --git a/qfunctionconfigurator/qfunctionconfigurator.cpp b/qfunctionconfigurator/qfunctionconfigurator.cpp
index f1ed3001..b9728bd7 100644
--- a/qfunctionconfigurator/qfunctionconfigurator.cpp
+++ b/qfunctionconfigurator/qfunctionconfigurator.cpp
@@ -1,3 +1,5 @@
+#include "../opentrack/options.hpp"
+using namespace options;
#include "qfunctionconfigurator/qfunctionconfigurator.h"
#include <QPainter>
#include <QPaintEvent>
@@ -19,11 +21,8 @@ QFunctionConfigurator::QFunctionConfigurator(QWidget *parent) :
}
void QFunctionConfigurator::setConfig(Map* config, const QString& name) {
- QSettings settings("opentrack");
- QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString();
- QSettings iniFile( currentFile, QSettings::IniFormat );
-
- config->loadSettings(iniFile, name);
+ mem<QSettings> iniFile = group::ini_file();
+ config->loadSettings(*iniFile, name);
_config = config;
_draw_function = true;
update_range();