Debian Codegraph @

src/lomiriappmenu/gmenumodelplatformmenu.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2016 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * 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
17 // Local
18 #include "gmenumodelplatformmenu.h"
19 #include "gmenumodelexporter.h"
20 #include "registry.h"
21 #include "menuregistrar.h"
22 #include "logging.h"
23
24 // Qt
25 #include <QDebug>
26 #include <QWindow>
27 #include <QCoreApplication>
28
29 #define BAR_DEBUG_MSG qCDebug(lomiriappmenu).nospace() << "LomiriPlatformMenuBar[" << (void*)this <<"]::" << __func__
30 #define MENU_DEBUG_MSG qCDebug(lomiriappmenu).nospace() << "LomiriPlatformMenu[" << (void*)this <<"]::" << __func__
31 #define ITEM_DEBUG_MSG qCDebug(lomiriappmenu).nospace() << "LomiriPlatformMenuItem[" << (void*)this <<"]::" << __func__
32
33 namespace {
34
35 int logRecusion = 0;
36
37 }
38
39 QDebug operator<<(QDebug stream, LomiriPlatformMenuBar* bar) {
40 if (bar) return bar->operator<<(stream);
41 return stream;
42 }
43 QDebug operator<<(QDebug stream, LomiriPlatformMenu* menu) {
44 if (menu) return menu->operator<<(stream);
45 return stream;
46 }
47 QDebug operator<<(QDebug stream, LomiriPlatformMenuItem* menuItem) {
48 if (menuItem) return menuItem->operator<<(stream);
49 return stream;
50 }
51
52 LomiriPlatformMenuBar::LomiriPlatformMenuBar()
53 : m_exporter(new LomiriMenuBarExporter(this))
54 , m_registrar(new LomiriMenuRegistrar())
55 , m_ready(false)
56 {
57 BAR_DEBUG_MSG << "()";
58
59 connect(this, &LomiriPlatformMenuBar::menuInserted, this, &LomiriPlatformMenuBar::structureChanged);
60 connect(this,&LomiriPlatformMenuBar::menuRemoved, this, &LomiriPlatformMenuBar::structureChanged);
61 }
62
63 LomiriPlatformMenuBar::~LomiriPlatformMenuBar()
64 {
65 BAR_DEBUG_MSG << "()";
66 }
67
68 void LomiriPlatformMenuBar::insertMenu(QPlatformMenu *menu, QPlatformMenu *before)
69 {
70 BAR_DEBUG_MSG << "(menu=" << menu << ", before=" << before << ")";
71
72 if (m_menus.contains(menu)) return;
73
74 if (!before) {
75 m_menus.push_back(menu);
76 } else {
77 for (auto iter = m_menus.begin(); iter != m_menus.end(); ++iter) {
78 if (*iter == before) {
79 m_menus.insert(iter, menu);
80 break;
81 }
82 }
83 }
84 Q_EMIT menuInserted(menu);
85 }
86
87 void LomiriPlatformMenuBar::removeMenu(QPlatformMenu *menu)
88 {
89 BAR_DEBUG_MSG << "(menu=" << menu << ")";
90
91 QMutableListIterator<QPlatformMenu*> iterator(m_menus);
92 while(iterator.hasNext()) {
93 if (iterator.next() == menu) {
94 iterator.remove();
95 break;
96 }
97 }
98 Q_EMIT menuRemoved(menu);
99 }
100
101 void LomiriPlatformMenuBar::syncMenu(QPlatformMenu *menu)
102 {
103 BAR_DEBUG_MSG << "(menu=" << menu << ")";
104
105 Q_UNUSED(menu)
106 }
107
108 void LomiriPlatformMenuBar::handleReparent(QWindow *parentWindow)
109 {
110 BAR_DEBUG_MSG << "(parentWindow=" << parentWindow << ")";
111
112 setReady(true);
113 m_registrar->registerMenuForWindow(parentWindow, QDBusObjectPath(m_exporter->menuPath()));
114 }
115
116 QPlatformMenu *LomiriPlatformMenuBar::menuForTag(quintptr tag) const
117 {
118 Q_FOREACH(QPlatformMenu* menu, m_menus) {
119 if (menu->tag() == tag) {
120 return menu;
121 }
122 }
123 return nullptr;
124 }
125
126 const QList<QPlatformMenu *> LomiriPlatformMenuBar::menus() const
127 {
128 return m_menus;
129 }
130
131 QDebug LomiriPlatformMenuBar::operator<<(QDebug stream)
132 {
133 stream.nospace().noquote() << QString("%1").arg("", logRecusion, QLatin1Char('\t'))
134 << "LomiriPlatformMenuBar(this=" << (void*)this << ")" << Qt::endl;
135 Q_FOREACH(QPlatformMenu* menu, m_menus) {
136 auto myMenu = static_cast<LomiriPlatformMenu*>(menu);
137 if (myMenu) {
138 logRecusion++;
139 stream << myMenu;
140 logRecusion--;
141 }
142 }
143
144 return stream;
145 }
146
147 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
148 QPlatformMenu *LomiriPlatformMenuBar::createMenu() const
149 {
150 return new LomiriPlatformMenu();
151 }
152 #endif
153
154 void LomiriPlatformMenuBar::setReady(bool isReady)
155 {
156 if (m_ready != isReady) {
157 m_ready = isReady;
158 Q_EMIT ready();
159 }
160 }
161
162 //////////////////////////////////////////////////////////////
163
164 LomiriPlatformMenu::LomiriPlatformMenu()
165 : m_tag(reinterpret_cast<quintptr>(this))
166 , m_parentWindow(nullptr)
167 , m_exporter(nullptr)
168 , m_registrar(nullptr)
169 {
170 MENU_DEBUG_MSG << "()";
171
172 connect(this, &LomiriPlatformMenu::menuItemInserted, this, &LomiriPlatformMenu::structureChanged);
173 connect(this, &LomiriPlatformMenu::menuItemRemoved, this, &LomiriPlatformMenu::structureChanged);
174 }
175
176 LomiriPlatformMenu::~LomiriPlatformMenu()
177 {
178 MENU_DEBUG_MSG << "()";
179 }
180
181 void LomiriPlatformMenu::insertMenuItem(QPlatformMenuItem *menuItem, QPlatformMenuItem *before)
182 {
183 MENU_DEBUG_MSG << "(menuItem=" << menuItem << ", before=" << before << ")";
184
185 if (m_menuItems.contains(menuItem)) return;
186
187 if (!before) {
188 m_menuItems.push_back(menuItem);
189 } else {
190 for (auto iter = m_menuItems.begin(); iter != m_menuItems.end(); ++iter) {
191 if (*iter == before) {
192 m_menuItems.insert(iter, menuItem);
193 break;
194 }
195 }
196 }
197
198 Q_EMIT menuItemInserted(menuItem);
199 }
200
201 void LomiriPlatformMenu::removeMenuItem(QPlatformMenuItem *menuItem)
202 {
203 MENU_DEBUG_MSG << "(menuItem=" << menuItem << ")";
204
205 QMutableListIterator<QPlatformMenuItem*> iterator(m_menuItems);
206 while(iterator.hasNext()) {
207 if (iterator.next() == menuItem) {
208 iterator.remove();
209 break;
210 }
211 }
212 Q_EMIT menuItemRemoved(menuItem);
213 }
214
215 void LomiriPlatformMenu::syncMenuItem(QPlatformMenuItem *menuItem)
216 {
217 MENU_DEBUG_MSG << "(menuItem=" << menuItem << ")";
218
219 Q_UNUSED(menuItem)
220 }
221
222 void LomiriPlatformMenu::syncSeparatorsCollapsible(bool enable)
223 {
224 MENU_DEBUG_MSG << "(enable=" << enable << ")";
225 Q_UNUSED(enable)
226 }
227
228 void LomiriPlatformMenu::setTag(quintptr tag)
229 {
230 MENU_DEBUG_MSG << "(tag=" << tag << ")";
231 m_tag = tag;
232 }
233
234 quintptr LomiriPlatformMenu::tag() const
235 {
236 return m_tag;
237 }
238
239 void LomiriPlatformMenu::setText(const QString &text)
240 {
241 MENU_DEBUG_MSG << "(text=" << text << ")";
242 if (m_text != text) {
243 m_text = text;
244 }
245 }
246
247 void LomiriPlatformMenu::setIcon(const QIcon &icon)
248 {
249 MENU_DEBUG_MSG << "(icon=" << icon.name() << ")";
250
251 if (!icon.isNull() || (!m_icon.isNull() && icon.isNull())) {
252 m_icon = icon;
253 }
254 }
255
256 void LomiriPlatformMenu::setEnabled(bool enabled)
257 {
258 MENU_DEBUG_MSG << "(enabled=" << enabled << ")";
259
260 if (m_enabled != enabled) {
261 m_enabled = enabled;
262 Q_EMIT enabledChanged(enabled);
263 }
264 }
265
266 void LomiriPlatformMenu::setVisible(bool isVisible)
267 {
268 MENU_DEBUG_MSG << "(visible=" << isVisible << ")";
269
270 if (m_visible != isVisible) {
271 m_visible = isVisible;
272 }
273 }
274
275 void LomiriPlatformMenu::setMinimumWidth(int width)
276 {
277 MENU_DEBUG_MSG << "(width=" << width << ")";
278
279 Q_UNUSED(width)
280 }
281
282 void LomiriPlatformMenu::setFont(const QFont &font)
283 {
284 MENU_DEBUG_MSG << "(font=" << font << ")";
285
286 Q_UNUSED(font)
287 }
288
289 void LomiriPlatformMenu::showPopup(const QWindow *parentWindow, const QRect &targetRect, const QPlatformMenuItem *item)
290 {
291 MENU_DEBUG_MSG << "(parentWindow=" << parentWindow << ", targetRect=" << targetRect << ", item=" << item << ")";
292
293 if (!m_exporter) {
294 m_exporter.reset(new LomiriMenuExporter(this));
295 m_exporter->exportModels();
296 }
297
298 if (parentWindow != m_parentWindow) {
299 if (m_parentWindow) {
300 m_registrar->unregisterMenu();
301 }
302
303 m_parentWindow = parentWindow;
304
305 if (m_parentWindow) {
306 if (!m_registrar) m_registrar.reset(new LomiriMenuRegistrar);
307 m_registrar->registerMenuForWindow(const_cast<QWindow*>(m_parentWindow),
308 QDBusObjectPath(m_exporter->menuPath()));
309 }
310 }
311
312 Q_UNUSED(targetRect);
313 Q_UNUSED(item);
314 setVisible(true);
315 }
316
317 void LomiriPlatformMenu::dismiss()
318 {
319 MENU_DEBUG_MSG << "()";
320
321 if (m_registrar) { m_registrar->unregisterMenu(); }
322 if (m_exporter) { m_exporter->unexportModels(); }
323 }
324
325 QPlatformMenuItem *LomiriPlatformMenu::menuItemAt(int position) const
326 {
327 if (position < 0 || position >= m_menuItems.count()) return nullptr;
328 return m_menuItems.at(position);
329 }
330
331 QPlatformMenuItem *LomiriPlatformMenu::menuItemForTag(quintptr tag) const
332 {
333 Q_FOREACH(QPlatformMenuItem* menuItem, m_menuItems) {
334 if (menuItem->tag() == tag) {
335 return menuItem;
336 }
337 }
338 return nullptr;
339 }
340
341 QPlatformMenuItem *LomiriPlatformMenu::createMenuItem() const
342 {
343 return new LomiriPlatformMenuItem();
344 }
345
346 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
347 QPlatformMenu *LomiriPlatformMenu::createSubMenu() const
348 {
349 return new LomiriPlatformMenu();
350 }
351 #endif
352
353 const QList<QPlatformMenuItem *> LomiriPlatformMenu::menuItems() const
354 {
355 return m_menuItems;
356 }
357
358 QDebug LomiriPlatformMenu::operator<<(QDebug stream)
359 {
360 stream.nospace().noquote() << QString("%1").arg("", logRecusion, QLatin1Char('\t'))
361 << "LomiriPlatformMenu(this=" << (void*)this << ", text=\"" << m_text << "\")" << Qt::endl;
362 Q_FOREACH(QPlatformMenuItem* item, m_menuItems) {
363 logRecusion++;
364 auto myItem = static_cast<LomiriPlatformMenuItem*>(item);
365 if (myItem) {
366 stream << myItem;
367 }
368 logRecusion--;
369 }
370 return stream;
371 }
372
373 //////////////////////////////////////////////////////////////
374
375 LomiriPlatformMenuItem::LomiriPlatformMenuItem()
376 : m_menu(nullptr)
377 , m_tag(reinterpret_cast<quintptr>(this))
378 {
379 ITEM_DEBUG_MSG << "()";
380 }
381
382 LomiriPlatformMenuItem::~LomiriPlatformMenuItem()
383 {
384 ITEM_DEBUG_MSG << "()";
385 }
386
387 void LomiriPlatformMenuItem::setTag(quintptr tag)
388 {
389 ITEM_DEBUG_MSG << "(tag=" << tag << ")";
390 m_tag = tag;
391 }
392
393 quintptr LomiriPlatformMenuItem::tag() const
394 {
395 return m_tag;
396 }
397
398 void LomiriPlatformMenuItem::setText(const QString &text)
399 {
400 ITEM_DEBUG_MSG << "(text=" << text << ")";
401 if (m_text != text) {
402 m_text = text;
403 }
404 }
405
406 void LomiriPlatformMenuItem::setIcon(const QIcon &icon)
407 {
408 ITEM_DEBUG_MSG << "(icon=" << icon.name() << ")";
409
410 if (!icon.isNull() || (!m_icon.isNull() && icon.isNull())) {
411 m_icon = icon;
412 }
413 }
414
415 void LomiriPlatformMenuItem::setVisible(bool isVisible)
416 {
417 ITEM_DEBUG_MSG << "(visible=" << isVisible << ")";
418 if (m_visible != isVisible) {
419 m_visible = isVisible;
420 Q_EMIT visibleChanged(m_visible);
421 }
422 }
423
424 void LomiriPlatformMenuItem::setIsSeparator(bool isSeparator)
425 {
426 ITEM_DEBUG_MSG << "(separator=" << isSeparator << ")";
427 if (m_separator != isSeparator) {
428 m_separator = isSeparator;
429 }
430 }
431
432 void LomiriPlatformMenuItem::setFont(const QFont &font)
433 {
434 ITEM_DEBUG_MSG << "(font=" << font << ")";
435 Q_UNUSED(font);
436 }
437
438 void LomiriPlatformMenuItem::setRole(QPlatformMenuItem::MenuRole role)
439 {
440 ITEM_DEBUG_MSG << "(role=" << role << ")";
441 Q_UNUSED(role);
442 }
443
444 void LomiriPlatformMenuItem::setCheckable(bool checkable)
445 {
446 ITEM_DEBUG_MSG << "(checkable=" << checkable << ")";
447 if (m_checkable != checkable) {
448 m_checkable = checkable;
449 }
450 }
451
452 void LomiriPlatformMenuItem::setChecked(bool isChecked)
453 {
454 ITEM_DEBUG_MSG << "(checked=" << isChecked << ")";
455 if (m_checked != isChecked) {
456 m_checked = isChecked;
457 Q_EMIT checkedChanged(isChecked);
458 }
459 }
460
461 void LomiriPlatformMenuItem::setShortcut(const QKeySequence &shortcut)
462 {
463 ITEM_DEBUG_MSG << "(shortcut=" << shortcut << ")";
464 if (m_shortcut != shortcut) {
465 m_shortcut = shortcut;
466 }
467 }
468
469 void LomiriPlatformMenuItem::setEnabled(bool enabled)
470 {
471 ITEM_DEBUG_MSG << "(enabled=" << enabled << ")";
472 if (m_enabled != enabled) {
473 m_enabled = enabled;
474 Q_EMIT enabledChanged(enabled);
475 }
476 }
477
478 void LomiriPlatformMenuItem::setIconSize(int size)
479 {
480 ITEM_DEBUG_MSG << "(size=" << size << ")";
481 Q_UNUSED(size);
482 }
483
484 void LomiriPlatformMenuItem::setMenu(QPlatformMenu *menu)
485 {
486 ITEM_DEBUG_MSG << "(menu=" << menu << ")";
487 if (m_menu != menu) {
488 m_menu = menu;
489
490 if (menu) {
491 connect(menu, &QObject::destroyed,
492 this, [this] { setMenu(nullptr); });
493 }
494 }
495 }
496
497 QPlatformMenu *LomiriPlatformMenuItem::menu() const
498 {
499 return m_menu;
500 }
501
502 QDebug LomiriPlatformMenuItem::operator<<(QDebug stream)
503 {
504 QString properties = "text=\"" + m_text + "\"";
505
506 stream.nospace().noquote() << QString("%1").arg("", logRecusion, QLatin1Char('\t'))
507 << "LomiriPlatformMenuItem(this=" << (void*)this << ", "
508 << (m_separator ? "Separator" : properties) << ")" << Qt::endl;
509 if (m_menu) {
510 auto myMenu = static_cast<LomiriPlatformMenu*>(m_menu);
511 if (myMenu) {
512 logRecusion++;
513 stream << myMenu;
514 logRecusion--;
515 }
516 }
517 return stream;
518 }