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 popup menus appear at touch position

Carsten Hartenfels11d00667ef7e
diff --git a/libs/ui/input/KisPopupWidgetAction.cpp b/libs/ui/input/KisPopupWidgetAction.cpp
index 6114587af2..47e5e098a3 100644
--- a/libs/ui/input/KisPopupWidgetAction.cpp
+++ b/libs/ui/input/KisPopupWidgetAction.cpp
@@ -10,6 +10,7 @@
#include <QCursor>
#include <QMenu>
+#include <QTouchEvent>
#include <klocalizedstring.h>
@@ -38,8 +39,7 @@ private:
//=================================================================
KisPopupWidgetAction::KisPopupWidgetAction()
- : KisAbstractInputAction("Show Popup Widget"),
- m_requestedWithStylus(false)
+ : KisAbstractInputAction("Show Popup Widget")
{
setName(i18n("Show Popup Widget"));
setDescription(i18n("Show the current tool's popup widget."));
@@ -52,24 +52,51 @@ KisPopupWidgetAction::~KisPopupWidgetAction()
void KisPopupWidgetAction::end(QEvent *event)
{
if (QMenu *popupMenu = inputManager()->toolProxy()->popupActionsMenu()) { // Handle popup menus...
- m_requestedWithStylus = event && event->type() == QEvent::TabletPress;
+ QEvent::Type requestingEventType = event ? event->type() : QEvent::None;
+
+ // Touch events don't update the cursor position.
+ QPointF touchPos;
+ if (requestingEventType == QEvent::TouchBegin) {
+ const QTouchEvent *touchEvent = static_cast<const QTouchEvent *>(event);
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+ const QList<QEventPoint> &touchPoints = event->points();
+#else
+ const QList<QTouchEvent::TouchPoint> &touchPoints = touchEvent->touchPoints();
+#endif
+ if (touchPoints.isEmpty()) {
+ // Getting zero touch points can happen on Android when pressing
+ // on the screen with an entire palm. Punt to using the cursor
+ // position after all.
+ requestingEventType = QEvent::None;
+ } else {
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+ touchPos = touchPoints.constFirst().globalPosition();
+#else
+ touchPos = touchPoints.constFirst().screenPos();
+#endif
+ }
+ }
/**
* Opening a menu changes the focus of the windows, so we should not open it
* inside the filtering loop. Just raise it using the timer.
*/
- QTimer::singleShot(0, this, [this, popupMenu](){
+ QTimer::singleShot(0, this, [popupMenu, requestingEventType, touchPos](){
if (popupMenu) {
- QPoint stylusOffset;
+ QPoint popupPos;
QScopedPointer<SinglePressEventEater> eventEater;
- if (m_requestedWithStylus) {
+ if (requestingEventType == QEvent::TabletPress) {
eventEater.reset(new SinglePressEventEater());
popupMenu->installEventFilter(eventEater.data());
- stylusOffset += QPoint(10,10);
+ popupPos = QCursor::pos() + QPoint(10,10);
+ } else if (requestingEventType == QEvent::TouchBegin) {
+ popupPos = touchPos.toPoint();
+ } else {
+ popupPos = QCursor::pos();
}
- popupMenu->exec(QCursor::pos() + stylusOffset);
+ popupMenu->exec(popupPos);
popupMenu->clear();
}
});
diff --git a/libs/ui/input/KisPopupWidgetAction.h b/libs/ui/input/KisPopupWidgetAction.h
index ce7f756dc2..900e8c1246 100644
--- a/libs/ui/input/KisPopupWidgetAction.h
+++ b/libs/ui/input/KisPopupWidgetAction.h
@@ -35,9 +35,6 @@ public:
int priority() const override {return 1;}
void end(QEvent *) override;
-
-private:
- bool m_requestedWithStylus;
};
#endif // KIS_POPUP_WIDGET_ACTION_H