on 22208df help sign in
unity8/Panel/Indicators/DefaultIndicatorPage.qml
218 lines 6.6 kB history raw
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 */
21import QtQuick 2.0
22import Ubuntu.Components 0.1 as Components
23import Unity.Indicators 0.1 as Indicators
25Indicators.IndicatorBase {
26 id: main
28 //const
29 property bool contentActive: false
30 property string title: rootActionState.title
31 property alias emptyText: emptyLabel.text
32 property alias highlightFollowsCurrentItem : mainMenu.highlightFollowsCurrentItem
34 Indicators.UnityMenuModelStack {
35 id: menuStack
36 head: contentActive ? main.menuModel : null
38 property var rootMenu: null
40 onTailChanged: {
41 if (!tail) {
42 rootMenu = null;
43 } else if (rootMenu != tail) {
44 if (tail.get(0, "type") === rootMenuType) {
45 rootMenu = menuStack.tail.submenu(0);
46 push(rootMenu, 0);
47 } else {
48 rootMenu = null;
49 }
50 }
51 }
52 }
54 Connections {
55 target: menuStack.tail
56 onRowsInserted: {
57 if (menuStack.rootMenu !== menuStack.tail && menuStack.tail.get(0, "type") === rootMenuType) {
58 menuStack.rootMenu = menuStack.tail.submenu(0);
59 menuStack.push(menuStack.rootMenu, 0);
60 }
61 }
62 onModelReset: {
63 if (menuStack.rootMenu !== menuStack.tail && menuStack.tail.get(0, "type") === rootMenuType) {
64 menuStack.rootMenu = menuStack.tail.submenu(0);
65 menuStack.push(menuStack.rootMenu, 0);
66 }
67 }
68 }
70 ListView {
71 id: mainMenu
72 objectName: "mainMenu"
73 model: menuStack.rootMenu
75 anchors {
76 fill: parent
77 bottomMargin: Qt.inputMethod.visible ? (Qt.inputMethod.keyboardRectangle.height - main.anchors.bottomMargin) : 0
79 Behavior on bottomMargin {
80 NumberAnimation {
81 duration: 175
82 easing.type: Easing.OutQuad
83 }
84 }
85 // TODO - does ever frame.
86 onBottomMarginChanged: {
87 mainMenu.positionViewAtIndex(mainMenu.currentIndex, ListView.End)
88 }
89 }
91 // Ensure all delegates are cached in order to improve smoothness of scrolling
92 cacheBuffer: 10000
94 // Only allow flicking if the content doesn't fit on the page
95 interactive: contentHeight > height
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;
111 currentIndex = selectedIndex;
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 }
125 delegate: Loader {
126 id: loader
127 objectName: "menuItem" + index
128 asynchronous: false
129 visible: height > 0
131 property int modelIndex: index
133 anchors {
134 left: parent.left
135 right: parent.right
136 }
138 sourceComponent: factory.load(model)
140 onLoaded: {
141 if (item.hasOwnProperty("menuSelected")) {
142 item.menuSelected = mainMenu.selectedIndex == index;
143 item.selectMenu.connect(function() { mainMenu.selectedIndex = index; });
144 item.deselectMenu.connect(function() { mainMenu.selectedIndex = -1; });
145 }
146 if (item.hasOwnProperty("menuData")) {
147 item.menuData = Qt.binding(function() { return model; });
148 }
149 if (item.hasOwnProperty("menuIndex")) {
150 item.menuIndex = Qt.binding(function() { return modelIndex; });
151 }
152 }
154 Binding {
155 target: item ? item : null
156 property: "objectName"
157 value: model.action
158 }
160 // TODO: Fixes lp#1243146
161 // This is a workaround for a Qt bug. https://bugreports.qt-project.org/browse/QTBUG-34351
162 Connections {
163 target: mainMenu
164 onSelectedIndexChanged: {
165 if (loader.item && loader.item.hasOwnProperty("menuSelected")) {
166 loader.item.menuSelected = mainMenu.selectedIndex == index;
167 }
168 }
169 }
170 }
171 }
173 Indicators.MenuItemFactory {
174 id: factory
175 menuModel: mainMenu.model ? mainMenu.model : null
176 }
178 Components.Label {
179 id: emptyLabel
180 objectName: "emptyLabel"
181 visible: mainMenu.count == 0
182 anchors {
183 top: parent.top
184 left: parent.left
185 right: parent.right
186 topMargin: units.gu(2)
187 }
188 wrapMode: Text.WordWrap
189 horizontalAlignment: Text.AlignHCenter
191 //style
192 color: "#e8e1d0"
193 fontSize: "medium"
195 text: "Empty!"
196 }
198 function start()
199 {
200 reset()
201 if (!contentActive) {
202 contentActive = true;
203 }
204 }
206 function stop()
207 {
208 if (contentActive) {
209 contentActive = false;
210 }
211 }
213 function reset()
214 {
215 mainMenu.selectedIndex = -1;
216 mainMenu.positionViewAtBeginning();
217 }