From 3a5f0134afc6d76711bd227f41edcee1ce54aab4 Mon Sep 17 00:00:00 2001 From: Thomas Wilshaw Date: Mon, 8 Nov 2021 20:51:38 +0000 Subject: [PATCH] Enable marker snapping --- app/widget/marker/marker.cpp | 18 +++++++++++++++--- app/widget/timeruler/seekablewidget.h | 8 +++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/widget/marker/marker.cpp b/app/widget/marker/marker.cpp index b39216fa6..cd503470a 100644 --- a/app/widget/marker/marker.cpp +++ b/app/widget/marker/marker.cpp @@ -158,10 +158,22 @@ void Marker::mouseMoveEvent(QMouseEvent* e) if (drag_allowed_) { dragging_ = true; - int new_pos = marker_start_x_ + e->globalPos().x() - click_position_.x(); + int new_position = marker_start_x_ + e->globalPos().x() - click_position_.x(); - if (new_pos > -marker_width_ / 2 && new_pos < static_cast(parent())->width() - marker_width_ / 2) { - this->move(new_pos-2, this->pos().y()); + rational marker_time = static_cast(parent())->ScreenToTime(new_position); + + if (Core::instance()->snapping()) { + rational movement; + static_cast(parent())->GetSnapService()->SnapPoint({marker_time}, &movement); + if (!movement.isNull()) { + marker_time += movement; + } + + new_position = static_cast(parent())->TimeToScene(marker_time); + } + + if (new_position > -marker_width_ / 2 && new_position < static_cast(parent())->width() - marker_width_ / 2) { + this->move(new_position-2, this->pos().y()); repaint(); } } diff --git a/app/widget/timeruler/seekablewidget.h b/app/widget/timeruler/seekablewidget.h index 7438245a3..cbbcf939f 100644 --- a/app/widget/timeruler/seekablewidget.h +++ b/app/widget/timeruler/seekablewidget.h @@ -49,6 +49,8 @@ public: void SetSnapService(SnapService* service); + SnapService* GetSnapService() { return snap_service_; }; + bool IsDraggingPlayhead() const { return dragging_; @@ -66,6 +68,9 @@ public: void SeekToScreenPoint(int screen); + int TimeToScreen(const rational& time) const; + rational ScreenToTime(int x) const; + public slots: void SetTime(const rational &r); @@ -91,9 +96,6 @@ protected: TimelinePoints* timeline_points() const; - int TimeToScreen(const rational& time) const; - rational ScreenToTime(int x) const; - void DrawPlayhead(QPainter* p, int x, int y); inline const int& text_height() const {