GenerateVariableMenuOperation.cpp
196 lines
| 6.6 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r210 | #include "Visualization/operations/GenerateVariableMenuOperation.h" | ||
Alexandre Leroux
|
r222 | #include "Visualization/operations/MenuBuilder.h" | ||
Alexandre Leroux
|
r210 | |||
#include "Visualization/VisualizationGraphWidget.h" | ||||
#include "Visualization/VisualizationTabWidget.h" | ||||
#include "Visualization/VisualizationZoneWidget.h" | ||||
#include <Variable/Variable.h> | ||||
#include <QMenu> | ||||
Alexandre Leroux
|
r211 | #include <QStack> | ||
Q_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation, "GenerateVariableMenuOperation") | ||||
Alexandre Leroux
|
r210 | struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate { | ||
explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable) | ||||
Alexandre Leroux
|
r324 | : m_Variable{variable}, m_PlotMenuBuilder{menu}, m_UnplotMenuBuilder{menu} | ||
Alexandre Leroux
|
r210 | { | ||
} | ||||
Alexandre Leroux
|
r309 | void visitRootEnter() | ||
{ | ||||
// Creates the root menu | ||||
Alexandre Leroux
|
r323 | m_PlotMenuBuilder.addMenu(QObject::tr("Plot"), QIcon{":/icones/plot.png"}); | ||
Alexandre Leroux
|
r324 | m_UnplotMenuBuilder.addMenu(QObject::tr("Unplot"), QIcon{":/icones/unplot.png"}); | ||
Alexandre Leroux
|
r309 | } | ||
void visitRootLeave() | ||||
{ | ||||
// Closes the root menu | ||||
Alexandre Leroux
|
r323 | m_PlotMenuBuilder.closeMenu(); | ||
Alexandre Leroux
|
r324 | m_UnplotMenuBuilder.closeMenu(); | ||
Alexandre Leroux
|
r309 | } | ||
Alexandre Leroux
|
r212 | void visitNodeEnter(const IVisualizationWidget &container) | ||
{ | ||||
// Opens a new menu associated to the node | ||||
Alexandre Leroux
|
r323 | m_PlotMenuBuilder.addMenu(container.name()); | ||
Alexandre Leroux
|
r325 | m_UnplotMenuBuilder.addMenu(container.name()); | ||
Alexandre Leroux
|
r212 | } | ||
template <typename ActionFun> | ||||
Alexandre Leroux
|
r326 | void visitNodeLeavePlot(const IVisualizationWidget &container, const QString &actionName, | ||
ActionFun actionFunction) | ||||
Alexandre Leroux
|
r212 | { | ||
Alexandre Leroux
|
r214 | if (m_Variable && container.canDrop(*m_Variable)) { | ||
Alexandre Leroux
|
r323 | m_PlotMenuBuilder.addSeparator(); | ||
m_PlotMenuBuilder.addAction(actionName, actionFunction); | ||||
Alexandre Leroux
|
r214 | } | ||
Alexandre Leroux
|
r212 | // Closes the menu associated to the node | ||
Alexandre Leroux
|
r323 | m_PlotMenuBuilder.closeMenu(); | ||
Alexandre Leroux
|
r212 | } | ||
Alexandre Leroux
|
r326 | void visitNodeLeaveUnplot() | ||
{ | ||||
// Closes the menu associated to the node | ||||
m_UnplotMenuBuilder.closeMenu(); | ||||
} | ||||
Alexandre Leroux
|
r213 | template <typename ActionFun> | ||
Alexandre Leroux
|
r328 | void visitLeafPlot(const IVisualizationWidget &container, const QString &actionName, | ||
ActionFun actionFunction) | ||||
Alexandre Leroux
|
r213 | { | ||
if (m_Variable && container.canDrop(*m_Variable)) { | ||||
Alexandre Leroux
|
r323 | m_PlotMenuBuilder.addAction(actionName, actionFunction); | ||
Alexandre Leroux
|
r213 | } | ||
} | ||||
Alexandre Leroux
|
r328 | template <typename ActionFun> | ||
void visitLeafUnplot(const IVisualizationWidget &container, const QString &actionName, | ||||
ActionFun actionFunction) | ||||
{ | ||||
if (m_Variable && container.contains(*m_Variable)) { | ||||
m_UnplotMenuBuilder.addAction(actionName, actionFunction); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r210 | std::shared_ptr<Variable> m_Variable; | ||
Alexandre Leroux
|
r328 | MenuBuilder m_PlotMenuBuilder; ///< Builder for the 'Plot' menu | ||
MenuBuilder m_UnplotMenuBuilder; ///< Builder for the 'Unplot' menu | ||||
Alexandre Leroux
|
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) | ||||
Alexandre Leroux
|
r309 | |||
Alexandre Leroux
|
r324 | // 'Plot' and 'Unplot' menus | ||
Alexandre Leroux
|
r309 | impl->visitRootEnter(); | ||
Alexandre Leroux
|
r210 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationWidget *widget) | ||||
{ | ||||
// VisualizationWidget is not intended to accommodate a variable | ||||
Q_UNUSED(widget) | ||||
Alexandre Leroux
|
r309 | |||
Alexandre Leroux
|
r324 | // 'Plot' and 'Unplot' menus | ||
Alexandre Leroux
|
r309 | impl->visitRootLeave(); | ||
Alexandre Leroux
|
r210 | } | ||
void GenerateVariableMenuOperation::visitEnter(VisualizationTabWidget *tabWidget) | ||||
{ | ||||
Alexandre Leroux
|
r212 | if (tabWidget) { | ||
Alexandre Leroux
|
r325 | // 'Plot' and 'Unplot' menus | ||
Alexandre Leroux
|
r212 | impl->visitNodeEnter(*tabWidget); | ||
} | ||||
Alexandre Leroux
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit enter VisualizationTabWidget : the widget is null"); | ||||
} | ||||
Alexandre Leroux
|
r210 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationTabWidget *tabWidget) | ||||
{ | ||||
Alexandre Leroux
|
r212 | if (tabWidget) { | ||
Alexandre Leroux
|
r326 | // 'Plot' menu | ||
Alexandre Leroux
|
r361 | impl->visitNodeLeavePlot(*tabWidget, QObject::tr("Open in a new zone"), | ||
[ varW = std::weak_ptr<Variable>{impl->m_Variable}, tabWidget ]() { | ||||
if (auto var = varW.lock()) { | ||||
tabWidget->createZone(var); | ||||
} | ||||
}); | ||||
Alexandre Leroux
|
r326 | |||
// 'Unplot' menu | ||||
impl->visitNodeLeaveUnplot(); | ||||
Alexandre Leroux
|
r212 | } | ||
Alexandre Leroux
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit leave VisualizationTabWidget : the widget is null"); | ||||
} | ||||
Alexandre Leroux
|
r210 | } | ||
void GenerateVariableMenuOperation::visitEnter(VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
Alexandre Leroux
|
r212 | if (zoneWidget) { | ||
Alexandre Leroux
|
r325 | // 'Plot' and 'Unplot' menus | ||
Alexandre Leroux
|
r212 | impl->visitNodeEnter(*zoneWidget); | ||
} | ||||
Alexandre Leroux
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit enter VisualizationZoneWidget : the widget is null"); | ||||
} | ||||
Alexandre Leroux
|
r210 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
Alexandre Leroux
|
r212 | if (zoneWidget) { | ||
Alexandre Leroux
|
r326 | // 'Plot' menu | ||
impl->visitNodeLeavePlot( | ||||
Alexandre Leroux
|
r214 | *zoneWidget, QObject::tr("Open in a new graph"), | ||
Alexandre Leroux
|
r361 | [ varW = std::weak_ptr<Variable>{impl->m_Variable}, zoneWidget ]() { | ||
if (auto var = varW.lock()) { | ||||
zoneWidget->createGraph(var); | ||||
} | ||||
}); | ||||
Alexandre Leroux
|
r326 | |||
// 'Unplot' menu | ||||
impl->visitNodeLeaveUnplot(); | ||||
Alexandre Leroux
|
r212 | } | ||
Alexandre Leroux
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit leave VisualizationZoneWidget : the widget is null"); | ||||
} | ||||
Alexandre Leroux
|
r210 | } | ||
void GenerateVariableMenuOperation::visit(VisualizationGraphWidget *graphWidget) | ||||
{ | ||||
Alexandre Leroux
|
r213 | if (graphWidget) { | ||
Alexandre Leroux
|
r328 | // 'Plot' menu | ||
Alexandre Leroux
|
r361 | impl->visitLeafPlot(*graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()), | ||
[ varW = std::weak_ptr<Variable>{impl->m_Variable}, graphWidget ]() { | ||||
if (auto var = varW.lock()) { | ||||
graphWidget->addVariableUsingGraph(var); | ||||
} | ||||
}); | ||||
Alexandre Leroux
|
r328 | |||
// 'Unplot' menu | ||||
Alexandre Leroux
|
r361 | impl->visitLeafUnplot(*graphWidget, QObject::tr("Remove from %1").arg(graphWidget->name()), | ||
[ varW = std::weak_ptr<Variable>{impl->m_Variable}, graphWidget ]() { | ||||
if (auto var = varW.lock()) { | ||||
graphWidget->removeVariable(var); | ||||
} | ||||
}); | ||||
Alexandre Leroux
|
r213 | } | ||
Alexandre Leroux
|
r221 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit VisualizationGraphWidget : the widget is null"); | ||||
} | ||||
Alexandre Leroux
|
r210 | } | ||