Unity 8
 All Classes Functions Properties
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 
23 Item {
24  id: dashContent
25 
26  property var model: null
27  property var scopes: null
28  readonly property alias currentIndex: dashContentList.currentIndex
29 
30  signal scopeLoaded(string scopeId)
31  signal gotoScope(string scopeId)
32  signal openScope(var scope)
33  signal closePreview()
34 
35  // If we set the current scope index before the scopes have been added,
36  // then we need to wait until the loaded signals gets emitted from the scopes
37  property var set_current_index: undefined
38  Connections {
39  target: scopes
40  onLoadedChanged: {
41  if (scopes.loaded && set_current_index != undefined) {
42  setCurrentScopeAtIndex(set_current_index[0], set_current_index[1], set_current_index[2]);
43  set_current_index = undefined;
44  }
45  }
46  }
47 
48  function setCurrentScopeAtIndex(index, animate, reset) {
49  // if the scopes haven't loaded yet, then wait until they are.
50  if (!scopes.loaded) {
51  set_current_index = [ index, animate, reset ]
52  return;
53  }
54 
55  var storedMoveDuration = dashContentList.highlightMoveDuration
56  var storedMoveSpeed = dashContentList.highlightMoveVelocity
57  if (!animate) {
58  dashContentList.highlightMoveVelocity = units.gu(4167)
59  dashContentList.highlightMoveDuration = 0
60  }
61 
62  set_current_index = undefined;
63 
64  if (dashContentList.count > index)
65  {
66  dashContentList.currentIndex = index
67 
68  if (reset) {
69  dashContentList.currentItem.item.positionAtBeginning()
70  }
71  }
72 
73  if (!animate) {
74  dashContentList.highlightMoveDuration = storedMoveDuration
75  dashContentList.highlightMoveVelocity = storedMoveSpeed
76  }
77  }
78 
79  function closeScope(scope) {
80  dashContentList.currentItem.theScope.closeScope(scope)
81  }
82 
83  Item {
84  id: dashContentListHolder
85 
86  anchors.fill: parent
87 
88  ListView {
89  id: dashContentList
90  objectName: "dashContentList"
91 
92  interactive: dashContent.scopes.loaded && currentItem && !currentItem.moving
93 
94  anchors.fill: parent
95  model: dashContent.model
96  orientation: ListView.Horizontal
97  boundsBehavior: Flickable.DragAndOvershootBounds
98  flickDeceleration: units.gu(625)
99  maximumFlickVelocity: width * 5
100  snapMode: ListView.SnapOneItem
101  highlightMoveDuration: 250
102  highlightRangeMode: ListView.StrictlyEnforceRange
103  // TODO Investigate if we can switch to a smaller cache buffer when/if UbuntuShape gets more performant
104  cacheBuffer: 1073741823
105  onMovementStarted: currentItem.item.showHeader();
106  clip: parent.x != 0
107 
108  // If the number of items is less than the current index, then need to reset to another item.
109  onCountChanged: {
110  if (count > 0) {
111  if (currentIndex >= count) {
112  dashContent.setCurrentScopeAtIndex(count-1, true, true)
113  } else if (currentIndex < 0) {
114  // setting currentIndex directly, cause we don't want to loose set_current_index
115  dashContentList.currentIndex = 0
116  }
117  }
118  }
119 
120  delegate:
121  Loader {
122  width: ListView.view.width
123  height: ListView.view.height
124  asynchronous: true
125  // TODO This if will eventually go away since we're killing DashApps.qml
126  // once we move app closing to the spread
127  source: (scope.id == "clickscope") ? "DashApps.qml" : "GenericScopeView.qml"
128  objectName: scope.id + " loader"
129 
130  readonly property bool moving: item ? item.moving : false
131  readonly property var categoryView: item ? item.categoryView : null
132  readonly property var theScope: scope
133 
134  // these are needed for autopilot tests
135  readonly property string scopeId: scope.id
136  readonly property bool isCurrent: ListView.isCurrentItem
137  readonly property bool isLoaded: status == Loader.Ready
138 
139  onLoaded: {
140  item.objectName = scope.id
141  item.scope = Qt.binding(function() { return scope })
142  item.isCurrent = Qt.binding(function() { return visible && ListView.isCurrentItem })
143  dashContent.scopeLoaded(item.scope.id)
144  }
145  Connections {
146  target: isCurrent ? scope : null
147  onGotoScope: {
148  // Note here scopeId is the signal parameter and not the loader property
149  dashContent.gotoScope(scopeId);
150  }
151  onOpenScope: {
152  dashContent.openScope(scope);
153  }
154  }
155  Connections {
156  target: dashContent
157  onClosePreview: if (item) item.closePreview()
158  }
159 
160  Component.onDestruction: active = false
161  }
162  }
163  }
164 }