ui: merged nodeview/timelineview scrolling code

Greatly improves code quality and maintainability
This commit is contained in:
itsmattkc
2021-05-10 11:15:37 +10:00
parent 1c983f6b8c
commit 94b2cebeea
11 changed files with 105 additions and 130 deletions
-7
View File
@@ -305,13 +305,6 @@ void CurveView::VerticalScaleChangedEvent(double scale)
} }
} }
void CurveView::wheelEvent(QWheelEvent *event)
{
if (!HandleZoomFromScroll(event)) {
KeyframeViewBase::wheelEvent(event);
}
}
void CurveView::ContextMenuEvent(Menu &m) void CurveView::ContextMenuEvent(Menu &m)
{ {
m.addSeparator(); m.addSeparator();
-2
View File
@@ -66,8 +66,6 @@ protected:
virtual void VerticalScaleChangedEvent(double scale) override; virtual void VerticalScaleChangedEvent(double scale) override;
virtual void wheelEvent(QWheelEvent* event) override;
virtual void ContextMenuEvent(Menu &m) override; virtual void ContextMenuEvent(Menu &m) override;
private: private:
+41 -6
View File
@@ -22,12 +22,15 @@
#include <QMouseEvent> #include <QMouseEvent>
#include "config/config.h"
#include "core.h" #include "core.h"
namespace olive { namespace olive {
#define super QGraphicsView
HandMovableView::HandMovableView(QWidget* parent) : HandMovableView::HandMovableView(QWidget* parent) :
QGraphicsView(parent), super(parent),
dragging_hand_(false) dragging_hand_(false)
{ {
connect(Core::instance(), &Core::ToolChanged, this, &HandMovableView::ApplicationToolChanged); connect(Core::instance(), &Core::ToolChanged, this, &HandMovableView::ApplicationToolChanged);
@@ -62,7 +65,7 @@ bool HandMovableView::HandPress(QMouseEvent *event)
Qt::LeftButton, Qt::LeftButton,
event->modifiers()); event->modifiers());
QGraphicsView::mousePressEvent(&transformed); super::mousePressEvent(&transformed);
return true; return true;
} }
@@ -80,7 +83,7 @@ bool HandMovableView::HandMove(QMouseEvent *event)
Qt::LeftButton, Qt::LeftButton,
event->modifiers()); event->modifiers());
QGraphicsView::mouseMoveEvent(&transformed); super::mouseMoveEvent(&transformed);
} }
return dragging_hand_; return dragging_hand_;
} }
@@ -95,7 +98,7 @@ bool HandMovableView::HandRelease(QMouseEvent *event)
Qt::LeftButton, Qt::LeftButton,
event->modifiers()); event->modifiers());
QGraphicsView::mouseReleaseEvent(&transformed); super::mouseReleaseEvent(&transformed);
setInteractive(true); setInteractive(true);
setDragMode(pre_hand_drag_mode_); setDragMode(pre_hand_drag_mode_);
@@ -108,15 +111,47 @@ bool HandMovableView::HandRelease(QMouseEvent *event)
return false; return false;
} }
void HandMovableView::SetDefaultDragMode(QGraphicsView::DragMode mode) void HandMovableView::SetDefaultDragMode(HandMovableView::DragMode mode)
{ {
default_drag_mode_ = mode; default_drag_mode_ = mode;
setDragMode(default_drag_mode_); setDragMode(default_drag_mode_);
} }
const QGraphicsView::DragMode &HandMovableView::GetDefaultDragMode() const const HandMovableView::DragMode &HandMovableView::GetDefaultDragMode() const
{ {
return default_drag_mode_; return default_drag_mode_;
} }
bool HandMovableView::WheelEventIsAZoomEvent(QWheelEvent *event)
{
return (static_cast<bool>(event->modifiers() & Qt::ControlModifier) == !Config::Current()[QStringLiteral("ScrollZooms")].toBool());
}
void HandMovableView::wheelEvent(QWheelEvent *event)
{
if (WheelEventIsAZoomEvent(event)) {
if (!event->angleDelta().isNull()) {
qreal multiplier = 1.0 + (static_cast<qreal>(event->angleDelta().x() + event->angleDelta().y()) * 0.001);
QPointF cursor_pos;
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
cursor_pos = event->position();
#else
cursor_pos = event->posF();
#endif
ZoomIntoCursorPosition(event, multiplier, cursor_pos);
}
} else {
super::wheelEvent(event);
}
}
void HandMovableView::ZoomIntoCursorPosition(QWheelEvent *event, double multiplier, const QPointF &cursor_pos)
{
Q_UNUSED(event)
Q_UNUSED(multiplier)
Q_UNUSED(cursor_pos)
}
} }
@@ -43,6 +43,12 @@ protected:
void SetDefaultDragMode(DragMode mode); void SetDefaultDragMode(DragMode mode);
const DragMode& GetDefaultDragMode() const; const DragMode& GetDefaultDragMode() const;
static bool WheelEventIsAZoomEvent(QWheelEvent* event);
virtual void wheelEvent(QWheelEvent* event) override;
virtual void ZoomIntoCursorPosition(QWheelEvent* event, double multiplier, const QPointF &cursor_pos);
private: private:
bool dragging_hand_; bool dragging_hand_;
DragMode pre_hand_drag_mode_; DragMode pre_hand_drag_mode_;
-8
View File
@@ -43,14 +43,6 @@ void KeyframeView::SetElementY(const NodeInput &c, int y)
} }
} }
} }
void KeyframeView::wheelEvent(QWheelEvent *event)
{
if (!HandleZoomFromScroll(event)) {
KeyframeViewBase::wheelEvent(event);
}
}
void KeyframeView::SceneRectUpdateEvent(QRectF &rect) void KeyframeView::SceneRectUpdateEvent(QRectF &rect)
{ {
rect.setY(0); rect.setY(0);
-2
View File
@@ -39,8 +39,6 @@ public:
void SetElementY(const NodeInput& c, int y); void SetElementY(const NodeInput& c, int y);
protected: protected:
virtual void wheelEvent(QWheelEvent* event) override;
virtual void SceneRectUpdateEvent(QRectF& rect) override; virtual void SceneRectUpdateEvent(QRectF& rect) override;
public slots: public slots:
+4 -20
View File
@@ -622,24 +622,6 @@ void NodeView::mouseReleaseEvent(QMouseEvent *event)
super::mouseReleaseEvent(event); super::mouseReleaseEvent(event);
} }
void NodeView::wheelEvent(QWheelEvent *event)
{
if (TimeBasedView::WheelEventIsAZoomEvent(event)) {
qreal multiplier = 1.0 + (static_cast<qreal>(event->angleDelta().x() + event->angleDelta().y()) * 0.001);
QPointF cursor_pos;
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
cursor_pos = event->position();
#else
cursor_pos = event->posF();
#endif
ZoomIntoCursorPosition(multiplier, cursor_pos);
} else {
QWidget::wheelEvent(event);
}
}
void NodeView::UpdateSelectionCache() void NodeView::UpdateSelectionCache()
{ {
QVector<Node*> current_selection = scene_.GetSelectedNodes(); QVector<Node*> current_selection = scene_.GetSelectedNodes();
@@ -874,8 +856,10 @@ void NodeView::DisconnectSelectionChangedSignal()
disconnect(&scene_, &QGraphicsScene::selectionChanged, this, &NodeView::UpdateSelectionCache); disconnect(&scene_, &QGraphicsScene::selectionChanged, this, &NodeView::UpdateSelectionCache);
} }
void NodeView::ZoomIntoCursorPosition(double multiplier, const QPointF& cursor_pos) void NodeView::ZoomIntoCursorPosition(QWheelEvent *event, double multiplier, const QPointF& cursor_pos)
{ {
Q_UNUSED(event)
double test_scale = scale_ * multiplier; double test_scale = scale_ * multiplier;
if (test_scale > kMinimumScale) { if (test_scale > kMinimumScale) {
@@ -900,7 +884,7 @@ void NodeView::ZoomFromKeyboard(double multiplier)
cursor_pos = QPoint(width()/2, height()/2); cursor_pos = QPoint(width()/2, height()/2);
} }
ZoomIntoCursorPosition(multiplier, cursor_pos); ZoomIntoCursorPosition(nullptr, multiplier, cursor_pos);
} }
NodeView::NodeViewAttachNodesToCursor::NodeViewAttachNodesToCursor(NodeView *view, const QVector<Node *> &nodes) : NodeView::NodeViewAttachNodesToCursor::NodeViewAttachNodesToCursor(NodeView *view, const QVector<Node *> &nodes) :
+1 -3
View File
@@ -90,7 +90,7 @@ protected:
virtual void mouseMoveEvent(QMouseEvent *event) override; virtual void mouseMoveEvent(QMouseEvent *event) override;
virtual void mouseReleaseEvent(QMouseEvent* event) override; virtual void mouseReleaseEvent(QMouseEvent* event) override;
virtual void wheelEvent(QWheelEvent* event) override; virtual void ZoomIntoCursorPosition(QWheelEvent *event, double multiplier, const QPointF &cursor_pos) override;
private: private:
void AttachNodesToCursor(const QVector<Node *> &nodes); void AttachNodesToCursor(const QVector<Node *> &nodes);
@@ -106,8 +106,6 @@ private:
void ConnectSelectionChangedSignal(); void ConnectSelectionChangedSignal();
void DisconnectSelectionChangedSignal(); void DisconnectSelectionChangedSignal();
void ZoomIntoCursorPosition(double multiplier, const QPointF &cursor_pos);
void ZoomFromKeyboard(double multiplier); void ZoomFromKeyboard(double multiplier);
class NodeViewAttachNodesToCursor : public UndoCommand class NodeViewAttachNodesToCursor : public UndoCommand
+41 -70
View File
@@ -26,7 +26,6 @@
#include <QTimer> #include <QTimer>
#include "common/timecodefunctions.h" #include "common/timecodefunctions.h"
#include "config/config.h"
namespace olive { namespace olive {
@@ -99,6 +98,47 @@ void TimeBasedView::VerticalScaleChangedEvent(double)
{ {
} }
void TimeBasedView::ZoomIntoCursorPosition(QWheelEvent *event, double scale_multiplier, const QPointF &cursor_pos)
{
// If CTRL is held (or a preference is set to swap CTRL behavior), we zoom instead of scrolling
bool only_vertical = false;
bool only_horizontal = false;
// Ctrl+Shift limits to only one axis
// Alt switches between horizontal only (alt held) or vertical only (alt not held)
if (y_axis_enabled_) {
if (event->modifiers() & Qt::ShiftModifier) {
if (event->modifiers() & Qt::AltModifier) {
only_horizontal = true;
} else {
only_vertical = true;
}
}
} else {
only_horizontal = true;
}
if (!only_vertical) {
double new_x_scale = GetScale() * scale_multiplier;
int new_x_scroll = qRound(double(cursor_pos.x() + horizontalScrollBar()->value()) / GetScale() * new_x_scale - cursor_pos.x());
emit ScaleChanged(new_x_scale);
horizontalScrollBar()->setValue(new_x_scroll);
}
if (!only_horizontal) {
double new_y_scale = GetYScale() * scale_multiplier;
int new_y_scroll = qRound(double(cursor_pos.y() + verticalScrollBar()->value()) / GetYScale() * new_y_scale - cursor_pos.y());
SetYScale(new_y_scale);
verticalScrollBar()->setValue(new_y_scroll);
}
}
void TimeBasedView::SetYScale(const double &y_scale) void TimeBasedView::SetYScale(const double &y_scale)
{ {
y_scale_ = y_scale; y_scale_ = y_scale;
@@ -262,73 +302,4 @@ void TimeBasedView::ScaleChangedEvent(const double &scale)
viewport()->update(); viewport()->update();
} }
bool TimeBasedView::HandleZoomFromScroll(QWheelEvent *event)
{
if (WheelEventIsAZoomEvent(event)) {
// If CTRL is held (or a preference is set to swap CTRL behavior), we zoom instead of scrolling
if (!event->angleDelta().isNull()) {
bool only_vertical = false;
bool only_horizontal = false;
// Ctrl+Shift limits to only one axis
// Alt switches between horizontal only (alt held) or vertical only (alt not held)
if (y_axis_enabled_) {
if (event->modifiers() & Qt::ShiftModifier) {
if (event->modifiers() & Qt::AltModifier) {
only_horizontal = true;
} else {
only_vertical = true;
}
}
} else {
only_horizontal = true;
}
double scale_multiplier;
if (event->angleDelta().x() + event->angleDelta().y() > 0) {
scale_multiplier = 1.1;
} else {
scale_multiplier = 0.9;
}
QPointF cursor_pos;
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
cursor_pos = event->position();
#else
cursor_pos = event->posF();
#endif
if (!only_vertical) {
double new_x_scale = GetScale() * scale_multiplier;
int new_x_scroll = qRound(double(cursor_pos.x() + horizontalScrollBar()->value()) / GetScale() * new_x_scale - cursor_pos.x());
emit ScaleChanged(new_x_scale);
horizontalScrollBar()->setValue(new_x_scroll);
}
if (!only_horizontal) {
double new_y_scale = GetYScale() * scale_multiplier;
int new_y_scroll = qRound(double(cursor_pos.y() + verticalScrollBar()->value()) / GetYScale() * new_y_scale - cursor_pos.y());
SetYScale(new_y_scale);
verticalScrollBar()->setValue(new_y_scroll);
}
}
return true;
}
return false;
}
bool TimeBasedView::WheelEventIsAZoomEvent(QWheelEvent *event)
{
return (static_cast<bool>(event->modifiers() & Qt::ControlModifier) == !Config::Current()[QStringLiteral("ScrollZooms")].toBool());
}
} }
+1 -3
View File
@@ -50,8 +50,6 @@ public:
const double& GetYScale() const; const double& GetYScale() const;
void SetYScale(const double& y_scale); void SetYScale(const double& y_scale);
static bool WheelEventIsAZoomEvent(QWheelEvent* event);
bool IsDraggingPlayhead() const bool IsDraggingPlayhead() const
{ {
return dragging_playhead_; return dragging_playhead_;
@@ -78,7 +76,7 @@ protected:
virtual void VerticalScaleChangedEvent(double scale); virtual void VerticalScaleChangedEvent(double scale);
bool HandleZoomFromScroll(QWheelEvent* event); virtual void ZoomIntoCursorPosition(QWheelEvent *event, double multiplier, const QPointF &cursor_pos) override;
rational GetPlayheadTime() const; rational GetPlayheadTime() const;
@@ -36,8 +36,10 @@
namespace olive { namespace olive {
#define super TimeBasedView
TimelineView::TimelineView(Qt::Alignment vertical_alignment, QWidget *parent) : TimelineView::TimelineView(Qt::Alignment vertical_alignment, QWidget *parent) :
TimeBasedView(parent), super(parent),
selections_(nullptr), selections_(nullptr),
ghosts_(nullptr), ghosts_(nullptr),
show_beam_cursor_(false), show_beam_cursor_(false),
@@ -65,7 +67,7 @@ void TimelineView::mousePressEvent(QMouseEvent *event)
if (dragMode() != GetDefaultDragMode()) { if (dragMode() != GetDefaultDragMode()) {
// Use default behavior when hand dragging for instance // Use default behavior when hand dragging for instance
TimeBasedView::mousePressEvent(event); super::mousePressEvent(event);
return; return;
} }
@@ -82,7 +84,7 @@ void TimelineView::mouseMoveEvent(QMouseEvent *event)
} }
if (dragMode() != GetDefaultDragMode()) { if (dragMode() != GetDefaultDragMode()) {
TimeBasedView::mouseMoveEvent(event); super::mouseMoveEvent(event);
return; return;
} }
@@ -97,7 +99,7 @@ void TimelineView::mouseReleaseEvent(QMouseEvent *event)
} }
if (dragMode() != GetDefaultDragMode()) { if (dragMode() != GetDefaultDragMode()) {
TimeBasedView::mouseReleaseEvent(event); super::mouseReleaseEvent(event);
return; return;
} }
@@ -115,14 +117,14 @@ void TimelineView::mouseDoubleClickEvent(QMouseEvent *event)
void TimelineView::wheelEvent(QWheelEvent *event) void TimelineView::wheelEvent(QWheelEvent *event)
{ {
if (HandleZoomFromScroll(event)) { if (WheelEventIsAZoomEvent(event)) {
return; super::wheelEvent(event);
} else { } else {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
QPoint angle_delta = event->angleDelta(); QPoint angle_delta = event->angleDelta();
if (Config::Current()["InvertTimelineScrollAxes"].toBool() // Check if config is set to invert timeline axes if (Config::Current()[QStringLiteral("InvertTimelineScrollAxes")].toBool() // Check if config is set to invert timeline axes
&& event->source() != Qt::MouseEventSynthesizedBySystem) { // Never flip axes on Apple trackpads though && event->source() != Qt::MouseEventSynthesizedBySystem) { // Never flip axes on Apple trackpads though
angle_delta = QPoint(angle_delta.y(), angle_delta.x()); angle_delta = QPoint(angle_delta.y(), angle_delta.x());
} }
@@ -164,7 +166,7 @@ void TimelineView::wheelEvent(QWheelEvent *event)
); );
#endif #endif
QGraphicsView::wheelEvent(&e); super::wheelEvent(&e);
} }
} }
@@ -296,7 +298,7 @@ void TimelineView::drawForeground(QPainter *painter, const QRectF &rect)
} }
// Draw standard TimelineViewBase things (such as playhead) // Draw standard TimelineViewBase things (such as playhead)
TimeBasedView::drawForeground(painter, rect); super::drawForeground(painter, rect);
} }
void TimelineView::ToolChangedEvent(Tool::Item tool) void TimelineView::ToolChangedEvent(Tool::Item tool)