Debian Codegraph @

src/imports/Components/1.2/OptionSelector.qml

raw
1 /*
2 * Copyright 2012 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.0 as ListItem
19 import Lomiri.Components 1.2 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.2
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 selectedIndex
159 The index of the currently selected element in our list.
160 */
161 property alias selectedIndex: list.currentIndex
162
163 /*!
164 \qmlproperty bool currentlyExpanded
165 Is our list currently expanded?
166 */
167 property alias currentlyExpanded: listContainer.currentlyExpanded
168
169 /*!
170 \qmlproperty real itemHeight
171 Height of an individual list item.
172 */
173 readonly property alias itemHeight: list.itemHeight
174
175 /*!
176 Called when delegate is clicked.
177 */
178 signal delegateClicked(int index)
179
180 /*!
181 Called when the selector has finished expanding or collapsing.
182 */
183 signal expansionCompleted()
184
185 /*!
186 \internal
187 Trigger the action, passing the current index.
188 */
189 onDelegateClicked: {
190 trigger(index)
191 }
192
193 __height: column.height
194 showDivider: false
195
196 Column {
197 id: column
198
199 spacing: units.gu(2)
200 anchors {
201 left: parent.left
202 right: parent.right
203 }
204
205 Label {
206 id : label
207
208 text: optionSelector.text
209 visible: optionSelector.text !== "" ? true : false
210 }
211
212 Toolkit.StyledItem {
213 id: listContainer
214 objectName: "listContainer"
215 activeFocusOnPress: true
216
217 readonly property url chevron: __styleInstance.chevron
218 readonly property url tick: __styleInstance.tick
219 readonly property color themeColour: Theme.palette.selected.fieldText
220 readonly property alias colourImage: optionSelector.colourImage
221 property bool currentlyExpanded: expanded || multiSelection
222
223 anchors {
224 left: parent.left
225 right: parent.right
226 }
227 state: optionSelector.expanded ? "expanded" : "collapsed"
228 style: Theme.createStyleComponent("OptionSelectorStyle.qml", listContainer)
229 states: [ State {
230 name: "expanded"
231 when: listContainer.currentlyExpanded
232 PropertyChanges {
233 target: listContainer
234 height: list.contentHeight < containerHeight ? list.contentHeight : containerHeight
235 }
236 }, State {
237 name: "collapsed"
238 when: !listContainer.currentlyExpanded
239 PropertyChanges {
240 target: listContainer
241 height: list.itemHeight
242 }
243 }
244 ]
245
246 transitions: [ Transition {
247 SequentialAnimation {
248 Toolkit.LomiriNumberAnimation {
249 properties: "height"
250 duration: Toolkit.LomiriAnimation.BriskDuration
251 }
252 ScriptAction {
253 script: {
254 if (listContainer.currentlyExpanded) {
255 expansionCompleted();
256 } else {
257 list.positionViewAtIndex(selectedIndex, ListView.Beginning);
258 }
259 }
260 }
261 }
262 }
263 ]
264
265 ListView {
266 id: list
267
268 property int previousIndex: -1
269 readonly property alias expanded: optionSelector.expanded
270 readonly property alias multiSelection: optionSelector.multiSelection
271 readonly property alias container: listContainer
272 property real itemHeight
273 signal delegateClicked(int index)
274
275 onMovementStarted: optionSelector.requestFocus(Qt.MouseFocusReason)
276 onFlickStarted: optionSelector.requestFocus(Qt.MouseFocusReason)
277 Toolkit.Mouse.onClicked: optionSelector.requestFocus(Qt.MouseFocusReason)
278
279 onDelegateClicked: optionSelector.delegateClicked(index);
280 interactive: listContainer.height !== list.contentHeight && listContainer.currentlyExpanded ? true : false
281 clip: true
282 currentIndex: 0
283 model: optionSelector.model
284 anchors.fill: parent
285
286 delegate: optionSelector.delegate
287
288 Behavior on contentY {
289 Toolkit.LomiriNumberAnimation {
290 properties: "contentY"
291 duration: Toolkit.LomiriAnimation.BriskDuration
292 }
293 }
294 }
295 }
296 }
297 }