Debian Codegraph @

src/imports/Components/1.3/LomiriListView.qml

raw
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 1.3 as Toolkit
19
20 /*!
21 \qmltype LomiriListView
22 \inqmlmodule Lomiri.Components
23 \ingroup lomiri
24 \inherits ListView
25 \brief A ListView with special features tailored for a look and feel fitting the
26 Lomiri Touch platform.
27 The LomiriListView works just like a regular ListView, but it adds special features
28 such as expanding/collapsing items (when used together with the Expandable item).
29 It provides features like automatically positioning the expanding item when it
30 expands and collapsing it again when the user taps outside of it.
31
32 Examples:
33 \qml
34 import Lomiri.Components 1.3
35 import Lomiri.Components.ListItems 1.3 as ListItem
36
37 Item {
38 ListModel {
39 id: listModel
40 }
41
42 LomiriListView {
43 id: lomiriListView
44 anchors { left: parent.left; right: parent.right }
45 height: units.gu(24)
46 model: listModel
47
48 delegate: ListItem.Expandable {
49 id: expandingItem
50
51 expandedHeight: units.gu(30)
52
53 onClicked: {
54 lomiriListView.expandedIndex = index;
55 }
56 }
57 }
58 }
59 \endqml
60 */
61
62 ListView {
63 id: root
64
65 /*!
66 \deprecated
67 The index of the currently expanded item. -1 if no item is expanded.
68 \b {THIS PROPERTY IS DEPRECATED. USE THE NEW ListItem TO HANDLE EXPANSION}
69 */
70 property int expandedIndex: -1
71
72 /*!
73 \qmlproperty pullToRefresh LomiriListView::pullToRefresh
74 \readonly
75 \since Lomiri.Components 1.1
76
77 Attached PullToRefresh to control manual model refresh. The component is disabled
78 by default.
79
80 \qml
81 import QtQuick 2.4
82 import QtQuick.XmlListModel 2.0
83 import Lomiri.Components 1.3
84
85 LomiriListView {
86 width: units.gu(40)
87 height: units.gu(71)
88 model: XmlListModel {
89 source: "http://feeds.reuters.com/reuters/topNews"
90 query: "/rss/channel/item"
91 XmlRole { name: "title"; query: "title/string()" }
92 }
93 // let refresh control know when the refresh gets completed
94 pullToRefresh {
95 enabled: true
96 refreshing: model.status === XmlListModel.Loading
97 onRefresh: model.reload()
98 }
99 delegate: ListItem {
100 ListItemLayout {
101 title.text: modelData
102 }
103 onClicked: {
104 ListView.view.model.reload();
105 }
106 }
107 }
108 \endqml
109 */
110 readonly property alias pullToRefresh: refreshItem
111
112 PullToRefresh {
113 objectName: "listview_pulltorefresh"
114 id: refreshItem
115 enabled: false
116 }
117
118 QtObject {
119 id: priv
120
121 function positionViewAtIndexAnimated(expandedIndex) {
122 animation.from = root.contentY;
123 root.currentIndex = expandedIndex;
124 if (expandedIndex == root.count - 1) {
125 root.positionViewAtIndex(expandedIndex, ListView.End);
126 } else {
127 root.positionViewAtIndex(expandedIndex + 1, ListView.End);
128 }
129
130 var effectiveExpandedHeight = Math.min(root.currentItem.expandedHeight, root.height - root.currentItem.collapsedHeight);
131 if (root.contentY - root.originY == 0) {
132 if (((root.currentIndex + 1) * root.currentItem.collapsedHeight) + effectiveExpandedHeight > root.height) {
133 animation.to = ((root.currentIndex + 1) * root.currentItem.collapsedHeight + effectiveExpandedHeight) - root.height + root.originY
134 } else {
135 animation.to = root.originY
136 }
137 } else {
138 animation.to = root.contentY + (effectiveExpandedHeight - root.currentItem.collapsedHeight);
139 }
140 animation.start();
141 }
142
143 function requestFocus(reason) {
144 // lookup for the currentItem, and if it is a FocusScope, focus the view
145 // this will also focus the currentItem
146 if (root.currentItem && root.currentItem.hasOwnProperty("activeFocusOnPress")) {
147 // is the currentItem a ListItem?
148 if (QuickUtils.inherits(root.currentItem, "UCListItem")) {
149 root.currentItem.requestFocus(reason);
150 } else {
151 root.forceActiveFocus(reason);
152 }
153 }
154 }
155 }
156
157 focus: true
158
159 /*!
160 \internal
161 Grab focus when moved, flicked or clicked
162 */
163 onMovementStarted: priv.requestFocus(Qt.MouseFocusReason)
164 onFlickStarted: priv.requestFocus(Qt.MouseFocusReason)
165 Toolkit.Mouse.onClicked: priv.requestFocus(Qt.MouseFocusReason)
166
167 /*!
168 Expand the item at the given index.
169 */
170 onExpandedIndexChanged: {
171 if (expandedIndex < 0) {
172 return;
173 }
174 priv.positionViewAtIndexAnimated(expandedIndex, ListView.End)
175 }
176
177 LomiriNumberAnimation {
178 id: animation
179 target: root
180 property: "contentY"
181 }
182
183 MouseArea {
184 parent: contentItem
185 anchors.fill: parent
186 z: 2
187 enabled: root.expandedIndex != -1
188 onClicked: root.expandedIndex = -1;
189 }
190
191 // animate move displaced
192 moveDisplaced: Transition {
193 LomiriNumberAnimation {
194 properties: "x,y"
195 }
196 }
197
198 // highlight current item
199 highlight: Rectangle {
200 color: root.activeFocus
201 ? theme.palette.focused.background
202 : theme.palette.selected.background
203 width: root.currentItem ? root.currentItem.width : 0
204 height: root.currentItem ? root.currentItem.height : 0
205 // FIXME: use opacity yet, until we fix the palette's disabled.background color
206 opacity: root.currentItem && root.currentItem.enabled ? 1.0 : 0.5
207 }
208 highlightMoveDuration: 50
209 }