on xenial help sign in
Make MouseTouchAdaptor controllable from within QML
e1cc928Daniel d'Andrada
src/Dash/main.cpp+2−2
@@ -98,8 +98,7 @@ int main(int argc, const char *argv[])
98 // Needed only when manually testing on a desktop.
98 // Needed only when manually testing on a desktop.
99 MouseTouchAdaptor *mouseTouchAdaptor = 0;
99 MouseTouchAdaptor *mouseTouchAdaptor = 0;
100 if (parser.isSet(mousetouchOption)) {
100 if (parser.isSet(mousetouchOption)) {
101− mouseTouchAdaptor = new MouseTouchAdaptor;
101+ mouseTouchAdaptor = MouseTouchAdaptor::instance();
102− application->installNativeEventFilter(mouseTouchAdaptor);
103 }
102 }
104
103
105 QUrl source(::qmlDirectory()+"Dash/DashApplication.qml");
104 QUrl source(::qmlDirectory()+"Dash/DashApplication.qml");
@@ -115,6 +114,7 @@ int main(int argc, const char *argv[])
115 int result = application->exec();
114 int result = application->exec();
116
115
117 delete view;
116 delete view;
117+ delete mouseTouchAdaptor;
118 delete application;
118 delete application;
119
119
120 return result;
120 return result;
src/MouseTouchAdaptor.cpp+40−4
@@ -1,5 +1,5 @@
1/*
1/*
2− * Copyright (C) 2013 Canonical, Ltd.
2+ * Copyright (C) 2013,2015 Canonical, Ltd.
3 *
3 *
4 * This program is free software; you can redistribute it and/or modify
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
5 * it under the terms of the GNU General Public License as published by
@@ -20,12 +20,15 @@
20
20
21#include <qpa/qwindowsysteminterface.h>
21#include <qpa/qwindowsysteminterface.h>
22
22
23+#include <QCoreApplication>
23−#include <QtGui/QMouseEvent>
24+#include <QMouseEvent>
24−#include <QtTest/QTest>
25+#include <QTest>
25
26
26using QTest::QTouchEventSequence;
27using QTest::QTouchEventSequence;
27
28
28namespace {
29namespace {
30+MouseTouchAdaptor *g_instance = nullptr;
31+
29Qt::MouseButton translateMouseButton(xcb_button_t detail)
32Qt::MouseButton translateMouseButton(xcb_button_t detail)
30{
33{
31 switch (detail) {
34 switch (detail) {
@@ -39,16 +42,36 @@ Qt::MouseButton translateMouseButton(xcb_button_t detail)
39} // end of anonymous namespace
42} // end of anonymous namespace
40
43
41MouseTouchAdaptor::MouseTouchAdaptor()
44MouseTouchAdaptor::MouseTouchAdaptor()
42− : m_leftButtonIsPressed(false)
45+ : QObject(nullptr), m_leftButtonIsPressed(false), m_enabled(true)
43{
46{
47+ QCoreApplication::instance()->installNativeEventFilter(this);
48+
44 m_touchDevice = new QTouchDevice;
49 m_touchDevice = new QTouchDevice;
45 m_touchDevice->setType(QTouchDevice::TouchScreen);
50 m_touchDevice->setType(QTouchDevice::TouchScreen);
46 QWindowSystemInterface::registerTouchDevice(m_touchDevice);
51 QWindowSystemInterface::registerTouchDevice(m_touchDevice);
47}
52}
48
53
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+
49bool MouseTouchAdaptor::nativeEventFilter(const QByteArray & eventType,
68bool MouseTouchAdaptor::nativeEventFilter(const QByteArray & eventType,
50 void * message, long * /*result*/)
69 void * message, long * /*result*/)
51{
70{
71+ if (!m_enabled) {
72+ return false;
73+ }
74+
52 if (eventType != "xcb_generic_event_t") {
75 if (eventType != "xcb_generic_event_t") {
53 // wrong backend.
76 // wrong backend.
54 qWarning("MouseTouchAdaptor: XCB backend not in use. Adaptor inoperative!");
77 qWarning("MouseTouchAdaptor: XCB backend not in use. Adaptor inoperative!");
@@ -151,3 +174,16 @@ QWindow *MouseTouchAdaptor::findQWindowWithXWindowID(WId windowId)
151 Q_ASSERT(foundWindow);
174 Q_ASSERT(foundWindow);
152 return foundWindow;
175 return foundWindow;
153}
176}
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 @@
1/*
1/*
2− * Copyright (C) 2013 Canonical, Ltd.
2+ * Copyright (C) 2013,2015 Canonical, Ltd.
3 *
3 *
4 * This program is free software; you can redistribute it and/or modify
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
5 * it under the terms of the GNU General Public License as published by
@@ -27,15 +27,26 @@ class QMouseEvent;
27class QTouchDevice;
27class QTouchDevice;
28
28
29// Transforms QMouseEvents into single-finger QTouchEvents.
29// Transforms QMouseEvents into single-finger QTouchEvents.
30−class MouseTouchAdaptor : public QAbstractNativeEventFilter {
30+class MouseTouchAdaptor : public QObject, public QAbstractNativeEventFilter {
31−
31+ Q_OBJECT
32public:
32public:
33− MouseTouchAdaptor();
33+ virtual ~MouseTouchAdaptor();
34+
35+ static MouseTouchAdaptor* instance();
34
36
35 // Filters mouse events and posts the equivalent QTouchEvents.
37 // 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);
37
47
38private:
48private:
49+ MouseTouchAdaptor();
39
50
40 bool handleButtonPress(xcb_button_press_event_t *pressEvent);
51 bool handleButtonPress(xcb_button_press_event_t *pressEvent);
41 bool handleButtonRelease(xcb_button_release_event_t *releaseEvent);
52 bool handleButtonRelease(xcb_button_release_event_t *releaseEvent);
@@ -44,6 +55,8 @@ private:
44
55
45 QTouchDevice *m_touchDevice;
56 QTouchDevice *m_touchDevice;
46 bool m_leftButtonIsPressed;
57 bool m_leftButtonIsPressed;
58+
59+ bool m_enabled;
47};
60};
48
61
49#endif // MOUSE_TOUCH_ADAPTOR_H
62#endif // MOUSE_TOUCH_ADAPTOR_H
src/main.cpp+2−6
@@ -1,8 +1,5 @@
1/*
1/*
2− * Copyright (C) 2012-2014 Canonical, Ltd.
2+ * Copyright (C) 2012-2015 Canonical, Ltd.
3− *
4− * Authors:
5− * Gerry Boland <gerry.boland@canonical.com>
6 *
3 *
7 * This program is free software; you can redistribute it and/or modify
4 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
5 * it under the terms of the GNU General Public License as published by
@@ -130,8 +127,7 @@ Load the testability driver");
130 // Needed only when manually testing on a desktop.
127 // Needed only when manually testing on a desktop.
131 MouseTouchAdaptor *mouseTouchAdaptor = 0;
128 MouseTouchAdaptor *mouseTouchAdaptor = 0;
132 if (parser.isSet(mousetouchOption)) {
129 if (parser.isSet(mousetouchOption)) {
133− mouseTouchAdaptor = new MouseTouchAdaptor;
130+ mouseTouchAdaptor = MouseTouchAdaptor::instance();
134− application->installNativeEventFilter(mouseTouchAdaptor);
135 }
131 }
136
132
137 QUrl source(::qmlDirectory()+"Shell.qml");
133 QUrl source(::qmlDirectory()+"Shell.qml");
tests/qmltests/Panel/tst_Panel.qml+10−2
@@ -17,7 +17,7 @@
17import QtQuick 2.1
17import QtQuick 2.1
18import QtQuick.Layouts 1.1
18import QtQuick.Layouts 1.1
19import QtTest 1.0
19import QtTest 1.0
20−import Unity.Test 0.1 as UT
20+import Unity.Test 0.1
21import Ubuntu.Components 0.1
21import Ubuntu.Components 0.1
22import Unity.Indicators 0.1 as Indicators
22import Unity.Indicators 0.1 as Indicators
23import Ubuntu.Telephony 0.1 as Telephony
23import Ubuntu.Telephony 0.1 as Telephony
@@ -121,6 +121,14 @@ IndicatorTest {
121 }
121 }
122 }
122 }
123 }
123 }
124+
125+ Rectangle {
126+ Layout.preferredHeight: units.dp(1);
127+ Layout.fillWidth: true;
128+ color: "black"
129+ }
130+
131+ MouseTouchEmulationCheckbox {}
124 }
132 }
125 }
133 }
126
134
@@ -129,7 +137,7 @@ IndicatorTest {
129 phoneNumber: "+447812221111"
137 phoneNumber: "+447812221111"
130 }
138 }
131
139
132− UT.UnityTestCase {
140+ UnityTestCase {
133 name: "Panel"
141 name: "Panel"
134 when: windowShown
142 when: windowShown
135
143
tests/qmltests/tst_Shell.qml+3−2
@@ -29,7 +29,7 @@ import Unity.Application 0.1
29import Unity.Connectivity 0.1
29import Unity.Connectivity 0.1
30import Unity.Indicators 0.1
30import Unity.Indicators 0.1
31import Unity.Notifications 1.0
31import Unity.Notifications 1.0
32−import Unity.Test 0.1 as UT
32+import Unity.Test 0.1
33import Powerd 0.1
33import Powerd 0.1
34import Wizard 0.1 as Wizard
34import Wizard 0.1 as Wizard
35
35
@@ -158,6 +158,7 @@ Rectangle {
158 shellLoader.state = model[selectedIndex];
158 shellLoader.state = model[selectedIndex];
159 shellLoader.active = true;
159 shellLoader.active = true;
160 }
160 }
161+ MouseTouchEmulationCheckbox { color: "white" }
161 }
162 }
162 }
163 }
163 }
164 }
@@ -219,7 +220,7 @@ Rectangle {
219 phoneNumber: "+447812221111"
220 phoneNumber: "+447812221111"
220 }
221 }
221
222
222− UT.UnityTestCase {
223+ UnityTestCase {
223 id: testCase
224 id: testCase
224 name: "Shell"
225 name: "Shell"
225 when: windowShown
226 when: windowShown
tests/uqmlscene/CMakeLists.txt−1
@@ -2,7 +2,6 @@ add_executable(uqmlscene
2 ${shellapplication_MOC_SRCS}
2 ${shellapplication_MOC_SRCS}
3 ActiveFocusLogger.cpp
3 ActiveFocusLogger.cpp
4 main.cpp
4 main.cpp
5− ${CMAKE_SOURCE_DIR}/src/MouseTouchAdaptor.cpp
6 )
5 )
7
6
8qt5_use_modules(uqmlscene Qml Quick Test)
7qt5_use_modules(uqmlscene Qml Quick Test)
tests/uqmlscene/main.cpp−4
@@ -63,8 +63,6 @@
63#include <QtCore/QTranslator>
63#include <QtCore/QTranslator>
64#include <QtCore/QLibraryInfo>
64#include <QtCore/QLibraryInfo>
65
65
66−#include <MouseTouchAdaptor.h>
67−
68// UbuntuGestures lib
66// UbuntuGestures lib
69#include <TouchRegistry.h>
67#include <TouchRegistry.h>
70
68
@@ -436,8 +434,6 @@ int main(int argc, char ** argv)
436 app.setOrganizationName("Qt Project");
434 app.setOrganizationName("Qt Project");
437 app.setOrganizationDomain("qt-project.org");
435 app.setOrganizationDomain("qt-project.org");
438
436
439− MouseTouchAdaptor mouseTouchAdaptor;
440− app.installNativeEventFilter(&mouseTouchAdaptor);
441 // Create it before loading the module, so that TestUtil
437 // Create it before loading the module, so that TestUtil
442 // doesn't create one with a fake timer factory.
438 // doesn't create one with a fake timer factory.
443 // When interacting manually with a test we want the real deal.
439 // 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(
6 ${Qt5Gui_PRIVATE_INCLUDE_DIRS}
6 ${Qt5Gui_PRIVATE_INCLUDE_DIRS}
7 ${Qt5Quick_PRIVATE_INCLUDE_DIRS}
7 ${Qt5Quick_PRIVATE_INCLUDE_DIRS}
8 ${CMAKE_SOURCE_DIR}/libs/UbuntuGestures
8 ${CMAKE_SOURCE_DIR}/libs/UbuntuGestures
9+ ${CMAKE_SOURCE_DIR}/src
9)
10)
10
11
11set(UnityTestQML_SOURCES
12set(UnityTestQML_SOURCES
12 testutil.cpp
13 testutil.cpp
13 plugin.cpp
14 plugin.cpp
14 TouchEventSequenceWrapper.cpp
15 TouchEventSequenceWrapper.cpp
16+ ${CMAKE_SOURCE_DIR}/src/MouseTouchAdaptor.cpp
15)
17)
16
18
17add_library(UnityTestQml MODULE ${UnityTestQML_SOURCES})
19add_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 @@
1/*
1/*
2− * Copyright (C) 2012, 2013 Canonical, Ltd.
2+ * Copyright (C) 2012, 2013, 2015 Canonical, Ltd.
3 *
3 *
4 * This program is free software; you can redistribute it and/or modify
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
5 * it under the terms of the GNU General Public License as published by
@@ -17,20 +17,33 @@
17
17
18#include "plugin.h"
18#include "plugin.h"
19#include "testutil.h"
19#include "testutil.h"
20+#include <MouseTouchAdaptor.h>
20#include "TouchEventSequenceWrapper.h"
21#include "TouchEventSequenceWrapper.h"
21
22
22#include <qqml.h>
23#include <qqml.h>
23
24
25+namespace {
24−static QObject *testutil_provider(QQmlEngine* /* engine */, QJSEngine* /* scriptEngine */)
26+QObject *testutil_provider(QQmlEngine* /* engine */, QJSEngine* /* scriptEngine */)
25{
27{
26 return new TestUtil();
28 return new TestUtil();
27}
29}
30+QObject *getMouseTouchAdaptorQMLSingleton(QQmlEngine* /* engine */, QJSEngine* /* scriptEngine */)
31+{
32+ return MouseTouchAdaptor::instance();
33+}
34+} // anonymous namespace
28
35
29void UnityTestPlugin::registerTypes(const char *uri)
36void UnityTestPlugin::registerTypes(const char *uri)
30{
37{
31 Q_ASSERT(QLatin1String(uri) == QLatin1String("Unity.Test"));
38 Q_ASSERT(QLatin1String(uri) == QLatin1String("Unity.Test"));
39+
40+ // Ensure the instance gets created
41+ MouseTouchAdaptor::instance();
42+
32 // @uri Unity.Test
43 // @uri Unity.Test
33 qmlRegisterSingletonType<TestUtil>(uri, 0, 1, "Util", testutil_provider);
44 qmlRegisterSingletonType<TestUtil>(uri, 0, 1, "Util", testutil_provider);
34 qmlRegisterUncreatableType<TouchEventSequenceWrapper>(uri, 0, 1, "TouchEventSequence",
45 qmlRegisterUncreatableType<TouchEventSequenceWrapper>(uri, 0, 1, "TouchEventSequence",
35 "You cannot directly create a TouchEventSequence object.");
46 "You cannot directly create a TouchEventSequence object.");
47+ qmlRegisterSingletonType<MouseTouchAdaptor>(uri, 0, 1, "MouseTouchAdaptor",
48+ getMouseTouchAdaptorQMLSingleton);
36}
49}
tests/utils/modules/Unity/Test/qmldir+1
@@ -3,3 +3,4 @@ plugin UnityTestQml
3typeinfo Test.qmltypes
3typeinfo Test.qmltypes
4
4
5UnityTestCase 0.1 UnityTestCase.qml
5UnityTestCase 0.1 UnityTestCase.qml
6+MouseTouchEmulationCheckbox 0.1 MouseTouchEmulationCheckbox.qml