summaryrefslogtreecommitdiffstats
path: root/tests/manual/touchmocking
diff options
context:
space:
mode:
Diffstat (limited to 'tests/manual/touchmocking')
-rw-r--r--tests/manual/touchmocking/touchmockingapplication.cpp78
-rw-r--r--tests/manual/touchmocking/touchmockingapplication.h35
-rw-r--r--tests/manual/touchmocking/touchpoint.pngbin0 -> 1331 bytes
-rw-r--r--tests/manual/touchmocking/utils.h25
4 files changed, 138 insertions, 0 deletions
diff --git a/tests/manual/touchmocking/touchmockingapplication.cpp b/tests/manual/touchmocking/touchmockingapplication.cpp
new file mode 100644
index 000000000..feedae5cd
--- /dev/null
+++ b/tests/manual/touchmocking/touchmockingapplication.cpp
@@ -0,0 +1,78 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+#include "touchmockingapplication.h"
+
+#include <QCursor>
+#include <QEvent>
+#include <QPixmap>
+
+#if defined(QUICK_TOUCHBROWSER)
+# include <QQuickView>
+#endif
+
+#if defined(WIDGET_TOUCHBROWSER)
+# include <QMainWindow>
+#endif
+
+static inline bool isMouseEvent(QEvent *event)
+{
+ switch (event->type()) {
+ case QEvent::MouseMove:
+ case QEvent::MouseButtonPress:
+ case QEvent::MouseButtonRelease:
+ return true;
+ default:
+ return false;
+ }
+}
+
+TouchMockingApplication::TouchMockingApplication(int &argc, char **argv)
+ : Application(argc, argv), m_touchPoint(new QCursor(QPixmap(":touchpoint.png")))
+{
+}
+
+TouchMockingApplication::~TouchMockingApplication()
+{
+ delete m_touchPoint;
+}
+
+bool TouchMockingApplication::notify(QObject *target, QEvent *event)
+{
+ switch (event->type()) {
+ case QEvent::TouchBegin:
+ setOverrideCursor(*m_touchPoint);
+ break;
+ case QEvent::TouchEnd:
+ restoreCursor();
+ break;
+ default:
+ break;
+ }
+
+// All mouse events that are not accepted by the application will be translated to touch events
+// instead (see Qt::AA_SynthesizeTouchForUnhandledMouseEvents).
+#if defined(QUICK_TOUCHBROWSER)
+ if (isMouseEvent(event) && qobject_cast<QQuickView *>(target)) {
+ event->ignore();
+ return false;
+ }
+#elif defined(WIDGET_TOUCHBROWSER)
+ // Popups ignore touch evenets so we send MouseEvents directly.
+ if (isMouseEvent(event)) {
+ if (activePopupWidget()) {
+ restoreCursor();
+ } else {
+ event->ignore();
+ return false;
+ }
+ }
+#endif
+ return Application::notify(target, event);
+}
+
+void TouchMockingApplication::restoreCursor()
+{
+ while (overrideCursor())
+ restoreOverrideCursor();
+}
diff --git a/tests/manual/touchmocking/touchmockingapplication.h b/tests/manual/touchmocking/touchmockingapplication.h
new file mode 100644
index 000000000..4f6e744fc
--- /dev/null
+++ b/tests/manual/touchmocking/touchmockingapplication.h
@@ -0,0 +1,35 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+#ifndef TOUCHMOCKINGAPPLICATION_H
+#define TOUCHMOCKINGAPPLICATION_H
+
+#if defined(QUICK_TOUCHBROWSER)
+# include <QGuiApplication>
+using Application = QGuiApplication;
+#elif defined(WIDGET_TOUCHBROWSER)
+# include <QApplication>
+using Application = QApplication;
+#endif
+
+QT_BEGIN_NAMESPACE
+class QCursor;
+QT_END_NAMESPACE
+
+class TouchMockingApplication : public Application
+{
+ Q_OBJECT
+
+public:
+ TouchMockingApplication(int &argc, char **argv);
+ ~TouchMockingApplication();
+
+ virtual bool notify(QObject *, QEvent *) override;
+
+private:
+ void restoreCursor();
+
+ QCursor *m_touchPoint;
+};
+
+#endif // TOUCHMOCKINGAPPLICATION_H
diff --git a/tests/manual/touchmocking/touchpoint.png b/tests/manual/touchmocking/touchpoint.png
new file mode 100644
index 000000000..7649ee991
--- /dev/null
+++ b/tests/manual/touchmocking/touchpoint.png
Binary files differ
diff --git a/tests/manual/touchmocking/utils.h b/tests/manual/touchmocking/utils.h
new file mode 100644
index 000000000..12d493d3f
--- /dev/null
+++ b/tests/manual/touchmocking/utils.h
@@ -0,0 +1,25 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+#ifndef UTILS_H
+#define UTILS_H
+
+#include <QFileInfo>
+#include <QUrl>
+
+class Utils : public QObject
+{
+ Q_OBJECT
+public:
+ Q_INVOKABLE static QUrl fromUserInput(const QString &userInput);
+};
+
+inline QUrl Utils::fromUserInput(const QString &userInput)
+{
+ QFileInfo fileInfo(userInput);
+ if (fileInfo.exists())
+ return QUrl::fromLocalFile(fileInfo.absoluteFilePath());
+ return QUrl::fromUserInput(userInput);
+}
+
+#endif // UTILS_H