diff options
Diffstat (limited to 'spline-widget')
-rw-r--r-- | spline-widget/spline-widget.cpp | 26 | ||||
-rw-r--r-- | spline-widget/spline-widget.hpp | 11 | ||||
-rw-r--r-- | spline-widget/spline.cpp | 29 | ||||
-rw-r--r-- | spline-widget/spline.hpp | 6 |
4 files changed, 53 insertions, 19 deletions
diff --git a/spline-widget/spline-widget.cpp b/spline-widget/spline-widget.cpp index 45a90c6a..f10995d4 100644 --- a/spline-widget/spline-widget.cpp +++ b/spline-widget/spline-widget.cpp @@ -101,7 +101,8 @@ void spline_widget::drawBackground() _background = QPixmap(width(), height()); QPainter painter(&_background); - painter.fillRect(rect(), QColor::fromRgb(204, 204, 204)); + + painter.fillRect(rect(), QWidget::palette().color(QWidget::backgroundRole())); QColor bg_color(112, 154, 209); if (!isEnabled() && !_preview_only) @@ -308,11 +309,11 @@ void spline_widget::drawLine(QPainter& painter, const QPoint& start, const QPoin void spline_widget::mousePressEvent(QMouseEvent *e) { - if (!_config || !isEnabled()) - return; - - if (!is_in_bounds(e->pos())) + if (!_config || !isEnabled() || !is_in_bounds(e->pos())) + { + clearFocus(); return; + } const int point_pixel_closeness_limit = get_closeness_limit(); @@ -384,6 +385,13 @@ void spline_widget::mousePressEvent(QMouseEvent *e) void spline_widget::mouseMoveEvent(QMouseEvent *e) { + if (_preview_only && _config) + { + show_tooltip(e->pos()); + clearFocus(); + return; + } + if (!_config || !isEnabled() || !isActiveWindow() || (moving_control_point_idx != -1 && !hasFocus())) { clearFocus(); @@ -488,11 +496,15 @@ int spline_widget::get_closeness_limit() return std::max(iround(snap_x * c.x()), 1); } -void spline_widget::show_tooltip(const QPoint& pos, const QPointF& value_, const QString& prefix) +void spline_widget::show_tooltip(const QPoint& pos, const QPointF& value_) { const QPointF value = QPoint(0, 0) == value_ ? pixel_coord_to_point(pos) : value_; double x = value.x(), y = value.y(); + + if (_preview_only) + y = _config->get_value_no_save(x); + const int x_ = iround(x), y_ = iround(y); using std::fabs; @@ -508,7 +520,7 @@ void spline_widget::show_tooltip(const QPoint& pos, const QPointF& value_, const const QPoint pix(int(pos.x()) + add_x, int(pos.y()) + add_y); QToolTip::showText(mapToGlobal(pix), - QStringLiteral("value: %1%2x%3").arg(prefix).arg(x).arg(y), + QStringLiteral("value: %1x%2").arg(x).arg(y), this, rect(), 0); diff --git a/spline-widget/spline-widget.hpp b/spline-widget/spline-widget.hpp index d52e249c..921f44a2 100644 --- a/spline-widget/spline-widget.hpp +++ b/spline-widget/spline-widget.hpp @@ -32,6 +32,8 @@ class OPENTRACK_SPLINE_EXPORT spline_widget final : public QWidget Q_OBJECT Q_PROPERTY(QColor colorBezier READ colorBezier WRITE setColorBezier) Q_PROPERTY(bool is_preview_only READ is_preview_only WRITE set_preview_only) + Q_PROPERTY(int x_step READ x_step WRITE set_x_step) + Q_PROPERTY(int y_step READ y_step WRITE set_y_step) using points_t = spline::points_t; public: @@ -47,6 +49,12 @@ public: void force_redraw(); void set_preview_only(bool val); bool is_preview_only() const; + + double x_step() { return _x_step; } + double y_step() { return _y_step; } + void set_x_step(double val) { _x_step = val; } + void set_y_step(double val) { _y_step = val; } + void set_snap(double x, double y) { snap_x = x; snap_y = y; } void get_snap(double& x, double& y) const { x = snap_x; y = snap_y; } protected slots: @@ -57,7 +65,7 @@ protected slots: void reload_spline(); private: int get_closeness_limit(); - void show_tooltip(const QPoint& pos, const QPointF& value = QPointF(0, 0), const QString& prefix = QStringLiteral("")); + void show_tooltip(const QPoint& pos, const QPointF& value = QPointF(0, 0)); bool is_in_bounds(const QPoint& pos) const; void drawBackground(); @@ -89,6 +97,7 @@ private: QMetaObject::Connection connection; double snap_x, snap_y; + double _x_step, _y_step; int moving_control_point_idx; bool _draw_function, _preview_only; diff --git a/spline-widget/spline.cpp b/spline-widget/spline.cpp index 4742bdc8..57817d40 100644 --- a/spline-widget/spline.cpp +++ b/spline-widget/spline.cpp @@ -29,7 +29,8 @@ spline::spline(qreal maxx, qreal maxy, const QString& name) : _mutex(QMutex::Recursive), max_x(maxx), max_y(maxy), - activep(false) + activep(false), + validp(false) { set_bundle(options::make_bundle(name)); } @@ -62,7 +63,7 @@ void spline::removeAllPoints() { QMutexLocker l(&_mutex); s->points = points_t(); - update_interp_data(); + validp = false; } void spline::setMaxInput(qreal max_input) @@ -123,6 +124,12 @@ bool spline::getLastPoint(QPointF& point) float spline::getValueInternal(int x) { + if (!validp) + { + update_interp_data(); + validp = true; + } + float sign = x < 0 ? -1 : 1; x = std::abs(x); float ret; @@ -247,7 +254,7 @@ void spline::removePoint(int i) { points.erase(points.begin() + i); s->points = points; - update_interp_data(); + validp = false; } } @@ -259,7 +266,12 @@ void spline::addPoint(QPointF pt) points.push_back(pt); std::stable_sort(points.begin(), points.end(), sort_fn); s->points = points; - update_interp_data(); + validp = false; +} + +void spline::addPoint(double x, double y) +{ + addPoint(QPointF(x, y)); } void spline::movePoint(int idx, QPointF pt) @@ -274,7 +286,7 @@ void spline::movePoint(int idx, QPointF 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); s->points = points; - update_interp_data(); + validp = false; } } @@ -287,7 +299,7 @@ QList<QPointF> spline::getPoints() const int spline::get_point_count() const { QMutexLocker foo(&_mutex); - return s->points.get().size(); + return s->points().size(); } void spline::reload() @@ -322,7 +334,7 @@ void spline::set_bundle(bundle b) if (b) { - connection = QObject::connect(b.get(), &bundle_type::changed, + 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 @@ -389,8 +401,7 @@ void spline::recompute() last_input_value = QPointF(0, 0); activep = false; - - update_interp_data(); + validp = false; } // the return value is only safe to use with no spline::set_bundle calls diff --git a/spline-widget/spline.hpp b/spline-widget/spline.hpp index b79263ec..adabd33c 100644 --- a/spline-widget/spline.hpp +++ b/spline-widget/spline.hpp @@ -56,12 +56,13 @@ class OPENTRACK_SPLINE_EXPORT spline final std::vector<float> data; using interp_data_t = decltype(data); - static constexpr int value_count = 2048; + static constexpr int value_count = 4096; MyMutex _mutex; QPointF last_input_value; qreal max_x, max_y; volatile bool activep; + bool validp; public: using settings = spline_detail::settings; @@ -87,6 +88,7 @@ public: void removeAllPoints(); void addPoint(QPointF pt); + void addPoint(double x, double y); void movePoint(int idx, QPointF pt); QList<QPointF> getPoints() const; void setMaxInput(qreal MaxInput); @@ -99,6 +101,6 @@ public: mem<settings> get_settings(); mem<const settings> get_settings() const; - using points_t = decltype(s->points.get()); + using points_t = decltype(s->points()); int get_point_count() const; }; |