diff --git a/gui/include/Visualization/operations/GenerateVariableMenuOperation.h b/gui/include/Visualization/operations/GenerateVariableMenuOperation.h index 6bba41c..6849015 100644 --- a/gui/include/Visualization/operations/GenerateVariableMenuOperation.h +++ b/gui/include/Visualization/operations/GenerateVariableMenuOperation.h @@ -5,9 +5,13 @@ #include +#include + class QMenu; class Variable; +Q_DECLARE_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation) + /** * @brief The GenerateVariableMenuOperation class defines an operation that traverses all of * visualization widgets to determine which can accommodate a variable. The result of the operation diff --git a/gui/src/Visualization/operations/GenerateVariableMenuOperation.cpp b/gui/src/Visualization/operations/GenerateVariableMenuOperation.cpp index 6a36b22..6152a66 100644 --- a/gui/src/Visualization/operations/GenerateVariableMenuOperation.cpp +++ b/gui/src/Visualization/operations/GenerateVariableMenuOperation.cpp @@ -7,14 +7,69 @@ #include #include +#include + +Q_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation, "GenerateVariableMenuOperation") + +namespace { + +/// Helper assigned to build the hierarchical menu associated with a variable +struct MenuBuilder { + /** + * Ctor + * @param menu the parent menu + */ + explicit MenuBuilder(QMenu *menu) + { + if (menu) { + m_Menus.push(menu); + } + else { + qCCritical(LOG_GenerateVariableMenuOperation()) + << QObject::tr("No parent menu has been defined"); + } + } + + /** + * Adds a new menu to the current menu + * @param name the name of the menu + */ + void addMenu(const QString &name) + { + if (auto currMenu = currentMenu()) { + m_Menus.push(currMenu->addMenu(name)); + } + else { + qCCritical(LOG_GenerateVariableMenuOperation()) + << QObject::tr("No current menu to attach the new menu"); + } + } + + /// Closes the current menu + void closeMenu() + { + if (!m_Menus.isEmpty()) { + m_Menus.pop(); + } + } + + /// Gets the current menu (i.e. the top menu of the stack) + QMenu *currentMenu() const { return !m_Menus.isEmpty() ? m_Menus.top() : nullptr; } + + /// Stack of all menus currently opened + QStack m_Menus{}; +}; + +} // namespace struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate { explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr variable) - : m_Variable{variable} + : m_Variable{variable}, m_MenuBuilder{menu} { } std::shared_ptr m_Variable; + MenuBuilder m_MenuBuilder; }; GenerateVariableMenuOperation::GenerateVariableMenuOperation(QMenu *menu,