| 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 as ListItem |
| 19 |
import Lomiri.Components 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 |
| 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: function(event) { |
| 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 |
break; |
| 217 |
default: |
| 218 |
return; |
| 219 |
} |
| 220 |
var newIndex = Toolkit.MathUtils.clamp(list.currentIndex + increment, 0, list.count - 1); |
| 221 |
if (newIndex !== list.currentIndex) { |
| 222 |
event.accepted = true; |
| 223 |
} |
| 224 |
} |
| 225 |
Keys.onReleased: function(event) { |
| 226 |
var increment; |
| 227 |
switch (event.key) { |
| 228 |
case Qt.Key_Up: |
| 229 |
increment = -1; |
| 230 |
break; |
| 231 |
case Qt.Key_Down: |
| 232 |
increment = 1; |
| 233 |
break; |
| 234 |
case Qt.Key_Enter: |
| 235 |
case Qt.Key_Return: |
| 236 |
case Qt.Key_Space: |
| 237 |
if (!list.multiSelection) { |
| 238 |
event.accepted = true; |
| 239 |
listContainer.currentlyExpanded = !listContainer.currentlyExpanded; |
| 240 |
} |
| 241 |
break; |
| 242 |
default: |
| 243 |
return; |
| 244 |
} |
| 245 |
// Only change index while expanded |
| 246 |
if (!listContainer.currentlyExpanded) |
| 247 |
return; |
| 248 |
var newIndex = Toolkit.MathUtils.clamp(list.currentIndex + increment, 0, list.count - 1); |
| 249 |
if (newIndex !== list.currentIndex) { |
| 250 |
event.accepted = true; |
| 251 |
list.delegateClicked(newIndex); |
| 252 |
var shifted = (event.modifiers & Qt.ShiftModifier); |
| 253 |
if (list.multiSelection && !shifted) |
| 254 |
list.currentItem.selected = false; |
| 255 |
list.previousIndex = list.currentIndex; |
| 256 |
list.currentIndex = newIndex; |
| 257 |
if (list.multiSelection) |
| 258 |
list.currentItem.selected = shifted ? !list.currentItem.selected : true; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
Column { |
| 263 |
id: column |
| 264 |
|
| 265 |
spacing: units.gu(2) |
| 266 |
anchors { |
| 267 |
left: parent.left |
| 268 |
right: parent.right |
| 269 |
} |
| 270 |
|
| 271 |
Toolkit.Label { |
| 272 |
id : label |
| 273 |
|
| 274 |
text: optionSelector.text |
| 275 |
visible: optionSelector.text !== "" |
| 276 |
} |
| 277 |
|
| 278 |
Toolkit.StyledItem { |
| 279 |
id: listContainer |
| 280 |
objectName: "listContainer" |
| 281 |
property bool keyNavigationFocus: optionSelector.keyNavigationFocus |
| 282 |
Component.onCompleted: activeFocusOnTab = false |
| 283 |
|
| 284 |
readonly property url chevron: __styleInstance.chevron |
| 285 |
readonly property url tick: __styleInstance.tick |
| 286 |
readonly property color themeColour: theme.palette.normal.backgroundText |
| 287 |
readonly property alias colourImage: optionSelector.colourImage |
| 288 |
property bool currentlyExpanded: expanded || multiSelection |
| 289 |
|
| 290 |
anchors { |
| 291 |
left: parent.left |
| 292 |
right: parent.right |
| 293 |
} |
| 294 |
state: optionSelector.expanded ? "expanded" : "collapsed" |
| 295 |
styleName: "OptionSelectorStyle" |
| 296 |
states: [ State { |
| 297 |
name: "expanded" |
| 298 |
when: listContainer.currentlyExpanded |
| 299 |
PropertyChanges { |
| 300 |
target: listContainer |
| 301 |
height: list.contentHeight < containerHeight ? list.contentHeight : containerHeight |
| 302 |
} |
| 303 |
}, State { |
| 304 |
name: "collapsed" |
| 305 |
when: !listContainer.currentlyExpanded |
| 306 |
PropertyChanges { |
| 307 |
target: listContainer |
| 308 |
height: list.itemHeight |
| 309 |
} |
| 310 |
} |
| 311 |
] |
| 312 |
|
| 313 |
transitions: [ Transition { |
| 314 |
SequentialAnimation { |
| 315 |
Toolkit.LomiriNumberAnimation { |
| 316 |
properties: "height" |
| 317 |
duration: Toolkit.LomiriAnimation.BriskDuration |
| 318 |
} |
| 319 |
ScriptAction { |
| 320 |
script: { |
| 321 |
if (listContainer.currentlyExpanded) { |
| 322 |
expansionCompleted(); |
| 323 |
} else { |
| 324 |
list.positionViewAtIndex(selectedIndex, ListView.Beginning); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
] |
| 331 |
|
| 332 |
ListView { |
| 333 |
id: list |
| 334 |
parent: listContainer.__styleInstance.content |
| 335 |
|
| 336 |
property int previousIndex: -1 |
| 337 |
readonly property alias expanded: optionSelector.expanded |
| 338 |
readonly property alias multiSelection: optionSelector.multiSelection |
| 339 |
readonly property alias container: listContainer |
| 340 |
property real itemHeight |
| 341 |
signal delegateClicked(int index) |
| 342 |
|
| 343 |
onMovementStarted: optionSelector.requestFocus(Qt.MouseFocusReason) |
| 344 |
onFlickStarted: optionSelector.requestFocus(Qt.MouseFocusReason) |
| 345 |
Toolkit.Mouse.onClicked: optionSelector.requestFocus(Qt.MouseFocusReason) |
| 346 |
|
| 347 |
onDelegateClicked: optionSelector.delegateClicked(index); |
| 348 |
interactive: listContainer.height !== list.contentHeight && listContainer.currentlyExpanded |
| 349 |
clip: true |
| 350 |
currentIndex: 0 |
| 351 |
model: optionSelector.model |
| 352 |
anchors.fill: parent |
| 353 |
|
| 354 |
delegate: optionSelector.delegate |
| 355 |
|
| 356 |
Behavior on contentY { |
| 357 |
Toolkit.LomiriNumberAnimation { |
| 358 |
properties: "contentY" |
| 359 |
duration: Toolkit.LomiriAnimation.BriskDuration |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
} |