From b8fc5f4dac020c4c5d9c0931031ee10761ee58c3 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Sat, 9 Jun 2018 01:59:16 -0700 Subject: [PATCH] added more snapping --- olive.pro.user | 2 +- panels/timeline.cpp | 21 ++++++++++++++++ panels/timeline.h | 2 ++ ui/timelineheader.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++ ui/timelineheader.h | 26 +++++++++++++++++++ ui/timelinewidget.cpp | 13 ++++++---- 6 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 ui/timelineheader.cpp create mode 100644 ui/timelineheader.h diff --git a/olive.pro.user b/olive.pro.user index d8765f1e2..4aa700891 100644 --- a/olive.pro.user +++ b/olive.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/panels/timeline.cpp b/panels/timeline.cpp index 829367462..6d35be843 100644 --- a/panels/timeline.cpp +++ b/panels/timeline.cpp @@ -454,6 +454,27 @@ void Timeline::split_at_playhead() { if (split_selected) redraw_all_clips(); } +void Timeline::snap_to_clip(long* l) { + int limit = 10; + snapped = false; + for (int i=0;iclip_count();i++) { + Clip* c = sequence->get_clip(i); + if (*l > c->timeline_in-limit-1 && + *l < c->timeline_in+limit+1) { + *l = c->timeline_in; + snapped = true; + snap_point = c->timeline_in; + break; + } else if (*l > c->timeline_out-limit-1 && + *l < c->timeline_out+limit+1) { + *l = c->timeline_out; + snapped = true; + snap_point = c->timeline_out; + break; + } + } +} + long Timeline::getFrameFromScreenPoint(int x) { long f = round((float) x / zoom); if (f < 0) { diff --git a/panels/timeline.h b/panels/timeline.h index 28b5128a4..eb959f366 100644 --- a/panels/timeline.h +++ b/panels/timeline.h @@ -72,6 +72,8 @@ public: int getScreenPointFromFrame(long frame); long getFrameFromScreenPoint(int x); + void snap_to_clip(long* l); + Sequence* sequence; long playhead; diff --git a/ui/timelineheader.cpp b/ui/timelineheader.cpp new file mode 100644 index 000000000..a7c6b4f3a --- /dev/null +++ b/ui/timelineheader.cpp @@ -0,0 +1,58 @@ +#include "timelineheader.h" + +#include "panels/panels.h" +#include "panels/timeline.h" +#include "project/sequence.h" + +#include +#include + +#define PLAYHEAD_SIZE 6 + +TimelineHeader::TimelineHeader(QWidget *parent) : QWidget(parent) +{ + dragging = false; +} + +void set_playhead(QMouseEvent* event) { + long frame = panel_timeline->getFrameFromScreenPoint(event->pos().x()); + if (panel_timeline->snapping) { + panel_timeline->snap_to_clip(&frame); + } + panel_timeline->seek(frame); + panel_timeline->repaint_timeline(); +} + +void TimelineHeader::mousePressEvent(QMouseEvent* event) { + set_playhead(event); + dragging = true; +} + +void TimelineHeader::mouseMoveEvent(QMouseEvent* event) { + if (dragging) set_playhead(event); +} + +void TimelineHeader::mouseReleaseEvent(QMouseEvent *) { + dragging = false; +} + +void TimelineHeader::paintEvent(QPaintEvent*) { + if (panel_timeline->sequence != NULL) { + QPainter p(this); + p.setPen(Qt::gray); + int interval = panel_timeline->getScreenPointFromFrame(panel_timeline->sequence->frame_rate); + for (int i=0;igetScreenPointFromFrame(panel_timeline->playhead); + QPoint start(px, height()); + QPainterPath path; + path.moveTo(start); + path.lineTo(px-PLAYHEAD_SIZE, 0); + path.lineTo(px+PLAYHEAD_SIZE, 0); + path.lineTo(start); + p.fillPath(path, Qt::red); + } +} diff --git a/ui/timelineheader.h b/ui/timelineheader.h new file mode 100644 index 000000000..b24cc81c4 --- /dev/null +++ b/ui/timelineheader.h @@ -0,0 +1,26 @@ +#ifndef TIMELINEHEADER_H +#define TIMELINEHEADER_H + +#include + +class TimelineHeader : public QWidget +{ + Q_OBJECT +public: + explicit TimelineHeader(QWidget *parent = nullptr); + +protected: + void paintEvent(QPaintEvent*) override; + void mousePressEvent(QMouseEvent*) override; + void mouseMoveEvent(QMouseEvent*) override; + void mouseReleaseEvent(QMouseEvent*) override; + +private: + bool dragging; + +signals: + +public slots: +}; + +#endif // TIMELINEHEADER_H diff --git a/ui/timelinewidget.cpp b/ui/timelinewidget.cpp index 33040bad8..9d83270f1 100644 --- a/ui/timelinewidget.cpp +++ b/ui/timelinewidget.cpp @@ -539,11 +539,14 @@ void TimelineWidget::mouseMoveEvent(QMouseEvent *event) { if (panel_timeline->tool == TIMELINE_TOOL_EDIT || panel_timeline->tool == TIMELINE_TOOL_RAZOR) { int limit = panel_timeline->getFrameFromScreenPoint(10); - if (panel_timeline->snapping && - !panel_timeline->selecting && - panel_timeline->cursor_frame > panel_timeline->playhead-limit-1 && - panel_timeline->cursor_frame < panel_timeline->playhead+limit+1) { - panel_timeline->cursor_frame = panel_timeline->playhead; + if (panel_timeline->snapping) { + if (!panel_timeline->selecting && + panel_timeline->cursor_frame > panel_timeline->playhead-limit-1 && + panel_timeline->cursor_frame < panel_timeline->playhead+limit+1) { + panel_timeline->cursor_frame = panel_timeline->playhead; + } else { + panel_timeline->snap_to_clip(&panel_timeline->cursor_frame); + } } } if (panel_timeline->selecting) {