Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Make long-press context menus behave more cleanly

Carsten Hartenfels9d317626c093
diff --git a/libs/ui/KisLongPressEventFilter.cpp b/libs/ui/KisLongPressEventFilter.cpp
index 8a5a17a0d5..64877d295d 100644
--- a/libs/ui/KisLongPressEventFilter.cpp
+++ b/libs/ui/KisLongPressEventFilter.cpp
@@ -11,6 +11,7 @@
#include <QMenu>
#include <QMenuBar>
#include <QMouseEvent>
+#include <QScopedValueRollback>
#include <QStyleHints>
#include <QTimer>
#ifdef Q_OS_ANDROID
@@ -24,30 +25,42 @@ KisLongPressEventFilter::KisLongPressEventFilter(QObject *parent)
m_timer->setTimerType(Qt::CoarseTimer);
m_timer->setSingleShot(true);
connect(m_timer, &QTimer::timeout, this, &KisLongPressEventFilter::triggerLongPress);
+#ifdef Q_OS_ANDROID
+ m_longPressTimeout =
+ QAndroidJniObject::callStaticMethod<jint>("org/krita/android/MainActivity", "getLongPressTimeout", "()I");
+#endif
}
bool KisLongPressEventFilter::eventFilter(QObject *watched, QEvent *event)
{
- switch (event->type()) {
- case QEvent::MouseButtonPress:
- case QEvent::MouseButtonDblClick:
- handleMousePress(qobject_cast<QWidget *>(watched), static_cast<QMouseEvent *>(event));
- break;
- case QEvent::MouseMove:
- handleMouseMove(static_cast<QMouseEvent *>(event));
- break;
- case QEvent::MouseButtonRelease:
- cancel();
- break;
- default:
- break;
+ if (!m_handlingEvent) {
+ QScopedValueRollback rollback(m_handlingEvent, true);
+ switch (event->type()) {
+ case QEvent::MouseButtonPress:
+ if (handleMousePress(qobject_cast<QWidget *>(watched), static_cast<QMouseEvent *>(event))) {
+ event->setAccepted(true);
+ return true;
+ }
+ break;
+ case QEvent::MouseMove:
+ if (handleMouseMove(static_cast<QMouseEvent *>(event))) {
+ return true;
+ }
+ break;
+ case QEvent::MouseButtonDblClick:
+ case QEvent::MouseButtonRelease:
+ flush();
+ break;
+ default:
+ break;
+ }
}
return QObject::eventFilter(watched, event);
}
-void KisLongPressEventFilter::handleMousePress(QWidget *target, const QMouseEvent *me)
+bool KisLongPressEventFilter::handleMousePress(QWidget *target, const QMouseEvent *me)
{
- if (isContextMenuTarget(target)) {
+ if (me->buttons() == Qt::LeftButton && me->modifiers() == Qt::NoModifier && isContextMenuTarget(target)) {
const QStyleHints *sh = qApp->styleHints();
long long distance = qMax(MINIMUM_DISTANCE, sh->startDragDistance());
m_distanceSquared = distance * distance;
@@ -60,26 +73,47 @@ void KisLongPressEventFilter::handleMousePress(QWidget *target, const QMouseEven
#endif
m_target = target;
#ifdef Q_OS_ANDROID
- int longPressInterval =
- QAndroidJniObject::callStaticMethod<jint>("org/krita/android/MainActivity", "getLongPressTimeout", "()I");
+ int longPressInterval = m_longPressTimeout;
#else
int longPressInterval = sh->mousePressAndHoldInterval();
#endif
m_timer->start(qMax(MINIMUM_DELAY, longPressInterval));
+ return true;
} else {
cancel();
+ return false;
}
}
-void KisLongPressEventFilter::handleMouseMove(const QMouseEvent *me)
+bool KisLongPressEventFilter::handleMouseMove(const QMouseEvent *me)
{
+ if (m_timer->isActive()) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
- QPoint globalPos = me->globalPos();
+ QPoint globalPos = me->globalPos();
#else
- QPoint globalPos = me->globalPosition().toPoint();
+ QPoint globalPos = me->globalPosition().toPoint();
#endif
- if (m_timer->isActive() && !isWithinDistance(globalPos)) {
- cancel();
+ if (isWithinDistance(globalPos)) {
+ return true;
+ } else {
+ flush();
+ }
+ }
+ return false;
+}
+
+void KisLongPressEventFilter::flush()
+{
+ QWidget *target = m_target.data();
+ cancel();
+ if (target && target->isVisible()) {
+ QMouseEvent event(QEvent::MouseButtonPress,
+ m_pressLocalPos,
+ m_pressGlobalPos,
+ Qt::LeftButton,
+ Qt::LeftButton,
+ Qt::NoModifier);
+ qApp->sendEvent(target, &event);
}
}
@@ -99,7 +133,31 @@ bool KisLongPressEventFilter::isWithinDistance(const QPoint &globalPos) const
void KisLongPressEventFilter::triggerLongPress()
{
QWidget *target = m_target.data();
- if (isContextMenuTarget(target)) {
+ cancel();
+ if (!m_handlingEvent && isContextMenuTarget(target)) {
+ QScopedValueRollback rollback(m_handlingEvent, true);
+ // First we synchronously send a right click press and release so that
+ // the target widget can update its state correctly. Afterwards we post
+ // the context menu event to it so that'll open. As far as I can tell,
+ // that's what Qt does when you right-click on a widget as well.
+ {
+ QMouseEvent pressEvent(QEvent::MouseButtonPress,
+ m_pressLocalPos,
+ m_pressGlobalPos,
+ Qt::RightButton,
+ Qt::RightButton,
+ Qt::NoModifier);
+ qApp->sendEvent(target, &pressEvent);
+ }
+ {
+ QMouseEvent releaseEvent(QEvent::MouseButtonRelease,
+ m_pressLocalPos,
+ m_pressGlobalPos,
+ Qt::RightButton,
+ Qt::NoButton,
+ Qt::NoModifier);
+ qApp->sendEvent(target, &releaseEvent);
+ }
qApp->postEvent(target, new QContextMenuEvent(QContextMenuEvent::Mouse, m_pressLocalPos, m_pressGlobalPos));
}
}
diff --git a/libs/ui/KisLongPressEventFilter.h b/libs/ui/KisLongPressEventFilter.h
index cad800277f..3cdbaa0809 100644
--- a/libs/ui/KisLongPressEventFilter.h
+++ b/libs/ui/KisLongPressEventFilter.h
@@ -23,8 +23,9 @@ private:
static constexpr int MINIMUM_DELAY = 100;
static constexpr int MINIMUM_DISTANCE = 0;
- void handleMousePress(QWidget *target, const QMouseEvent *me);
- void handleMouseMove(const QMouseEvent *me);
+ bool handleMousePress(QWidget *target, const QMouseEvent *me);
+ bool handleMouseMove(const QMouseEvent *me);
+ void flush();
void cancel();
bool isWithinDistance(const QPoint &globalPos) const;
@@ -39,6 +40,10 @@ private:
QPoint m_pressLocalPos;
QPoint m_pressGlobalPos;
QPointer<QWidget> m_target;
+#ifdef Q_OS_ANDROID
+ int m_longPressTimeout;
+#endif
+ bool m_handlingEvent = false;
};
#endif