GenerateVariableMenuOperation.cpp
146 lines
| 4.6 KiB
| text/x-c
|
CppLexer
|
r210 | #include "Visualization/operations/GenerateVariableMenuOperation.h" | ||
|
r222 | #include "Visualization/operations/MenuBuilder.h" | ||
|
r210 | |||
#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") | ||||
|
r210 | struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate { | ||
explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable) | ||||
|
r323 | : m_Variable{variable}, m_PlotMenuBuilder{menu} | ||
|
r210 | { | ||
} | ||||
|
r309 | void visitRootEnter() | ||
{ | ||||
// Creates the root menu | ||||
|
r323 | m_PlotMenuBuilder.addMenu(QObject::tr("Plot"), QIcon{":/icones/plot.png"}); | ||
|
r309 | } | ||
void visitRootLeave() | ||||
{ | ||||
// Closes the root menu | ||||
|
r323 | m_PlotMenuBuilder.closeMenu(); | ||
|
r309 | } | ||
|
r212 | void visitNodeEnter(const IVisualizationWidget &container) | ||
{ | ||||
// Opens a new menu associated to the node | ||||
|
r323 | m_PlotMenuBuilder.addMenu(container.name()); | ||
|
r212 | } | ||
template <typename ActionFun> | ||||
|
r214 | void visitNodeLeave(const IVisualizationWidget &container, const QString &actionName, | ||
ActionFun actionFunction) | ||||
|
r212 | { | ||
|
r214 | if (m_Variable && container.canDrop(*m_Variable)) { | ||
|
r323 | m_PlotMenuBuilder.addSeparator(); | ||
m_PlotMenuBuilder.addAction(actionName, actionFunction); | ||||
|
r214 | } | ||
|
r212 | // Closes the menu associated to the node | ||
|
r323 | m_PlotMenuBuilder.closeMenu(); | ||
|
r212 | } | ||
|
r213 | template <typename ActionFun> | ||
void visitLeaf(const IVisualizationWidget &container, const QString &actionName, | ||||
ActionFun actionFunction) | ||||
{ | ||||
if (m_Variable && container.canDrop(*m_Variable)) { | ||||
|
r323 | m_PlotMenuBuilder.addAction(actionName, actionFunction); | ||
|
r213 | } | ||
} | ||||
|
r210 | std::shared_ptr<Variable> m_Variable; | ||
|
r323 | MenuBuilder m_PlotMenuBuilder; ///< Builder for the 'Plot' menu | ||
|
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) | ||||
|
r309 | |||
impl->visitRootEnter(); | ||||
|
r210 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationWidget *widget) | ||||
{ | ||||
// VisualizationWidget is not intended to accommodate a variable | ||||
Q_UNUSED(widget) | ||||
|
r309 | |||
impl->visitRootLeave(); | ||||
|
r210 | } | ||
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()), | ||||
r314 | [ var = impl->m_Variable, graphWidget ]() { graphWidget->addVariableUsingGraph(var); }); | |||
|
r213 | } | ||
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit VisualizationGraphWidget : the widget is null"); | ||||
} | ||||
|
r210 | } | ||