Debian Codegraph @

src/imports/Lomiri/Contacts/MultipleSelectionListView.qml

raw
1 /*
2 * Copyright (C) 2013 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
17 import QtQuick 2.4
18 import QtQuick.Window 2.4
19 import Lomiri.Components 1.3
20 import Lomiri.Components.Popups 1.3 as Popups
21
22 /*!
23 \qmltype ContactSimpleListView
24 \inqmlmodule Lomiri.Contacts 0.1
25 \ingroup ubuntu
26 \brief The MultipleSelectionListView provides a ListView with support to multiple selection
27
28 The MultipleSelectionListViewprovides a ListView with support to multiple selection which can be used by any
29 application.
30
31 Example:
32 \qml
33 import Lomiri.Contacts 0.1
34
35 MultipleSelectionListView {
36 id: view
37 anchors.fill: paret
38 model: 100
39 delegate: Rectangle {
40 width: parent.width
41 height: 100
42 color: view.selectedItems.indexOf(index) == -1 ? "white" : "blue"
43
44 MouseArea {
45 anchors.fill: parent
46 onClicked: {
47 if (view.isInSelectionModel) {
48 view.selectItem(index)
49 }
50 }
51 onPressAndHold: view.startSelection()
52 }
53 }
54 onSelectionDone: console.debug("Selected items:" + view.selectedItems)
55 }
56 \endqml
57 */
58
59 ListView {
60 id: listView
61
62 /*!
63 \qmlproperty model selectedItems
64
65 This property holds the list of selected items
66 */
67 readonly property alias selectedItems: visualModel.selectedItems
68 /*!
69 \qmlproperty bool multipleSelection
70
71 This property holds if the selection will accept multiple items or single items
72 */
73 property bool multipleSelection: true
74
75 /*!
76 \qmlproperty model listModel
77
78 This property holds the model providing data for the list.
79 */
80 property alias listModel: visualModel.model
81 /*!
82 \qmlproperty Component listDelegate
83
84 The delegate provides a template defining each item instantiated by the view.
85 */
86 property alias listDelegate: visualModel.delegate
87
88 /*!
89 \qmlproperty bool isInSelectionMode
90
91 This property holds a list with the index of selected items
92 */
93 readonly property bool isInSelectionMode: state === "selection"
94 /*!
95 This handler is called when the selection mode is finished without be canceled
96 */
97 signal selectionDone(var items)
98 /*!
99 This handler is called when the selection mode is canceled
100 */
101 signal selectionCanceled()
102
103 /*!
104 Start the selection mode on the list view.
105 */
106 function startSelection()
107 {
108 state = "selection"
109 }
110 /*!
111 Check if the item is selected
112 Returns true if the item was marked as selected or false if the item is unselected
113 */
114 function isSelected(item)
115 {
116 if (item && item.VisualDataModel) {
117 return (item.VisualDataModel.inSelected === true)
118 } else {
119 return false
120 }
121 }
122 /*!
123 Mark the item as selected
124 Returns true if the item was marked as selected or false if the item is already selected
125 */
126 function selectItem(item)
127 {
128 if (item.VisualDataModel.inSelected) {
129 return false
130 } else {
131 if (!multipleSelection) {
132 clearSelection()
133 }
134 item.VisualDataModel.inSelected = true
135 return true
136 }
137 }
138 /*!
139 Remove the index from the selected list
140 */
141 function deselectItem(item)
142 {
143 var result = false
144 if (item.VisualDataModel.inSelected) {
145 item.VisualDataModel.inSelected = false
146 result = true
147 }
148 return result
149 }
150 /*!
151 Finish the selection mode with sucess
152 */
153 function endSelection()
154 {
155 selectionDone(listView.selectedItems)
156 clearSelection()
157 state = ""
158 }
159 /*!
160 Cancel the selection
161 */
162 function cancelSelection()
163 {
164 selectionCanceled()
165 clearSelection()
166 state = ""
167 }
168 /*!
169 Remove any selected item from the selection list
170 */
171 function clearSelection()
172 {
173 if (selectedItems.count > 0) {
174 selectedItems.remove(0, selectedItems.count)
175 }
176 }
177 /*!
178 Select all items in the list
179 */
180 function selectAll()
181 {
182 if (multipleSelection) {
183 visualModel.items.addGroups(0, visualModel.items.count, ["selected"] )
184 }
185 }
186
187 model: visualModel
188
189 MultipleSelectionVisualModel {
190 id: visualModel
191 }
192
193 Component.onCompleted: {
194 // "Screen.devicePixelRatio === 1" means scaling is managed by the OS, in that case tune scroll velocity
195 var defaultGridPixel = 8
196 if (Screen.devicePixelRatio === 1 && Math.floor(units.gu(1)) != defaultGridPixel ) {
197 var scaleFactor = units.gridUnit / defaultGridPixel;
198 maximumFlickVelocity = maximumFlickVelocity * scaleFactor;
199 flickDeceleration = flickDeceleration * scaleFactor;
200 }
201 }
202 }