From f354146de80e818f5ea1b2f2e4512589fe260409 2017-12-07 08:41:25 From: Thibaud Rabillard Date: 2017-12-07 08:41:25 Subject: [PATCH] Put the align actions in sub menus --- diff --git a/gui/include/Actions/ActionsGuiController.h b/gui/include/Actions/ActionsGuiController.h index 68d3360..7dab26c 100644 --- a/gui/include/Actions/ActionsGuiController.h +++ b/gui/include/Actions/ActionsGuiController.h @@ -12,6 +12,11 @@ public: std::shared_ptr addSectionZoneAction(const QString &name, SelectionZoneAction::ExecuteFunction function); + + std::shared_ptr + addSectionZoneAction(const QStringList &subMenuList, const QString &name, + SelectionZoneAction::ExecuteFunction function); + QVector > selectionZoneActions() const; private: diff --git a/gui/include/Actions/SelectionZoneAction.h b/gui/include/Actions/SelectionZoneAction.h index 4e83c2e..93f6b4b 100644 --- a/gui/include/Actions/SelectionZoneAction.h +++ b/gui/include/Actions/SelectionZoneAction.h @@ -37,6 +37,15 @@ public: */ explicit SelectionZoneAction(const QString &name, ExecuteFunction fun); + /** + * @param name the name of the action, displayed to the user + * @param subMenusList the list of sub menus where the action should be inserted + * @param fun the function that will be called when the action is executed + * @sa execute() + */ + explicit SelectionZoneAction(const QStringList &subMenuList, const QString &name, + ExecuteFunction fun); + /// Sets the function which determine if the action should be enabled or disabled void setEnableFunction(EnableFunction fun); @@ -48,6 +57,9 @@ public: /// The name of the action QString name() const noexcept; + /// The path in the sub menus, if any + QStringList subMenuList() const noexcept; + public slots: /// Executes the action void execute(const QVector &item); diff --git a/gui/src/Actions/ActionsGuiController.cpp b/gui/src/Actions/ActionsGuiController.cpp index 6e9a3c3..b69e6c3 100644 --- a/gui/src/Actions/ActionsGuiController.cpp +++ b/gui/src/Actions/ActionsGuiController.cpp @@ -20,6 +20,16 @@ ActionsGuiController::addSectionZoneAction(const QString &name, return action; } +std::shared_ptr +ActionsGuiController::addSectionZoneAction(const QStringList &subMenuList, const QString &name, + SelectionZoneAction::ExecuteFunction function) +{ + auto action = std::make_shared(subMenuList, name, function); + impl->m_SelectionZoneActions.push_back(action); + + return action; +} + QVector > ActionsGuiController::selectionZoneActions() const { return impl->m_SelectionZoneActions; diff --git a/gui/src/Actions/SelectionZoneAction.cpp b/gui/src/Actions/SelectionZoneAction.cpp index b16ae98..0fcae06 100644 --- a/gui/src/Actions/SelectionZoneAction.cpp +++ b/gui/src/Actions/SelectionZoneAction.cpp @@ -4,20 +4,29 @@ Q_LOGGING_CATEGORY(LOG_SelectionZoneAction, "SelectionZoneAction") struct SelectionZoneAction::SelectionZoneActionPrivate { - explicit SelectionZoneActionPrivate(const QString &name, + explicit SelectionZoneActionPrivate(const QString &name, const QStringList &subMenuList, SelectionZoneAction::ExecuteFunction fun) - : m_Name{name}, m_Fun{std::move(fun)} + : m_Name{name}, m_SubMenuList{subMenuList}, m_Fun{std::move(fun)} { } QString m_Name; + QStringList m_SubMenuList; QKeySequence m_DisplayedShortcut; SelectionZoneAction::ExecuteFunction m_Fun; SelectionZoneAction::EnableFunction m_EnableFun = [](auto zones) { return true; }; }; SelectionZoneAction::SelectionZoneAction(const QString &name, ExecuteFunction fun) - : impl{spimpl::make_unique_impl(name, std::move(fun))} + : impl{spimpl::make_unique_impl(name, QStringList{}, + std::move(fun))} +{ +} + +SelectionZoneAction::SelectionZoneAction(const QStringList &subMenuList, const QString &name, + SelectionZoneAction::ExecuteFunction fun) + : impl{spimpl::make_unique_impl(name, subMenuList, + std::move(fun))} { } @@ -41,6 +50,11 @@ QString SelectionZoneAction::name() const noexcept return impl->m_Name; } +QStringList SelectionZoneAction::subMenuList() const noexcept +{ + return impl->m_SubMenuList; +} + void SelectionZoneAction::execute(const QVector &item) { impl->m_Fun(item); diff --git a/gui/src/Visualization/VisualizationActionManager.cpp b/gui/src/Visualization/VisualizationActionManager.cpp index e1d6a08..cb84119 100644 --- a/gui/src/Visualization/VisualizationActionManager.cpp +++ b/gui/src/Visualization/VisualizationActionManager.cpp @@ -24,40 +24,40 @@ void VisualizationActionManager::installSelectionZoneActions() auto alignEnableFuntion = [](auto items) { return items.count() > 1; }; // Vertical alignment actions - auto alignLeftAction - = actionController.addSectionZoneAction("Align Vertically / Left", [](auto zones) { - Q_ASSERT(zones.count() > 1); - auto ref = zones.takeFirst(); - ref->alignZonesVerticallyOnLeft(zones, false); - }); + auto alignLeftAction = actionController.addSectionZoneAction( + QStringList{"Align Vertically"}, "Left", [](auto zones) { + Q_ASSERT(zones.count() > 1); + auto ref = zones.takeFirst(); + ref->alignZonesVerticallyOnLeft(zones, false); + }); alignLeftAction->setEnableFunction(alignEnableFuntion); - auto alignLeftBorderAction - = actionController.addSectionZoneAction("Align Vertically / Left Borders", [](auto zones) { - Q_ASSERT(zones.count() > 1); - auto ref = zones.takeFirst(); - ref->alignZonesVerticallyOnLeft(zones, true); - }); + auto alignLeftBorderAction = actionController.addSectionZoneAction( + QStringList{"Align Vertically"}, "Left Borders", [](auto zones) { + Q_ASSERT(zones.count() > 1); + auto ref = zones.takeFirst(); + ref->alignZonesVerticallyOnLeft(zones, true); + }); alignLeftBorderAction->setEnableFunction(alignEnableFuntion); - auto alignRightAction - = actionController.addSectionZoneAction("Align Vertically / Right", [](auto zones) { - Q_ASSERT(zones.count() > 1); - auto ref = zones.takeFirst(); - ref->alignZonesVerticallyOnRight(zones, false); - }); + auto alignRightAction = actionController.addSectionZoneAction( + QStringList{"Align Vertically"}, "Right", [](auto zones) { + Q_ASSERT(zones.count() > 1); + auto ref = zones.takeFirst(); + ref->alignZonesVerticallyOnRight(zones, false); + }); alignRightAction->setEnableFunction(alignEnableFuntion); - auto alignRightBorderAction - = actionController.addSectionZoneAction("Align Vertically / Right Borders", [](auto zones) { - Q_ASSERT(zones.count() > 1); - auto ref = zones.takeFirst(); - ref->alignZonesVerticallyOnRight(zones, true); - }); + auto alignRightBorderAction = actionController.addSectionZoneAction( + QStringList{"Align Vertically"}, "Right Borders", [](auto zones) { + Q_ASSERT(zones.count() > 1); + auto ref = zones.takeFirst(); + ref->alignZonesVerticallyOnRight(zones, true); + }); alignRightBorderAction->setEnableFunction(alignEnableFuntion); auto alignLeftAndRightAction = actionController.addSectionZoneAction( - "Align Vertically / Left and Right", [](auto zones) { + QStringList{"Align Vertically"}, "Left and Right", [](auto zones) { Q_ASSERT(zones.count() > 1); auto ref = zones.takeFirst(); ref->alignZonesVerticallyOnLeft(zones, false); @@ -66,40 +66,40 @@ void VisualizationActionManager::installSelectionZoneActions() alignLeftAndRightAction->setEnableFunction(alignEnableFuntion); // Temporal alignment actions - auto alignLeftTemporallyAction - = actionController.addSectionZoneAction("Align Temporally / Left", [](auto zones) { - Q_ASSERT(zones.count() > 1); - auto ref = zones.takeFirst(); - ref->alignZonesTemporallyOnLeft(zones, false); - }); + auto alignLeftTemporallyAction = actionController.addSectionZoneAction( + QStringList{"Align Temporally"}, "Left", [](auto zones) { + Q_ASSERT(zones.count() > 1); + auto ref = zones.takeFirst(); + ref->alignZonesTemporallyOnLeft(zones, false); + }); alignLeftTemporallyAction->setEnableFunction(alignEnableFuntion); - auto alignLeftBorderTemporallyAction - = actionController.addSectionZoneAction("Align Temporally / Left Borders", [](auto zones) { - Q_ASSERT(zones.count() > 1); - auto ref = zones.takeFirst(); - ref->alignZonesTemporallyOnLeft(zones, true); - }); + auto alignLeftBorderTemporallyAction = actionController.addSectionZoneAction( + QStringList{"Align Temporally"}, "Left Borders", [](auto zones) { + Q_ASSERT(zones.count() > 1); + auto ref = zones.takeFirst(); + ref->alignZonesTemporallyOnLeft(zones, true); + }); alignLeftBorderTemporallyAction->setEnableFunction(alignEnableFuntion); - auto alignRightTemporallyAction - = actionController.addSectionZoneAction("Align Temporally / Right", [](auto zones) { - Q_ASSERT(zones.count() > 1); - auto ref = zones.takeFirst(); - ref->alignZonesTemporallyOnRight(zones, false); - }); + auto alignRightTemporallyAction = actionController.addSectionZoneAction( + QStringList{"Align Temporally"}, "Right", [](auto zones) { + Q_ASSERT(zones.count() > 1); + auto ref = zones.takeFirst(); + ref->alignZonesTemporallyOnRight(zones, false); + }); alignRightTemporallyAction->setEnableFunction(alignEnableFuntion); - auto alignRightBorderTemporallyAction - = actionController.addSectionZoneAction("Align Temporally / Right Borders", [](auto zones) { - Q_ASSERT(zones.count() > 1); - auto ref = zones.takeFirst(); - ref->alignZonesTemporallyOnRight(zones, true); - }); + auto alignRightBorderTemporallyAction = actionController.addSectionZoneAction( + QStringList{"Align Temporally"}, "Right Borders", [](auto zones) { + Q_ASSERT(zones.count() > 1); + auto ref = zones.takeFirst(); + ref->alignZonesTemporallyOnRight(zones, true); + }); alignRightBorderTemporallyAction->setEnableFunction(alignEnableFuntion); auto alignLeftAndRightTemporallyAction = actionController.addSectionZoneAction( - "Align Temporally / Left and Right", [](auto zones) { + QStringList{"Align Temporally"}, "Left and Right", [](auto zones) { Q_ASSERT(zones.count() > 1); auto ref = zones.takeFirst(); ref->alignZonesTemporallyOnLeft(zones, false); diff --git a/gui/src/Visualization/VisualizationGraphWidget.cpp b/gui/src/Visualization/VisualizationGraphWidget.cpp index b7d5b46..b4f7f81 100644 --- a/gui/src/Visualization/VisualizationGraphWidget.cpp +++ b/gui/src/Visualization/VisualizationGraphWidget.cpp @@ -617,6 +617,7 @@ void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept graphMenu.addAction(tr("Undo Zoom"), [this]() { undoZoom(); }); } + // Selection Zone Actions auto selectionZoneItem = impl->selectionZoneAt(pos, plot()); if (selectionZoneItem) { auto selectedItems = parentVisualizationWidget()->selectionZoneManager().selectedItems(); @@ -628,13 +629,39 @@ void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept graphMenu.addSeparator(); } + QHash subMenus; + QHash subMenusEnabled; + for (auto zoneAction : zoneActions) { - auto action = graphMenu.addAction(zoneAction->name()); - action->setEnabled(zoneAction->isEnabled(selectedItems)); + + auto isEnabled = zoneAction->isEnabled(selectedItems); + + auto menu = &graphMenu; + for (auto subMenuName : zoneAction->subMenuList()) { + if (!subMenus.contains(subMenuName)) { + menu = menu->addMenu(subMenuName); + subMenus[subMenuName] = menu; + subMenusEnabled[subMenuName] = isEnabled; + } + else { + menu = subMenus.value(subMenuName); + if (isEnabled) { + // The sub menu is enabled if at least one of its actions is enabled + subMenusEnabled[subMenuName] = true; + } + } + } + + auto action = menu->addAction(zoneAction->name()); + action->setEnabled(isEnabled); action->setShortcut(zoneAction->displayedShortcut()); QObject::connect(action, &QAction::triggered, [zoneAction, selectedItems]() { zoneAction->execute(selectedItems); }); } + + for (auto it = subMenus.cbegin(); it != subMenus.cend(); ++it) { + it.value()->setEnabled(subMenusEnabled[it.key()]); + } } if (!graphMenu.isEmpty()) {