on xenial help sign in
Improve code and tests
commitc75a8bc3c8bda5b51392bb551a46662d9635734b authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateThu 12 Mar 2015, 11:37 (12 years ago) parent1600408
4 files changed+32−23
plugins/Utils/inputwatcher.cpp+7−7 plugins/Utils/inputwatcher.h+5−3 qml/Stages/SurfaceContainer.qml+7−8 tests/qmltests/Stages/tst_SurfaceContainer.qml+13−5
plugins/Utils/inputwatcher.cpp+7−7
@@ -48,7 +48,7 @@ void InputWatcher::setTarget(QObject *value)
4848 m_target->installEventFilter(this);
4949}
5050
51−bool InputWatcher::pressed() const
51+bool InputWatcher::targetPressed() const
5252{
5353 return m_mousePressed || m_touchPressed;
5454}
@@ -93,10 +93,10 @@ void InputWatcher::setMousePressed(bool value)
9393 return;
9494 }
9595
96− bool oldPressed = pressed();
96+ bool oldPressed = targetPressed();
9797 m_mousePressed = value;
98− if (pressed() != oldPressed) {
99− Q_EMIT pressedChanged(pressed());
98+ if (targetPressed() != oldPressed) {
99+ Q_EMIT targetPressedChanged(targetPressed());
100100 }
101101}
102102
@@ -106,9 +106,9 @@ void InputWatcher::setTouchPressed(bool value)
106106 return;
107107 }
108108
109− bool oldPressed = pressed();
109+ bool oldPressed = targetPressed();
110110 m_touchPressed = value;
111− if (pressed() != oldPressed) {
112− Q_EMIT pressedChanged(pressed());
111+ if (targetPressed() != oldPressed) {
112+ Q_EMIT targetPressedChanged(targetPressed());
113113 }
114114}
plugins/Utils/inputwatcher.h+5−3
@@ -29,20 +29,22 @@ class InputWatcher : public QObject
2929{
3030 Q_OBJECT
3131 Q_PROPERTY(QObject* target READ target WRITE setTarget NOTIFY targetChanged)
32− Q_PROPERTY(bool pressed READ pressed NOTIFY pressedChanged)
32+
33+ // Whether the target object is pressed (by either touch or mouse)
34+ Q_PROPERTY(bool targetPressed READ targetPressed NOTIFY targetPressedChanged)
3335public:
3436 InputWatcher(QObject *parent = nullptr);
3537
3638 QObject *target() const;
3739 void setTarget(QObject *value);
3840
39− bool pressed() const;
41+ bool targetPressed() const;
4042
4143 bool eventFilter(QObject *watched, QEvent *event) override;
4244
4345Q_SIGNALS:
4446 void targetChanged(QObject *value);
45− void pressedChanged(bool value);
47+ void targetPressedChanged(bool value);
4648
4749private:
4850 void setMousePressed(bool value);
qml/Stages/SurfaceContainer.qml+7−8
@@ -1,5 +1,5 @@
11/*
2− * Copyright 2014 Canonical Ltd.
2+ * Copyright 2014-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 Lesser General Public License as published by
@@ -27,8 +27,6 @@ FocusScope {
2727 property int orientation
2828 property bool interactive
2929
30− signal surfacePressed()
31−
3230 onSurfaceChanged: {
3331 if (surface) {
3432 // Set the surface focus *after* it is added to the scene to
@@ -48,12 +46,13 @@ FocusScope {
4846
4947 InputWatcher {
5048 target: root.surface
51− onPressedChanged: {
52− if (pressed) {
49+ onTargetPressedChanged: {
50+ if (targetPressed && root.interactive) {
5351 root.focus = true;
54− if (root.interactive) {
55− root.forceActiveFocus();
56− }
52+ // The command above should suffice, but in case any parent FocusScope
53+ // is not focused for some reason (although it should), we ensure they're
54+ // all focused for good measure.
55+ root.forceActiveFocus();
5756 }
5857 }
5958 }
tests/qmltests/Stages/tst_SurfaceContainer.qml+13−5
@@ -1,5 +1,5 @@
11/*
2− * Copyright 2014 Canonical Ltd.
2+ * Copyright 2014-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
@@ -16,7 +16,7 @@
1616
1717import QtQuick 2.0
1818import QtTest 1.0
19−import Unity.Test 0.1 as UT
19+import Unity.Test 0.1
2020import ".."
2121import "../../../qml/Stages"
2222import Ubuntu.Components 0.1
@@ -32,6 +32,8 @@ Rectangle {
3232 id: surfaceContainerComponent
3333 SurfaceContainer {
3434 anchors.fill: parent
35+ orientation: Qt.PortraitOrientation
36+ interactive: interactiveCheckbox.checked
3537 }
3638 }
3739 Loader {
@@ -85,6 +87,11 @@ Rectangle {
8587 CheckBox {id: fullscreenCheckbox; checked: true; }
8688 Label { text: "fullscreen" }
8789 }
90+ Row {
91+ anchors { left: parent.left; right: parent.right }
92+ CheckBox {id: interactiveCheckbox; checked: true; }
93+ Label { text: "interactive" }
94+ }
8895 }
8996 }
9097
@@ -94,7 +101,7 @@ Rectangle {
94101 signalName: "surfaceDestroyed"
95102 }
96103
97− UT.UnityTestCase {
104+ UnityTestCase {
98105 id: testCase
99106 name: "SurfaceContainer"
100107 when: windowShown
@@ -110,6 +117,8 @@ Rectangle {
110117
111118 tryCompare(surfaceContainerLoader.item, "surface", null);
112119 surfaceSpy.clear();
120+
121+ interactiveCheckbox.checked = true;
113122 }
114123
115124 /*
@@ -145,14 +154,13 @@ Rectangle {
145154 surfaceContainer.surface.touchPressCount = 0;
146155 surfaceContainer.surface.touchReleaseCount = 0;
147156
148− surfaceContainer.interactive = true;
149157 tap(surfaceContainer, surfaceContainer.width / 2, surfaceContainer.height / 2);
150158
151159 // surface got touches as the surfaceContainer is interactive
152160 compare(surfaceContainer.surface.touchPressCount, 1)
153161 compare(surfaceContainer.surface.touchReleaseCount, 1);
154162
155− surfaceContainer.interactive = false;
163+ interactiveCheckbox.checked = false;
156164 tap(surfaceContainer, surfaceContainer.width / 2, surfaceContainer.height / 2);
157165
158166 // surface shouldn't get the touches from the second tap as the surfaceContainer