on f67eb69 help sign in
unity8/plugins/Unity/Indicators/qml/MenuItemFactory.qml
300 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 { id: divMenu; Indicators.DivMenuItem {} }
62 Component {
63 id: sliderMenu;
64 Indicators.SliderMenuItem {
65 property QtObject menuData: null
66 property var menuModel: menuFactory.menuModel
67 property int menuIndex: -1
68 property var extendedData: menuData && menuData.ext || undefined
70 text: menuData && menuData.label || ""
71 iconSource: menuData && menuData.icon || ""
72 minIcon: getExtendedProperty(extendedData, "minIcon", "")
73 maxIcon: getExtendedProperty(extendedData, "maxIcon", "")
75 minimumValue: getExtendedProperty(extendedData, "minValue", 0.0)
76 maximumValue: {
77 var maximum = getExtendedProperty(extendedData, "maxValue", 1.0);
78 if (maximum <= minimumValue) {
79 return minimumValue + 1;
80 }
81 return maximum;
82 }
83 value: menuData && menuData.actionState || 0.0
84 enabled: menuData && menuData.sensitive || false
86 onMenuModelChanged: {
87 loadAttributes();
88 }
89 onMenuIndexChanged: {
90 loadAttributes();
91 }
92 onChangeState: {
93 menuModel.changeState(menuIndex, value);
94 }
96 function loadAttributes() {
97 if (!menuModel || menuIndex == undefined) return;
98 menuModel.loadExtendedAttributes(menuIndex, {'min-value': 'double',
99 'max-value': 'double',
100 'min-icon': 'icon',
101 'max-icon': 'icon'});
102 }
103 }
104 }
106 Component {
107 id: buttonMenu;
108 Indicators.ButtonMenuItem {
109 property QtObject menuData: null
110 property var menuModel: menuFactory.menuModel
111 property int menuIndex: -1
113 text: menuData && menuData.label || ""
114 enabled: menuData && menuData.sensitive || false
116 onActivate: {
117 menuModel.activate(menuIndex);
118 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
119 }
120 }
121 }
122 Component {
123 id: sectionMenu;
124 Indicators.SectionMenuItem {
125 property QtObject menuData: null
126 property var menuIndex: undefined
128 text: menuData && menuData.label || ""
129 }
130 }
132 Component {
133 id: progressMenu;
134 Indicators.ProgressMenuItem {
135 property QtObject menuData: null
136 property int menuIndex: -1
138 text: menuData && menuData.label || ""
139 iconSource: menuData && menuData.icon || ""
140 value : menuData && menuData.actionState || 0.0
141 enabled: menuData && menuData.sensitive || false
142 }
143 }
145 Component {
146 id: standardMenu;
147 Indicators.StandardMenuItem {
148 property QtObject menuData: null
149 property int menuIndex: -1
151 text: menuData && menuData.label || ""
152 iconSource: menuData && menuData.icon || ""
153 enabled: menuData && menuData.sensitive || false
154 checkable: menuData ? (menuData.isCheck || menuData.isRadio) : false
155 checked: checkable ? menuData.isToggled : false
157 onActivate: {
158 menuModel.activate(menuIndex);
159 shell.hideIndicatorMenu(UbuntuAnimation.BriskDuration);
160 }
161 }
162 }
164 Component {
165 id: switchMenu;
166 Indicators.SwitchMenuItem {
167 property QtObject menuData: null
168 property int menuIndex: -1
170 text: menuData && menuData.label || ""
171 iconSource: menuData && menuData.icon || ""
172 enabled: menuData && menuData.sensitive || false
173 checked: menuData ? menuData.isToggled : false
175 onActivate: {
176 menuModel.activate(menuIndex);
177 shell.hideIndicatorMenu(UbuntuAnimation.BriskDuration);
178 }
179 }
180 }
182 Component {
183 id: wifiSection;
184 Indicators.SectionMenuItem {
185 property QtObject menuData: null
186 property var menuModel: menuFactory.menuModel
187 property int menuIndex: -1
188 property var extendedData: menuData && menuData.ext || undefined
190 text: menuData && menuData.label || ""
191 busy: getExtendedProperty(extendedData, "xCanonicalBusyAction", false)
193 onMenuModelChanged: {
194 loadAttributes();
195 }
196 onMenuIndexChanged: {
197 loadAttributes();
198 }
200 function loadAttributes() {
201 if (!menuModel || menuIndex == undefined) return;
202 menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-busy-action': 'bool'})
203 }
204 }
205 }
207 Component {
208 id: accessPoint;
209 ICNetwork.AccessPoint {
210 property QtObject menuData: null
211 property var menuModel: menuFactory.menuModel
212 property int menuIndex: -1
213 property var extendedData: menuData && menuData.ext || undefined
215 property var strengthAction: QMenuModel.UnityMenuAction {
216 model: menuModel
217 index: menuIndex
218 name: getExtendedProperty(extendedData, "xCanonicalWifiApStrengthAction", "")
219 }
221 text: menuData && menuData.label || ""
222 enabled: menuData && menuData.sensitive || false
223 secure: getExtendedProperty(extendedData, "xCanonicalWifiApIsSecure", false)
224 adHoc: getExtendedProperty(extendedData, "xCanonicalWifiApIsAdhoc", false)
225 checked: menuData ? menuData.isToggled : false
226 signalStrength: strengthAction.valid ? strengthAction.state : 0
228 onMenuModelChanged: {
229 loadAttributes();
230 }
231 onMenuIndexChanged: {
232 loadAttributes();
233 }
234 onActivate: {
235 menuModel.activate(menuIndex);
236 shell.hideIndicatorMenu(UbuntuAnimation.BriskDuration);
237 }
239 function loadAttributes() {
240 if (!menuModel || menuIndex == undefined) return;
241 menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-wifi-ap-is-adhoc': 'bool',
242 'x-canonical-wifi-ap-is-secure': 'bool',
243 'x-canonical-wifi-ap-strength-action': 'string'});
244 }
245 }
246 }
248 Component {
249 id: messageItem
250 ICMessaging.MessageMenuItemFactory {
251 menuModel: menuFactory.menuModel
252 }
253 }
255 Component {
256 id: groupedMessage
257 ICMessaging.GroupedMessage {
258 property QtObject menuData: null
259 property var menuModel: menuFactory.menuModel
260 property int menuIndex: -1
261 property var extendedData: menuData && menuData.ext || undefined
263 title: menuData && menuData.label || ""
264 appIcon: getExtendedProperty(extendedData, "icon", "qrc:/indicators/artwork/messaging/default_app.svg")
265 count: menuData && menuData.actionState.length > 0 ? menuData.actionState[0] : "0"
267 onMenuModelChanged: {
268 loadAttributes();
269 }
270 onMenuIndexChanged: {
271 loadAttributes();
272 }
273 onActivateApp: {
274 menuModel.activate(menuIndex, true);
275 shell.hideIndicatorMenu(UbuntuAnimation.FastDuration);
276 }
277 onDismiss: {
278 menuModel.activate(menuIndex, false);
279 }
281 function loadAttributes() {
282 if (!menuModel || menuIndex == undefined) return;
283 menuModel.loadExtendedAttributes(modelIndex, {'icon': 'icon'});
284 }
285 }
286 }
288 function load(modelData) {
289 if (modelData.type !== undefined) {
290 var component = _map[modelData.type];
291 if (component !== undefined) {
292 return component;
293 }
294 }
295 if (modelData.isSeparator) {
296 return divMenu;
297 }
298 return standardMenu;
299 }