GenerateVariableMenuOperation.cpp
220 lines
| 6.8 KiB
| text/x-c
|
CppLexer
|
r210 | #include "Visualization/operations/GenerateVariableMenuOperation.h" | ||
#include "Visualization/VisualizationGraphWidget.h" | ||||
#include "Visualization/VisualizationTabWidget.h" | ||||
#include "Visualization/VisualizationZoneWidget.h" | ||||
#include <Variable/Variable.h> | ||||
#include <QMenu> | ||||
|
r211 | #include <QStack> | ||
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"); | ||||
} | ||||
} | ||||
|
r213 | /** | ||
* Adds action to the current menu | ||||
* @param actionName the name of the action | ||||
* @param actionFunction the function that will be executed when the action is triggered | ||||
*/ | ||||
template <typename ActionFun> | ||||
void addAction(const QString &actionName, ActionFun actionFunction) | ||||
{ | ||||
if (auto currMenu = currentMenu()) { | ||||
currMenu->addAction(actionName, actionFunction); | ||||
} | ||||
else { | ||||
qCCritical(LOG_GenerateVariableMenuOperation()) | ||||
<< QObject::tr("No current menu to attach the action"); | ||||
} | ||||
} | ||||
|
r211 | /** | ||
* 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"); | ||||
} | ||||
} | ||||
|
r214 | /// Adds a separator to the current menu. The separator is added only if the menu already | ||
/// contains entries | ||||
void addSeparator() | ||||
{ | ||||
if (auto currMenu = currentMenu()) { | ||||
if (!currMenu->isEmpty()) { | ||||
currMenu->addSeparator(); | ||||
} | ||||
} | ||||
else { | ||||
qCCritical(LOG_GenerateVariableMenuOperation()) | ||||
<< QObject::tr("No current menu to attach the separator"); | ||||
} | ||||
} | ||||
|
r211 | /// Closes the current menu | ||
void closeMenu() | ||||
{ | ||||
if (!m_Menus.isEmpty()) { | ||||
|
r215 | if (auto closedMenu = m_Menus.pop()) { | ||
// Purge menu : if the closed menu has no entries, we remove it from its parent (the | ||||
// current menu) | ||||
if (auto currMenu = currentMenu()) { | ||||
if (closedMenu->isEmpty()) { | ||||
currMenu->removeAction(closedMenu->menuAction()); | ||||
} | ||||
} | ||||
} | ||||
|
r211 | } | ||
} | ||||
|
r221 | /// @return the current menu (i.e. the top menu of the stack), nullptr if there is no menu | ||
|
r211 | QMenu *currentMenu() const { return !m_Menus.isEmpty() ? m_Menus.top() : nullptr; } | ||
/// Stack of all menus currently opened | ||||
QStack<QMenu *> m_Menus{}; | ||||
}; | ||||
} // namespace | ||||
|
r210 | |||
struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate { | ||||
explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable) | ||||
|
r211 | : m_Variable{variable}, m_MenuBuilder{menu} | ||
|
r210 | { | ||
} | ||||
|
r212 | void visitNodeEnter(const IVisualizationWidget &container) | ||
{ | ||||
// Opens a new menu associated to the node | ||||
m_MenuBuilder.addMenu(container.name()); | ||||
} | ||||
template <typename ActionFun> | ||||
|
r214 | void visitNodeLeave(const IVisualizationWidget &container, const QString &actionName, | ||
ActionFun actionFunction) | ||||
|
r212 | { | ||
|
r214 | if (m_Variable && container.canDrop(*m_Variable)) { | ||
m_MenuBuilder.addSeparator(); | ||||
m_MenuBuilder.addAction(actionName, actionFunction); | ||||
} | ||||
|
r212 | // Closes the menu associated to the node | ||
m_MenuBuilder.closeMenu(); | ||||
} | ||||
|
r213 | template <typename ActionFun> | ||
void visitLeaf(const IVisualizationWidget &container, const QString &actionName, | ||||
ActionFun actionFunction) | ||||
{ | ||||
if (m_Variable && container.canDrop(*m_Variable)) { | ||||
m_MenuBuilder.addAction(actionName, actionFunction); | ||||
} | ||||
} | ||||
|
r210 | std::shared_ptr<Variable> m_Variable; | ||
|
r211 | MenuBuilder m_MenuBuilder; | ||
|
r210 | }; | ||
GenerateVariableMenuOperation::GenerateVariableMenuOperation(QMenu *menu, | ||||
std::shared_ptr<Variable> variable) | ||||
: impl{spimpl::make_unique_impl<GenerateVariableMenuOperationPrivate>(menu, variable)} | ||||
{ | ||||
} | ||||
void GenerateVariableMenuOperation::visitEnter(VisualizationWidget *widget) | ||||
{ | ||||
// VisualizationWidget is not intended to accommodate a variable | ||||
Q_UNUSED(widget) | ||||
} | ||||
void GenerateVariableMenuOperation::visitLeave(VisualizationWidget *widget) | ||||
{ | ||||
// VisualizationWidget is not intended to accommodate a variable | ||||
Q_UNUSED(widget) | ||||
} | ||||
void GenerateVariableMenuOperation::visitEnter(VisualizationTabWidget *tabWidget) | ||||
{ | ||||
|
r212 | if (tabWidget) { | ||
impl->visitNodeEnter(*tabWidget); | ||||
} | ||||
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit enter VisualizationTabWidget : the widget is null"); | ||||
} | ||||
|
r210 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationTabWidget *tabWidget) | ||||
{ | ||||
|
r212 | if (tabWidget) { | ||
|
r214 | impl->visitNodeLeave( | ||
*tabWidget, QObject::tr("Open in a new zone"), | ||||
[ var = impl->m_Variable, tabWidget ]() { tabWidget->createZone(var); }); | ||||
|
r212 | } | ||
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit leave VisualizationTabWidget : the widget is null"); | ||||
} | ||||
|
r210 | } | ||
void GenerateVariableMenuOperation::visitEnter(VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
|
r212 | if (zoneWidget) { | ||
impl->visitNodeEnter(*zoneWidget); | ||||
} | ||||
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit enter VisualizationZoneWidget : the widget is null"); | ||||
} | ||||
|
r210 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
|
r212 | if (zoneWidget) { | ||
|
r214 | impl->visitNodeLeave( | ||
*zoneWidget, QObject::tr("Open in a new graph"), | ||||
[ var = impl->m_Variable, zoneWidget ]() { zoneWidget->createGraph(var); }); | ||||
|
r212 | } | ||
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit leave VisualizationZoneWidget : the widget is null"); | ||||
} | ||||
|
r210 | } | ||
void GenerateVariableMenuOperation::visit(VisualizationGraphWidget *graphWidget) | ||||
{ | ||||
|
r213 | if (graphWidget) { | ||
impl->visitLeaf( | ||||
*graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()), | ||||
[ var = impl->m_Variable, graphWidget ]() { graphWidget->addVariable(var); }); | ||||
} | ||||
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit VisualizationGraphWidget : the widget is null"); | ||||
} | ||||
|
r210 | } | ||