diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2018-10-25 20:57:47 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2018-10-25 20:57:47 +0200 |
commit | ba1f992a958c383d3085cad4946bbf1b4fdebc04 (patch) | |
tree | 157aee6a54986c1f5aeb127ba86a00e945766b4f /spline/spline.cpp | |
parent | 309871bcc513298aa3760d9be3cc43d11553f761 (diff) |
spline: simplify
- don't copy-paste signal disconnection
- bundle and settings are never null after ctor
Diffstat (limited to 'spline/spline.cpp')
-rw-r--r-- | spline/spline.cpp | 106 |
1 files changed, 45 insertions, 61 deletions
diff --git a/spline/spline.cpp b/spline/spline.cpp index 203822e7..d8cd19b2 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -36,18 +36,10 @@ spline::~spline() { QMutexLocker l(&_mutex); - if (conn_changed) - { - QObject::disconnect(conn_changed); - QObject::disconnect(conn_maxx); - QObject::disconnect(conn_maxy); - conn_changed = {}; - conn_maxx = {}; - conn_maxy = {}; - } + disconnect_signals(); } -spline::spline() : spline("", "", Axis(-1)) {} +spline::spline() : spline(QString{}, QString{}, Axis(-1)) {} void spline::set_tracking_active(bool value) { @@ -321,9 +313,7 @@ void spline::invalidate_settings() QMutexLocker l(&_mutex); validp = false; - points = s->points; - emit s->recomputed(); } @@ -335,69 +325,49 @@ void spline::set_bundle(bundle b, const QString& axis_name, Axis axis) // the sentinel settings/bundle objects don't need any further branching once created if (!s || s->b != b) { - if (conn_changed) - { - QObject::disconnect(conn_changed); - QObject::disconnect(conn_maxx); - QObject::disconnect(conn_maxy); - } + disconnect_signals(); + if (!b) + b = make_bundle(QString{}); s = std::make_shared<settings>(b, axis_name, axis); - if (b) - { - conn_changed = QObject::connect(b.get(), &bundle_::changed, - s.get(), [&] { invalidate_settings(); }); - - // this isn't strictly necessary for the spline but helps the widget - conn_maxx = QObject::connect(&s->opts.clamp_x_, value_::value_changed<int>(), - ctx.get(), [&](double) { invalidate_settings(); }); - conn_maxy = QObject::connect(&s->opts.clamp_y_, value_::value_changed<int>(), - ctx.get(), [&](double) { invalidate_settings(); }); - } - - //points = s->points; - //validp = false; + conn_changed = QObject::connect(b.get(), &bundle_::changed, + s.get(), [&] { invalidate_settings(); }); + // this isn't strictly necessary for the spline but helps the widget + conn_maxx = QObject::connect(&s->opts.clamp_x_, value_::value_changed<int>(), + ctx.get(), [&](double) { invalidate_settings(); }); + conn_maxy = QObject::connect(&s->opts.clamp_y_, value_::value_changed<int>(), + ctx.get(), [&](double) { invalidate_settings(); }); invalidate_settings(); } - else if (!s) - points = points_t{}; } double spline::max_input() const { QMutexLocker l(&_mutex); - if (s) - { - using m = axis_opts::max_clamp; - const value<m>& clamp = s->opts.clamp_x_; - if (clamp == m::x1000 && !points.empty()) - return points[points.size() - 1].x(); - return s ? std::fabs(clamp.to<double>()) : 0; - } - return 0; + using m = axis_opts::max_clamp; + const value<m>& clamp = s->opts.clamp_x_; + if (clamp == m::x1000 && !points.empty()) + return points[points.size() - 1].x(); + return std::fabs(clamp.to<double>()); } double spline::max_output() const { QMutexLocker l(&_mutex); - if (s) - { - using m = axis_opts::max_clamp; - const value<m>& clamp = s->opts.clamp_y_; - if (clamp == m::x1000 && !points.empty()) - return points[points.size() - 1].y(); - return s ? std::fabs(clamp.to<double>()) : 0; - } - return 0; + using m = axis_opts::max_clamp; + const value<m>& clamp = s->opts.clamp_y_; + if (clamp == m::x1000 && !points.empty()) + return points[points.size() - 1].y(); + return std::fabs(clamp.to<double>()); } -void spline::ensure_valid(points_t& the_points) +void spline::ensure_valid(points_t& points_) { QMutexLocker foo(&_mutex); - QList<QPointF> list = the_points; + QList<QPointF> list = points_; // storing to s->points fires bundle::changed and that leads to an infinite loop // thus, only store if we can't help it @@ -405,8 +375,8 @@ void spline::ensure_valid(points_t& the_points) const int sz = list.size(); - QList<QPointF> ret_list, ret_list_2; - ret_list.reserve(sz), ret_list_2.reserve(sz); + QList<QPointF> ret_, tmp; + ret_.reserve(sz), tmp.reserve(sz); const double maxx = max_input(), maxy = max_output(); @@ -428,20 +398,20 @@ void spline::ensure_valid(points_t& the_points) ); if (!overlap) - ret_list_2.append(pt); + tmp.append(pt); if (pt.x() - 1e-2 < maxx && pt.x() >= 0 && pt.y() - 1e-2 < maxy && pt.y() >= 0 && !overlap) { - ret_list.push_back(pt); + ret_.push_back(pt); } } - if (ret_list != the_points) + if (ret_ != points_) { - points = std::move(ret_list_2); + points = std::move(tmp); s->points = points; - the_points = std::move(ret_list); + points_ = std::move(ret_); } last_input_value = {}; @@ -479,6 +449,20 @@ double spline::bucket_size_coefficient(const QList<QPointF>& points) const return clamp((value_count-1) / clamp(last_x, eps, maxx), 0., (value_count-1)); } +void spline::disconnect_signals() +{ + if (conn_changed) + { + QObject::disconnect(conn_changed); + QObject::disconnect(conn_maxx); + QObject::disconnect(conn_maxy); + + conn_changed = {}; + conn_maxx = {}; + conn_maxy = {}; + } +} + namespace spline_detail { settings::settings(bundle const& b, const QString& axis_name, Axis idx): |