Unity 8
 All Classes Functions
DashContent.qml
1 /*
2  * Copyright (C) 2013, 2014 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.0
18 import Ubuntu.Components 0.1
19 import Unity 0.2
20 import Utils 0.1
21 import "../Components"
22 import "../Components/Flickables" as Flickables
23 
24 Item {
25  id: dashContent
26 
27  property bool forceNonInteractive: false
28  property alias scopes: dashContentList.model
29  readonly property alias currentIndex: dashContentList.currentIndex
30  readonly property string currentScopeId: dashContentList.currentItem ? dashContentList.currentItem.scopeId : ""
31  readonly property var currentScope: dashContentList.currentItem ? dashContentList.currentItem.theScope : null
32  readonly property bool subPageShown: dashContentList.currentItem && dashContentList.currentItem.item ?
33  dashContentList.currentItem.item.subPageShown : false
34  readonly property bool processing: dashContentList.currentItem && dashContentList.currentItem.item
35  && dashContentList.currentItem.item.processing || false
36  readonly property bool pageHeaderTotallyVisible: dashContentList.currentItem && dashContentList.currentItem.item
37  && dashContentList.currentItem.item.pageHeaderTotallyVisible || false
38 
39  signal scopeLoaded(string scopeId)
40  signal gotoScope(string scopeId)
41  signal openScope(var scope)
42  signal closePreview()
43 
44  // If we set the current scope index before the scopes have been added,
45  // then we need to wait until the loaded signals gets emitted from the scopes
46  property var set_current_index: undefined
47  Connections {
48  target: scopes
49  onLoadedChanged: {
50  if (scopes.loaded && set_current_index != undefined) {
51  setCurrentScopeAtIndex(set_current_index[0], set_current_index[1], set_current_index[2]);
52  set_current_index = undefined;
53  }
54  }
55  }
56 
57  Connections {
58  target: UriHandler
59  onOpened: dashContentList.currentItem.theScope.performQuery(uris[0])
60  }
61 
62  function setCurrentScopeAtIndex(index, animate, reset) {
63  // if the scopes haven't loaded yet, then wait until they are.
64  if (!scopes.loaded) {
65  set_current_index = [ index, animate, reset ]
66  return;
67  }
68 
69  var storedMoveDuration = dashContentList.highlightMoveDuration
70  var storedMoveSpeed = dashContentList.highlightMoveVelocity
71  if (!animate) {
72  dashContentList.highlightMoveVelocity = units.gu(4167)
73  dashContentList.highlightMoveDuration = 0
74  }
75 
76  set_current_index = undefined;
77 
78  if (dashContentList.count > index)
79  {
80  dashContentList.currentIndex = index
81 
82  if (reset) {
83  dashContentList.currentItem.item.positionAtBeginning()
84  }
85  }
86 
87  if (!animate) {
88  dashContentList.highlightMoveDuration = storedMoveDuration
89  dashContentList.highlightMoveVelocity = storedMoveSpeed
90  }
91  }
92 
93  Item {
94  id: dashContentListHolder
95 
96  anchors.fill: parent
97 
98  DashBackground {
99  anchors.fill: parent
100  }
101 
102  Flickables.ListView {
103  id: dashContentList
104  objectName: "dashContentList"
105 
106  interactive: !dashContent.forceNonInteractive && dashContent.scopes.loaded && currentItem
107  && !currentItem.moving && !currentItem.navigationShown && !currentItem.subPageShown
108  anchors.fill: parent
109  orientation: ListView.Horizontal
110  boundsBehavior: Flickable.DragAndOvershootBounds
111  snapMode: ListView.SnapOneItem
112  highlightMoveDuration: 250
113  highlightRangeMode: ListView.StrictlyEnforceRange
114  // TODO Investigate if we can switch to a smaller cache buffer when/if UbuntuShape gets more performant
115  // 1073741823 is s^30 -1. A quite big number so that you have "infinite" cache, but not so
116  // big so that if you add if with itself you're outside the 2^31 int range
117  cacheBuffer: 1073741823
118  onMovementStarted: currentItem.item.showHeader();
119  clip: parent.x != 0
120 
121  // TODO QTBUG-40846 and QTBUG-40848
122  // The remove transition doesn't happen when removing the last item
123  // And can't work around it because index is reset to -1 regardless of
124  // ListView.delayRemove
125 
126  remove: Transition {
127  SequentialAnimation {
128  PropertyAction { property: "layer.enabled"; value: true }
129  PropertyAction { property: "ListView.delayRemove"; value: true }
130  ParallelAnimation {
131  PropertyAnimation { properties: "scale"; to: 0.25; duration: UbuntuAnimation.SnapDuration }
132  PropertyAnimation { properties: "y"; to: dashContent.height; duration: UbuntuAnimation.SnapDuration }
133  }
134  PropertyAction { property: "ListView.delayRemove"; value: false }
135  }
136  }
137  removeDisplaced: Transition {
138  PropertyAnimation { property: "x"; duration: UbuntuAnimation.SnapDecision }
139  }
140 
141  // If the number of items is less than the current index, then need to reset to another item.
142  onCountChanged: {
143  if (count > 0) {
144  if (currentIndex >= count) {
145  dashContent.setCurrentScopeAtIndex(count-1, true, true)
146  } else if (currentIndex < 0) {
147  // setting currentIndex directly, cause we don't want to loose set_current_index
148  dashContentList.currentIndex = 0
149  }
150  }
151  }
152 
153  delegate:
154  Loader {
155  width: ListView.view.width
156  height: ListView.view.height
157  opacity: { // hide delegate if offscreen
158  var xPositionRelativetoView = ListView.view.contentX - x
159  return (xPositionRelativetoView > -width && xPositionRelativetoView < width) ? 1 : 0
160  }
161  asynchronous: true
162  source: "GenericScopeView.qml"
163  objectName: scope.id + " loader"
164 
165  readonly property bool moving: item ? item.moving : false
166  readonly property bool navigationShown: item ? item.navigationShown : false
167  readonly property bool subPageShown: item ? item.subPageShown : false
168  readonly property var categoryView: item ? item.categoryView : null
169  readonly property var theScope: scope
170 
171  // these are needed for autopilot tests
172  readonly property string scopeId: scope.id
173  readonly property bool isCurrent: ListView.isCurrentItem
174  readonly property bool isLoaded: status == Loader.Ready
175 
176  onLoaded: {
177  item.objectName = scope.id
178  item.scope = Qt.binding(function() { return scope })
179  item.isCurrent = Qt.binding(function() { return visible && ListView.isCurrentItem })
180  dashContent.scopeLoaded(item.scope.id)
181  item.paginationCount = Qt.binding(function() { return dashContentList.count } )
182  item.paginationIndex = Qt.binding(function() { return dashContentList.currentIndex } )
183  item.holdingList = dashContentList;
184  item.forceNonInteractive = Qt.binding(function() { return dashContent.forceNonInteractive } )
185  }
186  Connections {
187  target: isCurrent ? scope : null
188  onGotoScope: {
189  // Note here scopeId is the signal parameter and not the loader property
190  dashContent.gotoScope(scopeId);
191  }
192  onOpenScope: {
193  dashContent.openScope(scope);
194  }
195  }
196  Connections {
197  target: dashContent
198  onClosePreview: if (item) item.closePreview()
199  }
200 
201  Component.onDestruction: active = false
202  }
203  }
204  }
205 }