Unity 8
 All Classes Functions
DefaultIndicatorPage.qml
1 /*
2  * Copyright 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 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  * Authors:
17  * Renato Araujo Oliveira Filho <renato@canonical.com>
18  * Nick Dedekind <nick.dedekind@canonical.com>
19  */
20 
21 import QtQuick 2.0
22 import Ubuntu.Components 0.1 as Components
23 import Unity.Indicators 0.1 as Indicators
24 
25 IndicatorBase {
26  id: main
27 
28  //const
29  property string title: rootActionState.title
30  property alias highlightFollowsCurrentItem : mainMenu.highlightFollowsCurrentItem
31 
32  Indicators.UnityMenuModelStack {
33  id: menuStack
34  head: main.menuModel
35 
36  property var rootMenu: null
37 
38  onTailChanged: {
39  if (!tail) {
40  rootMenu = null;
41  } else if (rootMenu != tail) {
42  if (tail.get(0, "type") === rootMenuType) {
43  rootMenu = menuStack.tail.submenu(0);
44  push(rootMenu, 0);
45  } else {
46  rootMenu = null;
47  }
48  }
49  }
50  }
51 
52  Connections {
53  target: menuStack.tail
54  onRowsInserted: {
55  if (menuStack.rootMenu !== menuStack.tail && menuStack.tail.get(0, "type") === rootMenuType) {
56  menuStack.rootMenu = menuStack.tail.submenu(0);
57  menuStack.push(menuStack.rootMenu, 0);
58  }
59  }
60  onModelReset: {
61  if (menuStack.rootMenu !== menuStack.tail && menuStack.tail.get(0, "type") === rootMenuType) {
62  menuStack.rootMenu = menuStack.tail.submenu(0);
63  menuStack.push(menuStack.rootMenu, 0);
64  }
65  }
66  }
67 
68  ListView {
69  id: mainMenu
70  objectName: "mainMenu"
71  model: menuStack.rootMenu
72 
73  anchors {
74  fill: parent
75  bottomMargin: Qt.inputMethod.visible ? (Qt.inputMethod.keyboardRectangle.height - main.anchors.bottomMargin) : 0
76 
77  Behavior on bottomMargin {
78  NumberAnimation {
79  duration: 175
80  easing.type: Easing.OutQuad
81  }
82  }
83  // TODO - does ever frame.
84  onBottomMarginChanged: {
85  mainMenu.positionViewAtIndex(mainMenu.currentIndex, ListView.End)
86  }
87  }
88 
89  // Don't load all the delegates (only max of 3 pages worth -1/0/+1)
90  cacheBuffer: Math.max(height * 3, units.gu(70))
91 
92  // Only allow flicking if the content doesn't fit on the page
93  interactive: contentHeight > height
94  // FIXME - https://bugreports.qt-project.org/browse/QTBUG-41207
95  boundsBehavior: Flickable.StopAtBounds
96 
97  property int selectedIndex: -1
98  property bool blockCurrentIndexChange: false
99  // for count = 0
100  onCountChanged: {
101  if (count == 0 && selectedIndex != -1) {
102  selectedIndex = -1;
103  }
104  }
105  // for highlight following
106  onSelectedIndexChanged: {
107  if (currentIndex != selectedIndex) {
108  var blocked = blockCurrentIndexChange;
109  blockCurrentIndexChange = true;
110 
111  currentIndex = selectedIndex;
112 
113  blockCurrentIndexChange = blocked;
114  }
115  }
116  // for item addition/removal
117  onCurrentIndexChanged: {
118  if (!blockCurrentIndexChange) {
119  if (selectedIndex != -1 && selectedIndex != currentIndex) {
120  selectedIndex = currentIndex;
121  }
122  }
123  }
124 
125  delegate: Loader {
126  id: loader
127  objectName: "menuItem" + index
128  asynchronous: true
129  width: ListView.view.width
130  visible: status == Loader.Ready
131 
132  property int modelIndex: index
133  sourceComponent: factory.load(model, main.identifier)
134 
135  onLoaded: {
136  if (item.hasOwnProperty("selected")) {
137  item.selected = mainMenu.selectedIndex == index;
138  }
139  if (item.hasOwnProperty("menuSelected")) {
140  item.menuSelected.connect(function() { mainMenu.selectedIndex = index; });
141  }
142  if (item.hasOwnProperty("menuDeselected")) {
143  item.menuDeselected.connect(function() { mainMenu.selectedIndex = -1; });
144  }
145  if (item.hasOwnProperty("menuData")) {
146  item.menuData = Qt.binding(function() { return model; });
147  }
148  if (item.hasOwnProperty("menuIndex")) {
149  item.menuIndex = Qt.binding(function() { return modelIndex; });
150  }
151  }
152 
153  Binding {
154  target: item ? item : null
155  property: "objectName"
156  value: model.action
157  }
158 
159  // TODO: Fixes lp#1243146
160  // This is a workaround for a Qt bug. https://bugreports.qt-project.org/browse/QTBUG-34351
161  Connections {
162  target: mainMenu
163  onSelectedIndexChanged: {
164  if (loader.item && loader.item.hasOwnProperty("selected")) {
165  loader.item.selected = mainMenu.selectedIndex == index;
166  }
167  }
168  }
169  }
170  }
171 
172  MenuItemFactory {
173  id: factory
174  rootModel: main.menuModel ? main.menuModel : null
175  menuModel: mainMenu.model ? mainMenu.model : null
176  }
177 
178  function reset()
179  {
180  mainMenu.positionViewAtBeginning();
181  }
182 }