moved timeline playhead line to its own class
This commit is contained in:
@@ -18,9 +18,13 @@ set(OLIVE_SOURCES
|
||||
${OLIVE_SOURCES}
|
||||
widget/timelineview/timelineview.h
|
||||
widget/timelineview/timelineview.cpp
|
||||
widget/timelineview/timelineviewrect.h
|
||||
widget/timelineview/timelineviewrect.cpp
|
||||
widget/timelineview/timelineviewclipitem.h
|
||||
widget/timelineview/timelineviewclipitem.cpp
|
||||
widget/timelineview/timelineviewghostitem.h
|
||||
widget/timelineview/timelineviewghostitem.cpp
|
||||
widget/timelineview/timelineviewplayheaditem.h
|
||||
widget/timelineview/timelineviewplayheaditem.cpp
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
@@ -29,21 +29,18 @@ TimelineView::TimelineView(QWidget *parent) :
|
||||
playhead_(0)
|
||||
{
|
||||
setScene(&scene_);
|
||||
setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
|
||||
setAlignment(Qt::AlignLeft | Qt::AlignTop);
|
||||
setDragMode(RubberBandDrag);
|
||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
||||
|
||||
// Set default scale
|
||||
SetScale(1.0);
|
||||
|
||||
// FIXME: Make CSS configurable
|
||||
playhead_line_ = new QGraphicsLineItem();
|
||||
playhead_line_->setPen(QPen(Qt::red));
|
||||
|
||||
// Ensure playhead line is always on top
|
||||
// Create playhead line and ensure it's always on top
|
||||
playhead_line_ = new TimelineViewPlayheadItem();
|
||||
playhead_line_->setZValue(100);
|
||||
|
||||
scene_.addItem(playhead_line_);
|
||||
|
||||
// Set default scale
|
||||
SetScale(1.0);
|
||||
}
|
||||
|
||||
void TimelineView::AddClip(ClipBlock *clip)
|
||||
@@ -70,7 +67,7 @@ void TimelineView::SetScale(const double &scale)
|
||||
item->SetScale(scale_);
|
||||
}
|
||||
|
||||
UpdatePlayheadPosition();
|
||||
playhead_line_->SetScale(scale_);
|
||||
}
|
||||
|
||||
void TimelineView::SetTimebase(const rational &timebase)
|
||||
@@ -81,7 +78,7 @@ void TimelineView::SetTimebase(const rational &timebase)
|
||||
item->SetTimebase(timebase_);
|
||||
}
|
||||
|
||||
UpdatePlayheadPosition();
|
||||
playhead_line_->SetTimebase(timebase_);
|
||||
}
|
||||
|
||||
void TimelineView::Clear()
|
||||
@@ -98,7 +95,7 @@ void TimelineView::SetTime(const int64_t time)
|
||||
{
|
||||
playhead_ = time;
|
||||
|
||||
UpdatePlayheadPosition();
|
||||
playhead_line_->SetPlayhead(playhead_);
|
||||
}
|
||||
|
||||
void TimelineView::mousePressEvent(QMouseEvent *event)
|
||||
@@ -147,14 +144,3 @@ void TimelineView::mouseReleaseEvent(QMouseEvent *event)
|
||||
ghost_items_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void TimelineView::UpdatePlayheadPosition()
|
||||
{
|
||||
if (timebase_.denominator() != 0) {
|
||||
double timebase_dbl = timebase_.ToDouble();
|
||||
|
||||
double playhead_x = double(playhead_ * timebase_.numerator()) / double(timebase_.denominator()) / timebase_dbl * scale_;
|
||||
|
||||
playhead_line_->setLine(playhead_x, 0, playhead_x, height());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "node/block/clip/clip.h"
|
||||
#include "timelineviewclipitem.h"
|
||||
#include "timelineviewghostitem.h"
|
||||
#include "timelineviewplayheaditem.h"
|
||||
|
||||
class TimelineView : public QGraphicsView
|
||||
{
|
||||
@@ -50,8 +51,6 @@ protected:
|
||||
virtual void mouseReleaseEvent(QMouseEvent *event) override;
|
||||
|
||||
private:
|
||||
void UpdatePlayheadPosition();
|
||||
|
||||
QGraphicsScene scene_;
|
||||
|
||||
double scale_;
|
||||
@@ -64,7 +63,7 @@ private:
|
||||
|
||||
QVector<TimelineViewGhostItem*> ghost_items_;
|
||||
|
||||
QGraphicsLineItem* playhead_line_;
|
||||
TimelineViewPlayheadItem* playhead_line_;
|
||||
};
|
||||
|
||||
#endif // TIMELINEVIEW_H
|
||||
|
||||
@@ -27,9 +27,8 @@
|
||||
#include <QStyleOptionGraphicsItem>
|
||||
|
||||
TimelineViewClipItem::TimelineViewClipItem(QGraphicsItem* parent) :
|
||||
QGraphicsRectItem(parent),
|
||||
clip_(nullptr),
|
||||
scale_(1.0)
|
||||
TimelineViewRect(parent),
|
||||
clip_(nullptr)
|
||||
{
|
||||
setBrush(Qt::white);
|
||||
setFlag(QGraphicsItem::ItemIsSelectable, true);
|
||||
@@ -38,31 +37,18 @@ TimelineViewClipItem::TimelineViewClipItem(QGraphicsItem* parent) :
|
||||
void TimelineViewClipItem::SetClip(ClipBlock *clip)
|
||||
{
|
||||
clip_ = clip;
|
||||
UpdateRect();
|
||||
}
|
||||
|
||||
void TimelineViewClipItem::SetTimebase(const rational &timebase)
|
||||
{
|
||||
timebase_ = timebase;
|
||||
UpdateRect();
|
||||
}
|
||||
|
||||
void TimelineViewClipItem::SetScale(const double &scale)
|
||||
{
|
||||
scale_ = scale;
|
||||
UpdateRect();
|
||||
}
|
||||
|
||||
void TimelineViewClipItem::UpdateRect()
|
||||
{
|
||||
if (clip_ == nullptr || timebase_.denominator() == 0) {
|
||||
if (clip_ == nullptr || !TimebaseIsValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
double timebase_dbl = timebase_.ToDouble();
|
||||
|
||||
double item_left = clip_->in().ToDouble() / timebase_dbl * scale_;
|
||||
double item_width = clip_->length().ToDouble() / timebase_dbl * scale_;
|
||||
double item_left = TimeToScreenCoord(clip_->in());
|
||||
double item_width = TimeToScreenCoord(clip_->length());
|
||||
|
||||
setRect(0, 0, item_width - 1, 100);
|
||||
setPos(item_left, 0.0);
|
||||
|
||||
@@ -21,33 +21,24 @@
|
||||
#ifndef TIMELINEVIEWCLIPITEM_H
|
||||
#define TIMELINEVIEWCLIPITEM_H
|
||||
|
||||
#include <QGraphicsRectItem>
|
||||
|
||||
#include "timelineviewrect.h"
|
||||
#include "node/block/clip/clip.h"
|
||||
#include "timelineviewghostitem.h"
|
||||
|
||||
class TimelineViewClipItem : public QGraphicsRectItem
|
||||
class TimelineViewClipItem : public TimelineViewRect
|
||||
{
|
||||
public:
|
||||
TimelineViewClipItem(QGraphicsItem* parent = nullptr);
|
||||
|
||||
void SetClip(ClipBlock* clip);
|
||||
|
||||
void SetTimebase(const rational& timebase);
|
||||
|
||||
void SetScale(const double& scale);
|
||||
|
||||
void UpdateRect();
|
||||
|
||||
protected:
|
||||
virtual void UpdateRect() override;
|
||||
|
||||
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
|
||||
|
||||
private:
|
||||
ClipBlock* clip_;
|
||||
|
||||
rational timebase_;
|
||||
|
||||
double scale_;
|
||||
};
|
||||
|
||||
#endif // TIMELINEVIEWCLIPITEM_H
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "timelineviewplayheaditem.h"
|
||||
|
||||
#include <QGraphicsScene>
|
||||
#include <QPainter>
|
||||
#include <QtMath>
|
||||
|
||||
TimelineViewPlayheadItem::TimelineViewPlayheadItem(QGraphicsItem *parent) :
|
||||
TimelineViewRect(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TimelineViewPlayheadItem::SetPlayhead(const int64_t &playhead)
|
||||
{
|
||||
playhead_ = playhead;
|
||||
|
||||
UpdateRect();
|
||||
}
|
||||
|
||||
void TimelineViewPlayheadItem::UpdateRect()
|
||||
{
|
||||
double x = TimeToScreenCoord(rational(playhead_ * timebase_.numerator(), timebase_.denominator()));
|
||||
double width = TimeToScreenCoord(timebase_);
|
||||
|
||||
setRect(0, 0, width, scene()->height());
|
||||
setPos(x, 0);
|
||||
}
|
||||
|
||||
void TimelineViewPlayheadItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||
{
|
||||
Q_UNUSED(option)
|
||||
Q_UNUSED(widget)
|
||||
|
||||
// FIXME: Make adjustable through CSS
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(QColor(255, 255, 255, 128));
|
||||
painter->drawRect(rect());
|
||||
|
||||
painter->setPen(Qt::red);
|
||||
painter->setBrush(Qt::NoBrush);
|
||||
painter->drawLine(QLineF(rect().topLeft(), rect().bottomLeft()));
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef TIMELINEVIEWPLAYHEADITEM_H
|
||||
#define TIMELINEVIEWPLAYHEADITEM_H
|
||||
|
||||
#include "timelineviewrect.h"
|
||||
|
||||
class TimelineViewPlayheadItem : public TimelineViewRect
|
||||
{
|
||||
public:
|
||||
TimelineViewPlayheadItem(QGraphicsItem* parent = nullptr);
|
||||
|
||||
void SetPlayhead(const int64_t& playhead);
|
||||
|
||||
protected:
|
||||
virtual void UpdateRect() override;
|
||||
|
||||
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
|
||||
|
||||
private:
|
||||
int64_t playhead_;
|
||||
};
|
||||
|
||||
#endif // TIMELINEVIEWPLAYHEADITEM_H
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "timelineviewrect.h"
|
||||
|
||||
TimelineViewRect::TimelineViewRect(QGraphicsItem* parent) :
|
||||
QGraphicsRectItem(parent),
|
||||
scale_(1.0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TimelineViewRect::SetTimebase(const rational &timebase)
|
||||
{
|
||||
timebase_ = timebase;
|
||||
timebase_dbl_ = timebase_.ToDouble();
|
||||
|
||||
UpdateRect();
|
||||
}
|
||||
|
||||
void TimelineViewRect::SetScale(const double &scale)
|
||||
{
|
||||
scale_ = scale;
|
||||
UpdateRect();
|
||||
}
|
||||
|
||||
bool TimelineViewRect::TimebaseIsValid()
|
||||
{
|
||||
return timebase_.denominator() != 0;
|
||||
}
|
||||
|
||||
double TimelineViewRect::TimeToScreenCoord(const rational &time)
|
||||
{
|
||||
return time.ToDouble() / timebase_dbl_ * scale_;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef TIMELINEVIEWRECT_H
|
||||
#define TIMELINEVIEWRECT_H
|
||||
|
||||
#include <QGraphicsRectItem>
|
||||
|
||||
#include "common/rational.h"
|
||||
|
||||
class TimelineViewRect : public QGraphicsRectItem
|
||||
{
|
||||
public:
|
||||
TimelineViewRect(QGraphicsItem* parent = nullptr);
|
||||
|
||||
void SetTimebase(const rational& timebase);
|
||||
|
||||
void SetScale(const double& scale);
|
||||
|
||||
protected:
|
||||
virtual void UpdateRect() = 0;
|
||||
|
||||
bool TimebaseIsValid();
|
||||
|
||||
double TimeToScreenCoord(const rational& time);
|
||||
|
||||
rational timebase_;
|
||||
|
||||
double timebase_dbl_;
|
||||
|
||||
double scale_;
|
||||
};
|
||||
|
||||
#endif // TIMELINEVIEWRECT_H
|
||||
@@ -40,9 +40,9 @@ public:
|
||||
|
||||
void SetCenteredText(bool c);
|
||||
|
||||
public slots:
|
||||
void SetTime(const int64_t &r);
|
||||
|
||||
public slots:
|
||||
void SetScroll(int s);
|
||||
|
||||
protected:
|
||||
|
||||
Reference in New Issue
Block a user