Lomiri
Loading...
Searching...
No Matches
TabletSideStageTouchGesture.qml
1/*
2 * Copyright (C) 2016 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
17import QtQuick 2.15
18import QtQml 2.15
19import Lomiri.Gestures 0.1
20
21TouchGestureArea {
22 id: root
23 minimumTouchPoints: 3
24 maximumTouchPoints: 3
25
26 property bool enableDrag: true
27 property Component dragComponent
28 property var dragComponentProperties: undefined
29
30 readonly property bool recognisedPress: status == TouchGestureArea.Recognized &&
31 touchPoints.length >= minimumTouchPoints &&
32 touchPoints.length <= maximumTouchPoints
33 readonly property bool recognisedDrag: priv.wasRecognisedPress && dragging
34
35 signal pressed(int x, int y)
36 signal clicked
37 signal dragStarted
38 signal dropped
39 signal cancelled
40
41 onEnabledChanged: {
42 if (!enabled) {
43 if (priv.dragObject) root.cancelled();
44 priv.wasRecognisedDrag = false;
45 priv.wasRecognisedPress = false;
46 }
47 }
48
49 onRecognisedPressChanged: {
50 if (recognisedPress) {
51 // get the app at the center of the gesture
52 var centerX = 0;
53 var centerY = 0;
54 for (var i = 0; i < touchPoints.length; i++) {
55 centerX += touchPoints[i].x;
56 centerY += touchPoints[i].y;
57 }
58 centerX = centerX/touchPoints.length;
59 centerY = centerY/touchPoints.length;
60
61 pressed(centerX, centerY);
62 priv.wasRecognisedPress = true;
63 }
64 }
65
66 onStatusChanged: {
67 if (status != TouchGestureArea.Recognized) {
68 if (status == TouchGestureArea.Rejected) {
69 root.cancelled();
70 } else if (status == TouchGestureArea.WaitingForTouch) {
71 if (priv.wasRecognisedPress) {
72 if (!priv.wasRecognisedDrag) {
73 root.clicked();
74 } else {
75 root.dropped();
76 }
77 }
78 }
79 priv.wasRecognisedDrag = false;
80 priv.wasRecognisedPress = false;
81 }
82 }
83
84 onRecognisedDragChanged: {
85 if (enableDrag && recognisedDrag) {
86 priv.wasRecognisedDrag = true;
87 root.dragStarted()
88 }
89 }
90
91 QtObject {
92 id: priv
93 property var dragObject: null
94
95 property bool wasRecognisedPress: false
96 property bool wasRecognisedDrag: false
97 }
98
99 onCancelled: {
100 if (priv.dragObject) {
101 var obj = priv.dragObject;
102 priv.dragObject = null;
103
104 obj.Drag.cancel();
105 obj.destroy();
106 }
107 }
108
109 onDragStarted: {
110 if (!dragComponent)
111 return;
112
113 if (dragComponentProperties) {
114 priv.dragObject = dragComponent.createObject(root, dragComponentProperties);
115 } else {
116 priv.dragObject = dragComponent.createObject(root);
117 }
118 priv.dragObject.Drag.start();
119 }
120
121 onDropped: {
122 if (priv.dragObject) {
123 var obj = priv.dragObject;
124 priv.dragObject = null;
125
126 obj.Drag.drop();
127 obj.destroy();
128 }
129 }
130
131 Binding {
132 target: priv.dragObject
133 when: priv.dragObject && priv.wasRecognisedDrag
134 property: "x"
135 value: {
136 if (!priv.dragObject) return 0;
137 var sum = 0;
138 for (var i = 0; i < root.touchPoints.length; i++) {
139 sum += root.touchPoints[i].x;
140 }
141 return sum/root.touchPoints.length - priv.dragObject.width/2;
142 }
143 restoreMode: Binding.RestoreBinding
144 }
145
146 Binding {
147 target: priv.dragObject
148 when: priv.dragObject && priv.wasRecognisedDrag
149 property: "y"
150 value: {
151 if (!priv.dragObject) return 0;
152 var sum = 0;
153 for (var i = 0; i < root.touchPoints.length; i++) {
154 sum += root.touchPoints[i].y;
155 }
156 return sum/root.touchPoints.length - priv.dragObject.height/2;
157 }
158 restoreMode: Binding.RestoreBinding
159 }
160}