Debian Codegraph @

src/imports/Components/ListItems/2.0/ItemSelector.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 "." as ListItem
19 import Lomiri.Components
20
21 /*!
22 \qmltype ItemSelector
23 \inqmlmodule Lomiri.Components.ListItems 1.0
24 \ingroup lomiri-listitems
25 \brief ListItem displaying either a single selected value or expanded multiple choice with an optional image and subtext when not expanded, when expanding it opens a
26 listing of all the possible values for selection with an additional option of always being expanded. If multiple choice is selected the list is expanded automatically.
27
28 Examples:
29 \qml
30 import Lomiri.Components.ListItems as ListItem
31 Column {
32 anchors.left: parent.left
33 anchors.right: parent.right
34 spacing: units.gu(3)
35
36 ListItem.ItemSelector {
37 text: i18n.tr("Label")
38 model: [i18n.tr("Value 1"),
39 i18n.tr("Value 2"),
40 i18n.tr("Value 3"),
41 i18n.tr("Value 4")]
42 }
43
44 ListItem.ItemSelector {
45 text: i18n.tr("Label")
46 expanded: true
47 model: [i18n.tr("Value 1"),
48 i18n.tr("Value 2"),
49 i18n.tr("Value 3"),
50 i18n.tr("Value 4")]
51 }
52
53 ListItem.ItemSelector {
54 text: i18n.tr("Multiple Selection")
55 alwaysExpanded: false
56 expanded: true
57 model: [i18n.tr("Value 1"),
58 i18n.tr("Value 2"),
59 i18n.tr("Value 3"),
60 i18n.tr("Value 4")]
61 }
62
63 ListItem.ItemSelector {
64 text: i18n.tr("Label")
65 model: customModel
66 expanded: true
67 colourImage: true
68 delegate: selectorDelegate
69 }
70
71 Component {
72 id: selectorDelegate
73 OptionSelectorDelegate { text: name; subText: description; iconSource: image }
74 }
75
76 ListModel {
77 id: customModel
78 ListElement { name: "Name 1"; description: "Description 1"; image: "images.png" }
79 ListElement { name: "Name 2"; description: "Description 2"; image: "images.png" }
80 ListElement { name: "Name 3"; description: "Description 3"; image: "images.png" }
81 ListElement { name: "Name 4"; description: "Description 4"; image: "images.png" }
82 }
83
84 ListItem.ItemSelector {
85 text: i18n.tr("Label")
86 model: [i18n.tr("Value 1"),
87 i18n.tr("Value 2"),
88 i18n.tr("Value 3"),
89 i18n.tr("Value 4"),
90 i18n.tr("Value 5"),
91 i18n.tr("Value 6"),
92 i18n.tr("Value 7"),
93 i18n.tr("Value 8")]
94 containerHeight: itemHeight * 4
95 }
96
97 ListItem.ItemSelector {
98 text: i18n.tr("Label")
99 expanded: true
100 model: [i18n.tr("Value 1"),
101 i18n.tr("Value 2"),
102 i18n.tr("Value 3"),
103 i18n.tr("Value 4"),
104 i18n.tr("Value 5"),
105 i18n.tr("Value 6"),
106 i18n.tr("Value 7"),
107 i18n.tr("Value 8")]
108 containerHeight: itemHeight * 4
109 }
110 }
111 \endqml
112 */
113
114 ListItem.Empty {
115 id: itemSelector
116
117 /*!
118 The list of values that will be shown under the label text. This is a model.
119 */
120 property var model
121
122 /*!
123 Specifies whether the list is always expanded.
124 */
125 property bool expanded: false
126
127 /*!
128 If multiple choice selection is enabled the list is always expanded.
129 */
130 property bool multiSelection: false
131
132 /*!
133 Colours image according to the fieldText colour of the theme, otherwise source colour is maintained.
134 */
135 property bool colourImage: false
136
137 /*!
138 ListView delegate.
139 */
140 property Component delegate: OptionSelectorDelegate { id: selectorDelegate }
141
142 /*!
143 Custom height for list container which allows scrolling inside the selector.
144 */
145 property real containerHeight: {
146 /*The reason for this slightly unconventional method of setting the container height
147 is due to the fact that if we set it to the selector height by default (which is
148 bound to the colum height) then we wouldn't be able to scroll to the end of the bottom
149 boundary. The text is also invisible if none is set so this is taken into account too.*/
150 var textHeight = text === "" ? 0 : label.height + column.spacing;
151 if (parent && parent.height > 0 && parent.height < list.contentHeight) {
152 return parent.height - textHeight;
153 } else {
154 return list.contentHeight;
155 }
156 }
157
158 /*!
159 \qmlproperty int selectedIndex
160 The index of the currently selected element in our list.
161 */
162 property alias selectedIndex: list.currentIndex
163
164 /*!
165 \qmlproperty bool currentlyExpanded
166 Is our list currently expanded?
167 */
168 property alias currentlyExpanded: listContainer.currentlyExpanded
169
170 /*!
171 \qmlproperty real itemHeight
172 Height of an individual list item.
173 */
174 readonly property alias itemHeight: list.itemHeight
175
176 /*!
177 Called when delegate is clicked.
178 */
179 signal delegateClicked(int index)
180
181 /*!
182 Called when the selector has finished expanding or collapsing.
183 */
184 signal expansionCompleted()
185
186 __height: column.height
187 showDivider: false
188
189 Column {
190 id: column
191
192 anchors {
193 left: parent.left
194 right: parent.right
195 }
196
197 ListItem.Standard {
198 id: label
199
200 text: itemSelector.text
201 visible: itemSelector.text !== "" ? true : false
202 }
203
204 ListItem.Standard {
205 id: listContainer
206 objectName: "listContainer"
207
208 readonly property url chevron: __styleInstance.chevron
209 readonly property url tick: __styleInstance.tick
210 readonly property color themeColour: theme.palette.selected.fieldText
211 readonly property alias colourImage: itemSelector.colourImage
212 property bool currentlyExpanded: expanded || multiSelection
213
214 anchors {
215 left: parent.left
216 right: parent.right
217 }
218 state: itemSelector.expanded ? "expanded" : "collapsed"
219 styleName: "ListItemOptionSelectorStyle"
220
221 states: [ State {
222 name: "expanded"
223 when: listContainer.currentlyExpanded
224 PropertyChanges {
225 target: listContainer
226 height: list.contentHeight < containerHeight ? list.contentHeight : containerHeight
227 }
228 }, State {
229 name: "collapsed"
230 when: !listContainer.currentlyExpanded
231 PropertyChanges {
232 target: listContainer
233 height: list.itemHeight
234 }
235 }
236 ]
237
238 transitions: [ Transition {
239 SequentialAnimation {
240 LomiriNumberAnimation {
241 properties: "height"
242 duration: LomiriAnimation.BriskDuration
243 }
244 ScriptAction {
245 script: {
246 if (listContainer.currentlyExpanded) {
247 expansionCompleted();
248 } else {
249 list.positionViewAtIndex(selectedIndex, ListView.Beginning);
250 }
251 }
252 }
253 }
254 }
255 ]
256
257 ListView {
258 id: list
259 objectName: "listView"
260
261 property int previousIndex: list.currentIndex
262 readonly property alias expanded: itemSelector.expanded
263 readonly property alias multiSelection: itemSelector.multiSelection
264 readonly property alias container: listContainer
265 property real itemHeight
266 signal delegateClicked(int index)
267
268 onDelegateClicked: itemSelector.delegateClicked(index);
269 interactive: listContainer.height !== list.contentHeight && listContainer.currentlyExpanded ? true : false
270 clip: true
271 currentIndex: 0
272 model: itemSelector.model
273 anchors.fill: parent
274
275 delegate: itemSelector.delegate
276
277 Behavior on contentY {
278 LomiriNumberAnimation {
279 properties: "contentY"
280 duration: LomiriAnimation.BriskDuration
281 }
282 }
283 }
284 }
285 }
286 }