on 2cf57fe help sign in
unity8/plugins/Unity/Indicators/qml/MenuItemFactory.qml
500 lines 18 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 Ubuntu.Settings.Menus 0.1 as Menus
22import QMenuModel 0.1 as QMenuModel
23import Utils 0.1 as Utils
24import Ubuntu.Components.ListItems 0.1 as ListItems
26Item {
27 id: menuFactory
29 property var menuModel: null
31 property var _map: {
32 "unity.widgets.systemsettings.tablet.volumecontrol" : sliderMenu,
33 "unity.widgets.systemsettings.tablet.switch" : switchMenu,
35 "com.canonical.indicator.button" : buttonMenu,
36 "com.canonical.indicator.div" : separatorMenu,
37 "com.canonical.indicator.section" : sectionMenu,
38 "com.canonical.indicator.progress" : progressMenu,
39 "com.canonical.indicator.slider" : sliderMenu,
40 "com.canonical.indicator.switch" : switchMenu,
41 "com.canonical.indicator.alarm" : alarmMenu,
42 "com.canonical.indicator.appointment" : appointmentMenu,
44 "com.canonical.indicator.messages.messageitem" : messageItem,
45 "com.canonical.indicator.messages.sourceitem" : groupedMessage,
47 "com.canonical.unity.slider" : sliderMenu,
48 "com.canonical.unity.switch" : switchMenu,
50 "com.canonical.unity.media-player" : mediaPayerMenu,
51 "com.canonical.unity.playback-item" : playbackItemMenu,
53 "unity.widgets.systemsettings.tablet.wifisection" : wifiSection,
54 "unity.widgets.systemsettings.tablet.accesspoint" : accessPoint,
55 }
57 function getExtendedProperty(object, propertyName, defaultValue) {
58 if (object && object.hasOwnProperty(propertyName)) {
59 return object[propertyName];
60 }
61 return defaultValue;
62 }
64 Component {
65 id: separatorMenu;
66 Menus.SeparatorMenu {
67 objectName: "separatorMenu"
68 }
69 }
71 Component {
72 id: sliderMenu;
73 Menus.SliderMenu {
74 objectName: "sliderMenu"
75 property QtObject menuData: null
76 property var menuModel: menuFactory.menuModel
77 property int menuIndex: -1
78 property var extendedData: menuData && menuData.ext || undefined
80 text: menuData && menuData.label || ""
81 iconSource: menuData && menuData.icon || ""
82 minIcon: getExtendedProperty(extendedData, "minIcon", "")
83 maxIcon: getExtendedProperty(extendedData, "maxIcon", "")
85 minimumValue: getExtendedProperty(extendedData, "minValue", 0.0)
86 maximumValue: {
87 var maximum = getExtendedProperty(extendedData, "maxValue", 1.0);
88 if (maximum <= minimumValue) {
89 return minimumValue + 1;
90 }
91 return maximum;
92 }
93 value: menuData && menuData.actionState || 0.0
94 enabled: menuData && menuData.sensitive || false
96 onMenuModelChanged: {
97 loadAttributes();
98 }
99 onMenuIndexChanged: {
100 loadAttributes();
101 }
102 onUpdated: {
103 menuModel.changeState(menuIndex, value);
104 }
106 function loadAttributes() {
107 if (!menuModel || menuIndex == -1) return;
108 menuModel.loadExtendedAttributes(menuIndex, {'min-value': 'double',
109 'max-value': 'double',
110 'min-icon': 'icon',
111 'max-icon': 'icon'});
112 }
113 }
114 }
116 Component {
117 id: buttonMenu;
118 Menus.ButtonMenu {
119 objectName: "buttonMenu"
120 property QtObject menuData: null
121 property var menuModel: menuFactory.menuModel
122 property int menuIndex: -1
124 buttonText: menuData && menuData.label || ""
125 enabled: menuData && menuData.sensitive || false
127 onTriggered: {
128 menuModel.activate(menuIndex);
129 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
130 }
131 }
132 }
133 Component {
134 id: sectionMenu;
135 Menus.SectionMenu {
136 objectName: "sectionMenu"
137 property QtObject menuData: null
138 property var menuIndex: undefined
140 text: menuData && menuData.label || ""
141 busy: false
142 }
143 }
145 Component {
146 id: progressMenu;
147 Menus.ProgressValueMenu {
148 objectName: "progressMenu"
149 property QtObject menuData: null
150 property int menuIndex: -1
152 text: menuData && menuData.label || ""
153 iconSource: menuData && menuData.icon || ""
154 value : menuData && menuData.actionState || 0.0
155 enabled: menuData && menuData.sensitive || false
156 }
157 }
159 Component {
160 id: standardMenu;
161 ListItems.Standard {
162 objectName: "standardMenu"
163 property QtObject menuData: null
164 property int menuIndex: -1
166 text: menuData && menuData.label || ""
167 iconSource: menuData && menuData.icon || ""
168 enabled: menuData && menuData.sensitive || false
170 onTriggered: {
171 menuModel.activate(menuIndex);
172 shell.hideIndicatorMenu(UbuntuAnimation.BriskDuration);
173 }
174 }
175 }
177 Component {
178 id: checkableMenu;
179 Menus.CheckableMenu {
180 objectName: "checkableMenu"
181 property QtObject menuData: null
182 property int menuIndex: -1
184 text: menuData && menuData.label || ""
185 enabled: menuData && menuData.sensitive || false
186 checked: menuData && menuData.isToggled || false
188 onTriggered: {
189 menuModel.activate(menuIndex);
190 shell.hideIndicatorMenu(UbuntuAnimation.BriskDuration);
191 }
192 }
193 }
196 Component {
197 id: switchMenu;
198 Menus.SwitchMenu {
199 objectName: "switchMenu"
200 property QtObject menuData: null
201 property int menuIndex: -1
203 text: menuData && menuData.label || ""
204 iconSource: menuData && menuData.icon || ""
205 enabled: menuData && menuData.sensitive || false
206 checked: menuData && menuData.isToggled || false
208 onTriggered: {
209 menuModel.activate(menuIndex);
210 shell.hideIndicatorMenu(UbuntuAnimation.BriskDuration);
211 }
212 }
213 }
215 Component {
216 id: alarmMenu;
217 Menus.EventMenu {
218 objectName: "alarmMenu"
219 property QtObject menuData: null
220 property var menuModel: menuFactory.menuModel
221 property int menuIndex: -1
222 property var extendedData: menuData && menuData.ext || undefined
223 // TODO - bug #1260728
224 property var timeFormatter: Utils.GDateTimeFormatter {
225 time: getExtendedProperty(extendedData, "xCanonicalTime", 0)
226 format: getExtendedProperty(extendedData, "xCanonicalTimeFormat", "")
227 }
229 text: menuData && menuData.label || ""
230 iconSource: menuData && menuData.icon || "image://theme/alarm-clock"
231 time: timeFormatter.timeString
232 enabled: menuData && menuData.sensitive || false
234 onMenuModelChanged: {
235 loadAttributes();
236 }
237 onMenuIndexChanged: {
238 loadAttributes();
239 }
240 onTriggered: {
241 menuModel.activate(menuIndex);
242 }
244 function loadAttributes() {
245 if (!menuModel || menuIndex == -1) return;
246 menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-time': 'int64',
247 'x-canonical-time-format': 'string'});
248 }
249 }
250 }
252 Component {
253 id: appointmentMenu;
254 Menus.EventMenu {
255 objectName: "appointmentMenu"
256 property QtObject menuData: null
257 property var menuModel: menuFactory.menuModel
258 property int menuIndex: -1
259 property var extendedData: menuData && menuData.ext || undefined
260 // TODO - bug #1260728
261 property var timeFormatter: Utils.GDateTimeFormatter {
262 time: getExtendedProperty(extendedData, "xCanonicalTime", 0)
263 format: getExtendedProperty(extendedData, "xCanonicalTimeFormat", "")
264 }
266 text: menuData && menuData.label || ""
267 iconSource: menuData && menuData.icon || "image://theme/calendar"
268 time: timeFormatter.timeString
269 eventColor: menu && menu.ext.hasOwnProperty("xCanonicalColor") ? menu.ext.xCanonicalColor : Qt.rgba(0.0, 0.0, 0.0, 0.0)
270 enabled: menuData && menuData.sensitive || false
272 onMenuModelChanged: {
273 loadAttributes();
274 }
275 onMenuIndexChanged: {
276 loadAttributes();
277 }
278 onTriggered: {
279 menuModel.activate(menuIndex);
280 }
282 function loadAttributes() {
283 if (!menuModel || menuIndex == -1) return;
284 menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-color': 'string',
285 'x-canonical-time': 'int64',
286 'x-canonical-time-format': 'string'});
287 }
288 }
289 }
291 Component {
292 id: wifiSection;
293 Menus.SectionMenu {
294 objectName: "wifiSection"
295 property QtObject menuData: null
296 property var menuModel: menuFactory.menuModel
297 property int menuIndex: -1
298 property var extendedData: menuData && menuData.ext || undefined
300 text: menuData && menuData.label || ""
301 busy: getExtendedProperty(extendedData, "xCanonicalBusyAction", false)
303 onMenuModelChanged: {
304 loadAttributes();
305 }
306 onMenuIndexChanged: {
307 loadAttributes();
308 }
310 function loadAttributes() {
311 if (!menuModel || menuIndex == -1) return;
312 menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-busy-action': 'bool'})
313 }
314 }
315 }
317 Component {
318 id: accessPoint;
319 Menus.AccessPointMenu {
320 objectName: "accessPoint"
321 property QtObject menuData: null
322 property var menuModel: menuFactory.menuModel
323 property int menuIndex: -1
324 property var extendedData: menuData && menuData.ext || undefined
326 property var strengthAction: QMenuModel.UnityMenuAction {
327 model: menuModel
328 index: menuIndex
329 name: getExtendedProperty(extendedData, "xCanonicalWifiApStrengthAction", "")
330 }
332 text: menuData && menuData.label || ""
333 enabled: menuData && menuData.sensitive || false
334 checked: menuData && menuData.isToggled || false
335 secure: getExtendedProperty(extendedData, "xCanonicalWifiApIsSecure", false)
336 adHoc: getExtendedProperty(extendedData, "xCanonicalWifiApIsAdhoc", false)
337 signalStrength: strengthAction.valid ? strengthAction.state : 0
339 onMenuModelChanged: {
340 loadAttributes();
341 }
342 onMenuIndexChanged: {
343 loadAttributes();
344 }
345 onTriggered: {
346 menuModel.activate(menuIndex);
347 shell.hideIndicatorMenu(UbuntuAnimation.BriskDuration);
348 }
350 function loadAttributes() {
351 if (!menuModel || menuIndex == -1) return;
352 menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-wifi-ap-is-adhoc': 'bool',
353 'x-canonical-wifi-ap-is-secure': 'bool',
354 'x-canonical-wifi-ap-strength-action': 'string'});
355 }
356 }
357 }
359 Component {
360 id: messageItem
361 MessageMenuItemFactory {
362 objectName: "messageItem"
363 menuModel: menuFactory.menuModel
364 }
365 }
367 Component {
368 id: groupedMessage
369 Menus.GroupedMessageMenu {
370 objectName: "groupedMessage"
371 property QtObject menuData: null
372 property var menuModel: menuFactory.menuModel
373 property int menuIndex: -1
374 property var extendedData: menuData && menuData.ext || undefined
376 text: menuData && menuData.label || ""
377 iconSource: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_app.svg")
378 count: menuData && menuData.actionState.length > 0 ? menuData.actionState[0] : "0"
379 enabled: menuData && menuData.sensitive || false
380 removable: true
382 onMenuModelChanged: {
383 loadAttributes();
384 }
385 onMenuIndexChanged: {
386 loadAttributes();
387 }
388 onClicked: {
389 menuModel.activate(menuIndex, true);
390 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
391 }
392 onDismissed: {
393 menuModel.activate(menuIndex, false);
394 }
396 function loadAttributes() {
397 if (!menuModel || menuIndex == -1) return;
398 menuModel.loadExtendedAttributes(modelIndex, {'icon': 'icon'});
399 }
400 }
401 }
403 Component {
404 id: mediaPayerMenu;
405 Menus.MediaPlayerMenu {
406 objectName: "mediaPayerMenu"
407 property QtObject menuData: null
408 property var menuModel: menuFactory.menuModel
409 property int menuIndex: -1
410 property var actionState: menuData && menuData.actionState || undefined
412 playerIcon: menuData && menuData.icon || ""
413 playerName: menuData && menuData.label || ""
415 albumArt: getExtendedProperty(actionState, "art-url", "")
416 song: getExtendedProperty(actionState, "title", "unknown")
417 artist: getExtendedProperty(actionState, "artist", "unknown")
418 album: getExtendedProperty(actionState, "album", "unknown")
419 running: getExtendedProperty(actionState, "running", false)
420 state: getExtendedProperty(actionState, "state", "")
421 enabled: menuData && menuData.sensitive || false
423 onTriggered: {
424 model.activate(modelIndex);
425 }
426 }
427 }
429 Component {
430 id: playbackItemMenu;
431 Menus.PlaybackItemMenu {
432 objectName: "playbackItemMenu"
433 property QtObject menuData: null
434 property var menuModel: menuFactory.menuModel
435 property int menuIndex: -1
436 property var extendedData: menuData && menuData.ext || undefined
438 property var playAction: QMenuModel.UnityMenuAction {
439 model: menuModel
440 index: menuIndex
441 name: getExtendedProperty(extendedData, "xCanonicalPlayAction", "")
442 }
443 property var nextAction: QMenuModel.UnityMenuAction {
444 model: menuModel
445 index: menuIndex
446 name: getExtendedProperty(extendedData, "xCanonicalNextAction", "")
447 }
448 property var previousAction: QMenuModel.UnityMenuAction {
449 model: menuModel
450 index: menuIndex
451 name: getExtendedProperty(extendedData, "xCanonicalPreviousAction", "")
452 }
454 playing: playAction.state === "Playing"
455 canPlay: playAction.valid
456 canGoNext: nextAction.valid
457 canGoPrevious: previousAction.valid
458 enabled: menuData && menuData.sensitive || false
460 onPlay: {
461 playAction.activate();
462 }
463 onNext: {
464 nextAction.activate();
465 }
466 onPrevious: {
467 previousAction.activate();
468 }
469 onMenuModelChanged: {
470 loadAttributes();
471 }
472 onMenuIndexChanged: {
473 loadAttributes();
474 }
476 function loadAttributes() {
477 if (!menuModel || menuIndex == -1) return;
478 menuModel.loadExtendedAttributes(modelIndex, {'x-canonical-play-action': 'string',
479 'x-canonical-next-action': 'string',
480 'x-canonical-previous-action': 'string'});
481 }
482 }
483 }
485 function load(modelData) {
486 if (modelData.type !== undefined) {
487 var component = _map[modelData.type];
488 if (component !== undefined) {
489 return component;
490 }
491 }
492 if (modelData.isCheck || modelData.isRadio) {
493 return checkableMenu;
494 }
495 if (modelData.isSeparator) {
496 return separatorMenu;
497 }
498 return standardMenu;
499 }