##// END OF EJS Templates
Implements visit of graph...
Implements visit of graph When a graph is visited, we check if it can contains the variable, and if it's the case, we add an action to the current menu to open the variable in this graph.

File last commit:

r213:946a2291304e
r213:946a2291304e
Show More
GenerateVariableMenuOperation.cpp
167 lines | 4.6 KiB | text/x-c | CppLexer
/ gui / src / Visualization / operations / GenerateVariableMenuOperation.cpp
Alexandre Leroux
Creates the visitor that generates a hierarchical menu associated with a variable....
r210 #include "Visualization/operations/GenerateVariableMenuOperation.h"
#include "Visualization/VisualizationGraphWidget.h"
#include "Visualization/VisualizationTabWidget.h"
#include "Visualization/VisualizationZoneWidget.h"
#include <Variable/Variable.h>
#include <QMenu>
Alexandre Leroux
Defines a menu builder...
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");
}
}
Alexandre Leroux
Implements visit of graph...
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");
}
}
Alexandre Leroux
Defines a menu builder...
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");
}
}
/// Closes the current menu
void closeMenu()
{
if (!m_Menus.isEmpty()) {
m_Menus.pop();
}
}
/// Gets the current menu (i.e. the top menu of the stack)
QMenu *currentMenu() const { return !m_Menus.isEmpty() ? m_Menus.top() : nullptr; }
/// Stack of all menus currently opened
QStack<QMenu *> m_Menus{};
};
} // namespace
Alexandre Leroux
Creates the visitor that generates a hierarchical menu associated with a variable....
r210
struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate {
explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable)
Alexandre Leroux
Defines a menu builder...
r211 : m_Variable{variable}, m_MenuBuilder{menu}
Alexandre Leroux
Creates the visitor that generates a hierarchical menu associated with a variable....
r210 {
}
Alexandre Leroux
Implements visit of tab and zone...
r212 void visitNodeEnter(const IVisualizationWidget &container)
{
// Opens a new menu associated to the node
m_MenuBuilder.addMenu(container.name());
}
template <typename ActionFun>
void visitNodeLeave()
{
// Closes the menu associated to the node
m_MenuBuilder.closeMenu();
}
Alexandre Leroux
Implements visit of graph...
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);
}
}
Alexandre Leroux
Creates the visitor that generates a hierarchical menu associated with a variable....
r210 std::shared_ptr<Variable> m_Variable;
Alexandre Leroux
Defines a menu builder...
r211 MenuBuilder m_MenuBuilder;
Alexandre Leroux
Creates the visitor that generates a hierarchical menu associated with a variable....
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)
{
Alexandre Leroux
Implements visit of tab and zone...
r212 if (tabWidget) {
impl->visitNodeEnter(*tabWidget);
}
Alexandre Leroux
Creates the visitor that generates a hierarchical menu associated with a variable....
r210 }
void GenerateVariableMenuOperation::visitLeave(VisualizationTabWidget *tabWidget)
{
Alexandre Leroux
Implements visit of tab and zone...
r212 if (tabWidget) {
impl->visitNodeLeave();
}
Alexandre Leroux
Creates the visitor that generates a hierarchical menu associated with a variable....
r210 }
void GenerateVariableMenuOperation::visitEnter(VisualizationZoneWidget *zoneWidget)
{
Alexandre Leroux
Implements visit of tab and zone...
r212 if (zoneWidget) {
impl->visitNodeEnter(*zoneWidget);
}
Alexandre Leroux
Creates the visitor that generates a hierarchical menu associated with a variable....
r210 }
void GenerateVariableMenuOperation::visitLeave(VisualizationZoneWidget *zoneWidget)
{
Alexandre Leroux
Implements visit of tab and zone...
r212 if (zoneWidget) {
impl->visitNodeLeave();
}
Alexandre Leroux
Creates the visitor that generates a hierarchical menu associated with a variable....
r210 }
void GenerateVariableMenuOperation::visit(VisualizationGraphWidget *graphWidget)
{
Alexandre Leroux
Implements visit of graph...
r213 if (graphWidget) {
impl->visitLeaf(
*graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()),
[ var = impl->m_Variable, graphWidget ]() { graphWidget->addVariable(var); });
}
Alexandre Leroux
Creates the visitor that generates a hierarchical menu associated with a variable....
r210 }