Debian Codegraph @

src/imports/Components/1.2/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.2
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.0 as ListItem
46 import Lomiri.Components 1.2 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 if (!listView.multiSelection) {
117 listView.previousIndex = listView.currentIndex;
118 listView.currentIndex = index;
119 } else {
120 selected = !selected;
121 }
122 }
123
124 if (!listView.expanded && !listView.multiSelection) {
125 listView.container.currentlyExpanded = !listView.container.currentlyExpanded;
126 }
127 }
128
129 Component.onCompleted: {
130 height = listView.itemHeight = childrenRect.height;
131 }
132
133 //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.
134 states: [ State {
135 name: "dividerExpanded"
136 when: listView.container.state === "expanded" && index === listView.currentIndex
137 PropertyChanges {
138 target: option.divider
139 opacity: 1
140 }
141 }, State {
142 name: "dividerCollapsed"
143 when: listView.container.state === "collapsed" && index === listView.currentIndex
144 PropertyChanges {
145 target: option.divider
146 opacity: 0
147 }
148 }
149 ]
150
151 //As with our states, we apply the transition with our divider as the target.
152 transitions: [ Transition {
153 from: "dividerExpanded"
154 to: "dividerCollapsed"
155 Toolkit.LomiriNumberAnimation {
156 target: option.divider
157 properties: "opacity"
158 duration: Toolkit.LomiriAnimation.SlowDuration
159 }
160 }
161 ]
162
163 resources: [
164 Connections {
165 target: listView.container
166 function onCurrentlyExpandedChanged() {
167 imageExpansion.stop();
168 selectedImageCollapse.stop();
169 deselectedImageCollapse.stop();
170
171 if (listView.container.currentlyExpanded === true) {
172 if (!option.selected) {
173 optionExpansion.start();
174
175 //Ensure a source change. This solves a bug which happens occasionaly when source is switched correctly. Probably related to the image.source binding.
176 image.source = listView.container.tick
177 } else {
178 imageExpansion.start();
179 }
180 } else {
181 if (!option.selected) {
182 optionCollapse.start();
183 } else {
184 if (listView.previousIndex !== listView.currentIndex)
185 selectedImageCollapse.start();
186 else {
187 deselectedImageCollapse.start();
188 }
189 }
190 }
191 }
192 }, SequentialAnimation {
193 id: imageExpansion
194
195 PropertyAnimation {
196 target: image
197 properties: "opacity"
198 from : 1.0
199 to: 0.0
200 duration: Toolkit.LomiriAnimation.FastDuration
201 }
202 PauseAnimation { duration: Toolkit.LomiriAnimation.BriskDuration - Toolkit.LomiriAnimation.FastDuration }
203 PropertyAction {
204 target: image
205 property: "source"
206 value: listView.container.tick
207 }
208 PropertyAnimation {
209 target: image
210 properties: "opacity"
211 from : 0.0
212 to: 1.0
213 duration: Toolkit.LomiriAnimation.FastDuration
214 }
215 }, PropertyAnimation {
216 id: optionExpansion
217
218 target: option
219 properties: "opacity"
220 from : 0.0
221 to: 1.0
222 duration: Toolkit.LomiriAnimation.SlowDuration
223 }, SequentialAnimation {
224 id: deselectedImageCollapse
225
226 PauseAnimation { duration: Toolkit.LomiriAnimation.BriskDuration }
227 PropertyAnimation {
228 target: image
229 properties: "opacity"
230 from : 1.0
231 to: 0.0
232 duration: Toolkit.LomiriAnimation.FastDuration
233 }
234 PauseAnimation { duration: Toolkit.LomiriAnimation.FastDuration }
235 PropertyAction {
236 target: image
237 property: "source"
238 value: listView.container.chevron
239 }
240 PropertyAnimation {
241 target: image
242 properties: "opacity"
243 from : 0.0
244 to: 1.0
245 duration: Toolkit.LomiriAnimation.FastDuration
246 }
247 }, SequentialAnimation {
248 id: selectedImageCollapse
249
250 PropertyAnimation {
251 target: image
252 properties: "opacity"
253 from : 0.0
254 to: 1.0
255 duration: Toolkit.LomiriAnimation.FastDuration
256 }
257 PauseAnimation { duration: Toolkit.LomiriAnimation.BriskDuration - Toolkit.LomiriAnimation.FastDuration }
258 PropertyAnimation {
259 target: image
260 properties: "opacity"
261 from : 1.0
262 to: 0.0
263 duration: Toolkit.LomiriAnimation.FastDuration
264 }
265 PauseAnimation { duration: Toolkit.LomiriAnimation.FastDuration }
266 PropertyAction {
267 target: image
268 property: "source"
269 value: listView.container.chevron
270 }
271 PropertyAnimation {
272 target: image
273 properties: "opacity"
274 from : 0.0
275 to: 1.0
276 duration: Toolkit.LomiriAnimation.FastDuration
277 }
278 }, PropertyAnimation {
279 id: optionCollapse
280 target: option
281 properties: "opacity"
282 from : 1.0
283 to: 0.0
284 duration: Toolkit.LomiriAnimation.SlowDuration
285 }
286 ]
287
288 Row {
289 spacing: units.gu(1)
290
291 anchors {
292 left: parent.left
293 leftMargin: units.gu(2)
294 verticalCenter: parent.verticalCenter
295 }
296
297 Image {
298 id: leftIcon
299 objectName: "icon"
300
301 height: constrainImage ? option.height : sourceSize.height
302 source: option.iconSource
303 fillMode: constrainImage ? Image.PreserveAspectFit : Image.Stretch
304
305 ShaderEffect {
306 property color colour: assetColour
307 property Image source: parent
308
309 width: source.width
310 height: source.height
311 visible: colourImage
312
313 fragmentShader: fragColourShader
314 }
315 }
316
317 Column {
318 anchors {
319 verticalCenter: parent.verticalCenter
320 }
321 Label {
322 text: option.text === "" ? modelData : option.text
323 }
324 Label {
325 text: option.subText
326 visible: option.subText !== "" ? true : false
327 fontSize: "small"
328 }
329 }
330 }
331
332 Image {
333 id: image
334
335 width: units.gu(2)
336 height: units.gu(2)
337 source: listView.expanded || listView.multiSelection ? listView.container.tick : listView.container.chevron
338 opacity: option.selected ? 1.0 : 0.0
339 anchors {
340 right: parent.right
341 rightMargin: units.gu(2)
342 verticalCenter: parent.verticalCenter
343 }
344
345 //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.
346 Behavior on opacity {
347 enabled: listView.expanded
348
349 Toolkit.LomiriNumberAnimation {
350 properties: "opacity"
351 duration: Toolkit.LomiriAnimation.FastDuration
352 }
353 }
354
355 ShaderEffect {
356 property color colour: assetColour
357 property Image source: parent
358
359 width: source.width
360 height: source.height
361
362 fragmentShader: fragColourShader
363 }
364 }
365 }