moved more functions to the base class in the vein of the previous commit

This commit is contained in:
itsmattkc
2019-12-25 02:41:40 +11:00
parent 4b48a73db3
commit d705a27d54
4 changed files with 88 additions and 87 deletions
@@ -41,30 +41,10 @@ TimelineView::TimelineView(const TrackType &type, Qt::Alignment vertical_alignme
Q_ASSERT(vertical_alignment == Qt::AlignTop || vertical_alignment == Qt::AlignBottom);
setAlignment(Qt::AlignLeft | vertical_alignment);
setScene(&scene_);
setDragMode(NoDrag);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setBackgroundRole(QPalette::Window);
setContextMenuPolicy(Qt::CustomContextMenu);
connect(&scene_, SIGNAL(changed(const QList<QRectF>&)), this, SLOT(UpdateSceneRect()));
// Create end item
end_item_ = new TimelineViewEndItem();
scene_.addItem(end_item_);
// Set default scale
SetScale(1.0);
}
void TimelineView::SetScale(const double &scale)
{
scale_ = scale;
// Force redraw for playhead
viewport()->update();
end_item_->SetScale(scale_);
}
void TimelineView::SelectAll()
@@ -170,13 +150,6 @@ void TimelineView::dropEvent(QDropEvent *event)
emit DragDropped(&timeline_event);
}
void TimelineView::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
UpdateSceneRect();
}
Stream::Type TimelineView::TrackTypeToStreamType(TrackType track_type)
{
switch (track_type) {
@@ -270,43 +243,3 @@ void TimelineView::UserSetTime(const int64_t &time)
SetTime(time);
emit TimeChanged(time);
}
void TimelineView::UpdateSceneRect()
{
QRectF bounding_rect = scene_.itemsBoundingRect();
// Ensure the scene height is always AT LEAST the height of the view
// The scrollbar appears to have a 1px margin on the top and bottom, hence the -2
int minimum_height = height() - horizontalScrollBar()->height() - 2;
if (alignment() & Qt::AlignBottom) {
// Ensure the scene left and bottom are always 0
bounding_rect.setBottomLeft(QPointF(0, 0));
if (bounding_rect.top() > minimum_height) {
bounding_rect.setTop(-minimum_height);
}
} else {
// Ensure the scene left and top are always 0
bounding_rect.setTopLeft(QPointF(0, 0));
if (bounding_rect.height() < minimum_height) {
bounding_rect.setHeight(minimum_height);
}
}
// Ensure the scene is always the full length of the timeline with a gap at the end to work with
end_item_->SetEndPadding(width()/4);
// If the scene is already this rect, do nothing
if (scene_.sceneRect() == bounding_rect) {
return;
}
scene_.setSceneRect(bounding_rect);
}
void TimelineView::SetEndTime(const rational &length)
{
end_item_->SetEndTime(length);
}
@@ -32,7 +32,6 @@
#include "timelineviewbase.h"
#include "timelineviewblockitem.h"
#include "timelineviewmouseevent.h"
#include "timelineviewenditem.h"
#include "timelineviewghostitem.h"
#include "widget/timelinewidget/undo/undo.h"
#include "undo/undostack.h"
@@ -50,14 +49,10 @@ public:
Qt::Alignment vertical_alignment = Qt::AlignTop,
QWidget* parent = nullptr);
void SetScale(const double& scale);
void SelectAll();
void DeselectAll();
void SetEndTime(const rational& length);
int GetTrackY(int track_index);
int GetTrackHeight(int track_index);
@@ -67,8 +62,6 @@ public:
void ConnectTrackList(TrackList* list);
signals:
void ScaleChanged(double scale);
void MousePressed(TimelineViewMouseEvent* event);
void MouseMoved(TimelineViewMouseEvent* event);
void MouseReleased(TimelineViewMouseEvent* event);
@@ -90,8 +83,6 @@ protected:
virtual void dragLeaveEvent(QDragLeaveEvent *event) override;
virtual void dropEvent(QDropEvent *event) override;
virtual void resizeEvent(QResizeEvent *event) override;
private:
TrackType ConnectedTrackType();
Stream::Type TrackTypeToStreamType(TrackType track_type);
@@ -107,18 +98,8 @@ private:
TrackList* connected_track_list_;
QGraphicsScene scene_;
TimelineViewEndItem* end_item_;
TrackType type_;
private slots:
/**
* @brief Slot called whenever the view resizes or the scene contents change to enforce minimum scene sizes
*/
void UpdateSceneRect();
};
#endif // TIMELINEVIEW_H
@@ -2,6 +2,7 @@
#include <QGraphicsRectItem>
#include <QMouseEvent>
#include <QScrollBar>
#include "common/timecodefunctions.h"
@@ -11,6 +12,26 @@ TimelineViewBase::TimelineViewBase(QWidget *parent) :
playhead_scene_left_(-1),
playhead_scene_right_(-1)
{
setScene(&scene_);
// Create end item
end_item_ = new TimelineViewEndItem();
scene_.addItem(end_item_);
// Set default scale
SetScale(1.0);
connect(&scene_, SIGNAL(changed(const QList<QRectF>&)), this, SLOT(UpdateSceneRect()));
}
void TimelineViewBase::SetScale(const double &scale)
{
scale_ = scale;
// Force redraw for playhead
viewport()->update();
end_item_->SetScale(scale_);
}
void TimelineViewBase::SetTimebase(const rational &timebase)
@@ -78,3 +99,50 @@ bool TimelineViewBase::PlayheadRelease(QMouseEvent *event)
{
return dragging_playhead_;
}
void TimelineViewBase::SetEndTime(const rational &length)
{
end_item_->SetEndTime(length);
}
void TimelineViewBase::UpdateSceneRect()
{
QRectF bounding_rect = scene_.itemsBoundingRect();
// Ensure the scene height is always AT LEAST the height of the view
// The scrollbar appears to have a 1px margin on the top and bottom, hence the -2
int minimum_height = height() - horizontalScrollBar()->height() - 2;
if (alignment() & Qt::AlignBottom) {
// Ensure the scene left and bottom are always 0
bounding_rect.setBottomLeft(QPointF(0, 0));
if (bounding_rect.top() > minimum_height) {
bounding_rect.setTop(-minimum_height);
}
} else {
// Ensure the scene left and top are always 0
bounding_rect.setTopLeft(QPointF(0, 0));
if (bounding_rect.height() < minimum_height) {
bounding_rect.setHeight(minimum_height);
}
}
// Ensure the scene is always the full length of the timeline with a gap at the end to work with
end_item_->SetEndPadding(width()/4);
// If the scene is already this rect, do nothing
if (scene_.sceneRect() == bounding_rect) {
return;
}
scene_.setSceneRect(bounding_rect);
}
void TimelineViewBase::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
UpdateSceneRect();
}
@@ -4,6 +4,7 @@
#include <QGraphicsView>
#include "timelineplayhead.h"
#include "timelineviewenditem.h"
#include "widget/timelinewidget/timelinescaledobject.h"
class TimelineViewBase : public QGraphicsView, public TimelineScaledObject
@@ -12,7 +13,9 @@ class TimelineViewBase : public QGraphicsView, public TimelineScaledObject
public:
TimelineViewBase(QWidget* parent = nullptr);
virtual void drawForeground(QPainter *painter, const QRectF &rect) override;
void SetScale(const double& scale);
void SetEndTime(const rational& length);
public slots:
void SetTimebase(const rational& timebase);
@@ -22,7 +25,13 @@ public slots:
signals:
void TimeChanged(const int64_t& time);
void ScaleChanged(double scale);
protected:
virtual void drawForeground(QPainter *painter, const QRectF &rect) override;
virtual void resizeEvent(QResizeEvent *event) override;
rational GetPlayheadTime();
bool PlayheadPress(QMouseEvent* event);
@@ -39,6 +48,16 @@ private:
bool dragging_playhead_;
TimelineViewEndItem* end_item_;
QGraphicsScene scene_;
private slots:
/**
* @brief Slot called whenever the view resizes or the scene contents change to enforce minimum scene sizes
*/
void UpdateSceneRect();
};
#endif // TIMELINEVIEWBASE_H