on 0bc5663 help sign in
unity8/Dash/GenericScopeView.qml
330 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 item.canEnableTerminationMode = Qt.binding(function() { return scopeView.isCurrent })
101 } else {
102 item.model = Qt.binding(function() { return results })
103 }
104 item.objectName = Qt.binding(function() { return categoryId })
105 if (item.expandable) {
106 var shouldFilter = categoryId != categoryView.expandedCategoryId;
107 if (shouldFilter != item.filter) {
108 item.filter = shouldFilter;
109 }
110 }
111 }
113 Component.onDestruction: {
114 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
115 scopeView.enableHeightBehaviorOnNextCreation = item.enableHeightBehaviorOnNextCreation;
116 }
117 }
119 Connections {
120 target: rendererLoader.item
121 onClicked: {
122 effect.positionPx = mapToItem(categoryView, 0, itemY).y
123 scopeView.scope.activate(delegateItem.model.uri,
124 delegateItem.model.icon,
125 delegateItem.model.category,
126 0,
127 delegateItem.model.mimetype,
128 delegateItem.model.title,
129 delegateItem.model.comment,
130 delegateItem.model.dndUri,
131 delegateItem.model.metadata)
132 }
133 onPressAndHold: {
134 effect.positionPx = mapToItem(categoryView, 0, itemY).y
135 scopeView.scope.preview( delegateItem.model.uri,
136 delegateItem.model.icon,
137 delegateItem.model.category,
138 0,
139 delegateItem.model.mimetype,
140 delegateItem.model.title,
141 delegateItem.model.comment,
142 delegateItem.model.dndUri,
143 delegateItem.model.metadata)
144 }
145 }
146 Connections {
147 target: categoryView
148 onExpandedCategoryIdChanged: {
149 var item = rendererLoader.item;
150 if (item.expandable) {
151 var shouldFilter = categoryId != categoryView.expandedCategoryId;
152 if (shouldFilter != item.filter) {
153 // If the filter animation will be seen start it, otherwise, just flip the switch
154 var shrinkingVisible = shouldFilter && y + item.collapsedHeight < categoryView.height;
155 var growingVisible = !shouldFilter && y + height < categoryView.height;
156 if (shrinkingVisible || growingVisible) {
157 item.startFilterAnimation(shouldFilter)
158 } else {
159 item.filter = shouldFilter;
160 }
161 if (!shouldFilter) {
162 categoryView.maximizeVisibleArea(index, item.uncollapsedHeight);
163 }
164 }
165 }
166 }
167 }
168 }
169 }
171 sectionProperty: "name"
172 sectionDelegate: ListItems.Header {
173 property var delegate: categoryView.item(delegateIndex)
174 width: categoryView.width
175 text: section
176 image: {
177 if (delegate && delegate.expandable)
178 return delegate.filtered ? "graphics/header_handlearrow.png" : "graphics/header_handlearrow2.png"
179 return "";
180 }
181 onClicked: {
182 if (categoryView.expandedCategoryId != delegate.category)
183 categoryView.expandedCategoryId = delegate.category;
184 else
185 categoryView.expandedCategoryId = "";
186 }
187 }
188 pageHeader: PageHeader {
189 id: pageHeader
190 objectName: "pageHeader"
191 width: categoryView.width
192 text: scopeView.scope.name
193 searchEntryEnabled: true
194 scope: scopeView.scope
195 }
196 }
198 function getDefaultRendererId(contentType) {
199 switch (contentType) {
200 default: return "grid";
201 }
202 }
204 function getRenderer(rendererId, contentType, rendererHint) {
205 if (rendererId == "default") {
206 rendererId = getDefaultRendererId(contentType);
207 }
208 switch (rendererId) {
209 case "grid": {
210 switch (contentType) {
211 case "video": return "Video/VideoFilterGrid.qml";
212 case "music": return "Music/MusicFilterGrid.qml";
213 case "apps": {
214 if (rendererHint == "toggled")
215 return "Apps/DashPluginFilterGrid.qml";
216 else
217 return "Generic/GenericFilterGrid.qml";
218 }
219 case "weather": return "Generic/WeatherFilterGrid.qml";
220 default: return "Generic/GenericFilterGrid.qml";
221 }
222 }
223 case "carousel": {
224 switch (contentType) {
225 case "music": return "Music/MusicCarousel.qml";
226 default: return "Generic/GenericCarousel.qml";
227 }
228 }
229 case "special": {
230 switch (contentType) {
231 case "apps": return "Apps/RunningApplicationsGrid.qml";
232 default: return "Generic/GenericFilterGrid.qml";
233 }
234 }
235 default: return "Generic/GenericFilterGrid.qml";
236 }
237 }
239 OpenEffect {
240 id: effect
241 anchors {
242 fill: parent
243 bottomMargin: -bottomOverflow
244 }
245 sourceItem: categoryView
247 enabled: gap > 0.0
249 topGapPx: (1 - gap) * positionPx
250 topOpacity: (1 - gap * 1.2)
251 bottomGapPx: positionPx + gap * (targetBottomGapPx - positionPx)
252 bottomOverflow: units.gu(20)
253 bottomOpacity: 1 - (gap * 0.8)
255 property int targetBottomGapPx: height - units.gu(8) - bottomOverflow
256 property real gap: previewLoader.open ? 1.0 : 0.0
258 Behavior on gap {
259 NumberAnimation {
260 duration: 200
261 easing.type: Easing.InOutQuad
262 onRunningChanged: {
263 if (!previewLoader.open && !running) {
264 previewLoader.onScreen = false
265 }
266 }
267 }
268 }
269 }
271 Connections {
272 target: scopeView.scope
273 onPreviewReady: {
274 previewLoader.previewData = preview
275 previewLoader.open = true
276 }
277 }
279 PreviewDelegateMapper {
280 id: previewDelegateMapper
281 }
283 Connections {
284 ignoreUnknownSignals: true
285 target: previewLoader.valid ? previewLoader.item : null
286 onClose: {
287 previewLoader.open = false
288 }
289 }
291 Loader {
292 objectName: "previewLoader"
293 id: previewLoader
294 property var previewData
295 height: effect.bottomGapPx - effect.topGapPx
296 anchors {
297 top: parent.top
298 topMargin: effect.topGapPx
299 left: parent.left
300 right: parent.right
301 }
302 source: onScreen ? previewDelegateMapper.map(previewLoader.previewData.rendererName) : ""
304 property bool open: false
305 property bool onScreen: false
306 property bool valid: item !== null
308 onOpenChanged: {
309 if (open) {
310 onScreen = true
311 }
312 }
314 onLoaded: {
315 item.previewData = Qt.binding(function() { return previewLoader.previewData })
316 }
317 }
319 // TODO: Move as InverseMouseArea to DashPreview
320 MouseArea {
321 enabled: previewLoader.onScreen
322 anchors {
323 fill: parent
324 topMargin: effect.bottomGapPx
325 }
326 onClicked: {
327 previewLoader.open = false;
328 }
329 }