Skip to content

Commit

Permalink
macOS: gesture support in timeline (wip)
Browse files Browse the repository at this point in the history
Ref: #272
  • Loading branch information
rodlie committed Dec 15, 2024
1 parent 335e120 commit e423168
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 deletions.
46 changes: 41 additions & 5 deletions src/app/GUI/keysview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,33 @@ void KeysView::resizeEvent(QResizeEvent *e) {
if(mGraphViewed) graphResizeEvent(e);
}

void KeysView::wheelEvent(QWheelEvent *e) {
if(mGraphViewed) {
graphWheelEvent(e);
void KeysView::wheelEvent(QWheelEvent *e)
{
#ifdef Q_OS_MAC
const bool alt = e->modifiers() & Qt::AltModifier;
const bool ctrl = e->modifiers() & Qt::ControlModifier;
const bool shift = e->modifiers() & Qt::ShiftModifier;
const bool noMod = !alt && !ctrl && !shift;
if (e->phase() != Qt::NoScrollPhase && noMod) {
if (e->phase() == Qt::ScrollBegin) {
mPanEvent = false;
const auto x = e->angleDelta().x();
const auto y = e->angleDelta().y();
if (x < 0) {
if (y > 0 || x < y || y == 0 || x == y) { mPanEvent = true; }
} else if (x > 0) {
if (y < 0 || x > y || y == 0 || x == y) { mPanEvent = true; }
}
} else if (e->phase() == Qt::ScrollEnd) { mPanEvent = false; }
//qDebug() << e->angleDelta().x() << e->angleDelta().y() << mPanEvent;
if (mPanEvent) { emit panEventSignal(e); }
}
#endif
if (mGraphViewed) {
if (!mPanEvent) { graphWheelEvent(e); }
} else {
emit wheelEventSignal(e);
if(mSelecting) {
if (!mPanEvent) { emit wheelEventSignal(e); }
if (mSelecting) {
const QPointF posU = mapFromGlobal(QCursor::pos()) +
QPointF(-eSizesUI::widget/2, 0.);
mSelectionRect.setBottom(posU.y() + mViewedTop);
Expand Down Expand Up @@ -485,6 +506,21 @@ bool KeysView::KFT_keyPressEvent(QKeyEvent *event)
return true;
}

#ifdef Q_OS_MAC
bool KeysView::event(QEvent *e)
{
if (e->type() == QEvent::NativeGesture) {
auto g = dynamic_cast<QNativeGestureEvent*>(e);
if (g->gestureType() == Qt::ZoomNativeGesture ||
g->gestureType() == Qt::SmartZoomNativeGesture) {
emit nativeEventSignal(g);
return true;
}
}
return QWidget::event(e);
}
#endif

void KeysView::drawKeys(QPainter * const p,
const qreal pixelsPerFrame,
const FrameRange &viewedFrameRange) {
Expand Down
12 changes: 12 additions & 0 deletions src/app/GUI/keysview.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ class KeysView : public QWidget, public KeyFocusTarget {

QPoint mLastMovePos;

#ifdef Q_OS_MAC
bool event(QEvent *e);
#endif

void resizeEvent(QResizeEvent *e);
void wheelEvent(QWheelEvent *e);
void mousePressEvent(QMouseEvent *e);
Expand All @@ -142,6 +146,12 @@ class KeysView : public QWidget, public KeyFocusTarget {
void changedViewedFrames(FrameRange);
void wheelEventSignal(QWheelEvent*);
void statusMessage(const QString &message);

#ifdef Q_OS_MAC
void nativeEventSignal(QNativeGestureEvent *e);
void panEventSignal(QWheelEvent *e);
#endif

public:
static QColor sGetAnimatorColor(const int i);

Expand Down Expand Up @@ -267,6 +277,8 @@ class KeysView : public QWidget, public KeyFocusTarget {
int mSavedMinViewedFrame = 0;
int mSavedMaxViewedFrame = 0;

bool mPanEvent = false;

// graph

bool graph_mValueLinesVisible = true;
Expand Down
8 changes: 8 additions & 0 deletions src/app/GUI/timelinewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ TimelineWidget::TimelineWidget(Document &document,
this, &TimelineWidget::setViewedFrameRange);
connect(mKeysView, &KeysView::wheelEventSignal,
mFrameRangeScrollBar, &FrameScrollBar::callWheelEvent);

#ifdef Q_OS_MAC
connect(mKeysView, &KeysView::panEventSignal,
mFrameRangeScrollBar, &FrameScrollBar::callPanEvent);
connect(mKeysView, &KeysView::nativeEventSignal,
mFrameRangeScrollBar, &FrameScrollBar::callNativeGestures);
#endif

mKeysViewLayout->addWidget(mFrameRangeScrollBar);
//mSceneChooser->setCurrentScene(mDocument.fActiveScene); // why?

Expand Down
42 changes: 42 additions & 0 deletions src/ui/widgets/framescrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,48 @@ void FrameScrollBar::callWheelEvent(QWheelEvent *event)
update();
}

#ifdef Q_OS_MAC
void FrameScrollBar::callNativeGestures(QNativeGestureEvent *e)
{
if (!e || !mRange) { return; }
bool triggered = false;

if (e->gestureType() == Qt::ZoomNativeGesture) {
int newFramesSpan = mViewedFramesSpan;
if (e->value() < 0) { newFramesSpan *= 0.85; }
else { newFramesSpan *= 1.15; }
setFramesSpan(newFramesSpan);
triggered = true;
} else if (e->gestureType() == Qt::SmartZoomNativeGesture) {
setFramesSpan(mFrameRange.EMAX);
triggered = true;
}

if (!triggered) { return; }
emitTriggeredChange();
update();
}

void FrameScrollBar::callPanEvent(QWheelEvent *e)
{
if (!e || !mRange) { return; }
bool triggered = false;

if (e->angleDelta().x() == 0) { return; }
if (e->angleDelta().x() > 0) {
setFirstViewedFrame(mFirstViewedFrame - mViewedFramesSpan / 20);
triggered = true;
} else {
setFirstViewedFrame(mFirstViewedFrame + mViewedFramesSpan / 20);
triggered = true;
}

if (!triggered) { return; }
emitTriggeredChange();
update();
}
#endif

void FrameScrollBar::emitChange() {
emit frameRangeChange(getViewedRange());
}
Expand Down
5 changes: 5 additions & 0 deletions src/ui/widgets/framescrollbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class UI_EXPORT FrameScrollBar : public QWidget
void setCanvasFrameRange(const FrameRange& range);
void callWheelEvent(QWheelEvent *event);

#ifdef Q_OS_MAC
void callNativeGestures(QNativeGestureEvent *e);
void callPanEvent(QWheelEvent *e);
#endif

protected:
qreal posToFrame(int xPos);
void paintEvent(QPaintEvent *);
Expand Down

0 comments on commit e423168

Please sign in to comment.