on 492e5ad help sign in
unity8/Dash/GenericScopeView.qml
321 lines 12 kB history raw
1/*
2 * Copyright (C) 2013 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 */
17import QtQuick 2.0
18import Ubuntu.Components 0.1
19import "../Components"
20import "../Components/ListItems" as ListItems
22ScopeView {
23 id: scopeView
24 readonly property alias previewShown: previewLoader.onScreen
25 property bool enableHeightBehaviorOnNextCreation: false
27 onIsCurrentChanged: {
28 pageHeader.resetSearch();
29 previewLoader.open = false;
30 }
32 onMovementStarted: categoryView.showHeader()
34 onPositionedAtBeginning: categoryView.positionAtBeginning()
36 Binding {
37 target: scopeView.scope
38 property: "searchQuery"
39 value: pageHeader.searchQuery
40 }
42 Binding {
43 target: pageHeader
44 property: "searchQuery"
45 value: scopeView.scope.searchQuery
46 }
48 Connections {
49 target: panel
50 onSearchClicked: if (isCurrent) {
51 pageHeader.triggerSearch()
52 categoryView.showHeader()
53 }
54 }
56 Connections {
57 target: scopeView.scope
58 onShowDash: previewLoader.open = false;
59 onHideDash: previewLoader.open = false;
60 }
62 ScopeListView {
63 id: categoryView
64 objectName: "categoryListView"
65 anchors.fill: parent
66 model: scopeView.categories
67 forceNoClip: previewLoader.onScreen
69 onAtYEndChanged: if (atYEnd) endReached()
70 onMovingChanged: if (moving && atYEnd) endReached()
72 property string expandedCategoryId: ""
74 delegate: ListItems.Base {
75 highlightWhenPressed: false
77 readonly property bool expandable: rendererLoader.item ? rendererLoader.item.expandable : false
78 readonly property bool filtered: rendererLoader.item ? rendererLoader.item.filter : true
79 readonly property string category: categoryId
81 Loader {
82 id: rendererLoader
83 anchors {
84 top: parent.top
85 left: parent.left
86 right: parent.right
87 }
89 source: getRenderer(model.renderer, model.contentType, model.rendererHint)
91 onLoaded: {
92 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
93 item.enableHeightBehavior = scopeView.enableHeightBehaviorOnNextCreation;
94 scopeView.enableHeightBehaviorOnNextCreation = false;
95 }
96 if (source.toString().indexOf("Apps/RunningApplicationsGrid.qml") != -1) {
97 // TODO: the running apps grid doesn't support standard scope results model yet
98 item.firstModel = Qt.binding(function() { return results.firstModel })
99 item.secondModel = Qt.binding(function() { return results.secondModel })
100 } else {
101 item.model = Qt.binding(function() { return results })
102 }
103 item.objectName = Qt.binding(function() { return categoryId })
104 if (item.expandable) {
105 var shouldFilter = categoryId != categoryView.expandedCategoryId;
106 if (shouldFilter != item.filter) {
107 item.filter = shouldFilter;
108 }
109 }
110 }
112 Component.onDestruction: {
113 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
114 scopeView.enableHeightBehaviorOnNextCreation = item.enableHeightBehaviorOnNextCreation;
115 }
116 }
118 Connections {
119 target: rendererLoader.item
120 onClicked: {
121 effect.positionPx = mapToItem(categoryView, 0, itemY).y
122 scopeView.scope.activate(delegateItem.model.uri,
123 delegateItem.model.icon,
124 delegateItem.model.category,
125 0,
126 delegateItem.model.mimetype,
127 delegateItem.model.title,
128 delegateItem.model.comment,
129 delegateItem.model.dndUri,
130 delegateItem.model.metadata)
131 }
132 onPressAndHold: {
133 effect.positionPx = mapToItem(categoryView, 0, itemY).y
134 scopeView.scope.preview( delegateItem.model.uri,
135 delegateItem.model.icon,
136 delegateItem.model.category,
137 0,
138 delegateItem.model.mimetype,
139 delegateItem.model.title,
140 delegateItem.model.comment,
141 delegateItem.model.dndUri,
142 delegateItem.model.metadata)
143 }
144 }
145 Connections {
146 target: categoryView
147 onExpandedCategoryIdChanged: {
148 var item = rendererLoader.item;
149 if (item.expandable) {
150 var shouldFilter = categoryId != categoryView.expandedCategoryId;
151 if (shouldFilter != item.filter) {
152 // If the filter animation will be seen start it, otherwise, just flip the switch
153 var shrinkingVisible = shouldFilter && y + item.collapsedHeight < categoryView.height;
154 var growingVisible = !shouldFilter && y + height < categoryView.height;
155 if (shrinkingVisible || growingVisible) {
156 item.startFilterAnimation(shouldFilter)
157 } else {
158 item.filter = shouldFilter;
159 }
160 if (!shouldFilter) {
161 categoryView.maximizeVisibleArea(index, item.uncollapsedHeight);
162 }
163 }
164 }
165 }
166 }
167 }
168 }
170 sectionProperty: "name"
171 sectionDelegate: ListItems.Header {
172 property var delegate: categoryView.item(delegateIndex)
173 width: categoryView.width
174 text: section
175 image: {
176 if (delegate && delegate.expandable)
177 return delegate.filtered ? "graphics/header_handlearrow.png" : "graphics/header_handlearrow2.png"
178 return "";
179 }
180 onClicked: {
181 if (categoryView.expandedCategoryId != delegate.category)
182 categoryView.expandedCategoryId = delegate.category;
183 else
184 categoryView.expandedCategoryId = "";
185 }
186 }
187 pageHeader: PageHeader {
188 id: pageHeader
189 objectName: "pageHeader"
190 width: categoryView.width
191 text: scopeView.scope.name
192 searchEntryEnabled: true
193 }
194 }
196 function getDefaultRendererId(contentType) {
197 switch (contentType) {
198 default: return "grid";
199 }
200 }
202 function getRenderer(rendererId, contentType, rendererHint) {
203 if (rendererId == "default") {
204 rendererId = getDefaultRendererId(contentType);
205 }
206 switch (rendererId) {
207 case "grid": {
208 switch (contentType) {
209 case "video": return "Generic/GenericFilterGridPotrait.qml";
210 case "music": return "Music/MusicFilterGrid.qml";
211 default: return "Generic/GenericFilterGrid.qml";
212 }
213 }
214 case "carousel": {
215 switch (contentType) {
216 case "music": return "Music/MusicCarousel.qml";
217 default: return "Generic/GenericCarousel.qml";
218 }
219 }
220 case "special": {
221 switch (contentType) {
222 case "apps": return "Apps/RunningApplicationsGrid.qml";
223 default: return "Generic/GenericFilterGrid.qml";
224 }
225 }
226 default: return "Generic/GenericFilterGrid.qml";
227 }
228 }
230 OpenEffect {
231 id: effect
232 anchors {
233 fill: parent
234 bottomMargin: -bottomOverflow
235 }
236 sourceItem: categoryView
238 enabled: gap > 0.0
240 topGapPx: (1 - gap) * positionPx
241 topOpacity: (1 - gap * 1.2)
242 bottomGapPx: positionPx + gap * (targetBottomGapPx - positionPx)
243 bottomOverflow: units.gu(20)
244 bottomOpacity: 1 - (gap * 0.8)
246 property int targetBottomGapPx: height - units.gu(8) - bottomOverflow
247 property real gap: previewLoader.open ? 1.0 : 0.0
249 Behavior on gap {
250 NumberAnimation {
251 duration: 200
252 easing.type: Easing.InOutQuad
253 onRunningChanged: {
254 if (!previewLoader.open && !running) {
255 previewLoader.onScreen = false
256 }
257 }
258 }
259 }
260 }
262 Connections {
263 target: scopeView.scope
264 onPreviewReady: {
265 previewLoader.previewData = preview
266 previewLoader.open = true
267 }
268 }
270 PreviewDelegateMapper {
271 id: previewDelegateMapper
272 }
274 Connections {
275 ignoreUnknownSignals: true
276 target: previewLoader.valid ? previewLoader.item : null
277 onClose: {
278 previewLoader.open = false
279 }
280 }
282 Loader {
283 objectName: "previewLoader"
284 id: previewLoader
285 property var previewData
286 height: effect.bottomGapPx - effect.topGapPx
287 anchors {
288 top: parent.top
289 topMargin: effect.topGapPx
290 left: parent.left
291 right: parent.right
292 }
293 source: onScreen ? previewDelegateMapper.map(previewLoader.previewData.rendererName) : ""
295 property bool open: false
296 property bool onScreen: false
297 property bool valid: item !== null
299 onOpenChanged: {
300 if (open) {
301 onScreen = true
302 }
303 }
305 onLoaded: {
306 item.previewData = Qt.binding(function() { return previewLoader.previewData })
307 }
308 }
310 // TODO: Move as InverseMouseArea to DashPreview
311 MouseArea {
312 enabled: previewLoader.onScreen
313 anchors {
314 fill: parent
315 topMargin: effect.bottomGapPx
316 }
317 onClicked: {
318 previewLoader.open = false;
319 }
320 }