Debian Codegraph @

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

raw
1 /*
2 * Copyright 2015 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.3
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.3
39 import Lomiri.Components.ListItems 1.3 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 // exposed for OptionSelector
281 property alias __highlight: highlight
282 Rectangle {
283 id: highlight
284
285 z: -1
286 visible: !priv.removed && emptyListItem.swipingState === ""
287 ? emptyListItem.selected
288 || (emptyListItem.highlightWhenPressed && emptyListItem.pressed)
289 : false
290 anchors {
291 left: parent.left
292 right: parent.right
293 top: parent.top
294 }
295 height: emptyListItem.height - bottomDividerLine.height
296 color: theme.palette.highlighted.background
297 }
298
299 ThinDivider {
300 id: bottomDividerLine
301 anchors.bottom: parent.bottom
302 visible: showDivider && !priv.removed
303 }
304
305 Item {
306 id: bodyMargins
307
308 clip: body.x != 0
309 visible: body.height > 0
310 anchors {
311 left: parent.left
312 right: parent.right
313 top: parent.top
314 bottom: parent.bottom
315 }
316
317 Item {
318 id: body
319
320 anchors {
321 top: parent.top
322 bottom: parent.bottom
323 }
324
325 width: parent.width
326
327 Behavior on x {
328 enabled: !priv.held
329 SequentialAnimation {
330 LomiriNumberAnimation {
331 }
332 ScriptAction {
333 script: {
334 if (__mouseArea.drag.target !== null) { // if not from resetDrag()
335 confirmRemovalDialog.waitingForConfirmation = true
336 priv.commitDrag()
337 }
338 }
339 }
340 }
341 }
342
343 onXChanged: {
344 if (x > 0) {
345 backgroundIndicator.state = "SwipingRight"
346 } else {
347 backgroundIndicator.state = ""
348 }
349 }
350 }
351
352 Item {
353 id: backgroundIndicator
354
355 opacity: 0.0
356 visible: false
357 anchors {
358 left: parent.left
359 right: parent.right
360 top: parent.top
361 bottom: parent.bottom
362 }
363 LayoutMirroring.enabled: false
364 LayoutMirroring.childrenInherit: true
365
366 Item {
367 id: confirmRemovalDialog
368 objectName: "confirmRemovalDialog"
369
370 property bool waitingForConfirmation: false
371
372 visible: false
373 width: units.gu(15)
374 x: body.x - width - units.gu(2)
375 anchors {
376 top: parent.top
377 bottom: parent.bottom
378 }
379
380 Row {
381 anchors {
382 top: parent.top
383 bottom: parent.bottom
384 left: parent.left
385 right: parent.right
386 leftMargin: units.gu(2)
387 rightMargin: units.gu(2)
388 }
389
390 spacing: units.gu(2)
391 Image {
392 source: Qt.resolvedUrl("../../artwork/delete.png")
393 fillMode: Image.Pad
394 anchors {
395 verticalCenter: parent.verticalCenter
396 }
397 width: units.gu(5)
398 }
399 Label {
400 text: i18n.dtr('lomiri-ui-toolkit', "Delete")
401 verticalAlignment: Text.AlignVCenter
402 anchors {
403 verticalCenter: parent.verticalCenter
404 }
405 width: units.gu(7)
406 textSize: Label.Medium
407 }
408 }
409
410 MouseArea {
411 visible: confirmRemovalDialog.waitingForConfirmation
412 anchors.fill: parent
413 onClicked: removeItemAnimation.start()
414 }
415 }
416
417
418 states: [
419 State {
420 name: "SwipingRight"
421
422 AnchorChanges {
423 target: backgroundIndicator
424 anchors.left: parent.left
425 anchors.right: body.left
426 }
427
428 PropertyChanges {
429 target: backgroundIndicator
430 opacity: 1.0
431 }
432
433 PropertyChanges {
434 target: confirmRemovalDialog
435 x: body.x - confirmRemovalDialog.width - units.gu(2)
436 }
437 }
438 ]
439 }
440 }
441
442 SequentialAnimation {
443 id: removeItemAnimation
444
445 running: false
446 LomiriNumberAnimation {
447 target: emptyListItem
448 property: "height"
449 to: 0
450 }
451 ScriptAction {
452 script: {
453 priv.removed = true
454 itemRemoved()
455 priv.resetDrag()
456 }
457 }
458 }
459
460 Connections {
461 target: (emptyListItem.removable) ? __mouseArea : null
462
463 function onPressed() {
464 priv.pressedPosition = mouse.x
465 }
466
467 function onMouseXChanged() {
468 var mouseOffset = priv.pressedPosition - mouse.x
469 if ((priv.pressedPosition != -1) && !priv.held && ( Math.abs(mouseOffset) >= priv.mouseMoveOffset)) {
470 priv.startDrag();
471 }
472 }
473
474 function onClicked() {
475 if (body.x != 0) {
476 priv.resetDrag()
477 }
478 }
479
480 function onReleased() {
481 priv.endDrag();
482 }
483
484 function onCanceled() {
485 priv.endDrag();
486 }
487 }
488 }