Debian Codegraph @

src/imports/Components/ListItems/1.2/Empty.qml

raw
1 /*
2 * Copyright 2012, 2013, 2014 Canonical Ltd.
3 *
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
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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 1.2
19
20 /*!
21 \qmltype Empty
22 \inqmlmodule Lomiri.Components.ListItems 1.0
23 \ingroup lomiri-listitems
24 \brief A list item with no contents.
25 \note \b{The component is deprecated. Use ListItem component instead.}
26 The Empty class can be used for generic list items containing other
27 components such as buttons. It is selectable and can take mouse clicks.
28 It will attempt to detect if a thin dividing line at the bottom of the
29 item is suitable, but this behaviour can be overridden (using \l showDivider).
30 For specific types of list items, see its subclasses.
31
32 The item will still remain in memory after being removed from the list so it is up to the
33 application to destroy it. This can be handled by the signal \l itemRemoved that is fired
34 after all animation is done.
35
36 Examples:
37 \qml
38 import Lomiri.Components 1.2
39 import Lomiri.Components.ListItems 1.0 as ListItem
40
41 Item {
42 Model {
43 id: contactModel
44
45 ListElement {
46 name: "Bill Smith"
47 number: "555 3264"
48 }
49 ListElement {
50 name: "John Brown"
51 number: "555 8426"
52 }
53 }
54
55 ListView {
56 width: 180; height: 200
57 model: contactModel
58
59 delegate: ListItem.Empty {
60 height: units.gu(6)
61 removable: true
62 onItemRemoved: contactModel.remove(index)
63 Text {
64 text: name + " " + number
65 anchors.centerIn: parent
66 }
67 }
68 }
69 }
70 \endqml
71
72 See the documentation of the derived classes of Empty for more examples.
73 */
74 AbstractButton {
75 id: emptyListItem
76
77 /*!
78 Specifies whether the list item is selected.
79 */
80 property bool selected: false
81
82 /*!
83 Highlight the list item when it is pressed.
84 This is used to disable the highlighting of the full list item
85 when custom highlighting needs to be implemented (for example in
86 ListItem.Standard which can have a split).
87 */
88 property bool highlightWhenPressed: true
89
90 /*!
91 Defines if this item can be removed or not.
92 */
93 property bool removable: false
94
95 /*!
96 Defines if the item needs confirmation before removing by swiping.
97 \qmlproperty bool confirmRemoval
98 */
99 property alias confirmRemoval: confirmRemovalDialog.visible
100
101 /*!
102 \qmlproperty string swipingState
103 The current swiping state ("SwipingRight" or "")
104 */
105 readonly property alias swipingState: backgroundIndicator.state
106
107 /*!
108 \qmlproperty bool waitingConfirmationForRemoval
109 Defines if the item is waiting for the user interaction during the swipe to delete
110 */
111 readonly property alias waitingConfirmationForRemoval: confirmRemovalDialog.waitingForConfirmation
112
113 /*!
114 This handler is called when the item is removed from the list
115 */
116 signal itemRemoved
117
118 /*!
119 \internal
120 Defines the height of the ListItem, so correct height of this component, including divider
121 line is calculated.
122 */
123 property int __height: units.gu(6)
124
125 /*!
126 Set to show or hide the thin bottom divider line (drawn by the \l ThinDivider component).
127 This line is shown by default except in cases where this item is the delegate of a ListView.
128 */
129 property bool showDivider: true
130
131 /*!
132 \internal
133 Reparent so that the visuals of the children does not
134 occlude the bottom divider line.
135 */
136 default property alias children: body.data
137
138 /*!
139 \internal
140 Allows derived class to proper add items inside of this element
141 */
142 property alias __contents: body
143
144 /*!
145 \qmlproperty list<Item> backgroundIndicator
146 Defines the item background item to be showed during the item swiping
147 */
148 property alias backgroundIndicator: backgroundIndicator.children
149
150 /*!
151 \qmlproperty ThinDivider divider
152 Exposes our the bottom line divider.
153 */
154 property alias divider: bottomDividerLine
155
156 /*! \internal
157 The spacing inside the list item.
158 */
159 property real __contentsMargins: units.gu(2)
160
161 /*!
162 Cancel item romoval
163 */
164 function cancelItemRemoval()
165 {
166 priv.resetDrag()
167 priv.removed = false
168 }
169
170 width: parent ? parent.width : units.gu(31)
171 implicitHeight: priv.removed ? 0 : __height + bottomDividerLine.height
172 __mouseArea.drag.axis: Drag.XAxis
173
174 // Keep compatible with the old version
175 height: implicitHeight
176
177 LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft
178 LayoutMirroring.childrenInherit: true
179
180 /*! \internal */
181 QtObject {
182 id: priv
183
184 /*! \internal
185 Defines the offset used when the item will start to move
186 */
187 readonly property int mouseMoveOffset: units.gu(1)
188
189 /*! \internal
190 Defines the offset limit to consider the item removed
191 */
192 readonly property int itemMoveOffset: confirmRemoval ? width * 0.5 : width * 0.3
193
194 /*! \internal
195 Defines the initial pressed position
196 */
197 property int pressedPosition: -1
198
199 /*! \internal
200 Defines if the item is moving or not
201 */
202 property bool held: false
203
204 /*! \internal
205 Defines if the item should be removed after the animation or not
206 */
207 property bool removeItem: false
208
209 /*! \internal
210 Defines if the item was removed or not
211 */
212 property bool removed: false
213
214 /*! \internal
215 notify the start of the drag operation
216 */
217 function startDrag() {
218 __mouseArea.drag.target = body
219 held = true
220 __mouseArea.drag.maximumX = parent.width
221 __mouseArea.drag.minimumX = 0
222 backgroundIndicator.visible = true
223 }
224
225 /*! \internal
226 Resets the item dragging state
227 */
228 function resetDrag() {
229 confirmRemovalDialog.waitingForConfirmation = false
230 held = false // before body.x to ensure animation
231 __mouseArea.drag.target = null // stops waitingForConfirmation = true in animation
232 body.x = 0
233 pressedPosition = -1
234 removeItem = false
235 backgroundIndicator.opacity = 0.0
236 backgroundIndicator.visible = false
237 }
238
239 /*! \internal
240 Commit the necessary changes to remove or not the item based on the mouse final position
241 */
242 function commitDrag() {
243 if (removeItem) {
244 if (!confirmRemoval) {
245 removeItemAnimation.start()
246 }
247 } else {
248 resetDrag()
249 }
250 }
251
252 /*! \internal
253 notify the releaso of the mouse button and the end of the drag operation
254 */
255 function endDrag() {
256 if (Math.abs(body.x) < itemMoveOffset && held == true) {
257 held = false
258 removeItem = false
259 if (body.x == 0) {
260 resetDrag()
261 } else {
262 body.x = 0;
263 }
264 } else if (held == true) {
265 held = false
266 removeItem = true
267 var finalX = body.width
268 if (emptyListItem.confirmRemoval) {
269 finalX = itemMoveOffset
270 }
271 if (body.x > 0) {
272 body.x = finalX
273 } else {
274 body.x = -finalX
275 }
276 }
277 }
278 }
279
280 Rectangle {
281 id: highlight
282
283 z: -1
284 visible: !priv.removed && emptyListItem.swipingState === ""
285 ? emptyListItem.selected
286 || (emptyListItem.highlightWhenPressed && emptyListItem.pressed)
287 : false
288 anchors {
289 left: parent.left
290 right: parent.right
291 top: parent.top
292 }
293 height: emptyListItem.height - bottomDividerLine.height
294 color: Theme.palette.selected.background
295 }
296
297 ThinDivider {
298 id: bottomDividerLine
299 anchors.bottom: parent.bottom
300 visible: showDivider && !priv.removed
301 }
302
303 Item {
304 id: bodyMargins
305
306 clip: body.x != 0
307 visible: body.height > 0
308 anchors {
309 left: parent.left
310 right: parent.right
311 top: parent.top
312 bottom: parent.bottom
313 }
314
315 Item {
316 id: body
317
318 anchors {
319 top: parent.top
320 bottom: parent.bottom
321 }
322
323 width: parent.width
324
325 Behavior on x {
326 enabled: !priv.held
327 SequentialAnimation {
328 LomiriNumberAnimation {
329 }
330 ScriptAction {
331 script: {
332 if (__mouseArea.drag.target !== null) { // if not from resetDrag()
333 confirmRemovalDialog.waitingForConfirmation = true
334 priv.commitDrag()
335 }
336 }
337 }
338 }
339 }
340
341 onXChanged: {
342 if (x > 0) {
343 backgroundIndicator.state = "SwipingRight"
344 } else {
345 backgroundIndicator.state = ""
346 }
347 }
348 }
349
350 Item {
351 id: backgroundIndicator
352
353 opacity: 0.0
354 visible: false
355 anchors {
356 left: parent.left
357 right: parent.right
358 top: parent.top
359 bottom: parent.bottom
360 }
361 LayoutMirroring.enabled: false
362 LayoutMirroring.childrenInherit: true
363
364 Item {
365 id: confirmRemovalDialog
366 objectName: "confirmRemovalDialog"
367
368 property bool waitingForConfirmation: false
369
370 visible: false
371 width: units.gu(15)
372 x: body.x - width - units.gu(2)
373 anchors {
374 top: parent.top
375 bottom: parent.bottom
376 }
377
378 Row {
379 anchors {
380 top: parent.top
381 bottom: parent.bottom
382 left: parent.left
383 right: parent.right
384 leftMargin: units.gu(2)
385 rightMargin: units.gu(2)
386 }
387
388 spacing: units.gu(2)
389 Image {
390 source: Qt.resolvedUrl("../../artwork/delete.png")
391 fillMode: Image.Pad
392 anchors {
393 verticalCenter: parent.verticalCenter
394 }
395 width: units.gu(5)
396 }
397 Label {
398 text: i18n.dtr('lomiri-ui-toolkit', "Delete")
399 verticalAlignment: Text.AlignVCenter
400 anchors {
401 verticalCenter: parent.verticalCenter
402 }
403 width: units.gu(7)
404 fontSize: "medium"
405 }
406 }
407
408 MouseArea {
409 visible: confirmRemovalDialog.waitingForConfirmation
410 anchors.fill: parent
411 onClicked: removeItemAnimation.start()
412 }
413 }
414
415
416 states: [
417 State {
418 name: "SwipingRight"
419
420 AnchorChanges {
421 target: backgroundIndicator
422 anchors.left: parent.left
423 anchors.right: body.left
424 }
425
426 PropertyChanges {
427 target: backgroundIndicator
428 opacity: 1.0
429 }
430
431 PropertyChanges {
432 target: confirmRemovalDialog
433 x: body.x - confirmRemovalDialog.width - units.gu(2)
434 }
435 }
436 ]
437 }
438 }
439
440 SequentialAnimation {
441 id: removeItemAnimation
442
443 running: false
444 LomiriNumberAnimation {
445 target: emptyListItem
446 property: "height"
447 to: 0
448 }
449 ScriptAction {
450 script: {
451 priv.removed = true
452 itemRemoved()
453 priv.resetDrag()
454 }
455 }
456 }
457
458 Connections {
459 target: (emptyListItem.removable) ? __mouseArea : null
460
461 function onPressed() {
462 priv.pressedPosition = mouse.x
463 }
464
465 function onMouseXChanged() {
466 var mouseOffset = priv.pressedPosition - mouse.x
467 if ((priv.pressedPosition != -1) && !priv.held && ( Math.abs(mouseOffset) >= priv.mouseMoveOffset)) {
468 priv.startDrag();
469 }
470 }
471
472 function onClicked() {
473 if (body.x != 0) {
474 priv.resetDrag()
475 }
476 }
477
478 function onReleased() {
479 priv.endDrag();
480 }
481
482 function onCanceled() {
483 priv.endDrag();
484 }
485 }
486 }