on xenial help sign in
Make Ubuntu.Gestures components install TouchRegistry by themselves
c5d926cDaniel d'Andrada
libs/UbuntuGestures/CandidateInactivityTimer.cpp+7−2
@@ -19,12 +19,12 @@
19namespace UbuntuGestures {
19namespace UbuntuGestures {
20
20
21CandidateInactivityTimer::CandidateInactivityTimer(int touchId, QQuickItem *candidate,
21CandidateInactivityTimer::CandidateInactivityTimer(int touchId, QQuickItem *candidate,
22− AbstractTimerFactory &timerFactory, QObject *parent)
22+ AbstractTimer *timer, QObject *parent)
23 : QObject(parent)
23 : QObject(parent)
24+ , m_timer(timer)
24 , m_touchId(touchId)
25 , m_touchId(touchId)
25 , m_candidate(candidate)
26 , m_candidate(candidate)
26{
27{
27− m_timer = timerFactory.createTimer(this);
28 connect(m_timer, &AbstractTimer::timeout,
28 connect(m_timer, &AbstractTimer::timeout,
29 this, &CandidateInactivityTimer::onTimeout);
29 this, &CandidateInactivityTimer::onTimeout);
30 m_timer->setInterval(durationMs);
30 m_timer->setInterval(durationMs);
@@ -32,6 +32,11 @@ CandidateInactivityTimer::CandidateInactivityTimer(int touchId, QQuickItem *cand
32 m_timer->start();
32 m_timer->start();
33}
33}
34
34
35+CandidateInactivityTimer::~CandidateInactivityTimer()
36+{
37+ delete m_timer;
38+}
39+
35void CandidateInactivityTimer::onTimeout()
40void CandidateInactivityTimer::onTimeout()
36{
41{
37 qWarning("[TouchRegistry] Candidate for touch %d defaulted!", m_touchId);
42 qWarning("[TouchRegistry] Candidate for touch %d defaulted!", m_touchId);
libs/UbuntuGestures/CandidateInactivityTimer.h+3−1
@@ -29,9 +29,11 @@ class UBUNTUGESTURES_EXPORT CandidateInactivityTimer : public QObject {
29 Q_OBJECT
29 Q_OBJECT
30public:
30public:
31 CandidateInactivityTimer(int touchId, QQuickItem *candidate,
31 CandidateInactivityTimer(int touchId, QQuickItem *candidate,
32− AbstractTimerFactory &timerFactory,
32+ AbstractTimer *timer,
33 QObject *parent = nullptr);
33 QObject *parent = nullptr);
34
34
35+ virtual ~CandidateInactivityTimer();
36+
35 const int durationMs = 1000;
37 const int durationMs = 1000;
36
38
37Q_SIGNALS:
39Q_SIGNALS:
libs/UbuntuGestures/TouchRegistry.cpp+16−12
@@ -41,20 +41,10 @@ using namespace UbuntuGestures;
41TouchRegistry *TouchRegistry::m_instance = nullptr;
41TouchRegistry *TouchRegistry::m_instance = nullptr;
42
42
43TouchRegistry::TouchRegistry(QObject *parent)
43TouchRegistry::TouchRegistry(QObject *parent)
44− : TouchRegistry(parent, new TimerFactory)
45−{
46−}
47−
48−TouchRegistry::TouchRegistry(QObject *parent, AbstractTimerFactory *timerFactory)
49 : QObject(parent)
44 : QObject(parent)
50 , m_inDispatchLoop(false)
45 , m_inDispatchLoop(false)
51− , m_timerFactory(timerFactory)
46+ , m_timerFactory(new TimerFactory)
52{
47{
53− if (m_instance == nullptr) {
54− m_instance = this;
55− } else {
56− qFatal("Cannot have more than one instance of TouchRegistry. It must be a singleton.");
57− }
58}
48}
59
49
60TouchRegistry::~TouchRegistry()
50TouchRegistry::~TouchRegistry()
@@ -64,6 +54,20 @@ TouchRegistry::~TouchRegistry()
64 delete m_timerFactory;
54 delete m_timerFactory;
65}
55}
66
56
57+TouchRegistry *TouchRegistry::instance()
58+{
59+ if (m_instance == nullptr) {
60+ m_instance = new TouchRegistry;
61+ }
62+ return m_instance;
63+}
64+
65+void TouchRegistry::setTimerFactory(AbstractTimerFactory *timerFactory)
66+{
67+ delete m_timerFactory;
68+ m_timerFactory = timerFactory;
69+}
70+
67void TouchRegistry::update(const QTouchEvent *event)
71void TouchRegistry::update(const QTouchEvent *event)
68{
72{
69 #if TOUCHREGISTRY_DEBUG
73 #if TOUCHREGISTRY_DEBUG
@@ -261,7 +265,7 @@ void TouchRegistry::addCandidateOwnerForTouch(int id, QQuickItem *candidate)
261 candidateInfo.state = CandidateInfo::Undecided;
265 candidateInfo.state = CandidateInfo::Undecided;
262 candidateInfo.item = candidate;
266 candidateInfo.item = candidate;
263 candidateInfo.inactivityTimer = new CandidateInactivityTimer(id, candidate,
267 candidateInfo.inactivityTimer = new CandidateInactivityTimer(id, candidate,
264− *m_timerFactory,
268+ m_timerFactory->createTimer(),
265 this);
269 this);
266 connect(candidateInfo.inactivityTimer, &CandidateInactivityTimer::candidateDefaulted,
270 connect(candidateInfo.inactivityTimer, &CandidateInactivityTimer::candidateDefaulted,
267 this, &TouchRegistry::rejectCandidateOwnerForTouch);
271 this, &TouchRegistry::rejectCandidateOwnerForTouch);
libs/UbuntuGestures/TouchRegistry.h+8−6
@@ -25,6 +25,7 @@
25
25
26#include "UbuntuGesturesGlobal.h"
26#include "UbuntuGesturesGlobal.h"
27#include "CandidateInactivityTimer.h"
27#include "CandidateInactivityTimer.h"
28+#include "Timer.h"
28#include "Pool.h"
29#include "Pool.h"
29
30
30namespace UbuntuGestures {
31namespace UbuntuGestures {
@@ -93,15 +94,10 @@ class UBUNTUGESTURES_EXPORT TouchRegistry : public QObject
93{
94{
94 Q_OBJECT
95 Q_OBJECT
95public:
96public:
96− TouchRegistry(QObject *parent = nullptr);
97− // Useful for tests, where you should feed a fake timer
98− TouchRegistry(QObject *parent, UbuntuGestures::AbstractTimerFactory *timerFactory);
99−
100 virtual ~TouchRegistry();
97 virtual ~TouchRegistry();
101
98
102 // Returns a pointer to the application's TouchRegistry instance.
99 // Returns a pointer to the application's TouchRegistry instance.
103− // If no instance has been allocated, null is returned.
104− static TouchRegistry *instance() { return m_instance; }
100+ static TouchRegistry *instance();
105
101
106 void update(const QTouchEvent *event);
102 void update(const QTouchEvent *event);
107
103
@@ -128,10 +124,16 @@ public:
128 // but would nonetheless like to be kept up-to-date on its state.
124 // but would nonetheless like to be kept up-to-date on its state.
129 void addTouchWatcher(int touchId, QQuickItem *watcherItem);
125 void addTouchWatcher(int touchId, QQuickItem *watcherItem);
130
126
127+ // Useful for tests, where you should use fake timers
128+ void setTimerFactory(UbuntuGestures::AbstractTimerFactory *timerFactory);
129+
131private Q_SLOTS:
130private Q_SLOTS:
132 void rejectCandidateOwnerForTouch(int id, QQuickItem *candidate);
131 void rejectCandidateOwnerForTouch(int id, QQuickItem *candidate);
133
132
134private:
133private:
134+ // Only instance() can cronstruct one
135+ TouchRegistry(QObject *parent = nullptr);
136+
135 class CandidateInfo {
137 class CandidateInfo {
136 public:
138 public:
137 enum {
139 enum {
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+2
@@ -712,6 +712,8 @@ void DirectionalDragArea::itemChange(ItemChange change, const ItemChangeData &va
712{
712{
713 if (change == QQuickItem::ItemSceneChange) {
713 if (change == QQuickItem::ItemSceneChange) {
714 if (value.window != nullptr) {
714 if (value.window != nullptr) {
715+ value.window->installEventFilter(TouchRegistry::instance());
716+
715 // TODO: Handle window->screen() changes (ie window changing screens)
717 // TODO: Handle window->screen() changes (ie window changing screens)
716 qreal pixelsPerMm = value.window->screen()->physicalDotsPerInch() / 25.4;
718 qreal pixelsPerMm = value.window->screen()->physicalDotsPerInch() / 25.4;
717 d->setPixelsPerMm(pixelsPerMm);
719 d->setPixelsPerMm(pixelsPerMm);
plugins/Ubuntu/Gestures/TouchGate.cpp+10
@@ -18,6 +18,7 @@
18
18
19#include <QCoreApplication>
19#include <QCoreApplication>
20#include <QDebug>
20#include <QDebug>
21+#include <QQuickWindow>
21
22
22#include <TouchOwnershipEvent.h>
23#include <TouchOwnershipEvent.h>
23#include <TouchRegistry.h>
24#include <TouchRegistry.h>
@@ -102,6 +103,15 @@ void TouchGate::touchEvent(QTouchEvent *event)
102 }
103 }
103}
104}
104
105
106+void TouchGate::itemChange(ItemChange change, const ItemChangeData &value)
107+{
108+ if (change == QQuickItem::ItemSceneChange) {
109+ if (value.window != nullptr) {
110+ value.window->installEventFilter(TouchRegistry::instance());
111+ }
112+ }
113+}
114+
105void TouchGate::touchOwnershipEvent(TouchOwnershipEvent *event)
115void TouchGate::touchOwnershipEvent(TouchOwnershipEvent *event)
106{
116{
107 // TODO: Optimization: batch those actions as TouchOwnershipEvents
117 // TODO: Optimization: batch those actions as TouchOwnershipEvents
plugins/Ubuntu/Gestures/TouchGate.h+1
@@ -57,6 +57,7 @@ Q_SIGNALS:
57
57
58protected:
58protected:
59 void touchEvent(QTouchEvent *event) override;
59 void touchEvent(QTouchEvent *event) override;
60+ void itemChange(ItemChange change, const ItemChangeData &value) override;
60
61
61private Q_SLOTS:
62private Q_SLOTS:
62 void onEnabledChanged();
63 void onEnabledChanged();
src/Dash/main.cpp−6
@@ -31,9 +31,6 @@
31#include "../ApplicationArguments.h"
31#include "../ApplicationArguments.h"
32#include "../CachingNetworkManagerFactory.h"
32#include "../CachingNetworkManagerFactory.h"
33
33
34−// Ubuntu Gestures
35−#include <TouchRegistry.h>
36−
37int main(int argc, const char *argv[])
34int main(int argc, const char *argv[])
38{
35{
39 QGuiApplication *application = new QGuiApplication(argc, (char**)argv);
36 QGuiApplication *application = new QGuiApplication(argc, (char**)argv);
@@ -91,9 +88,6 @@ int main(int argc, const char *argv[])
91 view->setTitle("Scopes");
88 view->setTitle("Scopes");
92 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
89 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
93
90
94− TouchRegistry touchRegistry;
95− view->installEventFilter(&touchRegistry);
96−
97 // You will need this if you want to interact with touch-only components using a mouse
91 // You will need this if you want to interact with touch-only components using a mouse
98 // Needed only when manually testing on a desktop.
92 // Needed only when manually testing on a desktop.
99 MouseTouchAdaptor *mouseTouchAdaptor = 0;
93 MouseTouchAdaptor *mouseTouchAdaptor = 0;
src/main.cpp−5
@@ -31,9 +31,6 @@
31#include "ApplicationArguments.h"
31#include "ApplicationArguments.h"
32#include "CachingNetworkManagerFactory.h"
32#include "CachingNetworkManagerFactory.h"
33
33
34−// Ubuntu Gestures
35−#include <TouchRegistry.h>
36−
37int main(int argc, const char *argv[])
34int main(int argc, const char *argv[])
38{
35{
39 bool isMirServer = false;
36 bool isMirServer = false;
@@ -146,8 +143,6 @@ Load the testability driver");
146 if (parser.isSet(framelessOption)) {
143 if (parser.isSet(framelessOption)) {
147 view->setFlags(Qt::FramelessWindowHint);
144 view->setFlags(Qt::FramelessWindowHint);
148 }
145 }
149− TouchRegistry touchRegistry;
150− view->installEventFilter(&touchRegistry);
151
146
152 // You will need this if you want to interact with touch-only components using a mouse
147 // You will need this if you want to interact with touch-only components using a mouse
153 // Needed only when manually testing on a desktop.
148 // Needed only when manually testing on a desktop.
tests/libs/UbuntuGestures/tst_TouchRegistry.cpp+4−2
@@ -712,7 +712,8 @@ void tst_TouchRegistry::rejectingTouchfterItsEnd()
712void tst_TouchRegistry::removeOldUndecidedCandidates()
712void tst_TouchRegistry::removeOldUndecidedCandidates()
713{
713{
714 FakeTimerFactory *fakeTimerFactory = new FakeTimerFactory;
714 FakeTimerFactory *fakeTimerFactory = new FakeTimerFactory;
715− TouchRegistry *touchRegistry = new TouchRegistry(nullptr, fakeTimerFactory);
715+ TouchRegistry *touchRegistry = new TouchRegistry;
716+ touchRegistry->setTimerFactory(fakeTimerFactory);
716
717
717 DummyCandidate undecidedCandidate;
718 DummyCandidate undecidedCandidate;
718 undecidedCandidate.setObjectName("undecided");
719 undecidedCandidate.setObjectName("undecided");
@@ -762,7 +763,8 @@ void tst_TouchRegistry::removeOldUndecidedCandidates()
762void tst_TouchRegistry::interimOwnerWontGetUnownedTouchEvents()
763void tst_TouchRegistry::interimOwnerWontGetUnownedTouchEvents()
763{
764{
764 FakeTimerFactory *fakeTimerFactory = new FakeTimerFactory;
765 FakeTimerFactory *fakeTimerFactory = new FakeTimerFactory;
765− TouchRegistry *touchRegistry = new TouchRegistry(nullptr, fakeTimerFactory);
766+ TouchRegistry *touchRegistry = new TouchRegistry;
767+ touchRegistry->setTimerFactory(fakeTimerFactory);
766
768
767 DummyCandidate undecidedCandidate;
769 DummyCandidate undecidedCandidate;
768 undecidedCandidate.setObjectName("undecided");
770 undecidedCandidate.setObjectName("undecided");
tests/plugins/Ubuntu/Gestures/GestureTest.cpp+2−1
@@ -53,7 +53,8 @@ void GestureTest::init()
53
53
54 m_fakeTimerFactory = new FakeTimerFactory;
54 m_fakeTimerFactory = new FakeTimerFactory;
55
55
56− m_touchRegistry = new TouchRegistry(nullptr, m_fakeTimerFactory);
56+ m_touchRegistry = TouchRegistry::instance();
57+ m_touchRegistry->setTimerFactory(m_fakeTimerFactory);
57 m_view->installEventFilter(m_touchRegistry);
58 m_view->installEventFilter(m_touchRegistry);
58
59
59 qApp->processEvents();
60 qApp->processEvents();
tests/plugins/Ubuntu/Gestures/tst_TouchGate.cpp+1−2
@@ -104,10 +104,9 @@ void tst_TouchGate::cleanupTestCase()
104
104
105void tst_TouchGate::init()
105void tst_TouchGate::init()
106{
106{
107− touchRegistry = new TouchRegistry;
107+ touchRegistry = TouchRegistry::instance();
108
108
109 view = createView();
109 view = createView();
110− view->installEventFilter(touchRegistry);
111 view->setSource(QUrl::fromLocalFile("touchGateExample.qml"));
110 view->setSource(QUrl::fromLocalFile("touchGateExample.qml"));
112 view->show();
111 view->show();
113 QVERIFY(QTest::qWaitForWindowExposed(view));
112 QVERIFY(QTest::qWaitForWindowExposed(view));
tests/qmltests/Components/tst_DragHandle.cpp−8
@@ -26,7 +26,6 @@
26#include <DirectionalDragArea.h>
26#include <DirectionalDragArea.h>
27#include <DirectionalDragArea_p.h>
27#include <DirectionalDragArea_p.h>
28#include <Timer.h>
28#include <Timer.h>
29−#include <TouchRegistry.h>
30
29
31using namespace UbuntuGestures;
30using namespace UbuntuGestures;
32
31
@@ -61,7 +60,6 @@ private:
61
60
62 QQuickView *createView();
61 QQuickView *createView();
63 QQuickView *m_view;
62 QQuickView *m_view;
64− TouchRegistry *m_touchRegistry;
65 QTouchDevice *m_device;
63 QTouchDevice *m_device;
66 FakeTimer *m_fakeTimer;
64 FakeTimer *m_fakeTimer;
67 QSharedPointer<FakeTimeSource> m_fakeTimeSource;
65 QSharedPointer<FakeTimeSource> m_fakeTimeSource;
@@ -85,10 +83,7 @@ void tst_DragHandle::cleanupTestCase()
85
83
86void tst_DragHandle::init()
84void tst_DragHandle::init()
87{
85{
88− m_touchRegistry = new TouchRegistry;
89−
90 m_view = createView();
86 m_view = createView();
91− m_view->installEventFilter(m_touchRegistry);
92 m_view->setSource(QUrl::fromLocalFile(TEST_DIR"/tst_DragHandle.qml"));
87 m_view->setSource(QUrl::fromLocalFile(TEST_DIR"/tst_DragHandle.qml"));
93 m_view->show();
88 m_view->show();
94 QVERIFY(QTest::qWaitForWindowExposed(m_view));
89 QVERIFY(QTest::qWaitForWindowExposed(m_view));
@@ -108,9 +103,6 @@ void tst_DragHandle::cleanup()
108 delete m_view;
103 delete m_view;
109 m_view = 0;
104 m_view = 0;
110
105
111− delete m_touchRegistry;
112− m_touchRegistry = 0;
113−
114 delete m_fakeTimer;
106 delete m_fakeTimer;
115 m_fakeTimer = 0;
107 m_fakeTimer = 0;
116
108
tests/uqmlscene/main.cpp−10
@@ -63,9 +63,6 @@
63#include <QtCore/QTranslator>
63#include <QtCore/QTranslator>
64#include <QtCore/QLibraryInfo>
64#include <QtCore/QLibraryInfo>
65
65
66−// UbuntuGestures lib
67−#include <TouchRegistry.h>
68−
69#define UQMLSCENE_DEBUG_ACTIVE_FOCUS 0
66#define UQMLSCENE_DEBUG_ACTIVE_FOCUS 0
70
67
71#if UQMLSCENE_DEBUG_ACTIVE_FOCUS
68#if UQMLSCENE_DEBUG_ACTIVE_FOCUS
@@ -434,11 +431,6 @@ int main(int argc, char ** argv)
434 app.setOrganizationName("Qt Project");
431 app.setOrganizationName("Qt Project");
435 app.setOrganizationDomain("qt-project.org");
432 app.setOrganizationDomain("qt-project.org");
436
433
437− // Create it before loading the module, so that TestUtil
438− // doesn't create one with a fake timer factory.
439− // When interacting manually with a test we want the real deal.
440− new TouchRegistry;
441−
442#ifndef QT_NO_TRANSLATION
434#ifndef QT_NO_TRANSLATION
443 QTranslator translator;
435 QTranslator translator;
444 QTranslator qtTranslator;
436 QTranslator qtTranslator;
@@ -512,8 +504,6 @@ int main(int argc, char ** argv)
512 #if UQMLSCENE_DEBUG_ACTIVE_FOCUS
504 #if UQMLSCENE_DEBUG_ACTIVE_FOCUS
513 activeFocusLogger.setWindow(qxView);
505 activeFocusLogger.setWindow(qxView);
514 #endif
506 #endif
515− TouchRegistry::instance()->setParent(qxView);
516− qxView->installEventFilter(TouchRegistry::instance());
517 window = qxView;
507 window = qxView;
518 // Set window default properties; the qml can still override them
508 // Set window default properties; the qml can still override them
519 QString oname = contentItem->objectName();
509 QString oname = contentItem->objectName();
tests/utils/modules/Unity/Test/UnityTestCase.qml−2
@@ -382,8 +382,6 @@ TestCase {
382 }
382 }
383
383
384 Component.onCompleted: {
384 Component.onCompleted: {
385− UT.Util.ensureTouchRegistryInstalled();
386−
387 var rootItem = parent;
385 var rootItem = parent;
388 while (rootItem.parent != undefined) {
386 while (rootItem.parent != undefined) {
389 rootItem = rootItem.parent;
387 rootItem = rootItem.parent;
tests/utils/modules/Unity/Test/testutil.cpp+8−20
@@ -31,6 +31,7 @@ TestUtil::TestUtil(QObject *parent)
31 : QObject(parent)
31 : QObject(parent)
32 , m_targetWindow(0)
32 , m_targetWindow(0)
33 , m_touchDevice(0)
33 , m_touchDevice(0)
34+ , m_putFakeTimerFactoryInTouchRegistry(false)
34{
35{
35}
36}
36
37
@@ -59,7 +60,13 @@ TouchEventSequenceWrapper *TestUtil::touchEvent(QQuickItem *item)
59{
60{
60 ensureTargetWindow();
61 ensureTargetWindow();
61 ensureTouchDevice();
62 ensureTouchDevice();
62− ensureTouchRegistryInstalled();
63+
64+ // Tests can be *very* slow to run and we don't want things timing out because
65+ // of that. So give it fake timers to use (they will never time out)
66+ if (!m_putFakeTimerFactoryInTouchRegistry) {
67+ TouchRegistry::instance()->setTimerFactory(new FakeTimerFactory);
68+ m_putFakeTimerFactoryInTouchRegistry = true;
69+ }
63
70
64 return new TouchEventSequenceWrapper(
71 return new TouchEventSequenceWrapper(
65 QTest::touchEvent(m_targetWindow, m_touchDevice, /* autoCommit */ false), item);
72 QTest::touchEvent(m_targetWindow, m_touchDevice, /* autoCommit */ false), item);
@@ -79,22 +86,3 @@ void TestUtil::ensureTouchDevice()
79 QWindowSystemInterface::registerTouchDevice(m_touchDevice);
86 QWindowSystemInterface::registerTouchDevice(m_touchDevice);
80 }
87 }
81}
88}
82−
83−void TestUtil::ensureTouchRegistryInstalled()
84−{
85− if (TouchRegistry::instance())
86− return;
87−
88− if (!m_targetWindow)
89− return;
90−
91− // Tests can be *very* slow to run and we don't want things timing out because
92− // of that. So give it fake timers to use (they will never time out)
93− TouchRegistry *touchRegistry = new TouchRegistry(this, new FakeTimerFactory);
94−
95− QQuickView *view = qobject_cast<QQuickView*>(m_targetWindow);
96− if (view) {
97− view->installEventFilter(touchRegistry);
98− touchRegistry->setParent(view);
99− }
100−}
tests/utils/modules/Unity/Test/testutil.h+1−1
@@ -34,7 +34,6 @@ public:
34
34
35 Q_INVOKABLE bool isInstanceOf(QObject*, QString);
35 Q_INVOKABLE bool isInstanceOf(QObject*, QString);
36 Q_INVOKABLE TouchEventSequenceWrapper *touchEvent(QQuickItem *item);
36 Q_INVOKABLE TouchEventSequenceWrapper *touchEvent(QQuickItem *item);
37− Q_INVOKABLE void ensureTouchRegistryInstalled();
38
37
39private:
38private:
40 void ensureTargetWindow();
39 void ensureTargetWindow();
@@ -42,6 +41,7 @@ private:
42
41
43 QWindow *m_targetWindow;
42 QWindow *m_targetWindow;
44 QTouchDevice *m_touchDevice;
43 QTouchDevice *m_touchDevice;
44+ bool m_putFakeTimerFactoryInTouchRegistry;
45};
45};
46
46
47QML_DECLARE_TYPE(TestUtil)
47QML_DECLARE_TYPE(TestUtil)