swift
Loading...
Searching...
No Matches
menuaction.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "gui/menus/menuaction.h"
5
6#include <algorithm>
7
8#include <QAction>
9#include <QMap>
10#include <QMenu>
11#include <QStringList>
12#include <QtGlobal>
13
15#include "gui/guiapplication.h"
16#include "misc/icons.h"
17#include "misc/slot.h"
18
19using namespace swift::misc;
20
21namespace swift::gui::menus
22{
23 CMenuAction::CMenuAction(const QIcon &icon, const QString &title, const QString &path, bool separator)
24 : m_icon(icon), m_title(title), m_path(path.trimmed()), m_separator(separator)
25 {}
26
27 CMenuAction::CMenuAction(const QString &path, bool separator) : m_path(path.trimmed()), m_separator(separator) {}
28
29 CMenuAction::CMenuAction(QAction *action, const QString &path, bool separator)
30 : m_action(action), m_icon(action->icon()), m_title(action->text()), m_path(path.trimmed()),
31 m_separator(separator)
32 {}
33
35 {
36 if (!m_action)
37 {
38 return false;
39 }
40 return m_action->isCheckable();
41 }
42
44 {
45 if (m_action)
46 {
47 m_action->setChecked(checked);
48 }
49 }
50
52 {
53 if (!m_action)
54 {
55 return false;
56 }
57 return m_action->isEnabled();
58 }
59
61 {
62 if (m_action)
63 {
64 m_action->setEnabled(enabled);
65 }
66 }
67
68 bool CMenuAction::hasNoPathWithSeparator() const { return m_separator && this->hasNoPath(); }
69
70 bool CMenuAction::hasNoPath() const { return m_path.isEmpty() || m_path == pathNone(); }
71
73 {
74 if (m_icon.isNull())
75 {
76 return {};
77 }
78 return m_icon.pixmap(m_icon.actualSize(QSize(16, 16)));
79 }
80
81 void CMenuAction::setEnabled(bool enabled)
82 {
83 Q_ASSERT_X(m_action, Q_FUNC_INFO, "No action");
84 m_action->setEnabled(enabled);
85 }
86
88 {
89 static const CMenuAction subdir(CIcons::appDatabase16(), "Database", CMenuAction::pathViewDatabase());
90 return subdir;
91 }
92
94 {
95 static const CMenuAction subdir(CIcons::appModels16(), "Consolidate models",
97 return subdir;
98 }
99
101 {
102 static const CMenuAction subdir(CIcons::appAtc16(), "COM", CMenuAction::pathClientCom());
103 return subdir;
104 }
105
107 {
108 static const CMenuAction subdir(CIcons::appAircraft16(), "Render models",
110 return subdir;
111 }
112
114 {
115 static const CMenuAction subdir(CIcons::appAircraft16(), "Display models",
117 return subdir;
118 }
119
121 {
122 static const CMenuAction subdir(CIcons::appAircraft16(), "Data transfer",
124 return subdir;
125 }
126
128 {
129 static const CMenuAction subdir(CIcons::appSimulator16(), "Simulator", CMenuAction::pathSimulator());
130 return subdir;
131 }
132
134 {
135 if (m_path.contains('/'))
136 {
137 if (m_path.endsWith('/'))
138 {
139 return {};
140 }
141 const int i = m_path.lastIndexOf('/');
142 return m_path.mid(i + 1);
143 }
144 return {};
145 }
146
147 void CMenuActions::splitSubMenus(const QString &key, QList<CMenuAction> &actions, QList<CMenuAction> &menus) const
148 {
149 QList<CMenuAction> myActions(m_actions.values(key));
150 QList<CMenuAction> checkableActions;
151 std::reverse(myActions.begin(), myActions.end()); // the order is reverse because of the insert multi value
152 for (const CMenuAction &action : myActions)
153 {
154 if (action.isSeparator())
155 {
156 actions.append(action);
157 }
158 else if (action.isSubMenu() || !action.getQAction())
159 {
160 menus.append(action);
161 }
162 else
163 {
164 if (action.isCheckableQAction())
165 {
166 checkableActions.append(action);
167 }
168 else
169 {
170 actions.append(action);
171 }
172 }
173 }
174 actions.append(checkableActions); // checkable actions at end
175 }
176
178 {
179 for (const CMenuAction &action : actions)
180 {
181 this->addAction(action);
182 }
183 }
184
186 {
187 QList<QAction *> qActions;
188 for (const CMenuAction &a : m_actions)
189 {
190 qActions.append(a.getQAction());
191 }
192 return qActions;
193 }
194
196 {
197 if (m_actions.contains(path))
198 {
199 return QList<CMenuAction>();
200 };
201 const QList<CMenuAction> allActions(m_actions.values(path));
202 QList<CMenuAction> menuActions;
203 for (const CMenuAction &a : allActions)
204 {
205 if (a.isSubMenu() || !a.getQAction())
206 {
207 menuActions.append(a);
208 }
209 }
210 return menuActions;
211 }
212
213 bool CMenuActions::containsMenu(const QString &path) const
214 {
215 if (!m_actions.contains(path))
216 {
217 return false;
218 }
219 return getMenuActions(path).size() > 0;
220 }
221
223 {
224 return this->addMenu(subdirAction.getIcon(), subdirAction.getTitle(), subdirAction.getPath());
225 }
226
228 {
229 return this->addMenu(QIcon(), title, path);
230 }
231
233 {
234 static const CMenuAction separatorDummy(QIcon(), "_SEP_", path);
235 this->addAction(separatorDummy);
236 }
237
238 CMenuAction CMenuActions::addMenu(const QIcon &icon, const QString &title, const QString &path)
239 {
240 const QList<CMenuAction> exisitingMenu(this->getMenuActions(path));
241 if (!exisitingMenu.isEmpty())
242 {
243 const CMenuAction existingAction(exisitingMenu.first());
244 Q_ASSERT_X(exisitingMenu.size() > 1, Q_FUNC_INFO, "Redundant menu entries");
245 Q_ASSERT_X(existingAction.getTitle() != title, Q_FUNC_INFO, "Title mismatch");
246 if (icon.isNull() || existingAction.hasIcon())
247 {
248 return existingAction.getQAction();
249 }
250 return existingAction;
251 }
252
253 CMenuAction menuAction(icon, title, path);
254 menuAction.setSubMenu(true);
255 return this->addAction(menuAction);
256 }
257
259 {
260 Q_ASSERT_X(!menuAction.getPath().isEmpty(), Q_FUNC_INFO, "Need path");
261 m_actions.insert(menuAction.getPath(), menuAction);
262 return menuAction;
263 }
264
266 {
267 CMenuAction a;
268 CMenuActions menuActions;
269 for (const CMenuAction &action : actions.m_actions)
270 {
271 a = this->addAction(action);
272 menuActions.addAction(a);
273 }
274 return menuActions;
275 }
276
278 {
279 return this->addAction(CMenuAction(action, path));
280 }
281
283 {
284 if (actions.isEmpty())
285 {
286 return {};
287 }
288 CMenuAction menuAction;
289 CMenuActions menuActions;
290 for (QAction *a : actions)
291 {
292 menuAction = this->addAction(a, path);
293 menuActions.addAction(menuAction);
294 }
295 return menuActions;
296 }
297
298 CMenuAction CMenuActions::addAction(QAction *action, const QString &text, const QString &path,
299 const CSlot<void()> &slot, const QKeySequence &shortcut)
300 {
301 if (action)
302 {
303 return this->addAction(action, path);
304 }
305 return this->addAction(text, path, slot, shortcut);
306 }
307
308 CMenuAction CMenuActions::addAction(QAction *action, const QString &text, const QString &path, QObject *actionOwner,
309 const CSlot<void()> &slot, const QKeySequence &shortcut)
310 {
311 if (action)
312 {
313 return this->addAction(action, path);
314 }
315 return this->addAction(text, path, actionOwner, slot, shortcut);
316 }
317
318 CMenuAction CMenuActions::addAction(QAction *action, const QIcon &icon, const QString &text, const QString &path,
319 const CSlot<void()> &slot, const QKeySequence &shortcut)
320 {
321 if (action)
322 {
323 return this->addAction(action, path);
324 }
325 return this->addAction(icon, text, path, slot, shortcut);
326 }
327
328 CMenuAction CMenuActions::addAction(QAction *action, const QIcon &icon, const QString &text, const QString &path,
329 QObject *actionOwner, const CSlot<void()> &slot, const QKeySequence &shortcut)
330 {
331 if (action)
332 {
333 return this->addAction(action, path);
334 }
335 Q_ASSERT_X(actionOwner, Q_FUNC_INFO, "Need action owner"); // in this case nullptr as actionOwner is not allowed
336 return this->addAction(icon, text, path, actionOwner, slot, shortcut);
337 }
338
339 CMenuAction CMenuActions::addAction(const QString &text, const QString &path, QObject *actionOwner,
340 const QKeySequence &shortcut)
341 {
342 return this->addAction(QIcon(), text, path, actionOwner, shortcut);
343 }
344
345 CMenuAction CMenuActions::addAction(const QIcon &actionIcon, const QString &text, const QString &path,
346 QObject *actionOwner, const QKeySequence &shortcut)
347 {
348 QAction *action =
349 actionIcon.isNull() ? new QAction(text, actionOwner) : new QAction(actionIcon, text, actionOwner);
350 action->setShortcut(shortcut);
351 const CMenuAction ma(action, path);
352 return this->addAction(ma);
353 }
354
355 CMenuAction CMenuActions::addAction(const QIcon &actionIcon, const QString &text, const QString &path,
356 QObject *actionOwner, const CSlot<void()> &slot, const QKeySequence &shortcut)
357 {
358 CMenuAction action = this->addAction(actionIcon, text, path, actionOwner, shortcut);
359 QAction::connect(action.getQAction(), &QAction::triggered, [slot](bool checked) {
360 slot();
361 Q_UNUSED(checked);
362 });
363 return action;
364 }
365
366 CMenuAction CMenuActions::addAction(const QIcon &actionIcon, const QString &text, const QString &path)
367 {
368 return this->addAction(actionIcon, text, path, nullptr);
369 }
370
371 CMenuAction CMenuActions::addAction(const QIcon &actionIcon, const QString &text, const QString &path,
372 const CSlot<void()> &slot, const QKeySequence &shortcut)
373 {
374 return this->addAction(actionIcon, text, path, slot.object(), slot, shortcut);
375 }
376
377 CMenuAction CMenuActions::addAction(const QString &text, const QString &path, const CSlot<void()> &slot,
378 const QKeySequence &shortcut)
379 {
380 return this->addAction(QIcon(), text, path, slot.object(), slot, shortcut);
381 }
382
383 CMenuAction CMenuActions::addAction(const QString &text, const QString &path, QObject *actionOwner,
384 const CSlot<void()> &slot, const QKeySequence &shortcut)
385 {
386 return this->addAction(QIcon(), text, path, actionOwner, slot, shortcut);
387 }
388
389 void CMenuActions::toQMenu(QMenu &menu, bool separateGroups) const
390 {
391 if (m_actions.isEmpty())
392 {
393 return;
394 }
395 const QStringList keys = m_actions.uniqueKeys(); // Sorted ascending keys, we need a menu for all those keys
396
397 QMap<QString, QMenu *> subMenus; // all sub menus
398 QString lastKey;
399
400 for (const QString &key : keys)
401 {
402 bool handledSeparator = false;
403 const int pathDepth = CMenuActions::pathDepth(key); // 0 based
404
405 QList<CMenuAction> actions;
406 QList<CMenuAction> menus;
407 this->splitSubMenus(key, actions, menus); // splits actions and (sub) menus for that key
408 if (actions.isEmpty())
409 {
410 // No actions directly for that level
411 continue;
412 }
413 if (!menu.isEmpty() && separateGroups)
414 {
415 // no separator a) if there is already one b) key roots are the same (such entries belong together)
416 const bool noSeparator = (!menu.actions().isEmpty() && menu.actions().last()->isSeparator()) ||
417 (isSameKeyRoot(key, lastKey));
418 if (!noSeparator)
419 {
420 menu.addSeparator();
421 }
422 handledSeparator = true;
423 }
424
425 int noActionsWithoutPath = 0;
426 QMenu *currentMenu = nullptr;
427
428 // reverse iteration because same key values are inserted and have reverse order
429 for (const CMenuAction &menuAction : std::as_const(actions))
430 {
431 // create submenu if required
432 if (!currentMenu)
433 {
434 currentMenu = currentMenuForAction(menu, menuAction, menus, subMenus, key, pathDepth);
435 }
436 Q_ASSERT_X(currentMenu, Q_FUNC_INFO, "Missing menu");
437 Q_ASSERT_X(menuAction.isSubMenu() || menuAction.isSeparator() || menuAction.getQAction(), Q_FUNC_INFO,
438 "Wrong type");
439
440 if (menuAction.isSeparator())
441 {
442 if (menu.isEmpty()) continue;
443 currentMenu->addSeparator();
444 continue;
445 }
446
447 if (menuAction.hasNoPath())
448 {
449 noActionsWithoutPath++;
450
451 // separator for unclassfied items
452 if ((!handledSeparator || noActionsWithoutPath > 1) && menuAction.hasNoPathWithSeparator())
453 {
454 menu.addSeparator();
455 handledSeparator = false;
456 }
457 }
458
459 // either add to submenu or menu
460 currentMenu->addAction(menuAction.getQAction());
461
462 // menu has ownership if there is no other parent
463 if (menuAction.getQAction() && !menuAction.getQAction()->parent())
464 {
465 menuAction.getQAction()->setParent(&menu);
466 }
467 } // actions
468
469 // clean up empty sub menus
470 if (currentMenu && currentMenu->isEmpty())
471 {
472 menu.removeAction(currentMenu->menuAction());
473 }
474
475 // remember last key
476 lastKey = key;
477
478 } // keys
479 }
480
481 QList<CMenuAction> CMenuActions::toQList() const { return m_actions.values(); }
482
483 CMenuActions::operator QList<QAction *>() const
484 {
485 QList<QAction *> qActions;
486 for (const CMenuAction &a : m_actions)
487 {
488 if (!a.getQAction())
489 {
490 continue;
491 }
492 qActions.append(a.getQAction());
493 }
494 return qActions;
495 }
496
498 {
500 {
501 return {};
502 }
504 }
505
507 {
509 {
510 return {};
511 }
512 return this->addMenu(CIcons::appSimulator16(), "Simulator", CMenuAction::pathSimulator());
513 }
514
516 {
518 {
519 return {};
520 }
521 const bool canConnectDb =
522 sGui && sGui->getWebDataServices() && sGui->getWebDataServices()->hasSuccesfullyConnectedSwiftDb();
523 const QString text(canConnectDb ? "Stash tools" : "Stash tools (Warning: no DB!)");
525 }
526
528 {
530 {
531 return {};
532 }
533 return this->addMenu(CIcons::appDbStash16(), "Edit models", CMenuAction::pathModelStashEditor());
534 }
535
537 {
539 {
540 return {};
541 }
543 }
544
553
555 {
557 {
558 return {};
559 }
560 return this->addMenu(CIcons::appModels16(), "Model set", CMenuAction::pathModelSet());
561 }
562
564 {
565 if (this->containsMenu(CMenuAction::subMenuCom().getPath()))
566 {
567 return {};
568 }
569 return this->addAction(CMenuAction::subMenuCom());
570 }
571
573 {
574 if (this->containsMenu(CMenuAction::subMenuDisplayModels().getPath()))
575 {
576 return {};
577 }
579 }
580
582 {
583 if (this->containsMenu(CMenuAction::subMenuRenderModels().getPath()))
584 {
585 return {};
586 }
588 }
589
591 {
592 if (this->containsMenu(CMenuAction::subMenuDataTransfer().getPath()))
593 {
594 return {};
595 }
597 }
598
599 QMenu *CMenuActions::currentMenuForAction(QMenu &menu, const CMenuAction &menuAction,
600 const QList<CMenuAction> &menus, QMap<QString, QMenu *> &subMenus,
601 const QString &key, int pathDepth)
602 {
603 if (pathDepth < 1)
604 {
605 return &menu;
606 }
607 QMenu *parentMenu = &menu;
608 if (pathDepth > 1)
609 {
610 // find the corresponding submenu. If this is empty the next higher level will be choosen
611 // if not found at all, use top level menu
612 parentMenu = findUpwardsInMenus(key, subMenus);
613 if (!parentMenu)
614 {
615 parentMenu = &menu;
616 }
617 }
618
619 // explicity menu?
620 QMenu *subMenu = nullptr;
621 if (menus.isEmpty())
622 {
623 subMenu = parentMenu->addMenu(menuAction.getLastPathPart());
624 }
625 else
626 {
627 const CMenuAction menuFound(menus.first());
628 subMenu = parentMenu->addMenu(menuFound.getIcon(), menuFound.getTitle());
629 }
630 Q_ASSERT_X(subMenu, Q_FUNC_INFO, "Could not create sub menu");
631
632 if (subMenu)
633 {
634 subMenu->setParent(parentMenu);
635 }
636
637 if (pathDepth > 0 && subMenu)
638 {
639 subMenus.insert(key, subMenu);
640 }
641 return subMenu;
642 }
643
644 int CMenuActions::pathDepth(const QString &path)
645 {
646 if (path.isEmpty())
647 {
648 return 0;
649 }
650 int c = path.count('/');
651 return c > 0 ? c : 0;
652 }
653
654 QString CMenuActions::parentPath(const QString &currentPath)
655 {
656 if (!currentPath.contains('/'))
657 {
658 return {};
659 }
660 const int i = currentPath.lastIndexOf('/');
661 return currentPath.left(i);
662 }
663
664 QString CMenuActions::keyRoot(const QString &key)
665 {
666 const int i = key.lastIndexOf('.');
667 if (i < 0)
668 {
669 return {};
670 }
671 return key.left(i);
672 }
673
674 bool CMenuActions::isSameKeyRoot(const QString &key1, const QString &key2)
675 {
676 const int i1 = key1.lastIndexOf('.');
677 if (i1 < 0)
678 {
679 return false;
680 }
681 const int i2 = key2.lastIndexOf('.');
682 if (i2 < 0 || i1 != i2)
683 {
684 return false;
685 }
686 return key1.left(i1) == key2.left(i2);
687 }
688
689 QMenu *CMenuActions::findUpwardsInMenus(const QString &key, const QMap<QString, QMenu *> &menus)
690 {
691 QString k = key;
692 while (!k.isEmpty() && !menus.isEmpty())
693 {
694 if (menus.contains(k))
695 {
696 return menus[k];
697 }
698 k = parentPath(k);
699 }
700 return nullptr;
701 }
702} // namespace swift::gui::menus
Wraps a QAction with extra metadata to allow proper sorting for a QMenu.
Definition menuaction.h:26
bool hasNoPathWithSeparator() const
No path and separator wanted.
const QString & getPath() const
Path.
Definition menuaction.h:62
void setSubMenu(bool menu)
Is menu?
Definition menuaction.h:80
void setEnabled(bool enabled)
Set enabled / disabled (allows to gray out).
static const CMenuAction & subMenuRenderModels()
Predefined sub sub menus.
static const CMenuAction & subMenuConsolidateModels()
Predefined sub sub menus.
static const QString & pathClientSimulationRender()
Client simulation/display related.
Definition menuaction.h:244
CMenuAction(const QIcon &icon, const QString &title, const QString &path=pathNone(), bool separator=false)
Constructor.
static const QString & pathModelStashEditor()
Stash editor sub menu.
Definition menuaction.h:178
static const QString & pathClientSimulationTransfer()
Client simulation/display related.
Definition menuaction.h:251
static const CMenuAction & subMenuDataTransfer()
Predefined sub sub menus.
QAction * getQAction() const
Action.
Definition menuaction.h:47
static const QString & pathClientCom()
Client COM related.
Definition menuaction.h:209
void setActionEnabled(bool enabled)
Enable action (allow gray out).
bool hasIcon() const
Has icon?
Definition menuaction.h:95
static const CMenuAction & subMenuSimulator()
Predefined sub sub menus.
static const QString & pathNone()
No key.
Definition menuaction.h:108
static const QString & pathModelConsolidate()
Consolidate.
Definition menuaction.h:164
static const QString & pathViewOrder()
Order submenus.
Definition menuaction.h:295
QPixmap getPixmap() const
Icon as pixmap.
static const QString & pathModelSet()
Model set.
Definition menuaction.h:150
const QString & getTitle() const
Title.
Definition menuaction.h:68
bool hasNoPath() const
No path?
static const CMenuAction & subMenuDatabase()
Predefined sub sub menus.
static const CMenuAction & subMenuDisplayModels()
Predefined sub sub menus.
bool isCheckableQAction() const
Checkable QAction.
static const QString & pathViewDatabase()
Database.
Definition menuaction.h:274
static const QString & pathSimulator()
Simulator sub menu.
Definition menuaction.h:122
const QIcon & getIcon() const
Icon.
Definition menuaction.h:89
static const CMenuAction & subMenuCom()
Predefined sub sub menus.
void setActionChecked(bool checked)
Set a checkable action, QAction::setChecked.
bool isActionEnabled() const
Enabled action?
static const QString & pathModelStash()
Stash sub menu.
Definition menuaction.h:171
static const QString & pathClientSimulationDisplay()
Client simulation/display related.
Definition menuaction.h:237
QString getLastPathPart() const
Last part of the path, e.g. "Foo/Bar" -> "Bar".
CMenuAction addMenuSimulator()
Simulator menu.
QList< QAction * > getQActions() const
QActions.
CMenuAction addMenuConsolidateModels()
Consolidate models menu.
void addSeparator(const QString &path)
Add a separator.
int size() const
Elements.
Definition menuaction.h:408
CMenuAction addMenuDisplayModels()
Add display model menu.
CMenuAction addMenuStash()
Stash menu.
CMenuActions getMenuActions(const QString &path) const
All menu actions.
CMenuActions addActions(const CMenuActions &actions)
Add menu actions, returns last valid QAction.
CMenuAction addMenuCom()
Add COM model menu.
void toQMenu(QMenu &menu, bool separateGroups) const
Insert the sorted actions to the menu.
CMenuAction addAction(const CMenuAction &menuAction)
Add menu action.
CMenuAction addMenuStashEditor()
Stash menu.
CMenuAction addMenuViewOrder()
View order menu.
CMenuAction addMenu(const CMenuAction &subdirAction)
Add a sub menu.
CMenuAction addMenuModelSet()
Model set menu.
CMenuAction addMenuDatabase()
Database menu.
QList< CMenuAction > toQList() const
To QList.
CMenuAction addMenuRenderModels()
Add render model menu.
bool containsMenu(const QString &path) const
Menu already available?
CMenuActions()=default
Constructor.
CMenuAction addMenuDataTransfer()
Add data transfer menu.
static const QPixmap & appModels16()
Models.
static const QPixmap & appAircraft16()
Application aircrafts.
static const QPixmap & appDbStash16()
Stash.
static const QPixmap & appSimulator16()
Application simulator.
static const QPixmap & arrowMediumEast16()
Arrow.
static const QPixmap & appDatabase16()
App.database.
static const QPixmap & appAtc16()
Application ATC.
Callable wrapper for a member function with function signature F.
Definition slot.h:65
Free functions in swift::misc.
void setShortcut(const QKeySequence &shortcut)
void triggered(bool checked)
bool isNull() const const
void append(QList< T > &&value)
T & first()
bool isEmpty() const const
T & last()
qsizetype size() const const
bool contains(const Key &key) const const
QMap< Key, T >::iterator insert(QMap< Key, T >::const_iterator pos, const Key &key, const T &value)
bool isEmpty() const const
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * addMenu(QMenu *menu)
QAction * addSeparator()
bool isEmpty() const const
QAction * menuAction() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
qsizetype count() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype lastIndexOf(QChar ch, Qt::CaseSensitivity cs) const const
QString left(qsizetype n) &&
QList< QAction * > actions() const const
void removeAction(QAction *action)
void setParent(QWidget *parent)