Debian Codegraph @

src/imports/Components/1.3/OptionSelectorDelegate.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 /*!
18 \qmltype OptionSelectorDelegate
19 \inqmlmodule Lomiri.Components
20 \ingroup lomiri-components
21 \brief OptionSelector delegate which can display text, subtext and an image from a custom model.
22
23 Examples:
24 \qml
25 import Lomiri.Components 1.3
26 Column {
27 width: 250
28 OptionSelector {
29 text: i18n.tr("Label")
30 model: customModel
31 delegate: OptionSelectorDelegate { text: name; subText: description; iconSource: image }
32 }
33 ListModel {
34 id: customModel
35 ListElement { name: "Name 1"; description: "Description 1"; image: "images.png" }
36 ListElement { name: "Name 2"; description: "Description 2"; image: "images.png" }
37 ListElement { name: "Name 3"; description: "Description 3"; image: "images.png" }
38 ListElement { name: "Name 4"; description: "Description 4"; image: "images.png" }
39 }
40 }
41 \endqml
42 */
43
44 import QtQuick 2.4
45 import Lomiri.Components.ListItems 1.3 as ListItem
46 import Lomiri.Components 1.3 as Toolkit
47
48 ListItem.Empty {
49 id: option
50
51 __height: units.gu(5)
52
53 /*!
54 Subtext which appears below the main text.
55 */
56 property string subText
57
58 /*!
59 \deprecated
60
61 \b{Use iconName or iconSource instead.}
62
63 Left icon url.
64 */
65 property url icon: iconSource
66 onIconChanged: if (icon != iconSource) {
67 console.warn("WARNING: OptionSelectorDelegate.icon is DEPRECATED. " +
68 "Use iconName and iconSource instead.")
69 }
70
71 /*!
72 Constrains the size of the image to nothing greater than that of the delegate. Changes fillMode to Image.PreserveAspectFit.
73 */
74 property bool constrainImage: false
75
76 /*!
77 Whether or not left image is coloured by our theme.
78 */
79 property bool colourImage: listView.container.colourImage
80
81 /*!
82 Colour of left image.
83 */
84 property color assetColour: listView.container.themeColour
85
86 /*!
87 OptionSelector's ListView.
88 */
89 readonly property ListView listView: ListView.view
90
91 /*!
92 Colourising fragment shader.
93 */
94 readonly property string fragColourShader:
95 "varying highp vec2 qt_TexCoord0;
96 uniform sampler2D source;
97 uniform lowp vec4 colour;
98 uniform lowp float qt_Opacity;
99
100 void main() {
101 lowp vec4 sourceColour = texture2D(source, qt_TexCoord0);
102 gl_FragColor = colour * sourceColour.a * qt_Opacity;
103 }"
104
105 showDivider: index !== listView.count - 1 ? 1 : 0
106 highlightWhenPressed: false
107 selected: ListView.isCurrentItem
108 anchors {
109 left: listView.contentItem.left
110 right: listView.contentItem.right
111 }
112 onClicked: {
113 if (listView.container.currentlyExpanded) {
114 listView.delegateClicked(index);
115
116 listView.previousIndex = listView.currentIndex;
117 listView.currentIndex = index;
118 if (listView.multiSelection)
119 selected = !selected;
120 }
121
122 if (!listView.expanded && !listView.multiSelection) {
123 listView.container.currentlyExpanded = !listView.container.currentlyExpanded;
124 }
125 }
126
127 Binding {
128 target: listView
129 property: "itemHeight"
130 value: childrenRect.height
131 when: childrenRect.height != 0
132 }
133
134 //Since we don't want to add states to our divider, we use the exposed alias provided in Empty to access it and alter it's opacity from here.
135 states: [ State {
136 name: "dividerExpanded"
137 when: listView.container.state === "expanded" && index === listView.currentIndex
138 PropertyChanges {
139 target: option.divider
140 opacity: 1
141 }
142 }, State {
143 name: "dividerCollapsed"
144 when: listView.container.state === "collapsed" && index === listView.currentIndex
145 PropertyChanges {
146 target: option.divider
147 opacity: 0
148 }
149 }
150 ]
151
152 //As with our states, we apply the transition with our divider as the target.
153 transitions: [ Transition {
154 from: "dividerExpanded"
155 to: "dividerCollapsed"
156 Toolkit.LomiriNumberAnimation {
157 target: option.divider
158 properties: "opacity"
159 duration: Toolkit.LomiriAnimation.SlowDuration
160 }
161 }
162 ]
163
164 resources: [
165 Connections {
166 target: listView.container
167 function onCurrentlyExpandedChanged() {
168 imageExpansion.stop();
169 selectedImageCollapse.stop();
170 deselectedImageCollapse.stop();
171
172 if (listView.container.currentlyExpanded === true) {
173 if (!option.selected) {
174 optionExpansion.start();
175
176 //Ensure a source change. This solves a bug which happens occasionaly when source is switched correctly. Probably related to the image.source binding.
177 image.source = listView.container.tick
178 } else {
179 imageExpansion.start();
180 }
181 } else {
182 if (!option.selected) {
183 optionCollapse.start();
184 } else {
185 if (listView.previousIndex !== listView.currentIndex)
186 selectedImageCollapse.start();
187 else {
188 deselectedImageCollapse.start();
189 }
190 }
191 }
192 }
193 }, SequentialAnimation {
194 id: imageExpansion
195
196 PropertyAnimation {
197 target: image
198 properties: "opacity"
199 from : 1.0
200 to: 0.0
201 duration: Toolkit.LomiriAnimation.FastDuration
202 }
203 PauseAnimation { duration: Toolkit.LomiriAnimation.BriskDuration - Toolkit.LomiriAnimation.FastDuration }
204 PropertyAction {
205 target: image
206 property: "source"
207 value: listView.container.tick
208 }
209 PropertyAnimation {
210 target: image
211 properties: "opacity"
212 from : 0.0
213 to: 1.0
214 duration: Toolkit.LomiriAnimation.FastDuration
215 }
216 }, PropertyAnimation {
217 id: optionExpansion
218
219 target: option
220 properties: "opacity"
221 from : 0.0
222 to: 1.0
223 duration: Toolkit.LomiriAnimation.SlowDuration
224 }, SequentialAnimation {
225 id: deselectedImageCollapse
226
227 PauseAnimation { duration: Toolkit.LomiriAnimation.BriskDuration }
228 PropertyAnimation {
229 target: image
230 properties: "opacity"
231 from : 1.0
232 to: 0.0
233 duration: Toolkit.LomiriAnimation.FastDuration
234 }
235 PauseAnimation { duration: Toolkit.LomiriAnimation.FastDuration }
236 PropertyAction {
237 target: image
238 property: "source"
239 value: listView.container.chevron
240 }
241 PropertyAnimation {
242 target: image
243 properties: "opacity"
244 from : 0.0
245 to: 1.0
246 duration: Toolkit.LomiriAnimation.FastDuration
247 }
248 }, SequentialAnimation {
249 id: selectedImageCollapse
250
251 PropertyAnimation {
252 target: image
253 properties: "opacity"
254 from : 0.0
255 to: 1.0
256 duration: Toolkit.LomiriAnimation.FastDuration
257 }
258 PauseAnimation { duration: Toolkit.LomiriAnimation.BriskDuration - Toolkit.LomiriAnimation.FastDuration }
259 PropertyAnimation {
260 target: image
261 properties: "opacity"
262 from : 1.0
263 to: 0.0
264 duration: Toolkit.LomiriAnimation.FastDuration
265 }
266 PauseAnimation { duration: Toolkit.LomiriAnimation.FastDuration }
267 PropertyAction {
268 target: image
269 property: "source"
270 value: listView.container.chevron
271 }
272 PropertyAnimation {
273 target: image
274 properties: "opacity"
275 from : 0.0
276 to: 1.0
277 duration: Toolkit.LomiriAnimation.FastDuration
278 }
279 }, PropertyAnimation {
280 id: optionCollapse
281 target: option
282 properties: "opacity"
283 from : 1.0
284 to: 0.0
285 duration: Toolkit.LomiriAnimation.SlowDuration
286 }
287 ]
288
289 Row {
290 spacing: units.gu(1)
291
292 anchors {
293 left: parent.left
294 leftMargin: units.gu(2)
295 verticalCenter: parent.verticalCenter
296 }
297
298 Image {
299 id: leftIcon
300 objectName: "icon"
301
302 height: constrainImage ? option.height : sourceSize.height
303 source: option.iconSource
304 fillMode: constrainImage ? Image.PreserveAspectFit : Image.Stretch
305
306 ShaderEffect {
307 property color colour: assetColour
308 property Image source: parent
309
310 width: source.width
311 height: source.height
312 visible: colourImage
313
314 fragmentShader: fragColourShader
315 }
316 }
317
318 Column {
319 anchors {
320 verticalCenter: parent.verticalCenter
321 }
322 clip: true
323 width: option.width - leftIcon.width - image.width
324 - parent.spacing * 2 - parent.anchors.leftMargin
325
326 Toolkit.Label {
327 text: option.text === "" ? modelData : option.text
328 width: parent.width
329 elide: Text.ElideRight
330 }
331 Toolkit.Label {
332 text: option.subText
333 width: parent.width
334 elide: Text.ElideRight
335 visible: option.subText !== "" ? true : false
336 textSize: Toolkit.Label.Small
337 }
338 }
339
340 Image {
341 id: image
342
343 width: units.gu(2)
344 height: units.gu(2)
345 source: listView.expanded || listView.multiSelection ? listView.container.tick : listView.container.chevron
346 opacity: option.selected ? 1.0 : 0.0
347 anchors {
348 verticalCenter: parent.verticalCenter
349 }
350
351 //Our behaviour is only enabled for our expanded list due to flickering bugs in relation to all this other animations running on the expanding version.
352 Behavior on opacity {
353 enabled: listView.expanded
354
355 Toolkit.LomiriNumberAnimation {
356 properties: "opacity"
357 duration: Toolkit.LomiriAnimation.FastDuration
358 }
359 }
360
361 ShaderEffect {
362 property color colour: assetColour
363 property Image source: parent
364
365 width: source.width
366 height: source.height
367
368 fragmentShader: fragColourShader
369 }
370 }
371 }
372 }