Skip to content

Commit

Permalink
Timeline: support interactive markers
Browse files Browse the repository at this point in the history
Initial implementation, may contain bugs.
  • Loading branch information
rodlie committed Nov 13, 2024
1 parent 9a57e3f commit def181e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/core/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,18 @@ bool Canvas::editMarker(const int frame,
return false;
}

void Canvas::moveMarkerFrame(const int markerFrame,
const int newFrame)
{
if (markerFrame == newFrame) { return; }
qDebug() << "moveMarkerFrame" << markerFrame << newFrame;
int index = getMarkerIndex(markerFrame);
if (index >= 0) {
mMarkers.at(index).frame = newFrame;
emit newFrameRange(mRange);
}
}

const QString Canvas::getMarkerText(int frame)
{
for (const auto &mark: mMarkers) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ class CORE_EXPORT Canvas : public CanvasBase
bool editMarker(const int frame,
const QString &title,
const bool enabled);
void moveMarkerFrame(const int markerFrame,
const int newFrame);
const QString getMarkerText(int frame);
int getMarkerIndex(const int frame);
const std::vector<FrameMarker> getMarkers();
Expand Down
43 changes: 33 additions & 10 deletions src/ui/widgets/framescrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "framescrollbar.h"
#include <QMouseEvent>
#include <QPainter>
#include <QMenu>

#include "GUI/global.h"
#include "colorhelpers.h"
#include "appsupport.h"
Expand All @@ -38,6 +40,7 @@ FrameScrollBar::FrameScrollBar(const int minSpan,
QWidget *parent)
: QWidget(parent)
, mFm(QFontMetrics(font()))
, mGrabbedMarker({"", false, 0})
{
mDisplayTime = AppSupport::getSettings("ui",
"DisplayTimecode",
Expand Down Expand Up @@ -199,12 +202,14 @@ void FrameScrollBar::paintEvent(QPaintEvent *) {
}

// draw handle
QPainterPath path;
path.moveTo(handleRect.left() + (handleRect.width() / 2), handleRect.bottom());
path.lineTo(handleRect.topLeft());
path.lineTo(handleRect.topRight());
path.lineTo(handleRect.left() + (handleRect.width() / 2), handleRect.bottom());
p.fillPath(path, ThemeSupport::getThemeHighlightColor());
if (!mGrabbedMarker.enabled) {
QPainterPath path;
path.moveTo(handleRect.left() + (handleRect.width() / 2), handleRect.bottom());
path.lineTo(handleRect.topLeft());
path.lineTo(handleRect.topRight());
path.lineTo(handleRect.left() + (handleRect.width() / 2), handleRect.bottom());
p.fillPath(path, ThemeSupport::getThemeHighlightColor());
}
}

p.end();
Expand Down Expand Up @@ -310,7 +315,7 @@ bool FrameScrollBar::setFirstViewedFrame(const int firstFrame) {
}

}
#include <QMenu>

void FrameScrollBar::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton && !mRange) {
Expand Down Expand Up @@ -425,6 +430,12 @@ void FrameScrollBar::mousePressEvent(QMouseEvent *event)
}
mPressed = true;
mLastMousePressFrame = posToFrame(event->x() );
bool hasMarker = mCurrentCanvas ? mCurrentCanvas->hasMarker(mLastMousePressFrame) : false;
if (event->button() == Qt::LeftButton && hasMarker) { // grab current marker
mGrabbedMarker.enabled = true;
mGrabbedMarker.frame = mLastMousePressFrame;
return;
}
if (mLastMousePressFrame < mFirstViewedFrame ||
mLastMousePressFrame > mFirstViewedFrame + mViewedFramesSpan) {
setFirstViewedFrame(qRound(mLastMousePressFrame - mViewedFramesSpan/2.));
Expand All @@ -434,17 +445,29 @@ void FrameScrollBar::mousePressEvent(QMouseEvent *event)
update();
}

void FrameScrollBar::mouseMoveEvent(QMouseEvent *event) {
void FrameScrollBar::mouseMoveEvent(QMouseEvent *event)
{
qreal newFrame = posToFrame(event->x() );
if (mGrabbedMarker.enabled && mCurrentCanvas) { // move grabbed marker
if (mCurrentCanvas->hasMarker(newFrame)) { return; }
mCurrentCanvas->moveMarkerFrame(mGrabbedMarker.frame, newFrame);
mGrabbedMarker.frame = newFrame;
return;
}
int moveFrame = qRound(newFrame - mLastMousePressFrame);
if(setFirstViewedFrame(mSavedFirstFrame + moveFrame)) {
if (setFirstViewedFrame(mSavedFirstFrame + moveFrame)) {
emitTriggeredChange();
update();
}
}

void FrameScrollBar::mouseReleaseEvent(QMouseEvent *) {
void FrameScrollBar::mouseReleaseEvent(QMouseEvent *)
{
mPressed = false;
if (mGrabbedMarker.enabled) { // release grabbed marker
mGrabbedMarker.enabled = false;
mGrabbedMarker.frame = 0;
}
update();
}

Expand Down
2 changes: 2 additions & 0 deletions src/ui/widgets/framescrollbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class UI_EXPORT FrameScrollBar : public QWidget

QFontMetrics mFm;

FrameMarker mGrabbedMarker;

QColor mHandleColor = ThemeSupport::getThemeButtonBorderColor();
qptr<Canvas> mCurrentCanvas;
};
Expand Down

0 comments on commit def181e

Please sign in to comment.