on xenial help sign in
Improve code and tests
c75a8bcDaniel d'Andrada
plugins/Utils/inputwatcher.cpp+7−7
@@ -48,7 +48,7 @@ void InputWatcher::setTarget(QObject *value)
48 m_target->installEventFilter(this);
48 m_target->installEventFilter(this);
49}
49}
50
50
51−bool InputWatcher::pressed() const
51+bool InputWatcher::targetPressed() const
52{
52{
53 return m_mousePressed || m_touchPressed;
53 return m_mousePressed || m_touchPressed;
54}
54}
@@ -93,10 +93,10 @@ void InputWatcher::setMousePressed(bool value)
93 return;
93 return;
94 }
94 }
95
95
96− bool oldPressed = pressed();
96+ bool oldPressed = targetPressed();
97 m_mousePressed = value;
97 m_mousePressed = value;
98− if (pressed() != oldPressed) {
98+ if (targetPressed() != oldPressed) {
99− Q_EMIT pressedChanged(pressed());
99+ Q_EMIT targetPressedChanged(targetPressed());
100 }
100 }
101}
101}
102
102
@@ -106,9 +106,9 @@ void InputWatcher::setTouchPressed(bool value)
106 return;
106 return;
107 }
107 }
108
108
109− bool oldPressed = pressed();
109+ bool oldPressed = targetPressed();
110 m_touchPressed = value;
110 m_touchPressed = value;
111− if (pressed() != oldPressed) {
111+ if (targetPressed() != oldPressed) {
112− Q_EMIT pressedChanged(pressed());
112+ Q_EMIT targetPressedChanged(targetPressed());
113 }
113 }
114}
114}
plugins/Utils/inputwatcher.h+5−3
@@ -29,20 +29,22 @@ class InputWatcher : public QObject
29{
29{
30 Q_OBJECT
30 Q_OBJECT
31 Q_PROPERTY(QObject* target READ target WRITE setTarget NOTIFY targetChanged)
31 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)
33public:
35public:
34 InputWatcher(QObject *parent = nullptr);
36 InputWatcher(QObject *parent = nullptr);
35
37
36 QObject *target() const;
38 QObject *target() const;
37 void setTarget(QObject *value);
39 void setTarget(QObject *value);
38
40
39− bool pressed() const;
41+ bool targetPressed() const;
40
42
41 bool eventFilter(QObject *watched, QEvent *event) override;
43 bool eventFilter(QObject *watched, QEvent *event) override;
42
44
43Q_SIGNALS:
45Q_SIGNALS:
44 void targetChanged(QObject *value);
46 void targetChanged(QObject *value);
45− void pressedChanged(bool value);
47+ void targetPressedChanged(bool value);
46
48
47private:
49private:
48 void setMousePressed(bool value);
50 void setMousePressed(bool value);
qml/Stages/SurfaceContainer.qml+7−8
@@ -1,5 +1,5 @@
1/*
1/*
2− * Copyright 2014 Canonical Ltd.
2+ * Copyright 2014-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 Lesser General Public License as published by
5 * it under the terms of the GNU Lesser General Public License as published by
@@ -27,8 +27,6 @@ FocusScope {
27 property int orientation
27 property int orientation
28 property bool interactive
28 property bool interactive
29
29
30− signal surfacePressed()
31−
32 onSurfaceChanged: {
30 onSurfaceChanged: {
33 if (surface) {
31 if (surface) {
34 // Set the surface focus *after* it is added to the scene to
32 // Set the surface focus *after* it is added to the scene to
@@ -48,12 +46,13 @@ FocusScope {
48
46
49 InputWatcher {
47 InputWatcher {
50 target: root.surface
48 target: root.surface
51− onPressedChanged: {
49+ onTargetPressedChanged: {
52− if (pressed) {
50+ if (targetPressed && root.interactive) {
53 root.focus = true;
51 root.focus = true;
54− if (root.interactive) {
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();
55+ root.forceActiveFocus();
56− }
57 }
56 }
58 }
57 }
59 }
58 }
tests/qmltests/Stages/tst_SurfaceContainer.qml+13−5
@@ -1,5 +1,5 @@
1/*
1/*
2− * Copyright 2014 Canonical Ltd.
2+ * Copyright 2014-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
@@ -16,7 +16,7 @@
16
16
17import QtQuick 2.0
17import QtQuick 2.0
18import QtTest 1.0
18import QtTest 1.0
19−import Unity.Test 0.1 as UT
19+import Unity.Test 0.1
20import ".."
20import ".."
21import "../../../qml/Stages"
21import "../../../qml/Stages"
22import Ubuntu.Components 0.1
22import Ubuntu.Components 0.1
@@ -32,6 +32,8 @@ Rectangle {
32 id: surfaceContainerComponent
32 id: surfaceContainerComponent
33 SurfaceContainer {
33 SurfaceContainer {
34 anchors.fill: parent
34 anchors.fill: parent
35+ orientation: Qt.PortraitOrientation
36+ interactive: interactiveCheckbox.checked
35 }
37 }
36 }
38 }
37 Loader {
39 Loader {
@@ -85,6 +87,11 @@ Rectangle {
85 CheckBox {id: fullscreenCheckbox; checked: true; }
87 CheckBox {id: fullscreenCheckbox; checked: true; }
86 Label { text: "fullscreen" }
88 Label { text: "fullscreen" }
87 }
89 }
90+ Row {
91+ anchors { left: parent.left; right: parent.right }
92+ CheckBox {id: interactiveCheckbox; checked: true; }
93+ Label { text: "interactive" }
94+ }
88 }
95 }
89 }
96 }
90
97
@@ -94,7 +101,7 @@ Rectangle {
94 signalName: "surfaceDestroyed"
101 signalName: "surfaceDestroyed"
95 }
102 }
96
103
97− UT.UnityTestCase {
104+ UnityTestCase {
98 id: testCase
105 id: testCase
99 name: "SurfaceContainer"
106 name: "SurfaceContainer"
100 when: windowShown
107 when: windowShown
@@ -110,6 +117,8 @@ Rectangle {
110
117
111 tryCompare(surfaceContainerLoader.item, "surface", null);
118 tryCompare(surfaceContainerLoader.item, "surface", null);
112 surfaceSpy.clear();
119 surfaceSpy.clear();
120+
121+ interactiveCheckbox.checked = true;
113 }
122 }
114
123
115 /*
124 /*
@@ -145,14 +154,13 @@ Rectangle {
145 surfaceContainer.surface.touchPressCount = 0;
154 surfaceContainer.surface.touchPressCount = 0;
146 surfaceContainer.surface.touchReleaseCount = 0;
155 surfaceContainer.surface.touchReleaseCount = 0;
147
156
148− surfaceContainer.interactive = true;
149 tap(surfaceContainer, surfaceContainer.width / 2, surfaceContainer.height / 2);
157 tap(surfaceContainer, surfaceContainer.width / 2, surfaceContainer.height / 2);
150
158
151 // surface got touches as the surfaceContainer is interactive
159 // surface got touches as the surfaceContainer is interactive
152 compare(surfaceContainer.surface.touchPressCount, 1)
160 compare(surfaceContainer.surface.touchPressCount, 1)
153 compare(surfaceContainer.surface.touchReleaseCount, 1);
161 compare(surfaceContainer.surface.touchReleaseCount, 1);
154
162
155− surfaceContainer.interactive = false;
163+ interactiveCheckbox.checked = false;
156 tap(surfaceContainer, surfaceContainer.width / 2, surfaceContainer.height / 2);
164 tap(surfaceContainer, surfaceContainer.width / 2, surfaceContainer.height / 2);
157
165
158 // surface shouldn't get the touches from the second tap as the surfaceContainer
166 // surface shouldn't get the touches from the second tap as the surfaceContainer