Debian Codegraph @

src/imports/Components/1.3/OptionSelector.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.ListItems 1.3 as ListItem
19 import Lomiri.Components 1.3 as Toolkit
20
21 /*!
22 \qmltype OptionSelector
23 \inqmlmodule Lomiri.Components
24 \ingroup lomiri-components
25 \brief Component 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 1.3
31 Column {
32 spacing: units.gu(3)
33
34 OptionSelector {
35 text: i18n.tr("Label")
36 model: [i18n.tr("Value 1"),
37 i18n.tr("Value 2"),
38 i18n.tr("Value 3"),
39 i18n.tr("Value 4")]
40 }
41
42 OptionSelector {
43 text: i18n.tr("Label")
44 expanded: true
45 model: [i18n.tr("Value 1"),
46 i18n.tr("Value 2"),
47 i18n.tr("Value 3"),
48 i18n.tr("Value 4")]
49 }
50
51 OptionSelector {
52 objectName: "optionselector_multipleselection"
53 text: i18n.tr("Multiple Selection")
54 expanded: false
55 multiSelection: true
56 model: [i18n.tr("Value 1"),
57 i18n.tr("Value 2"),
58 i18n.tr("Value 3"),
59 i18n.tr("Value 4")]
60 }
61
62 OptionSelector {
63 text: i18n.tr("Label")
64 model: customModel
65 expanded: true
66 colourImage: true
67 delegate: selectorDelegate
68 }
69
70 Component {
71 id: selectorDelegate
72 OptionSelectorDelegate { text: name; subText: description; iconSource: image }
73 }
74
75 ListModel {
76 id: customModel
77 ListElement { name: "Name 1"; description: "Description 1"; image: "images.png" }
78 ListElement { name: "Name 2"; description: "Description 2"; image: "images.png" }
79 ListElement { name: "Name 3"; description: "Description 3"; image: "images.png" }
80 ListElement { name: "Name 4"; description: "Description 4"; image: "images.png" }
81 }
82
83 OptionSelector {
84 text: i18n.tr("Label")
85 model: [i18n.tr("Value 1"),
86 i18n.tr("Value 2"),
87 i18n.tr("Value 3"),
88 i18n.tr("Value 4"),
89 i18n.tr("Value 5"),
90 i18n.tr("Value 6"),
91 i18n.tr("Value 7"),
92 i18n.tr("Value 8")]
93 containerHeight: itemHeight * 4
94 }
95
96 OptionSelector {
97 text: i18n.tr("Label")
98 expanded: true
99 model: [i18n.tr("Value 1"),
100 i18n.tr("Value 2"),
101 i18n.tr("Value 3"),
102 i18n.tr("Value 4"),
103 i18n.tr("Value 5"),
104 i18n.tr("Value 6"),
105 i18n.tr("Value 7"),
106 i18n.tr("Value 8")]
107 containerHeight: itemHeight * 4
108 }
109 }
110 \endqml
111 */
112
113 ListItem.Empty {
114 id: optionSelector
115
116 /*!
117 The list of values that will be shown under the label text. This is a model.
118 */
119 property var model
120
121 /*!
122 Specifies whether the list is always expanded.
123 */
124 property bool expanded: false
125
126 /*!
127 If the multiple choice selection is enabled the list is always expanded.
128 */
129 property bool multiSelection: false
130
131 /*!
132 Colours image according to the fieldText colour of the theme, otherwise source colour is maintained.
133 */
134 property bool colourImage: false
135
136 /*!
137 ListView delegate.
138 */
139 property Component delegate: Toolkit.OptionSelectorDelegate {}
140
141 /*!
142 Custom height for list container which allows scrolling inside the selector.
143 */
144 property real containerHeight: {
145 /*The reason for this slightly unconventional method of setting the container height
146 is due to the fact that if we set it to the selector height by default (which is
147 bound to the colum height) then we wouldn't be able to scroll to the end of the bottom
148 boundary. The text is also invisible if none is set so this is taken into account too.*/
149 var textHeight = text === "" ? 0 : label.height + column.spacing;
150 if (parent && parent.height < list.contentHeight) {
151 return parent.height - textHeight;
152 } else {
153 list.contentHeight;
154 }
155 }
156
157 /*!
158 \qmlproperty int count
159 This property holds the number of items in the OptionSelector.
160 */
161 property alias count: list.count
162
163 /*!
164 \qmlproperty int selectedIndex
165 The index of the currently selected element in our list.
166 */
167 property alias selectedIndex: list.currentIndex
168
169 /*!
170 \qmlproperty bool currentlyExpanded
171 Is our list currently expanded?
172 */
173 property alias currentlyExpanded: listContainer.currentlyExpanded
174
175 /*!
176 \qmlproperty real itemHeight
177 Height of an individual list item.
178 */
179 readonly property alias itemHeight: list.itemHeight
180
181 /*!
182 Called when delegate is clicked.
183 */
184 signal delegateClicked(int index)
185
186 /*!
187 Called when the selector has finished expanding or collapsing.
188 */
189 signal expansionCompleted()
190
191 /*!
192 \internal
193 Trigger the action, passing the current index.
194 */
195 onDelegateClicked: {
196 trigger(index)
197 }
198
199 __height: column.height
200 showDivider: false
201
202 activeFocusOnTab: true
203
204 // Prevent scrolling on pressed when released changes the index
205 Keys.onPressed: {
206 // Only change index while expanded
207 if (!listContainer.currentlyExpanded)
208 return;
209 var increment;
210 switch (event.key) {
211 case Qt.Key_Up:
212 increment = -1;
213 break;
214 case Qt.Key_Down:
215 increment = 1;
216 default:
217 return;
218 }
219 var newIndex = Toolkit.MathUtils.clamp(list.currentIndex + increment, 0, list.count - 1);
220 if (newIndex != list.currentIndex) {
221 event.accepted = true;
222 }
223 }
224 Keys.onReleased: {
225 var increment;
226 switch (event.key) {
227 case Qt.Key_Up:
228 increment = -1;
229 break;
230 case Qt.Key_Down:
231 increment = 1;
232 break;
233 case Qt.Key_Enter:
234 case Qt.Key_Return:
235 case Qt.Key_Space:
236 if (!list.multiSelection) {
237 event.accepted = true;
238 listContainer.currentlyExpanded = !listContainer.currentlyExpanded;
239 }
240 default:
241 return;
242 }
243 // Only change index while expanded
244 if (!listContainer.currentlyExpanded)
245 return;
246 var newIndex = Toolkit.MathUtils.clamp(list.currentIndex + increment, 0, list.count - 1);
247 if (newIndex != list.currentIndex) {
248 event.accepted = true;
249 list.delegateClicked(newIndex);
250 var shifted = (event.modifiers & Qt.ShiftModifier);
251 if (list.multiSelection && !shifted)
252 list.currentItem.selected = false;
253 list.previousIndex = list.currentIndex;
254 list.currentIndex = newIndex;
255 if (list.multiSelection)
256 list.currentItem.selected = shifted ? !list.currentItem.selected : true;
257 }
258 }
259
260 Column {
261 id: column
262
263 spacing: units.gu(2)
264 anchors {
265 left: parent.left
266 right: parent.right
267 }
268
269 Toolkit.Label {
270 id : label
271
272 text: optionSelector.text
273 visible: optionSelector.text !== "" ? true : false
274 }
275
276 Toolkit.StyledItem {
277 id: listContainer
278 objectName: "listContainer"
279 property bool keyNavigationFocus: optionSelector.keyNavigationFocus
280 Component.onCompleted: activeFocusOnTab = false
281
282 readonly property url chevron: __styleInstance.chevron
283 readonly property url tick: __styleInstance.tick
284 readonly property color themeColour: theme.palette.normal.backgroundText
285 readonly property alias colourImage: optionSelector.colourImage
286 property bool currentlyExpanded: expanded || multiSelection
287
288 anchors {
289 left: parent.left
290 right: parent.right
291 }
292 state: optionSelector.expanded ? "expanded" : "collapsed"
293 styleName: "OptionSelectorStyle"
294 states: [ State {
295 name: "expanded"
296 when: listContainer.currentlyExpanded
297 PropertyChanges {
298 target: listContainer
299 height: list.contentHeight < containerHeight ? list.contentHeight : containerHeight
300 }
301 }, State {
302 name: "collapsed"
303 when: !listContainer.currentlyExpanded
304 PropertyChanges {
305 target: listContainer
306 height: list.itemHeight
307 }
308 }
309 ]
310
311 transitions: [ Transition {
312 SequentialAnimation {
313 Toolkit.LomiriNumberAnimation {
314 properties: "height"
315 duration: Toolkit.LomiriAnimation.BriskDuration
316 }
317 ScriptAction {
318 script: {
319 if (listContainer.currentlyExpanded) {
320 expansionCompleted();
321 } else {
322 list.positionViewAtIndex(selectedIndex, ListView.Beginning);
323 }
324 }
325 }
326 }
327 }
328 ]
329
330 ListView {
331 id: list
332 parent: listContainer.__styleInstance.content
333
334 property int previousIndex: -1
335 readonly property alias expanded: optionSelector.expanded
336 readonly property alias multiSelection: optionSelector.multiSelection
337 readonly property alias container: listContainer
338 property real itemHeight
339 signal delegateClicked(int index)
340
341 onMovementStarted: optionSelector.requestFocus(Qt.MouseFocusReason)
342 onFlickStarted: optionSelector.requestFocus(Qt.MouseFocusReason)
343 Toolkit.Mouse.onClicked: optionSelector.requestFocus(Qt.MouseFocusReason)
344
345 onDelegateClicked: optionSelector.delegateClicked(index);
346 interactive: listContainer.height !== list.contentHeight && listContainer.currentlyExpanded ? true : false
347 clip: true
348 currentIndex: 0
349 model: optionSelector.model
350 anchors.fill: parent
351
352 delegate: optionSelector.delegate
353
354 Behavior on contentY {
355 Toolkit.LomiriNumberAnimation {
356 properties: "contentY"
357 duration: Toolkit.LomiriAnimation.BriskDuration
358 }
359 }
360 }
361 }
362 }
363 }