GenerateVariableMenuOperation.cpp
130 lines
| 4.2 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r195 | #include "Visualization/operations/GenerateVariableMenuOperation.h" | ||
Alexandre Leroux
|
r207 | #include "Visualization/operations/MenuBuilder.h" | ||
Alexandre Leroux
|
r195 | |||
#include "Visualization/VisualizationGraphWidget.h" | ||||
#include "Visualization/VisualizationTabWidget.h" | ||||
#include "Visualization/VisualizationZoneWidget.h" | ||||
#include <Variable/Variable.h> | ||||
#include <QMenu> | ||||
Alexandre Leroux
|
r196 | #include <QStack> | ||
Q_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation, "GenerateVariableMenuOperation") | ||||
Alexandre Leroux
|
r195 | struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate { | ||
explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable) | ||||
Alexandre Leroux
|
r196 | : m_Variable{variable}, m_MenuBuilder{menu} | ||
Alexandre Leroux
|
r195 | { | ||
} | ||||
Alexandre Leroux
|
r197 | void visitNodeEnter(const IVisualizationWidget &container) | ||
{ | ||||
// Opens a new menu associated to the node | ||||
m_MenuBuilder.addMenu(container.name()); | ||||
} | ||||
template <typename ActionFun> | ||||
Alexandre Leroux
|
r199 | void visitNodeLeave(const IVisualizationWidget &container, const QString &actionName, | ||
ActionFun actionFunction) | ||||
Alexandre Leroux
|
r197 | { | ||
Alexandre Leroux
|
r199 | if (m_Variable && container.canDrop(*m_Variable)) { | ||
m_MenuBuilder.addSeparator(); | ||||
m_MenuBuilder.addAction(actionName, actionFunction); | ||||
} | ||||
Alexandre Leroux
|
r197 | // Closes the menu associated to the node | ||
m_MenuBuilder.closeMenu(); | ||||
} | ||||
Alexandre Leroux
|
r198 | 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); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r195 | std::shared_ptr<Variable> m_Variable; | ||
Alexandre Leroux
|
r196 | MenuBuilder m_MenuBuilder; | ||
Alexandre Leroux
|
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) | ||||
} | ||||
void GenerateVariableMenuOperation::visitLeave(VisualizationWidget *widget) | ||||
{ | ||||
// VisualizationWidget is not intended to accommodate a variable | ||||
Q_UNUSED(widget) | ||||
} | ||||
void GenerateVariableMenuOperation::visitEnter(VisualizationTabWidget *tabWidget) | ||||
{ | ||||
Alexandre Leroux
|
r197 | if (tabWidget) { | ||
impl->visitNodeEnter(*tabWidget); | ||||
} | ||||
Alexandre Leroux
|
r206 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit enter VisualizationTabWidget : the widget is null"); | ||||
} | ||||
Alexandre Leroux
|
r195 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationTabWidget *tabWidget) | ||||
{ | ||||
Alexandre Leroux
|
r197 | if (tabWidget) { | ||
Alexandre Leroux
|
r199 | impl->visitNodeLeave( | ||
*tabWidget, QObject::tr("Open in a new zone"), | ||||
[ var = impl->m_Variable, tabWidget ]() { tabWidget->createZone(var); }); | ||||
Alexandre Leroux
|
r197 | } | ||
Alexandre Leroux
|
r206 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit leave VisualizationTabWidget : the widget is null"); | ||||
} | ||||
Alexandre Leroux
|
r195 | } | ||
void GenerateVariableMenuOperation::visitEnter(VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
Alexandre Leroux
|
r197 | if (zoneWidget) { | ||
impl->visitNodeEnter(*zoneWidget); | ||||
} | ||||
Alexandre Leroux
|
r206 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit enter VisualizationZoneWidget : the widget is null"); | ||||
} | ||||
Alexandre Leroux
|
r195 | } | ||
void GenerateVariableMenuOperation::visitLeave(VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
Alexandre Leroux
|
r197 | if (zoneWidget) { | ||
Alexandre Leroux
|
r199 | impl->visitNodeLeave( | ||
*zoneWidget, QObject::tr("Open in a new graph"), | ||||
[ var = impl->m_Variable, zoneWidget ]() { zoneWidget->createGraph(var); }); | ||||
Alexandre Leroux
|
r197 | } | ||
Alexandre Leroux
|
r206 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit leave VisualizationZoneWidget : the widget is null"); | ||||
} | ||||
Alexandre Leroux
|
r195 | } | ||
void GenerateVariableMenuOperation::visit(VisualizationGraphWidget *graphWidget) | ||||
{ | ||||
Alexandre Leroux
|
r198 | if (graphWidget) { | ||
impl->visitLeaf( | ||||
*graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()), | ||||
[ var = impl->m_Variable, graphWidget ]() { graphWidget->addVariable(var); }); | ||||
} | ||||
Alexandre Leroux
|
r206 | else { | ||
qCCritical(LOG_GenerateVariableMenuOperation(), | ||||
"Can't visit VisualizationGraphWidget : the widget is null"); | ||||
} | ||||
Alexandre Leroux
|
r195 | } | ||