Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle panning touch gestures #5524

Merged
merged 7 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/widgets/helper/ChannelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <QDebug>
#include <QDesktopServices>
#include <QEasingCurve>
#include <QGestureEvent>
#include <QGraphicsBlurEffect>
#include <QJsonDocument>
#include <QMessageBox>
Expand Down Expand Up @@ -369,6 +370,8 @@ ChannelView::ChannelView(InternalCtor /*tag*/, QWidget *parent, Split *split,
this->scrollUpdateRequested();
});

this->grabGesture(Qt::PanGesture);

// TODO: Figure out if we need this, and if so, why
// StrongFocus means we can focus this event through clicking it
// and tabbing to it from another widget. I don't currently know
Expand Down Expand Up @@ -1786,8 +1789,75 @@ void ChannelView::leaveEvent(QEvent * /*event*/)
this->unpause(PauseReason::Mouse);
}

bool ChannelView::event(QEvent *event)
{
if (event->type() == QEvent::Gesture)
{
if (const auto *gestureEvent = dynamic_cast<QGestureEvent *>(event))
{
return this->gestureEvent(gestureEvent);
}
}

return BaseWidget::event(event);
}

bool ChannelView::gestureEvent(const QGestureEvent *event)
{
if (QGesture *pan = event->gesture(Qt::PanGesture))
{
if (const auto *gesture = dynamic_cast<QPanGesture *>(pan))
{
switch (gesture->state())
{
case Qt::GestureStarted: {
this->isPanning_ = true;
// Remove any selections and hide tooltip while panning
this->clearSelection();
this->tooltipWidget_->hide();
if (this->isScrolling_)
{
this->disableScrolling();
}

return true;
}
break;

case Qt::GestureUpdated: {
if (this->scrollBar_->isVisible())
{
this->scrollBar_->offset(-gesture->delta().y() * 0.1);
}

return true;
}
break;

case Qt::GestureFinished:
case Qt::GestureCanceled:
default: {
this->clearSelection();
this->isPanning_ = false;

return true;
}
break;
}
}
}

return false;
}

void ChannelView::mouseMoveEvent(QMouseEvent *event)
{
if (this->isPanning_)
{
// Don't do any text selection, hovering, etc while panning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this interact with mousePressEvent? Does mousePressEvent get called before? If so, it's worth checking that its state gets "reset", as it could set e.g. isLeftMouseDown_.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried changing the mouse press and release event handling while panning, but it caused some selection issues when doing mouse movement after panning.

return;
}

/// Pause on hover
if (float pauseTime = getSettings()->pauseOnHoverDuration;
pauseTime > 0.001F)
Expand Down
5 changes: 5 additions & 0 deletions src/widgets/helper/ChannelView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "widgets/TooltipWidget.hpp"

#include <pajlada/signals/signal.hpp>
#include <QGestureEvent>
#include <QMenu>
#include <QPaintEvent>
#include <QPointer>
Expand Down Expand Up @@ -216,6 +217,9 @@ class ChannelView final : public BaseWidget
#endif
void leaveEvent(QEvent * /*event*/) override;

bool event(QEvent *event) override;
bool gestureEvent(const QGestureEvent *event);

void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
Expand Down Expand Up @@ -374,6 +378,7 @@ class ChannelView final : public BaseWidget
QTimer clickTimer_;

bool isScrolling_ = false;
bool isPanning_ = false;
QPointF lastMiddlePressPosition_;
QPointF currentMousePosition_;
QTimer scrollTimer_;
Expand Down
Loading