4#include "gui/menus/menuaction.h"
21namespace swift::gui::menus
24 : m_icon(icon), m_title(title), m_path(path.trimmed()), m_separator(separator)
30 : m_action(action), m_icon(action->icon()), m_title(action->text()), m_path(path.trimmed()),
31 m_separator(separator)
40 return m_action->isCheckable();
47 m_action->setChecked(checked);
57 return m_action->isEnabled();
64 m_action->setEnabled(enabled);
78 return m_icon.pixmap(m_icon.actualSize(
QSize(16, 16)));
83 Q_ASSERT_X(m_action, Q_FUNC_INFO,
"No action");
84 m_action->setEnabled(enabled);
135 if (m_path.contains(
'/'))
137 if (m_path.endsWith(
'/'))
141 const int i = m_path.lastIndexOf(
'/');
142 return m_path.mid(i + 1);
151 std::reverse(myActions.begin(), myActions.end());
154 if (action.isSeparator())
158 else if (action.isSubMenu() || !action.getQAction())
160 menus.append(action);
164 if (action.isCheckableQAction())
166 checkableActions.
append(action);
174 actions.
append(checkableActions);
190 qActions.
append(a.getQAction());
197 if (m_actions.contains(path))
205 if (a.isSubMenu() || !a.getQAction())
215 if (!m_actions.contains(path))
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");
250 return existingAction;
260 Q_ASSERT_X(!menuAction.
getPath().
isEmpty(), Q_FUNC_INFO,
"Need path");
261 m_actions.insert(menuAction.
getPath(), menuAction);
269 for (
const CMenuAction &action : actions.m_actions)
305 return this->
addAction(text, path, slot, shortcut);
315 return this->
addAction(text, path, actionOwner, slot, shortcut);
325 return this->
addAction(icon, text, path, slot, shortcut);
335 Q_ASSERT_X(actionOwner, Q_FUNC_INFO,
"Need action owner");
336 return this->
addAction(icon, text, path, actionOwner, slot, shortcut);
342 return this->
addAction(
QIcon(), text, path, actionOwner, shortcut);
349 actionIcon.
isNull() ?
new QAction(text, actionOwner) :
new QAction(actionIcon, text, actionOwner);
368 return this->
addAction(actionIcon, text, path,
nullptr);
374 return this->
addAction(actionIcon, text, path, slot.object(), slot, shortcut);
380 return this->
addAction(
QIcon(), text, path, slot.object(), slot, shortcut);
386 return this->
addAction(
QIcon(), text, path, actionOwner, slot, shortcut);
391 if (m_actions.isEmpty())
400 for (
const QString &key : keys)
402 bool handledSeparator =
false;
403 const int pathDepth = CMenuActions::pathDepth(key);
407 this->splitSubMenus(key, actions, menus);
413 if (!menu.
isEmpty() && separateGroups)
417 (isSameKeyRoot(key, lastKey));
422 handledSeparator =
true;
425 int noActionsWithoutPath = 0;
426 QMenu *currentMenu =
nullptr;
429 for (
const CMenuAction &menuAction : std::as_const(actions))
434 currentMenu = currentMenuForAction(menu, menuAction, menus, subMenus, key, pathDepth);
436 Q_ASSERT_X(currentMenu, Q_FUNC_INFO,
"Missing menu");
437 Q_ASSERT_X(menuAction.isSubMenu() || menuAction.isSeparator() || menuAction.getQAction(), Q_FUNC_INFO,
440 if (menuAction.isSeparator())
447 if (menuAction.hasNoPath())
449 noActionsWithoutPath++;
452 if ((!handledSeparator || noActionsWithoutPath > 1) && menuAction.hasNoPathWithSeparator())
455 handledSeparator =
false;
460 currentMenu->
addAction(menuAction.getQAction());
463 if (menuAction.getQAction() && !menuAction.getQAction()->parent())
465 menuAction.getQAction()->setParent(&menu);
470 if (currentMenu && currentMenu->
isEmpty())
492 qActions.
append(a.getQAction());
521 const bool canConnectDb =
522 sGui && sGui->getWebDataServices() && sGui->getWebDataServices()->hasSuccesfullyConnectedSwiftDb();
523 const QString text(canConnectDb ?
"Stash tools" :
"Stash tools (Warning: no DB!)");
601 const QString &key,
int pathDepth)
607 QMenu *parentMenu = &menu;
612 parentMenu = findUpwardsInMenus(key, subMenus);
620 QMenu *subMenu =
nullptr;
627 const CMenuAction menuFound(menus.
first());
628 subMenu = parentMenu->
addMenu(menuFound.getIcon(), menuFound.getTitle());
630 Q_ASSERT_X(subMenu, Q_FUNC_INFO,
"Could not create sub menu");
637 if (pathDepth > 0 && subMenu)
639 subMenus.
insert(key, subMenu);
644 int CMenuActions::pathDepth(
const QString &path)
650 int c = path.
count(
'/');
651 return c > 0 ? c : 0;
654 QString CMenuActions::parentPath(
const QString ¤tPath)
661 return currentPath.
left(i);
664 QString CMenuActions::keyRoot(
const QString &key)
674 bool CMenuActions::isSameKeyRoot(
const QString &key1,
const QString &key2)
682 if (i2 < 0 || i1 != i2)
686 return key1.
left(i1) == key2.
left(i2);
689 QMenu *CMenuActions::findUpwardsInMenus(
const QString &key,
const QMap<QString, QMenu *> &menus)
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.
Free functions in swift::misc.
void setShortcut(const QKeySequence &shortcut)
void triggered(bool checked)
bool isNull() const const
void append(QList< T > &&value)
bool isEmpty() const const
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
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) &&