Debian Codegraph @

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