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. --- spline/spline.cpp | 9 ++------- spline/spline.hpp | 1 - 2 files changed, 2 insertions(+), 8 deletions(-) (limited to 'spline') diff --git a/spline/spline.cpp b/spline/spline.cpp index 195d68d6..58703e02 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -341,15 +341,10 @@ void spline::reload() s->b->reload(); } -void spline::save(QSettings& settings) -{ - QMutexLocker foo(&_mutex); - s->b->save_deferred(settings); -} - void spline::save() { - save(*group::ini_file()); + QMutexLocker foo(&_mutex); + s->b->save(); } void spline::set_bundle(bundle b) diff --git a/spline/spline.hpp b/spline/spline.hpp index 067967b5..146837c8 100644 --- a/spline/spline.hpp +++ b/spline/spline.hpp @@ -70,7 +70,6 @@ public: using settings = spline_detail::settings; void reload(); - void save(QSettings& s); void save(); void set_bundle(bundle b); -- cgit v1.2.3 From 03c5a15199b34b564314ac222d51ab687fc97a93 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 6 May 2017 13:23:44 +0200 Subject: get rid of the silly mem -> shared_ptr alias --- gui/main-window.cpp | 8 ++++---- gui/main-window.hpp | 8 ++++---- gui/main.cpp | 4 ++-- logic/selected-libraries.hpp | 11 +++++++---- logic/state.hpp | 2 +- logic/work.cpp | 2 +- logic/work.hpp | 2 +- spline/spline-widget.cpp | 2 +- spline/spline.cpp | 4 ++-- spline/spline.hpp | 6 +++--- 10 files changed, 26 insertions(+), 23 deletions(-) (limited to 'spline') diff --git a/gui/main-window.cpp b/gui/main-window.cpp index 96be298e..aa8e16f3 100644 --- a/gui/main-window.cpp +++ b/gui/main-window.cpp @@ -74,13 +74,13 @@ MainWindow::MainWindow() : { modules.filters().push_front(std::make_shared("", dylib::Filter)); - for (mem& x : modules.trackers()) + for (std::shared_ptr& x : modules.trackers()) ui.iconcomboTrackerSource->addItem(x->icon, x->name); - for (mem& x : modules.protocols()) + for (std::shared_ptr& x : modules.protocols()) ui.iconcomboProtocol->addItem(x->icon, x->name); - for (mem& x : modules.filters()) + for (std::shared_ptr& x : modules.filters()) ui.iconcomboFilter->addItem(x->icon, x->name); } @@ -601,7 +601,7 @@ inline bool MainWindow::mk_window(ptr& place, Args&&... params) } template -bool MainWindow::mk_dialog(mem lib, ptr& d) +bool MainWindow::mk_dialog(std::shared_ptr lib, ptr& d) { const bool just_created = mk_window_common(d, [&]() -> t* { if (lib && lib->Dialog) diff --git a/gui/main-window.hpp b/gui/main-window.hpp index 71e372f0..ef6143e7 100644 --- a/gui/main-window.hpp +++ b/gui/main-window.hpp @@ -68,15 +68,15 @@ class MainWindow : public QMainWindow, private State menu_action_tracker, menu_action_filter, menu_action_proto, menu_action_options, menu_action_mappings; - mem current_tracker() + std::shared_ptr current_tracker() { return modules.trackers().value(ui.iconcomboTrackerSource->currentIndex(), nullptr); } - mem current_protocol() + std::shared_ptr current_protocol() { return modules.protocols().value(ui.iconcomboProtocol->currentIndex(), nullptr); } - mem current_filter() + std::shared_ptr current_filter() { return modules.filters().value(ui.iconcomboFilter->currentIndex(), nullptr); } @@ -99,7 +99,7 @@ class MainWindow : public QMainWindow, private State // only use in impl file since no definition in header! template - bool mk_dialog(mem lib, ptr& d); + bool mk_dialog(std::shared_ptr lib, ptr& d); // idem template diff --git a/gui/main.cpp b/gui/main.cpp index 80040732..16e08b6e 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -129,13 +129,13 @@ main(int argc, char** argv) if (!QSettings(OPENTRACK_ORG).value("disable-translation", false).toBool()) { - (void) t.load(QLocale(), "", "", QCoreApplication::applicationDirPath() + "/" + OPENTRACK_I18N_PATH, ".qm"); + (void) t.load(QLocale(), "", "", QCoreApplication::applicationDirPath() + "/" OPENTRACK_I18N_PATH, ".qm"); (void) QCoreApplication::installTranslator(&t); } do { - mem w = std::make_shared(); + std::shared_ptr w = std::make_shared(); if (!w->isEnabled()) break; diff --git a/logic/selected-libraries.hpp b/logic/selected-libraries.hpp index 689cbec3..65e9733e 100644 --- a/logic/selected-libraries.hpp +++ b/logic/selected-libraries.hpp @@ -15,11 +15,14 @@ struct OTR_LOGIC_EXPORT SelectedLibraries { - using dylibptr = mem; - mem pTracker; - mem pFilter; - mem pProtocol; + using dylibptr = std::shared_ptr; + + std::shared_ptr pTracker; + std::shared_ptr pFilter; + std::shared_ptr pProtocol; + SelectedLibraries(QFrame* frame, dylibptr t, dylibptr p, dylibptr f); SelectedLibraries() : pTracker(nullptr), pFilter(nullptr), pProtocol(nullptr), correct(false) {} + bool correct; }; diff --git a/logic/state.hpp b/logic/state.hpp index f5892557..8bef71ad 100644 --- a/logic/state.hpp +++ b/logic/state.hpp @@ -26,5 +26,5 @@ struct State Modules modules; main_settings s; Mappings pose; - mem work; + std::shared_ptr work; }; diff --git a/logic/work.cpp b/logic/work.cpp index 7b9e550e..6829e62b 100644 --- a/logic/work.cpp +++ b/logic/work.cpp @@ -60,7 +60,7 @@ std::shared_ptr Work::make_logger(main_settings &s) } -Work::Work(Mappings& m, QFrame* frame, mem tracker_, mem filter_, mem proto_) : +Work::Work(Mappings& m, QFrame* frame, std::shared_ptr tracker_, std::shared_ptr filter_, std::shared_ptr proto_) : libs(frame, tracker_, filter_, proto_), logger(make_logger(s)), tracker(std::make_shared(m, libs, *logger)), diff --git a/logic/work.hpp b/logic/work.hpp index f1d5e401..dc32536c 100644 --- a/logic/work.hpp +++ b/logic/work.hpp @@ -35,7 +35,7 @@ struct OTR_LOGIC_EXPORT Work std::shared_ptr sc; std::vector keys; - Work(Mappings& m, QFrame* frame, mem tracker, mem filter, mem proto); + Work(Mappings& m, QFrame* frame, std::shared_ptr tracker, std::shared_ptr filter, std::shared_ptr proto); ~Work(); void reload_shortcuts(); bool is_ok() const; diff --git a/spline/spline-widget.cpp b/spline/spline-widget.cpp index fef03e82..c71626f0 100644 --- a/spline/spline-widget.cpp +++ b/spline/spline-widget.cpp @@ -59,7 +59,7 @@ void spline_widget::setConfig(spline* spl) if (spl) { - mem s = spl->get_settings(); + std::shared_ptr s = spl->get_settings(); connection = connect(s.get(), &spline::settings::recomputed, this, [this]() { reload_spline(); }, Qt::QueuedConnection); diff --git a/spline/spline.cpp b/spline/spline.cpp index 58703e02..c1f09db8 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -428,13 +428,13 @@ void spline::recompute() } // the return value is only safe to use with no spline::set_bundle calls -mem spline::get_settings() +std::shared_ptr spline::get_settings() { QMutexLocker foo(&_mutex); return s; } -mem spline::get_settings() const +std::shared_ptr spline::get_settings() const { QMutexLocker foo(&_mutex); return s; diff --git a/spline/spline.hpp b/spline/spline.hpp index 146837c8..acb1861a 100644 --- a/spline/spline.hpp +++ b/spline/spline.hpp @@ -52,7 +52,7 @@ class OTR_SPLINE_EXPORT spline final static QPointF ensure_in_bounds(const QList& points, double max_x, int i); static int element_count(const QList& points, double max_x); - mem s; + std::shared_ptr s; QMetaObject::Connection connection; std::vector data; @@ -99,8 +99,8 @@ public: bundle get_bundle(); void recompute(); - mem get_settings(); - mem get_settings() const; + std::shared_ptr get_settings(); + std::shared_ptr get_settings() const; using points_t = decltype(s->points()); int get_point_count() const; -- cgit v1.2.3 From ba038ce052af5ada52a0117c9f13e44cf1c63f1c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 6 May 2017 13:34:22 +0200 Subject: spline: refactor Generally nothing of importance, just readability. - There was a particularly scary typo where: const unsigned end = std::min(unsigned(value_count), unsigned(p2_x * mult_)); clamping to value_count doesn't make sense, given arbitrary p2_x. - Try harder to avoid assigning s->points. Also the overlap threshold constant was arbitrarily too high. - Sort predicate is meant to use strict ordering, not partial total order. --- spline/spline.cpp | 72 +++++++++++++++++++++++++++---------------------------- spline/spline.hpp | 2 +- 2 files changed, 37 insertions(+), 37 deletions(-) (limited to 'spline') diff --git a/spline/spline.cpp b/spline/spline.cpp index c1f09db8..277ab407 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -113,7 +113,7 @@ float spline::get_value_no_save_internal(double x) if (max_x > 0) x = std::fmin(max_x, x); - float q = float(x * precision(s->points)); + float q = float(x * bucket_size_coefficient(s->points)); int xi = (int)q; float yi = get_value_internal(xi); float yiplus1 = get_value_internal(xi+1); @@ -139,8 +139,7 @@ float spline::get_value_internal(int x) float sign = x < 0 ? -1 : 1; x = std::abs(x); - float ret; - ret = data[std::min(unsigned(x), unsigned(value_count)-1u)]; + float ret = data[std::min(unsigned(x), unsigned(value_count)-1u)]; return ret * sign; } @@ -183,7 +182,7 @@ int spline::element_count(const QList& points, double max_x) bool spline::sort_fn(const QPointF& one, const QPointF& two) { - return one.x() <= two.x(); + return one.x() < two.x(); } void spline::update_interp_data() @@ -197,11 +196,11 @@ void spline::update_interp_data() std::stable_sort(points.begin(), points.begin() + sz, sort_fn); - const double mult = precision(points); - const double mult_ = mult * 30; + const double c = bucket_size_coefficient(points); + const double c_interp = c * 30; for (unsigned i = 0; i < value_count; i++) - data[i] = -1; + data[i] = -16; if (sz < 2) { @@ -209,7 +208,7 @@ void spline::update_interp_data() { const double x = points[0].x(); const double y = points[0].y(); - const int max = clamp(int(x * precision(points)), 1, value_count-1); + const int max = clamp(iround(x * c), 1, value_count-1); for (int k = 0; k <= max; k++) { if (k < value_count) @@ -247,20 +246,21 @@ void spline::update_interp_data() }; // multiplier helps fill in all the x's needed - const unsigned end = std::min(unsigned(value_count), unsigned(p2_x * mult_)); - const unsigned start = std::max(0u, unsigned(p1_x * mult)); + const unsigned end = int(c_interp * (p2_x - p1_x)) + 1; - for (unsigned j = start; j < end; j++) + for (unsigned k = 0; k <= end; k++) { - const double t = (j - start) / (double) (end - start); + const double t = k / double(end); const double t2 = t*t; const double t3 = t*t*t; - const int x = iround(.5 * mult * (cx[0] + cx[1] * t + cx[2] * t2 + cx[3] * t3)); + const int x = int(.5 * c * (cx[0] + cx[1] * t + cx[2] * t2 + cx[3] * t3)); const float y = float(.5 * (cy[0] + cy[1] * t + cy[2] * t2 + cy[3] * t3)); if (x >= 0 && x < value_count) - data[unsigned(x)] = y; + { + data[x] = y; + } } } } @@ -268,7 +268,7 @@ void spline::update_interp_data() float last = 0; for (unsigned i = 0; i < unsigned(value_count); i++) { - if (data[i] < 0) + if (data[i] == -16) data[i] = last; last = data[i]; } @@ -317,7 +317,7 @@ void spline::move_point(int idx, QPointF pt) { points[idx] = pt; // we don't allow points to be reordered, but sort due to possible caller logic error - //std::stable_sort(points.begin(), points.end(), sort_fn); + std::stable_sort(points.begin(), points.end(), sort_fn); s->points = points; validp = false; } @@ -332,7 +332,7 @@ QList spline::get_points() const int spline::get_point_count() const { QMutexLocker foo(&_mutex); - return element_count(s->points, max_x);; + return element_count(s->points, max_x); } void spline::reload() @@ -389,12 +389,9 @@ void spline::recompute() QList list = s->points; // storing to s->points fires bundle::changed and that leads to an infinite loop - // only store if we can't help it + // thus, only store if we can't help it std::stable_sort(list.begin(), list.end(), sort_fn); - if (list != s->points) - s->points = list; - const int sz = list.size(); QList ret_list; @@ -405,16 +402,16 @@ void spline::recompute() QPointF& pt(list[i]); const bool overlap = progn( - for (int j = 0; j < i; j++) - { - QPointF& pt2(list[j]); - const double dist_sq = (pt.x() - pt2.x())*(pt.x() - pt2.x()); - static constexpr double overlap = .6; - if (dist_sq < overlap * overlap) - return true; - } - return false; - ); + for (int j = 0; j < i; j++) + { + const QPointF& pt2(list[j]); + const double dist_sq = QPointF::dotProduct(pt, pt2); + const double overlap = max_x / 360.; + if (dist_sq < overlap * overlap) + return true; + } + return false; + ); if (!overlap) ret_list.push_back(pt); } @@ -440,14 +437,17 @@ std::shared_ptr spline::get_settings() const return s; } -double spline::precision(const QList& points) const +double spline::bucket_size_coefficient(const QList& points) const { - // this adjusts the memoized range to the largest X value. empty space doesn't take value_count discrete points. + static constexpr double eps = 1e-4; + + if (unlikely(max_x < eps)) + return 0; + const int sz = element_count(points, max_x); - if (sz) - return clamp(value_count / clamp(points[sz - 1].x(), 1., max_x), 0., double(value_count)); + const double last_x = sz ? points[sz - 1].x() : max_x; - return value_count / clamp(max_x, 1., double(value_count)); + return clamp((value_count-1) / clamp(last_x, eps, max_x), 0., (value_count-1)); } namespace spline_detail { diff --git a/spline/spline.hpp b/spline/spline.hpp index acb1861a..7098f125 100644 --- a/spline/spline.hpp +++ b/spline/spline.hpp @@ -42,7 +42,7 @@ signals: class OTR_SPLINE_EXPORT spline final { - double precision(const QList& points) const; + double bucket_size_coefficient(const QList& points) const; void update_interp_data(); float get_value_internal(int x); void add_lone_point(); -- cgit v1.2.3 From 6ccd173d52f639bf95d82760fb9c3c1943b660d1 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 7 May 2017 08:09:39 +0200 Subject: spline: fix points deleting themselves on dragging to origin --- spline/spline.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'spline') diff --git a/spline/spline.cpp b/spline/spline.cpp index 277ab407..3787e0f9 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -405,8 +405,9 @@ void spline::recompute() for (int j = 0; j < i; j++) { const QPointF& pt2(list[j]); - const double dist_sq = QPointF::dotProduct(pt, pt2); - const double overlap = max_x / 360.; + const QPointF tmp(pt - pt2); + const double dist_sq = QPointF::dotProduct(tmp, tmp); + const double overlap = max_x / 500.; if (dist_sq < overlap * overlap) return true; } -- cgit v1.2.3 From 940ae0898c5e836c3d82fac85f5fa55bb75414a1 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 7 May 2017 08:10:32 +0200 Subject: spline: reformat --- spline/spline.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'spline') diff --git a/spline/spline.cpp b/spline/spline.cpp index 3787e0f9..6167de0d 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -363,17 +363,18 @@ void spline::set_bundle(bundle b) if (b) { connection = QObject::connect(b.get(), &bundle_::changed, - s.get(), [&]() { - // we're holding the mutex to allow signal disconnection in spline dtor - // before this slot gets called for the next time + s.get(), [&]() + { + // we're holding the mutex to allow signal disconnection in spline dtor + // before this slot gets called for the next time - // spline isn't a QObject and the connection context is incorrect + // spline isn't a QObject and the connection context is incorrect - QMutexLocker l(&_mutex); - recompute(); - emit s->recomputed(); - }, - Qt::QueuedConnection); + QMutexLocker l(&_mutex); + recompute(); + emit s->recomputed(); + }, + Qt::QueuedConnection); } recompute(); -- cgit v1.2.3 From 80deba23647e80895d2942ab2d3b8e247fb68f69 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 7 May 2017 11:29:12 +0200 Subject: spline: recompute spline config lazily --- spline/spline.cpp | 16 ++++++++-------- spline/spline.hpp | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'spline') diff --git a/spline/spline.cpp b/spline/spline.cpp index 6167de0d..76a6190f 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -25,7 +25,7 @@ constexpr int spline::value_count; spline::spline(qreal maxx, qreal maxy, const QString& name) : s(nullptr), - data(value_count, -1.f), + data(value_count, -16), _mutex(QMutex::Recursive), max_x(maxx), max_y(maxy), @@ -50,6 +50,7 @@ spline::spline() : spline(0, 0, "") {} void spline::set_tracking_active(bool value) { + QMutexLocker l(&_mutex); activep = value; } @@ -69,14 +70,14 @@ void spline::set_max_input(qreal max_input) { QMutexLocker l(&_mutex); max_x = max_input; - recompute(); + validp = false; } void spline::set_max_output(qreal max_output) { QMutexLocker l(&_mutex); max_y = max_output; - recompute(); + validp = false; } qreal spline::max_input() const @@ -133,6 +134,7 @@ float spline::get_value_internal(int x) { if (!validp) { + recompute(); update_interp_data(); validp = true; } @@ -371,15 +373,14 @@ void spline::set_bundle(bundle b) // spline isn't a QObject and the connection context is incorrect QMutexLocker l(&_mutex); - recompute(); + validp = false; + emit s->recomputed(); }, Qt::QueuedConnection); } - recompute(); - - emit s->recomputed(); + validp = false; } } @@ -423,7 +424,6 @@ void spline::recompute() last_input_value = QPointF(0, 0); activep = false; - validp = false; } // the return value is only safe to use with no spline::set_bundle calls diff --git a/spline/spline.hpp b/spline/spline.hpp index 7098f125..85287e38 100644 --- a/spline/spline.hpp +++ b/spline/spline.hpp @@ -63,7 +63,7 @@ class OTR_SPLINE_EXPORT spline final MyMutex _mutex; QPointF last_input_value; qreal max_x, max_y; - volatile bool activep; + bool activep; bool validp; public: -- cgit v1.2.3 From 3e913cdf543d5d4b2fd297eb27f3536be692100c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 7 May 2017 11:31:12 +0200 Subject: spline: rename, change call site --- spline/spline.cpp | 9 +++++---- spline/spline.hpp | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'spline') diff --git a/spline/spline.cpp b/spline/spline.cpp index 76a6190f..3c1c3eba 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -134,7 +134,6 @@ float spline::get_value_internal(int x) { if (!validp) { - recompute(); update_interp_data(); validp = true; } @@ -191,6 +190,8 @@ void spline::update_interp_data() { points_t points = s->points; + ensure_valid(points); + int sz = element_count(points, max_x); if (sz == 0) @@ -384,11 +385,11 @@ void spline::set_bundle(bundle b) } } -void spline::recompute() +void spline::ensure_valid(const QList& the_points) { QMutexLocker foo(&_mutex); - QList list = s->points; + QList list = the_points; // storing to s->points fires bundle::changed and that leads to an infinite loop // thus, only store if we can't help it @@ -419,7 +420,7 @@ void spline::recompute() ret_list.push_back(pt); } - if (ret_list != s->points) + if (ret_list != the_points) s->points = ret_list; last_input_value = QPointF(0, 0); diff --git a/spline/spline.hpp b/spline/spline.hpp index 85287e38..328d1ece 100644 --- a/spline/spline.hpp +++ b/spline/spline.hpp @@ -97,7 +97,7 @@ public: void set_tracking_active(bool value); bundle get_bundle(); - void recompute(); + void ensure_valid(const QList& the_points); std::shared_ptr get_settings(); std::shared_ptr get_settings() const; -- cgit v1.2.3 From ad496a028cee3e119177a3a7d9e9f4f0edb3f96c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 7 May 2017 19:23:32 +0200 Subject: spline: add comment --- spline/spline.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'spline') diff --git a/spline/spline.cpp b/spline/spline.cpp index 3c1c3eba..7ca8147c 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -447,6 +447,9 @@ double spline::bucket_size_coefficient(const QList& points) const if (unlikely(max_x < eps)) return 0; + // needed to fill the buckets up to the last control point. + // space between that point and max_x doesn't matter. + const int sz = element_count(points, max_x); const double last_x = sz ? points[sz - 1].x() : max_x; -- cgit v1.2.3