GenerateVariableMenuOperation.cpp
183 lines
| 5.9 KiB
| text/x-c
|
CppLexer
|
r195 | #include "Visualization/operations/GenerateVariableMenuOperation.h" | ||
|
r207 | #include "Visualization/operations/MenuBuilder.h" | ||
|
r195 | |||
#include "Visualization/VisualizationGraphWidget.h" | ||||
#include "Visualization/VisualizationTabWidget.h" | ||||
#include "Visualization/VisualizationZoneWidget.h" | ||||
#include <Variable/Variable.h> | ||||
#include <QMenu> | ||||
|
r196 | #include <QStack> | ||
Q_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation, "GenerateVariableMenuOperation") | ||||
|
r195 | struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate { | ||
explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable) | ||||
|
r298 | : m_Variable{variable}, m_PlotMenuBuilder{menu}, m_UnplotMenuBuilder{menu} | ||
|
r195 | { | ||
} | ||||
|
r286 | void visitRootEnter() | ||
{ | ||||
// Creates the root menu | ||||
|
r297 | m_PlotMenuBuilder.addMenu(QObject::tr("Plot"), QIcon{":/icones/plot.png"}); | ||
|
r298 | m_UnplotMenuBuilder.addMenu(QObject::tr("Unplot"), QIcon{":/icones/unplot.png"}); | ||
|
r286 | } | ||
void visitRootLeave() | ||||
{ | ||||
// Closes the root menu | ||||
|
r297 | m_PlotMenuBuilder.closeMenu(); | ||
|
r298 | m_UnplotMenuBuilder.closeMenu(); | ||
|
r286 | } | ||
|
r197 | void visitNodeEnter(const IVisualizationWidget &container) | ||
{ | ||||
// Opens a new menu associated to the node | ||||
|
r297 | m_PlotMenuBuilder.addMenu(container.name()); | ||
|
r299 | m_UnplotMenuBuilder.addMenu(container.name()); | ||
|
r197 | } | ||
template <typename ActionFun> | ||||
|
r300 | void visitNodeLeavePlot(const IVisualizationWidget &container, const QString &actionName, | ||
ActionFun actionFunction) | ||||
|
r197 | { | ||
|
r199 | if (m_Variable && container.canDrop(*m_Variable)) { | ||
|
r297 | m_PlotMenuBuilder.addSeparator(); | ||
m_PlotMenuBuilder.addAction(actionName, actionFunction); | ||||
|
r199 | } | ||
|
r197 | // Closes the menu associated to the node | ||
|
r297 | m_PlotMenuBuilder.closeMenu(); | ||
|
r197 | } | ||
|
r300 | void visitNodeLeaveUnplot() | ||
{ | ||||
// Closes the menu associated to the node | ||||
m_UnplotMenuBuilder.closeMenu(); | ||||
} | ||||
|
r198 | template <typename ActionFun> | ||
|
r302 | void visitLeafPlot(const IVisualizationWidget &container, const QString &actionName, | ||
ActionFun actionFunction) | ||||
|
r198 | { | ||
if (m_Variable && container.canDrop(*m_Variable)) { | ||||
|
r297 | m_PlotMenuBuilder.addAction(actionName, actionFunction); | ||
|
r198 | } | ||
} | ||||
|
r302 | 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); | ||||
} | ||||
} | ||||
|
r195 | std::shared_ptr<Variable> m_Variable; | ||
|
r302 | MenuBuilder m_PlotMenuBuilder; ///< Builder for the 'Plot' menu | ||
MenuBuilder m_UnplotMenuBuilder; ///< Builder for the 'Unplot' menu | ||||
|
r195 | }; | ||
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) | ||||
|
r286 | |||
|
r298 | // 'Plot' and 'Unplot' menus | ||
|
r286 | impl->visitRootEnter(); | ||
|
r195 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationWidget *widget) | ||||
{ | ||||
// VisualizationWidget is not intended to accommodate a variable | ||||
Q_UNUSED(widget) | ||||
|
r286 | |||
|
r298 | // 'Plot' and 'Unplot' menus | ||
|
r286 | impl->visitRootLeave(); | ||
|
r195 | } | ||
void GenerateVariableMenuOperation::visitEnter(VisualizationTabWidget *tabWidget) | ||||
{ | ||||
|
r197 | if (tabWidget) { | ||
|
r299 | // 'Plot' and 'Unplot' menus | ||
|
r197 | impl->visitNodeEnter(*tabWidget); | ||
} | ||||
|
r206 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit enter VisualizationTabWidget : the widget is null"); | ||||
} | ||||
|
r195 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationTabWidget *tabWidget) | ||||
{ | ||||
|
r197 | if (tabWidget) { | ||
|
r300 | // 'Plot' menu | ||
impl->visitNodeLeavePlot( | ||||
|
r199 | *tabWidget, QObject::tr("Open in a new zone"), | ||
[ var = impl->m_Variable, tabWidget ]() { tabWidget->createZone(var); }); | ||||
|
r300 | |||
// 'Unplot' menu | ||||
impl->visitNodeLeaveUnplot(); | ||||
|
r197 | } | ||
|
r206 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit leave VisualizationTabWidget : the widget is null"); | ||||
} | ||||
|
r195 | } | ||
void GenerateVariableMenuOperation::visitEnter(VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
|
r197 | if (zoneWidget) { | ||
|
r299 | // 'Plot' and 'Unplot' menus | ||
|
r197 | impl->visitNodeEnter(*zoneWidget); | ||
} | ||||
|
r206 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit enter VisualizationZoneWidget : the widget is null"); | ||||
} | ||||
|
r195 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
|
r197 | if (zoneWidget) { | ||
|
r300 | // 'Plot' menu | ||
impl->visitNodeLeavePlot( | ||||
|
r199 | *zoneWidget, QObject::tr("Open in a new graph"), | ||
[ var = impl->m_Variable, zoneWidget ]() { zoneWidget->createGraph(var); }); | ||||
|
r300 | |||
// 'Unplot' menu | ||||
impl->visitNodeLeaveUnplot(); | ||||
|
r197 | } | ||
|
r206 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit leave VisualizationZoneWidget : the widget is null"); | ||||
} | ||||
|
r195 | } | ||
void GenerateVariableMenuOperation::visit(VisualizationGraphWidget *graphWidget) | ||||
{ | ||||
|
r198 | if (graphWidget) { | ||
|
r302 | // 'Plot' menu | ||
impl->visitLeafPlot( | ||||
|
r198 | *graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()), | ||
r289 | [ var = impl->m_Variable, graphWidget ]() { graphWidget->addVariableUsingGraph(var); }); | |||
|
r302 | |||
// 'Unplot' menu | ||||
impl->visitLeafUnplot( | ||||
*graphWidget, QObject::tr("Remove from %1").arg(graphWidget->name()), | ||||
[ var = impl->m_Variable, graphWidget ]() { graphWidget->removeVariable(var); }); | ||||
|
r198 | } | ||
|
r206 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit VisualizationGraphWidget : the widget is null"); | ||||
} | ||||
|
r195 | } | ||