Debian Codegraph @

src/imports/Lomiri/AddressBook/ContactEditor/ValueSelector.qml

raw
1 /*
2 * Copyright (C) 2012-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 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.4
18 import Lomiri.Components 1.3
19
20 Item {
21 id: root
22
23 property bool readOnly: false
24 property bool active: false
25 property alias values: listView.model
26 property alias currentIndex: listView.currentIndex
27 readonly property bool expanded: (state === "expanded") && listView.opacity == 1.0
28 readonly property alias text: label.text
29
30 function selectItem(text)
31 {
32 for(var i=0; i < values.length; i++) {
33 if (values[i] == text) {
34 currentIndex = i
35 return
36 }
37 }
38 currentIndex = -1
39 }
40
41 function moveNext()
42 {
43 if (currentIndex < (values.length-1)) {
44 currentIndex++
45 } else {
46 currentIndex = 0
47 }
48 }
49
50 function movePrevious()
51 {
52 if (currentIndex > 0) {
53 currentIndex--
54 } else {
55 currentIndex = values.length - 1
56 }
57 }
58 onExpandedChanged: expanded && timer.start()
59
60 // FIXME: workaround to close list after a while.
61 // we cant rely on focus since it hides the keyboard.
62 Timer {
63 id: timer
64
65 interval: 5000
66 running: false
67 onTriggered: state = ""
68 }
69
70 Item {
71 id: title
72
73 anchors {
74 top: parent.top
75 bottom: parent.bottom
76 }
77 Behavior on x {
78 LomiriNumberAnimation { }
79 }
80 Label {
81 id: label
82
83 anchors {
84 verticalCenter: parent.verticalCenter
85 left: parent.left
86 }
87 width: contentWidth
88
89 text: root.currentIndex >= 0 ? root.values[root.currentIndex] : ""
90
91 // style
92 fontSize: root.active ? "medium" : "small"
93 }
94
95 Icon {
96 name: "go-next"
97 color: "black"
98 height: units.gu(1)
99 width: height
100 asynchronous: true
101
102 anchors {
103 verticalCenter: parent.verticalCenter
104 verticalCenterOffset: units.dp(2)
105 left: label.right
106 leftMargin: units.gu(0.5)
107 }
108 }
109 }
110
111 MouseArea {
112 anchors.fill: parent
113 onClicked: {
114 if (!readOnly) {
115 root.state = "expanded"
116 timer.restart()
117 }
118 }
119 }
120
121 ListView {
122 id: listView
123 objectName: "valuesListView"
124
125 anchors.fill: parent
126 clip: true
127 orientation: ListView.Horizontal
128 visible: !title.visible
129 snapMode: ListView.SnapToItem
130
131 delegate: Item {
132 objectName: "item_" + index
133
134 anchors {
135 top: parent.top
136 bottom: parent.bottom
137 }
138 width: arrow.width + listLabel.paintedWidth + units.gu(1)
139 opacity: currentIndex == index ? 1.0 : 0.4
140
141 Label {
142 id: listLabel
143
144 anchors {
145 verticalCenter: parent.verticalCenter
146 left: parent.left
147 }
148 width: paintedWidth
149 text: modelData
150
151 // style
152 fontSize: "medium"
153
154 MouseArea {
155 width: parent.width + units.gu(0.5)
156 height: parent.height + units.gu(0.5)
157 anchors.centerIn: parent
158 onClicked: {
159 timer.stop()
160 currentIndex = index
161 root.state = ""
162 }
163 }
164 }
165
166 Icon {
167 id: arrow
168
169 name: "go-next"
170 height: visible ? units.gu(1) : 0
171 width: height
172 visible: index < (listView.count - 1)
173 asynchronous: true
174
175 anchors {
176 verticalCenter: listLabel.verticalCenter
177 verticalCenterOffset: units.dp(2)
178 left: listLabel.right
179 leftMargin: units.gu(0.5)
180 }
181 }
182 }
183 }
184
185 transitions: [
186 Transition {
187 to: "expanded"
188 SequentialAnimation {
189 LomiriNumberAnimation {
190 target: title
191 property: "x"
192 to: (listView.currentItem.x - listView.contentX)
193 }
194 PropertyAction {
195 target: title
196 property: "visible"
197 value: false
198 }
199 LomiriNumberAnimation {
200 target: listView
201 property: "opacity"
202 from: 0.0
203 to: 1.0
204 }
205 }
206 },
207 Transition {
208 to: ""
209 SequentialAnimation {
210 LomiriNumberAnimation {
211 target: listView
212 property: "opacity"
213 from: 1.0
214 to: 0.0
215 }
216 PropertyAction {
217 target: title
218 property: "visible"
219 value: true
220 }
221 LomiriNumberAnimation {
222 target: title
223 property: "x"
224 to: 0
225 }
226 }
227 }
228 ]
229 }