Lomiri
WizardItemSelector.qml
1 /*
2  * Copyright (C) 2016 Canonical Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU 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 General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.12
18 import Lomiri.Components 1.3
19 import Lomiri.Components.ListItems 1.3 as ListItem
20 import "../Components"
21 
22 Rectangle {
23  id: optionToggle
24 
25  property bool expanded
26  property var model
27  property int selectedIndex: -1
28  readonly property double itemHeight: units.gu(4)
29  readonly property int maxVisibleItems: 6
30 
31  color: theme.palette.normal.foreground
32  height: expanded ? maxVisibleItems * itemHeight : itemHeight
33  Behavior on height {
34  LomiriNumberAnimation { id: heightAnimation }
35  }
36 
37  width: parent.width
38  radius: units.gu(0.6)
39  clip: true
40  border.width: units.dp(1)
41  border.color: theme.palette.normal.base
42 
43  Flickable {
44  id: flickable
45  interactive: expanded
46  flickableDirection: Flickable.VerticalFlick
47  width: parent.width
48  height: parent.height
49  contentHeight: optionToggleRepeater.count * itemHeight
50 
51  Column {
52  id: optionToggleContent
53  width: parent.width
54 
55  Repeater {
56  id: optionToggleRepeater
57  model: optionToggle.model
58 
59  delegate: Loader {
60  asynchronous: true
61  visible: status === Loader.Ready
62 
63  Component {
64  id: optionToggleEntry
65 
66  AbstractButton {
67  width: optionToggleContent.width
68  height: optionToggle.itemHeight
69  onClicked: {
70  if (expanded) {
71  selectedIndex = index;
72  }
73  expanded = !expanded
74  }
75 
76  ListItem.ThinDivider {
77  visible: expanded && index != 0
78  }
79 
80  Label {
81  id: delegateLabel
82  anchors {
83  left: parent.left
84  leftMargin: units.gu(1)
85  right: parent.right
86  rightMargin: units.gu(3)
87  verticalCenter: parent.verticalCenter
88  }
89 
90  width: parent.width
91  text: expanded ? modelData : optionToggle.model[selectedIndex]
92  color: textColor
93  font.weight: Font.Light
94  maximumLineCount: 1
95  elide: Text.ElideRight
96  }
97 
98  Icon {
99  anchors {
100  right: parent.right
101  rightMargin: units.gu(1)
102  verticalCenter: parent.verticalCenter
103  }
104 
105  visible: (index == 0 || !expanded) && !heightAnimation.running
106  name: expanded ? "up" : "down"
107  width: units.gu(1.5)
108  height: width
109  }
110 
111  Image {
112  anchors {
113  right: parent.right
114  rightMargin: units.gu(1)
115  verticalCenter: parent.verticalCenter
116  }
117  visible: expanded && index == optionToggle.selectedIndex && index != 0
118  height: units.gu(1.5)
119  fillMode: Image.PreserveAspectFit
120  source: Qt.resolvedUrl("Pages/data/Tick@30.png")
121  }
122  }
123  }
124  sourceComponent: optionToggleEntry
125  }
126  }
127  }
128  }
129 }