timelineview: removed "end item"

This was written as a hack to force our QGraphicsScenes to larger sizes. Turns
out it was completely unnecessary and can be done better.
This commit is contained in:
itsmattkc
2020-04-25 15:19:46 +10:00
parent acef99eb17
commit 17999eab7a
7 changed files with 52 additions and 168 deletions
@@ -28,8 +28,6 @@ set(OLIVE_SOURCES
widget/timelinewidget/view/timelineviewbase.cpp
widget/timelinewidget/view/timelineviewblockitem.h
widget/timelinewidget/view/timelineviewblockitem.cpp
widget/timelinewidget/view/timelineviewenditem.h
widget/timelinewidget/view/timelineviewenditem.cpp
widget/timelinewidget/view/timelineviewghostitem.h
widget/timelinewidget/view/timelineviewghostitem.cpp
PARENT_SCOPE
@@ -230,6 +230,17 @@ void TimelineView::ToolChangedEvent(Tool::Item tool)
}
}
void TimelineView::SceneRectUpdateEvent(QRectF &rect)
{
if (alignment() & Qt::AlignTop) {
rect.setTop(0);
rect.setBottom(GetHeightOfAllTracks() + height() / 2);
} else if (alignment() & Qt::AlignBottom) {
rect.setBottom(0);
rect.setTop(GetHeightOfAllTracks() - height() / 2);
}
}
Timeline::TrackType TimelineView::ConnectedTrackType()
{
if (connected_track_list_) {
@@ -279,7 +290,20 @@ TimelineViewMouseEvent TimelineView::CreateMouseEvent(const QPoint& pos, Qt::Key
return timeline_event;
}
int TimelineView::GetTrackY(int track_index)
int TimelineView::GetHeightOfAllTracks() const
{
if (connected_track_list_) {
if (alignment() & Qt::AlignTop) {
return GetTrackY(connected_track_list_->TrackCount());
} else {
return GetTrackY(connected_track_list_->TrackCount() - 1);
}
} else {
return 0;
}
}
int TimelineView::GetTrackY(int track_index) const
{
int y = 0;
@@ -301,7 +325,7 @@ int TimelineView::GetTrackY(int track_index)
return y;
}
int TimelineView::GetTrackHeight(int track_index)
int TimelineView::GetTrackHeight(int track_index) const
{
if (!connected_track_list_ || track_index >= connected_track_list_->TrackCount()) {
return TrackOutput::GetDefaultTrackHeight();
@@ -310,7 +334,7 @@ int TimelineView::GetTrackHeight(int track_index)
return connected_track_list_->TrackAt(track_index)->GetTrackHeight();
}
QPoint TimelineView::GetScrollCoordinates()
QPoint TimelineView::GetScrollCoordinates() const
{
return QPoint(horizontalScrollBar()->value(), verticalScrollBar()->value());
}
@@ -53,10 +53,10 @@ public:
void DeselectAll();
int GetTrackY(int track_index);
int GetTrackHeight(int track_index);
int GetTrackY(int track_index) const;
int GetTrackHeight(int track_index) const;
QPoint GetScrollCoordinates();
QPoint GetScrollCoordinates() const;
void SetScrollCoordinates(const QPoint& pt);
void ConnectTrackList(TrackList* list);
@@ -91,6 +91,8 @@ protected:
virtual void ToolChangedEvent(Tool::Item tool) override;
virtual void SceneRectUpdateEvent(QRectF& rect) override;
private:
Timeline::TrackType ConnectedTrackType();
Stream::Type TrackTypeToStreamType(Timeline::TrackType track_type);
@@ -100,6 +102,8 @@ private:
TimelineViewMouseEvent CreateMouseEvent(const QPoint &pos, Qt::KeyboardModifiers modifiers);
int GetHeightOfAllTracks() const;
int SceneToTrack(double y);
void UserSetTime(const int64_t& time);
@@ -44,10 +44,6 @@ TimelineViewBase::TimelineViewBase(QWidget *parent) :
{
setScene(&scene_);
// Create end item
end_item_ = new TimelineViewEndItem();
scene_.addItem(end_item_);
// Set default scale
SetScale(1.0);
@@ -223,54 +219,28 @@ qreal TimelineViewBase::GetPlayheadX()
void TimelineViewBase::SetEndTime(const rational &length)
{
end_item_->SetEndTime(length);
end_time_ = length;
UpdateSceneRect();
}
void TimelineViewBase::UpdateSceneRect()
{
QRectF bounding_rect = scene_.itemsBoundingRect();
if (limit_y_axis_) {
// Make a gap of half the viewport height
if (alignment() & Qt::AlignBottom) {
bounding_rect.setTop(bounding_rect.top() - height()/2);
} else {
bounding_rect.setBottom(bounding_rect.bottom() + height()/2);
}
// 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);
}
}
} else {
// We'll still limit the X to 0 since that behavior is desired by all TimelineViewBase derivatives
bounding_rect.setLeft(0);
}
// There's no need for a timeline to ever go below 0 on the X scale
bounding_rect.setLeft(0);
// Ensure the scene is always the full length of the timeline with a gap at the end to work with
end_item_->SetEndPadding(width()/2);
bounding_rect.setRight(TimeToScene(end_time_) + width() / 2);
// Any further rect processing from derivatives can be done here
SceneRectUpdateEvent(bounding_rect);
// If the scene is already this rect, do nothing
if (scene_.sceneRect() == bounding_rect) {
return;
if (scene_.sceneRect() != bounding_rect) {
scene_.setSceneRect(bounding_rect);
}
scene_.setSceneRect(bounding_rect);
}
void TimelineViewBase::PageScrollToPlayhead()
@@ -299,9 +269,10 @@ void TimelineViewBase::ScaleChangedEvent(const double &scale)
{
TimelineScaledObject::ScaleChangedEvent(scale);
end_item_->SetScale(scale);
// Update scene rect
UpdateSceneRect();
// Force redraw for playhead
// Force redraw for playhead if the above function didn't do it
viewport()->update();
}
@@ -25,7 +25,6 @@
#include "core.h"
#include "timelineplayhead.h"
#include "timelineviewenditem.h"
#include "widget/timelinewidget/timelinescaledobject.h"
OLIVE_NAMESPACE_ENTER
@@ -57,6 +56,8 @@ protected:
virtual void ScaleChangedEvent(const double& scale) override;
virtual void SceneRectUpdateEvent(QRectF&){}
bool HandleZoomFromScroll(QWheelEvent* event);
bool WheelEventIsAZoomEvent(QWheelEvent* event);
@@ -95,14 +96,14 @@ private:
bool dragging_hand_;
DragMode pre_hand_drag_mode_;
TimelineViewEndItem* end_item_;
QGraphicsScene scene_;
bool limit_y_axis_;
DragMode default_drag_mode_;
rational end_time_;
private slots:
/**
* @brief Slot called whenever the view resizes or the scene contents change to enforce minimum scene sizes
@@ -1,61 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "timelineviewenditem.h"
OLIVE_NAMESPACE_ENTER
TimelineViewEndItem::TimelineViewEndItem(QGraphicsItem *parent) :
TimelineViewRect(parent),
end_padding_(0)
{
}
void TimelineViewEndItem::SetEndTime(const rational &time)
{
end_time_ = time;
UpdateRect();
}
void TimelineViewEndItem::SetEndPadding(int padding)
{
end_padding_ = padding;
UpdateRect();
}
void TimelineViewEndItem::UpdateRect()
{
// Doesn't need to be more than one pixel
setRect(0, 0, 1, 1);
setPos(TimeToScene(end_time_) + end_padding_, 0);
}
void TimelineViewEndItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// Item is invisible, this is a no-op
Q_UNUSED(painter)
Q_UNUSED(option)
Q_UNUSED(widget)
}
OLIVE_NAMESPACE_EXIT
@@ -1,53 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef TIMELINEVIEWENDITEM_H
#define TIMELINEVIEWENDITEM_H
#include "timelineviewrect.h"
OLIVE_NAMESPACE_ENTER
/**
* @brief An item placed at the end point of the Timeline to ensure the correct scene size
*/
class TimelineViewEndItem : public TimelineViewRect
{
public:
TimelineViewEndItem(QGraphicsItem* parent = nullptr);
void SetEndTime(const rational& time);
void SetEndPadding(int padding);
virtual void UpdateRect() override;
protected:
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
private:
rational end_time_;
int end_padding_;
};
OLIVE_NAMESPACE_EXIT
#endif // TIMELINEVIEWENDITEM_H