on 551c708 help sign in
unity8/plugins/Unity/Indicators/qml/MenuItemFactory.qml
316 lines 11 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 * Nick Dedekind <nick.dedekind@canonical.com>
18 */
20import QtQuick 2.0
21import Unity.Indicators 0.1 as Indicators
22import Unity.Indicators.Network 0.1 as ICNetwork
23import Unity.Indicators.Messaging 0.1 as ICMessaging
24import QMenuModel 0.1 as QMenuModel
25import Ubuntu.Components 0.1
27Item {
28 id: menuFactory
30 property var menuModel: null
32 property var _map: {
33 "unity.widgets.systemsettings.tablet.volumecontrol" : sliderMenu,
34 "unity.widgets.systemsettings.tablet.switch" : switchMenu,
36 "com.canonical.indicator.button" : buttonMenu,
37 "com.canonical.indicator.div" : divMenu,
38 "com.canonical.indicator.section" : sectionMenu,
39 "com.canonical.indicator.progress" : progressMenu,
40 "com.canonical.indicator.slider" : sliderMenu,
41 "com.canonical.indicator.switch" : switchMenu,
43 "com.canonical.indicator.messages.messageitem" : messageItem,
44 "com.canonical.indicator.messages.sourceitem" : groupedMessage,
46 "com.canonical.unity.slider" : sliderMenu,
47 "com.canonical.unity.switch" : switchMenu,
49 "unity.widgets.systemsettings.tablet.wifisection" : wifiSection,
50 "unity.widgets.systemsettings.tablet.accesspoint" : accessPoint,
51 }
53 function getExtendedProperty(object, propertyName, defaultValue) {
54 if (object && object.hasOwnProperty(propertyName)) {
55 return object[propertyName];
56 }
57 return defaultValue;
58 }
60 Component {
61 id: divMenu;
62 Indicators.DivMenuItem {
63 objectName: "divMenu"
64 }
65 }
67 Component {
68 id: sliderMenu;
69 Indicators.SliderMenuItem {
70 objectName: "sliderMenu"
71 property QtObject menuData: null
72 property var menuModel: menuFactory.menuModel
73 property int menuIndex: -1
74 property var extendedData: menuData && menuData.ext || undefined
76 text: menuData && menuData.label || ""
77 iconSource: menuData && menuData.icon || ""
78 minIcon: getExtendedProperty(extendedData, "minIcon", "")
79 maxIcon: getExtendedProperty(extendedData, "maxIcon", "")
81 minimumValue: getExtendedProperty(extendedData, "minValue", 0.0)
82 maximumValue: {
83 var maximum = getExtendedProperty(extendedData, "maxValue", 1.0);
84 if (maximum <= minimumValue) {
85 return minimumValue + 1;
86 }
87 return maximum;
88 }
89 value: menuData && menuData.actionState || 0.0
90 enabled: menuData && menuData.sensitive || false
92 onMenuModelChanged: {
93 loadAttributes();
94 }
95 onMenuIndexChanged: {
96 loadAttributes();
97 }
98 onChangeState: {
99 menuModel.changeState(menuIndex, value);
100 }
102 function loadAttributes() {
103 if (!menuModel || menuIndex == undefined) return;
104 menuModel.loadExtendedAttributes(menuIndex, {'min-value': 'double',
105 'max-value': 'double',
106 'min-icon': 'icon',
107 'max-icon': 'icon'});
108 }
109 }
110 }
112 Component {
113 id: buttonMenu;
114 Indicators.ButtonMenuItem {
115 objectName: "buttonMenu"
116 property QtObject menuData: null
117 property var menuModel: menuFactory.menuModel
118 property int menuIndex: -1
120 buttonText: menuData && menuData.label || ""
121 enabled: menuData && menuData.sensitive || false
123 onActivate: {
124 menuModel.activate(menuIndex);
125 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
126 }
127 }
128 }
129 Component {
130 id: sectionMenu;
131 Indicators.SectionMenuItem {
132 objectName: "sectionMenu"
133 property QtObject menuData: null
134 property var menuIndex: undefined
136 text: menuData && menuData.label || ""
137 }
138 }
140 Component {
141 id: progressMenu;
142 Indicators.ProgressMenuItem {
143 objectName: "progressMenu"
144 property QtObject menuData: null
145 property int menuIndex: -1
147 text: menuData && menuData.label || ""
148 iconSource: menuData && menuData.icon || ""
149 value : menuData && menuData.actionState || 0.0
150 enabled: menuData && menuData.sensitive || false
151 }
152 }
154 Component {
155 id: standardMenu;
156 Indicators.StandardMenuItem {
157 objectName: "standardMenu"
158 property QtObject menuData: null
159 property int menuIndex: -1
161 text: menuData && menuData.label || ""
162 iconSource: menuData && menuData.icon || ""
163 enabled: menuData && menuData.sensitive || false
164 checkable: menuData ? (menuData.isCheck || menuData.isRadio) : false
165 checked: checkable ? menuData.isToggled : false
167 onActivate: {
168 menuModel.activate(menuIndex);
169 shell.hideIndicatorMenu(UbuntuAnimation.BriskDuration);
170 }
171 }
172 }
174 Component {
175 id: switchMenu;
176 Indicators.SwitchMenuItem {
177 objectName: "switchMenu"
178 property QtObject menuData: null
179 property int menuIndex: -1
181 text: menuData && menuData.label || ""
182 iconSource: menuData && menuData.icon || ""
183 enabled: menuData && menuData.sensitive || false
184 checked: menuData ? menuData.isToggled : false
186 onActivate: {
187 menuModel.activate(menuIndex);
188 shell.hideIndicatorMenu(UbuntuAnimation.BriskDuration);
189 }
190 }
191 }
193 Component {
194 id: wifiSection;
195 Indicators.SectionMenuItem {
196 objectName: "wifiSection"
197 property QtObject menuData: null
198 property var menuModel: menuFactory.menuModel
199 property int menuIndex: -1
200 property var extendedData: menuData && menuData.ext || undefined
202 text: menuData && menuData.label || ""
203 busy: getExtendedProperty(extendedData, "xCanonicalBusyAction", false)
205 onMenuModelChanged: {
206 loadAttributes();
207 }
208 onMenuIndexChanged: {
209 loadAttributes();
210 }
212 function loadAttributes() {
213 if (!menuModel || menuIndex == undefined) return;
214 menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-busy-action': 'bool'})
215 }
216 }
217 }
219 Component {
220 id: accessPoint;
221 ICNetwork.AccessPoint {
222 objectName: "accessPoint"
223 property QtObject menuData: null
224 property var menuModel: menuFactory.menuModel
225 property int menuIndex: -1
226 property var extendedData: menuData && menuData.ext || undefined
228 property var strengthAction: QMenuModel.UnityMenuAction {
229 model: menuModel
230 index: menuIndex
231 name: getExtendedProperty(extendedData, "xCanonicalWifiApStrengthAction", "")
232 }
234 label: menuData && menuData.label || ""
235 enabled: menuData && menuData.sensitive || false
236 secure: getExtendedProperty(extendedData, "xCanonicalWifiApIsSecure", false)
237 adHoc: getExtendedProperty(extendedData, "xCanonicalWifiApIsAdhoc", false)
238 checked: menuData ? menuData.isToggled : false
239 signalStrength: strengthAction.valid ? strengthAction.state : 0
241 onMenuModelChanged: {
242 loadAttributes();
243 }
244 onMenuIndexChanged: {
245 loadAttributes();
246 }
247 onActivate: {
248 menuModel.activate(menuIndex);
249 shell.hideIndicatorMenu(UbuntuAnimation.BriskDuration);
250 }
252 function loadAttributes() {
253 if (!menuModel || menuIndex == undefined) return;
254 menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-wifi-ap-is-adhoc': 'bool',
255 'x-canonical-wifi-ap-is-secure': 'bool',
256 'x-canonical-wifi-ap-strength-action': 'string'});
257 }
258 }
259 }
261 Component {
262 id: messageItem
263 ICMessaging.MessageMenuItemFactory {
264 objectName: "messageItem"
265 menuModel: menuFactory.menuModel
266 }
267 }
269 Component {
270 id: groupedMessage
271 ICMessaging.GroupedMessage {
272 objectName: "groupedMessage"
273 property QtObject menuData: null
274 property var menuModel: menuFactory.menuModel
275 property int menuIndex: -1
276 property var extendedData: menuData && menuData.ext || undefined
278 title: menuData && menuData.label || ""
279 appIcon: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_app.svg")
280 count: menuData && menuData.actionState.length > 0 ? menuData.actionState[0] : "0"
281 enabled: menuData && menuData.sensitive || false
283 onMenuModelChanged: {
284 loadAttributes();
285 }
286 onMenuIndexChanged: {
287 loadAttributes();
288 }
289 onActivateApp: {
290 menuModel.activate(menuIndex, true);
291 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
292 }
293 onDismiss: {
294 menuModel.activate(menuIndex, false);
295 }
297 function loadAttributes() {
298 if (!menuModel || menuIndex == undefined) return;
299 menuModel.loadExtendedAttributes(modelIndex, {'icon': 'icon'});
300 }
301 }
302 }
304 function load(modelData) {
305 if (modelData.type !== undefined) {
306 var component = _map[modelData.type];
307 if (component !== undefined) {
308 return component;
309 }
310 }
311 if (modelData.isSeparator) {
312 return divMenu;
313 }
314 return standardMenu;
315 }