on xenial help sign in
Make Ubuntu.Gestures components install TouchRegistry by themselves

That way Ubuntu.Gestures users don't have to write C++ code to do it. TouchRegistry then being an implementation detail they don't have to know about.

commitc5d926c70bb3d4d7995ab0fca235035f07eac299 authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateFri 17 Apr 2015, 15:31 (11 years ago) parent3628f62
17 files changed+63−78
libs/UbuntuGestures/CandidateInactivityTimer.cpp+7−2 libs/UbuntuGestures/CandidateInactivityTimer.h+3−1 libs/UbuntuGestures/TouchRegistry.cpp+16−12 libs/UbuntuGestures/TouchRegistry.h+8−6 plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+2 plugins/Ubuntu/Gestures/TouchGate.cpp+10 plugins/Ubuntu/Gestures/TouchGate.h+1 src/Dash/main.cpp−6 src/main.cpp−5 tests/libs/UbuntuGestures/tst_TouchRegistry.cpp+4−2 tests/plugins/Ubuntu/Gestures/GestureTest.cpp+2−1 tests/plugins/Ubuntu/Gestures/tst_TouchGate.cpp+1−2 tests/qmltests/Components/tst_DragHandle.cpp−8 tests/uqmlscene/main.cpp−10 tests/utils/modules/Unity/Test/UnityTestCase.qml−2 tests/utils/modules/Unity/Test/testutil.cpp+8−20 tests/utils/modules/Unity/Test/testutil.h+1−1
libs/UbuntuGestures/CandidateInactivityTimer.cpp+7−2
@@ -19,12 +19,12 @@
1919namespace UbuntuGestures {
2020
2121CandidateInactivityTimer::CandidateInactivityTimer(int touchId, QQuickItem *candidate,
22− AbstractTimerFactory &timerFactory, QObject *parent)
22+ AbstractTimer *timer, QObject *parent)
2323 : QObject(parent)
24+ , m_timer(timer)
2425 , m_touchId(touchId)
2526 , m_candidate(candidate)
2627{
27− m_timer = timerFactory.createTimer(this);
2828 connect(m_timer, &AbstractTimer::timeout,
2929 this, &CandidateInactivityTimer::onTimeout);
3030 m_timer->setInterval(durationMs);
@@ -32,6 +32,11 @@ CandidateInactivityTimer::CandidateInactivityTimer(int touchId, QQuickItem *cand
3232 m_timer->start();
3333}
3434
35+CandidateInactivityTimer::~CandidateInactivityTimer()
36+{
37+ delete m_timer;
38+}
39+
3540void CandidateInactivityTimer::onTimeout()
3641{
3742 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 {
2929 Q_OBJECT
3030public:
3131 CandidateInactivityTimer(int touchId, QQuickItem *candidate,
32− AbstractTimerFactory &timerFactory,
32+ AbstractTimer *timer,
3333 QObject *parent = nullptr);
3434
35+ virtual ~CandidateInactivityTimer();
36+
3537 const int durationMs = 1000;
3638
3739Q_SIGNALS:
libs/UbuntuGestures/TouchRegistry.cpp+16−12
@@ -41,20 +41,10 @@ using namespace UbuntuGestures;
4141TouchRegistry *TouchRegistry::m_instance = nullptr;
4242
4343TouchRegistry::TouchRegistry(QObject *parent)
44− : TouchRegistry(parent, new TimerFactory)
45−{
46−}
47−
48−TouchRegistry::TouchRegistry(QObject *parent, AbstractTimerFactory *timerFactory)
4944 : QObject(parent)
5045 , m_inDispatchLoop(false)
51− , m_timerFactory(timerFactory)
46+ , m_timerFactory(new TimerFactory)
5247{
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− }
5848}
5949
6050TouchRegistry::~TouchRegistry()
@@ -64,6 +54,20 @@ TouchRegistry::~TouchRegistry()
6454 delete m_timerFactory;
6555}
6656
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+
6771void TouchRegistry::update(const QTouchEvent *event)
6872{
6973 #if TOUCHREGISTRY_DEBUG
@@ -261,7 +265,7 @@ void TouchRegistry::addCandidateOwnerForTouch(int id, QQuickItem *candidate)
261265 candidateInfo.state = CandidateInfo::Undecided;
262266 candidateInfo.item = candidate;
263267 candidateInfo.inactivityTimer = new CandidateInactivityTimer(id, candidate,
264− *m_timerFactory,
268+ m_timerFactory->createTimer(),
265269 this);
266270 connect(candidateInfo.inactivityTimer, &CandidateInactivityTimer::candidateDefaulted,
267271 this, &TouchRegistry::rejectCandidateOwnerForTouch);
libs/UbuntuGestures/TouchRegistry.h+8−6
@@ -25,6 +25,7 @@
2525
2626#include "UbuntuGesturesGlobal.h"
2727#include "CandidateInactivityTimer.h"
28+#include "Timer.h"
2829#include "Pool.h"
2930
3031namespace UbuntuGestures {
@@ -93,15 +94,10 @@ class UBUNTUGESTURES_EXPORT TouchRegistry : public QObject
9394{
9495 Q_OBJECT
9596public:
96− TouchRegistry(QObject *parent = nullptr);
97− // Useful for tests, where you should feed a fake timer
98− TouchRegistry(QObject *parent, UbuntuGestures::AbstractTimerFactory *timerFactory);
99−
10097 virtual ~TouchRegistry();
10198
10299 // 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();
105101
106102 void update(const QTouchEvent *event);
107103
@@ -128,10 +124,16 @@ public:
128124 // but would nonetheless like to be kept up-to-date on its state.
129125 void addTouchWatcher(int touchId, QQuickItem *watcherItem);
130126
127+ // Useful for tests, where you should use fake timers
128+ void setTimerFactory(UbuntuGestures::AbstractTimerFactory *timerFactory);
129+
131130private Q_SLOTS:
132131 void rejectCandidateOwnerForTouch(int id, QQuickItem *candidate);
133132
134133private:
134+ // Only instance() can cronstruct one
135+ TouchRegistry(QObject *parent = nullptr);
136+
135137 class CandidateInfo {
136138 public:
137139 enum {
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+2
@@ -712,6 +712,8 @@ void DirectionalDragArea::itemChange(ItemChange change, const ItemChangeData &va
712712{
713713 if (change == QQuickItem::ItemSceneChange) {
714714 if (value.window != nullptr) {
715+ value.window->installEventFilter(TouchRegistry::instance());
716+
715717 // TODO: Handle window->screen() changes (ie window changing screens)
716718 qreal pixelsPerMm = value.window->screen()->physicalDotsPerInch() / 25.4;
717719 d->setPixelsPerMm(pixelsPerMm);
plugins/Ubuntu/Gestures/TouchGate.cpp+10
@@ -18,6 +18,7 @@
1818
1919#include <QCoreApplication>
2020#include <QDebug>
21+#include <QQuickWindow>
2122
2223#include <TouchOwnershipEvent.h>
2324#include <TouchRegistry.h>
@@ -102,6 +103,15 @@ void TouchGate::touchEvent(QTouchEvent *event)
102103 }
103104}
104105
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+
105115void TouchGate::touchOwnershipEvent(TouchOwnershipEvent *event)
106116{
107117 // TODO: Optimization: batch those actions as TouchOwnershipEvents
plugins/Ubuntu/Gestures/TouchGate.h+1
@@ -57,6 +57,7 @@ Q_SIGNALS:
5757
5858protected:
5959 void touchEvent(QTouchEvent *event) override;
60+ void itemChange(ItemChange change, const ItemChangeData &value) override;
6061
6162private Q_SLOTS:
6263 void onEnabledChanged();
src/Dash/main.cpp−6
@@ -31,9 +31,6 @@
3131#include "../ApplicationArguments.h"
3232#include "../CachingNetworkManagerFactory.h"
3333
34−// Ubuntu Gestures
35−#include <TouchRegistry.h>
36−
3734int main(int argc, const char *argv[])
3835{
3936 QGuiApplication *application = new QGuiApplication(argc, (char**)argv);
@@ -91,9 +88,6 @@ int main(int argc, const char *argv[])
9188 view->setTitle("Scopes");
9289 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
9390
94− TouchRegistry touchRegistry;
95− view->installEventFilter(&touchRegistry);
96−
9791 // You will need this if you want to interact with touch-only components using a mouse
9892 // Needed only when manually testing on a desktop.
9993 MouseTouchAdaptor *mouseTouchAdaptor = 0;
src/main.cpp−5
@@ -31,9 +31,6 @@
3131#include "ApplicationArguments.h"
3232#include "CachingNetworkManagerFactory.h"
3333
34−// Ubuntu Gestures
35−#include <TouchRegistry.h>
36−
3734int main(int argc, const char *argv[])
3835{
3936 bool isMirServer = false;
@@ -146,8 +143,6 @@ Load the testability driver");
146143 if (parser.isSet(framelessOption)) {
147144 view->setFlags(Qt::FramelessWindowHint);
148145 }
149− TouchRegistry touchRegistry;
150− view->installEventFilter(&touchRegistry);
151146
152147 // You will need this if you want to interact with touch-only components using a mouse
153148 // Needed only when manually testing on a desktop.
tests/libs/UbuntuGestures/tst_TouchRegistry.cpp+4−2
@@ -712,7 +712,8 @@ void tst_TouchRegistry::rejectingTouchfterItsEnd()
712712void tst_TouchRegistry::removeOldUndecidedCandidates()
713713{
714714 FakeTimerFactory *fakeTimerFactory = new FakeTimerFactory;
715− TouchRegistry *touchRegistry = new TouchRegistry(nullptr, fakeTimerFactory);
715+ TouchRegistry *touchRegistry = new TouchRegistry;
716+ touchRegistry->setTimerFactory(fakeTimerFactory);
716717
717718 DummyCandidate undecidedCandidate;
718719 undecidedCandidate.setObjectName("undecided");
@@ -762,7 +763,8 @@ void tst_TouchRegistry::removeOldUndecidedCandidates()
762763void tst_TouchRegistry::interimOwnerWontGetUnownedTouchEvents()
763764{
764765 FakeTimerFactory *fakeTimerFactory = new FakeTimerFactory;
765− TouchRegistry *touchRegistry = new TouchRegistry(nullptr, fakeTimerFactory);
766+ TouchRegistry *touchRegistry = new TouchRegistry;
767+ touchRegistry->setTimerFactory(fakeTimerFactory);
766768
767769 DummyCandidate undecidedCandidate;
768770 undecidedCandidate.setObjectName("undecided");
tests/plugins/Ubuntu/Gestures/GestureTest.cpp+2−1
@@ -53,7 +53,8 @@ void GestureTest::init()
5353
5454 m_fakeTimerFactory = new FakeTimerFactory;
5555
56− m_touchRegistry = new TouchRegistry(nullptr, m_fakeTimerFactory);
56+ m_touchRegistry = TouchRegistry::instance();
57+ m_touchRegistry->setTimerFactory(m_fakeTimerFactory);
5758 m_view->installEventFilter(m_touchRegistry);
5859
5960 qApp->processEvents();
tests/plugins/Ubuntu/Gestures/tst_TouchGate.cpp+1−2
@@ -104,10 +104,9 @@ void tst_TouchGate::cleanupTestCase()
104104
105105void tst_TouchGate::init()
106106{
107− touchRegistry = new TouchRegistry;
107+ touchRegistry = TouchRegistry::instance();
108108
109109 view = createView();
110− view->installEventFilter(touchRegistry);
111110 view->setSource(QUrl::fromLocalFile("touchGateExample.qml"));
112111 view->show();
113112 QVERIFY(QTest::qWaitForWindowExposed(view));
tests/qmltests/Components/tst_DragHandle.cpp−8
@@ -26,7 +26,6 @@
2626#include <DirectionalDragArea.h>
2727#include <DirectionalDragArea_p.h>
2828#include <Timer.h>
29−#include <TouchRegistry.h>
3029
3130using namespace UbuntuGestures;
3231
@@ -61,7 +60,6 @@ private:
6160
6261 QQuickView *createView();
6362 QQuickView *m_view;
64− TouchRegistry *m_touchRegistry;
6563 QTouchDevice *m_device;
6664 FakeTimer *m_fakeTimer;
6765 QSharedPointer<FakeTimeSource> m_fakeTimeSource;
@@ -85,10 +83,7 @@ void tst_DragHandle::cleanupTestCase()
8583
8684void tst_DragHandle::init()
8785{
88− m_touchRegistry = new TouchRegistry;
89−
9086 m_view = createView();
91− m_view->installEventFilter(m_touchRegistry);
9287 m_view->setSource(QUrl::fromLocalFile(TEST_DIR"/tst_DragHandle.qml"));
9388 m_view->show();
9489 QVERIFY(QTest::qWaitForWindowExposed(m_view));
@@ -108,9 +103,6 @@ void tst_DragHandle::cleanup()
108103 delete m_view;
109104 m_view = 0;
110105
111− delete m_touchRegistry;
112− m_touchRegistry = 0;
113−
114106 delete m_fakeTimer;
115107 m_fakeTimer = 0;
116108
tests/uqmlscene/main.cpp−10
@@ -63,9 +63,6 @@
6363#include <QtCore/QTranslator>
6464#include <QtCore/QLibraryInfo>
6565
66−// UbuntuGestures lib
67−#include <TouchRegistry.h>
68−
6966#define UQMLSCENE_DEBUG_ACTIVE_FOCUS 0
7067
7168#if UQMLSCENE_DEBUG_ACTIVE_FOCUS
@@ -434,11 +431,6 @@ int main(int argc, char ** argv)
434431 app.setOrganizationName("Qt Project");
435432 app.setOrganizationDomain("qt-project.org");
436433
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−
442434#ifndef QT_NO_TRANSLATION
443435 QTranslator translator;
444436 QTranslator qtTranslator;
@@ -512,8 +504,6 @@ int main(int argc, char ** argv)
512504 #if UQMLSCENE_DEBUG_ACTIVE_FOCUS
513505 activeFocusLogger.setWindow(qxView);
514506 #endif
515− TouchRegistry::instance()->setParent(qxView);
516− qxView->installEventFilter(TouchRegistry::instance());
517507 window = qxView;
518508 // Set window default properties; the qml can still override them
519509 QString oname = contentItem->objectName();
tests/utils/modules/Unity/Test/UnityTestCase.qml−2
@@ -382,8 +382,6 @@ TestCase {
382382 }
383383
384384 Component.onCompleted: {
385− UT.Util.ensureTouchRegistryInstalled();
386−
387385 var rootItem = parent;
388386 while (rootItem.parent != undefined) {
389387 rootItem = rootItem.parent;
tests/utils/modules/Unity/Test/testutil.cpp+8−20
@@ -31,6 +31,7 @@ TestUtil::TestUtil(QObject *parent)
3131 : QObject(parent)
3232 , m_targetWindow(0)
3333 , m_touchDevice(0)
34+ , m_putFakeTimerFactoryInTouchRegistry(false)
3435{
3536}
3637
@@ -59,7 +60,13 @@ TouchEventSequenceWrapper *TestUtil::touchEvent(QQuickItem *item)
5960{
6061 ensureTargetWindow();
6162 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+ }
6370
6471 return new TouchEventSequenceWrapper(
6572 QTest::touchEvent(m_targetWindow, m_touchDevice, /* autoCommit */ false), item);
@@ -79,22 +86,3 @@ void TestUtil::ensureTouchDevice()
7986 QWindowSystemInterface::registerTouchDevice(m_touchDevice);
8087 }
8188}
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:
3434
3535 Q_INVOKABLE bool isInstanceOf(QObject*, QString);
3636 Q_INVOKABLE TouchEventSequenceWrapper *touchEvent(QQuickItem *item);
37− Q_INVOKABLE void ensureTouchRegistryInstalled();
3837
3938private:
4039 void ensureTargetWindow();
@@ -42,6 +41,7 @@ private:
4241
4342 QWindow *m_targetWindow;
4443 QTouchDevice *m_touchDevice;
44+ bool m_putFakeTimerFactoryInTouchRegistry;
4545};
4646
4747QML_DECLARE_TYPE(TestUtil)