From 87c09c0ab5e1334e9877ee6fd7adeb1eb70d5929 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 6 May 2017 13:15:16 +0200 Subject: options: don't create QSettings all the time Update usages. --- options/group.cpp | 97 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 28 deletions(-) (limited to 'options/group.cpp') diff --git a/options/group.cpp b/options/group.cpp index 9a4bd912..028e3e48 100644 --- a/options/group.cpp +++ b/options/group.cpp @@ -8,46 +8,47 @@ #include "group.hpp" #include "defs.hpp" + +#include "compat/timer.hpp" + +#include + #include #include - #include namespace options { -group::group(const QString& name, std::shared_ptr conf) : name(name) +group::group(const QString& name) : name(name) { if (name == "") return; - conf->beginGroup(name); - for (auto& k_ : conf->childKeys()) - { - auto tmp = k_.toUtf8(); - QString k(tmp); - kvs[k] = conf->value(k_); - } - conf->endGroup(); -} - -group::group(const QString& name) : group(name, ini_file()) -{ + with_settings_object([&](QSettings& conf) { + conf.beginGroup(name); + for (auto& k_ : conf.childKeys()) + { + auto tmp = k_.toUtf8(); + QString k(tmp); + kvs[k] = conf.value(k_); + } + conf.endGroup(); + }); } void group::save() const -{ - save_deferred(*ini_file()); -} - -void group::save_deferred(QSettings& s) const { if (name == "") return; - s.beginGroup(name); - for (auto& i : kvs) - s.setValue(i.first, i.second); - s.endGroup(); + with_settings_object([&](QSettings& s) { + s.beginGroup(name); + for (auto& i : kvs) + s.setValue(i.first, i.second); + s.endGroup(); + + mark_ini_modified(); + }); } void group::put(const QString &s, const QVariant &d) @@ -103,12 +104,52 @@ QStringList group::ini_list() return list; } -std::shared_ptr group::ini_file() +void group::mark_ini_modified() +{ + QMutexLocker l(&cur_ini_mtx); + ini_modifiedp = true; +} + +QString group::cur_ini_pathname; +std::shared_ptr group::cur_ini; +QMutex group::cur_ini_mtx(QMutex::Recursive); +int group::ini_refcount = 0; +bool group::ini_modifiedp = false; + +std::shared_ptr group::cur_settings_object() { - const auto pathname = ini_pathname(); - if (pathname != "") - return std::make_shared(ini_pathname(), QSettings::IniFormat); - return std::make_shared(); + const QString pathname = ini_pathname(); + + if (pathname.isEmpty()) + return std::make_shared(); + + QMutexLocker l(&cur_ini_mtx); + + if (pathname != cur_ini_pathname) + { + cur_ini = std::make_shared(pathname, QSettings::IniFormat); + cur_ini_pathname = pathname; + } + + return cur_ini; } +group::saver_::~saver_() +{ + if (--ini_refcount == 0 && ini_modifiedp) + { + ini_modifiedp = false; + static Timer t; + const double tm = t.elapsed_seconds(); + qDebug() << QStringLiteral("%1.%2").arg(int(tm)).arg(int(std::fmod(tm, 1.)*10)) + << "saving .ini file" << cur_ini_pathname; + s.sync(); + } } + +group::saver_::saver_(QSettings& s, QMutex& mtx) : s(s), mtx(mtx), lck(&mtx) +{ + ini_refcount++; +} + +} // ns options -- cgit v1.2.3 From e07bde4423eee88d9f9a02b179480de3129d9c93 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 7 May 2017 08:08:38 +0200 Subject: options/group: nicely format debug timestamp --- options/group.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'options/group.cpp') diff --git a/options/group.cpp b/options/group.cpp index 028e3e48..3f800ff0 100644 --- a/options/group.cpp +++ b/options/group.cpp @@ -141,7 +141,7 @@ group::saver_::~saver_() ini_modifiedp = false; static Timer t; const double tm = t.elapsed_seconds(); - qDebug() << QStringLiteral("%1.%2").arg(int(tm)).arg(int(std::fmod(tm, 1.)*10)) + qDebug() << QStringLiteral("%1.%2").arg(int(tm)).arg(int(std::fmod(tm, 1.)*10)).toLatin1().data() << "saving .ini file" << cur_ini_pathname; s.sync(); } -- cgit v1.2.3 From 4c4c783d023cf1bb6a8d7d883bf8d3384f7b7da1 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 12 May 2017 15:43:50 +0200 Subject: minor fixes only --- dinput/keybinding-worker.cpp | 5 +++-- options/connector.cpp | 7 ++++--- options/group.cpp | 4 +++- options/scoped.cpp | 2 +- pose-widget/pose-widget.cpp | 4 ++-- pose-widget/pose-widget.hpp | 2 +- 6 files changed, 14 insertions(+), 10 deletions(-) (limited to 'options/group.cpp') diff --git a/dinput/keybinding-worker.cpp b/dinput/keybinding-worker.cpp index b94b762e..0e16ac71 100644 --- a/dinput/keybinding-worker.cpp +++ b/dinput/keybinding-worker.cpp @@ -9,6 +9,7 @@ #ifdef _WIN32 #include "keybinding-worker.hpp" +#include "compat/util.hpp" #include #include #include @@ -18,8 +19,8 @@ bool Key::should_process() { if (!enabled || (keycode == 0 && guid == "")) return false; - bool ret = !held || timer.elapsed_ms() > 100; - timer.start(); + bool ret = prog1(!held || timer.elapsed_ms() > 100, + timer.start()); return ret; } diff --git a/options/connector.cpp b/options/connector.cpp index 63d70ca7..075a57e1 100644 --- a/options/connector.cpp +++ b/options/connector.cpp @@ -83,11 +83,12 @@ void connector::on_value_created(const QString& name, value_type val) QMutexLocker l(get_mtx()); - if (on_value_destructed_impl(name, val)) + int i = 1; + while (on_value_destructed_impl(name, val)) { qWarning() << "options/connector: value created twice;" - << "bundle" - << val->b->name() + << "cnt" << i++ + << "bundle" << val->b->name() << "value-name" << name << "value-ptr" << quintptr(val); } diff --git a/options/group.cpp b/options/group.cpp index 3f800ff0..60e8a7b4 100644 --- a/options/group.cpp +++ b/options/group.cpp @@ -30,7 +30,9 @@ group::group(const QString& name) : name(name) { auto tmp = k_.toUtf8(); QString k(tmp); - kvs[k] = conf.value(k_); + QVariant val = conf.value(k_); + if (val.type() != QVariant::Invalid) + kvs[k] = std::move(val); } conf.endGroup(); }); diff --git a/options/scoped.cpp b/options/scoped.cpp index 96f4a261..58a4ee02 100644 --- a/options/scoped.cpp +++ b/options/scoped.cpp @@ -2,8 +2,8 @@ #include #include -// for std::abort() #include +#include #include diff --git a/pose-widget/pose-widget.cpp b/pose-widget/pose-widget.cpp index 54278c34..8646df30 100644 --- a/pose-widget/pose-widget.cpp +++ b/pose-widget/pose-widget.cpp @@ -47,7 +47,7 @@ void pose_widget::paintEvent(QPaintEvent* event) xform.with_image_lock([&](const QImage& image) { - p.drawImage(event->rect(), image, offset, offset); + p.drawImage(event->rect(), image, QRect(offset, offset, pose_transform::w, pose_transform::h)); }); } @@ -71,7 +71,7 @@ void pose_transform::run() project_quad_texture(); end: - portable::sleep(9); + portable::sleep(23); } } diff --git a/pose-widget/pose-widget.hpp b/pose-widget/pose-widget.hpp index 1d34778a..a27bf4b9 100644 --- a/pose-widget/pose-widget.hpp +++ b/pose-widget/pose-widget.hpp @@ -37,7 +37,7 @@ using lock_guard = std::unique_lock; class pose_widget; -class pose_transform final : private QThread +struct pose_transform final : private QThread { pose_transform(QWidget* dst); ~pose_transform(); -- cgit v1.2.3