on xenial help sign in
Make MouseTouchAdaptor controllable from within QML

Approved by: Michael Zanetti

commite1cc928ccd541d6638e99d62021294dae753f0d3 authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateWed 18 Mar 2015, 10:16 (12 years ago) parent603d590eacd201
12 files changed+136−28
src/Dash/main.cpp+2−2 src/MouseTouchAdaptor.cpp+40−4 src/MouseTouchAdaptor.h+18−5 src/main.cpp+2−6 tests/qmltests/Panel/tst_Panel.qml+10−2 tests/qmltests/tst_Shell.qml+3−2 tests/uqmlscene/CMakeLists.txt−1 tests/uqmlscene/main.cpp−4 tests/utils/modules/Unity/Test/CMakeLists.txt+2 tests/utils/modules/Unity/Test/MouseTouchEmulationCheckbox.qml+43 tests/utils/modules/Unity/Test/plugin.cpp+15−2 tests/utils/modules/Unity/Test/qmldir+1
src/Dash/main.cpp+2−2
@@ -98,8 +98,7 @@ int main(int argc, const char *argv[])
9898 // Needed only when manually testing on a desktop.
9999 MouseTouchAdaptor *mouseTouchAdaptor = 0;
100100 if (parser.isSet(mousetouchOption)) {
101− mouseTouchAdaptor = new MouseTouchAdaptor;
102− application->installNativeEventFilter(mouseTouchAdaptor);
101+ mouseTouchAdaptor = MouseTouchAdaptor::instance();
103102 }
104103
105104 QUrl source(::qmlDirectory()+"Dash/DashApplication.qml");
@@ -115,6 +114,7 @@ int main(int argc, const char *argv[])
115114 int result = application->exec();
116115
117116 delete view;
117+ delete mouseTouchAdaptor;
118118 delete application;
119119
120120 return result;
src/MouseTouchAdaptor.cpp+40−4
@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2013 Canonical, Ltd.
2+ * Copyright (C) 2013,2015 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -20,12 +20,15 @@
2020
2121#include <qpa/qwindowsysteminterface.h>
2222
23−#include <QtGui/QMouseEvent>
24−#include <QtTest/QTest>
23+#include <QCoreApplication>
24+#include <QMouseEvent>
25+#include <QTest>
2526
2627using QTest::QTouchEventSequence;
2728
2829namespace {
30+MouseTouchAdaptor *g_instance = nullptr;
31+
2932Qt::MouseButton translateMouseButton(xcb_button_t detail)
3033{
3134 switch (detail) {
@@ -39,16 +42,36 @@ Qt::MouseButton translateMouseButton(xcb_button_t detail)
3942} // end of anonymous namespace
4043
4144MouseTouchAdaptor::MouseTouchAdaptor()
42− : m_leftButtonIsPressed(false)
45+ : QObject(nullptr), m_leftButtonIsPressed(false), m_enabled(true)
4346{
47+ QCoreApplication::instance()->installNativeEventFilter(this);
48+
4449 m_touchDevice = new QTouchDevice;
4550 m_touchDevice->setType(QTouchDevice::TouchScreen);
4651 QWindowSystemInterface::registerTouchDevice(m_touchDevice);
4752}
4853
54+MouseTouchAdaptor::~MouseTouchAdaptor()
55+{
56+ g_instance = nullptr;
57+}
58+
59+MouseTouchAdaptor* MouseTouchAdaptor::instance()
60+{
61+ if (!g_instance) {
62+ g_instance = new MouseTouchAdaptor;
63+ }
64+
65+ return g_instance;
66+}
67+
4968bool MouseTouchAdaptor::nativeEventFilter(const QByteArray & eventType,
5069 void * message, long * /*result*/)
5170{
71+ if (!m_enabled) {
72+ return false;
73+ }
74+
5275 if (eventType != "xcb_generic_event_t") {
5376 // wrong backend.
5477 qWarning("MouseTouchAdaptor: XCB backend not in use. Adaptor inoperative!");
@@ -151,3 +174,16 @@ QWindow *MouseTouchAdaptor::findQWindowWithXWindowID(WId windowId)
151174 Q_ASSERT(foundWindow);
152175 return foundWindow;
153176}
177+
178+bool MouseTouchAdaptor::enabled() const
179+{
180+ return m_enabled;
181+}
182+
183+void MouseTouchAdaptor::setEnabled(bool value)
184+{
185+ if (value != m_enabled) {
186+ m_enabled = value;
187+ Q_EMIT enabledChanged(value);
188+ }
189+}
src/MouseTouchAdaptor.h+18−5
@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2013 Canonical, Ltd.
2+ * Copyright (C) 2013,2015 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -27,15 +27,26 @@ class QMouseEvent;
2727class QTouchDevice;
2828
2929// Transforms QMouseEvents into single-finger QTouchEvents.
30−class MouseTouchAdaptor : public QAbstractNativeEventFilter {
31−
30+class MouseTouchAdaptor : public QObject, public QAbstractNativeEventFilter {
31+ Q_OBJECT
3232public:
33− MouseTouchAdaptor();
33+ virtual ~MouseTouchAdaptor();
34+
35+ static MouseTouchAdaptor* instance();
3436
3537 // Filters mouse events and posts the equivalent QTouchEvents.
36− virtual bool nativeEventFilter(const QByteArray & eventType, void *message, long *result);
38+ bool nativeEventFilter(const QByteArray & eventType, void *message, long *result) override;
39+
40+ Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
41+
42+ bool enabled() const;
43+ void setEnabled(bool value);
44+
45+Q_SIGNALS:
46+ void enabledChanged(bool value);
3747
3848private:
49+ MouseTouchAdaptor();
3950
4051 bool handleButtonPress(xcb_button_press_event_t *pressEvent);
4152 bool handleButtonRelease(xcb_button_release_event_t *releaseEvent);
@@ -44,6 +55,8 @@ private:
4455
4556 QTouchDevice *m_touchDevice;
4657 bool m_leftButtonIsPressed;
58+
59+ bool m_enabled;
4760};
4861
4962#endif // MOUSE_TOUCH_ADAPTOR_H
src/main.cpp+2−6
@@ -1,8 +1,5 @@
11/*
2− * Copyright (C) 2012-2014 Canonical, Ltd.
3− *
4− * Authors:
5− * Gerry Boland <gerry.boland@canonical.com>
2+ * Copyright (C) 2012-2015 Canonical, Ltd.
63 *
74 * This program is free software; you can redistribute it and/or modify
85 * it under the terms of the GNU General Public License as published by
@@ -130,8 +127,7 @@ Load the testability driver");
130127 // Needed only when manually testing on a desktop.
131128 MouseTouchAdaptor *mouseTouchAdaptor = 0;
132129 if (parser.isSet(mousetouchOption)) {
133− mouseTouchAdaptor = new MouseTouchAdaptor;
134− application->installNativeEventFilter(mouseTouchAdaptor);
130+ mouseTouchAdaptor = MouseTouchAdaptor::instance();
135131 }
136132
137133 QUrl source(::qmlDirectory()+"Shell.qml");
tests/qmltests/Panel/tst_Panel.qml+10−2
@@ -17,7 +17,7 @@
1717import QtQuick 2.1
1818import QtQuick.Layouts 1.1
1919import QtTest 1.0
20−import Unity.Test 0.1 as UT
20+import Unity.Test 0.1
2121import Ubuntu.Components 0.1
2222import Unity.Indicators 0.1 as Indicators
2323import Ubuntu.Telephony 0.1 as Telephony
@@ -121,6 +121,14 @@ IndicatorTest {
121121 }
122122 }
123123 }
124+
125+ Rectangle {
126+ Layout.preferredHeight: units.dp(1);
127+ Layout.fillWidth: true;
128+ color: "black"
129+ }
130+
131+ MouseTouchEmulationCheckbox {}
124132 }
125133 }
126134
@@ -129,7 +137,7 @@ IndicatorTest {
129137 phoneNumber: "+447812221111"
130138 }
131139
132− UT.UnityTestCase {
140+ UnityTestCase {
133141 name: "Panel"
134142 when: windowShown
135143
tests/qmltests/tst_Shell.qml+3−2
@@ -29,7 +29,7 @@ import Unity.Application 0.1
2929import Unity.Connectivity 0.1
3030import Unity.Indicators 0.1
3131import Unity.Notifications 1.0
32−import Unity.Test 0.1 as UT
32+import Unity.Test 0.1
3333import Powerd 0.1
3434import Wizard 0.1 as Wizard
3535
@@ -158,6 +158,7 @@ Rectangle {
158158 shellLoader.state = model[selectedIndex];
159159 shellLoader.active = true;
160160 }
161+ MouseTouchEmulationCheckbox { color: "white" }
161162 }
162163 }
163164 }
@@ -219,7 +220,7 @@ Rectangle {
219220 phoneNumber: "+447812221111"
220221 }
221222
222− UT.UnityTestCase {
223+ UnityTestCase {
223224 id: testCase
224225 name: "Shell"
225226 when: windowShown
tests/uqmlscene/CMakeLists.txt−1
@@ -2,7 +2,6 @@ add_executable(uqmlscene
22 ${shellapplication_MOC_SRCS}
33 ActiveFocusLogger.cpp
44 main.cpp
5− ${CMAKE_SOURCE_DIR}/src/MouseTouchAdaptor.cpp
65 )
76
87qt5_use_modules(uqmlscene Qml Quick Test)
tests/uqmlscene/main.cpp−4
@@ -63,8 +63,6 @@
6363#include <QtCore/QTranslator>
6464#include <QtCore/QLibraryInfo>
6565
66−#include <MouseTouchAdaptor.h>
67−
6866// UbuntuGestures lib
6967#include <TouchRegistry.h>
7068
@@ -436,8 +434,6 @@ int main(int argc, char ** argv)
436434 app.setOrganizationName("Qt Project");
437435 app.setOrganizationDomain("qt-project.org");
438436
439− MouseTouchAdaptor mouseTouchAdaptor;
440− app.installNativeEventFilter(&mouseTouchAdaptor);
441437 // Create it before loading the module, so that TestUtil
442438 // doesn't create one with a fake timer factory.
443439 // When interacting manually with a test we want the real deal.
tests/utils/modules/Unity/Test/CMakeLists.txt+2
@@ -6,12 +6,14 @@ include_directories(
66 ${Qt5Gui_PRIVATE_INCLUDE_DIRS}
77 ${Qt5Quick_PRIVATE_INCLUDE_DIRS}
88 ${CMAKE_SOURCE_DIR}/libs/UbuntuGestures
9+ ${CMAKE_SOURCE_DIR}/src
910)
1011
1112set(UnityTestQML_SOURCES
1213 testutil.cpp
1314 plugin.cpp
1415 TouchEventSequenceWrapper.cpp
16+ ${CMAKE_SOURCE_DIR}/src/MouseTouchAdaptor.cpp
1517)
1618
1719add_library(UnityTestQml MODULE ${UnityTestQML_SOURCES})
tests/utils/modules/Unity/Test/MouseTouchEmulationCheckbox.qmlnew file+43
@@ -0,0 +1,43 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.0
18+import QtQuick.Layouts 1.1
19+import Ubuntu.Components 1.1
20+import Unity.Test 0.1
21+
22+RowLayout {
23+ id: root
24+ property alias color: label.color
25+
26+ Binding {
27+ target: MouseTouchAdaptor
28+ property: "enabled"
29+ value: checkbox.checked
30+ }
31+
32+ Layout.fillWidth: true
33+ CheckBox {
34+ id: checkbox
35+ checked: true
36+ activeFocusOnPress: false
37+ }
38+ Label {
39+ id: label
40+ text: "Mouse emulates touch"
41+ anchors.verticalCenter: parent.verticalCenter
42+ }
43+}
tests/utils/modules/Unity/Test/plugin.cpp+15−2
@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2012, 2013 Canonical, Ltd.
2+ * Copyright (C) 2012, 2013, 2015 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -17,20 +17,33 @@
1717
1818#include "plugin.h"
1919#include "testutil.h"
20+#include <MouseTouchAdaptor.h>
2021#include "TouchEventSequenceWrapper.h"
2122
2223#include <qqml.h>
2324
24−static QObject *testutil_provider(QQmlEngine* /* engine */, QJSEngine* /* scriptEngine */)
25+namespace {
26+QObject *testutil_provider(QQmlEngine* /* engine */, QJSEngine* /* scriptEngine */)
2527{
2628 return new TestUtil();
2729}
30+QObject *getMouseTouchAdaptorQMLSingleton(QQmlEngine* /* engine */, QJSEngine* /* scriptEngine */)
31+{
32+ return MouseTouchAdaptor::instance();
33+}
34+} // anonymous namespace
2835
2936void UnityTestPlugin::registerTypes(const char *uri)
3037{
3138 Q_ASSERT(QLatin1String(uri) == QLatin1String("Unity.Test"));
39+
40+ // Ensure the instance gets created
41+ MouseTouchAdaptor::instance();
42+
3243 // @uri Unity.Test
3344 qmlRegisterSingletonType<TestUtil>(uri, 0, 1, "Util", testutil_provider);
3445 qmlRegisterUncreatableType<TouchEventSequenceWrapper>(uri, 0, 1, "TouchEventSequence",
3546 "You cannot directly create a TouchEventSequence object.");
47+ qmlRegisterSingletonType<MouseTouchAdaptor>(uri, 0, 1, "MouseTouchAdaptor",
48+ getMouseTouchAdaptorQMLSingleton);
3649}
tests/utils/modules/Unity/Test/qmldir+1
@@ -3,3 +3,4 @@ plugin UnityTestQml
33typeinfo Test.qmltypes
44
55UnityTestCase 0.1 UnityTestCase.qml
6+MouseTouchEmulationCheckbox 0.1 MouseTouchEmulationCheckbox.qml