##// END OF EJS Templates
Merge pull request 157 from SCIQLOP-Initialisation develop...
Merge pull request 157 from SCIQLOP-Initialisation develop Develop

File last commit:

r215:b21a03be8ff6
r223:e10dbfb41b9a merge
Show More
GenerateVariableMenuOperation.cpp
200 lines | 6.0 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");
}
}
Alexandre Leroux
Completes visit of tab and zone...
r214 /// Adds a separator to the current menu. The separator is added only if the menu already
/// contains entries
void addSeparator()
{
if (auto currMenu = currentMenu()) {
if (!currMenu->isEmpty()) {
currMenu->addSeparator();
}
}
else {
qCCritical(LOG_GenerateVariableMenuOperation())
<< QObject::tr("No current menu to attach the separator");
}
}
Alexandre Leroux
Defines a menu builder...
r211 /// Closes the current menu
void closeMenu()
{
if (!m_Menus.isEmpty()) {
Alexandre Leroux
Adds a purge action to remove menus that have not entries after visit
r215 if (auto closedMenu = m_Menus.pop()) {
// Purge menu : if the closed menu has no entries, we remove it from its parent (the
// current menu)
if (auto currMenu = currentMenu()) {
if (closedMenu->isEmpty()) {
currMenu->removeAction(closedMenu->menuAction());
}
}
}
Alexandre Leroux
Defines a menu builder...
r211 }
}
/// 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>
Alexandre Leroux
Completes visit of tab and zone...
r214 void visitNodeLeave(const IVisualizationWidget &container, const QString &actionName,
ActionFun actionFunction)
Alexandre Leroux
Implements visit of tab and zone...
r212 {
Alexandre Leroux
Completes visit of tab and zone...
r214 if (m_Variable && container.canDrop(*m_Variable)) {
m_MenuBuilder.addSeparator();
m_MenuBuilder.addAction(actionName, actionFunction);
}
Alexandre Leroux
Implements visit of tab and zone...
r212 // 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) {
Alexandre Leroux
Completes visit of tab and zone...
r214 impl->visitNodeLeave(
*tabWidget, QObject::tr("Open in a new zone"),
[ var = impl->m_Variable, tabWidget ]() { tabWidget->createZone(var); });
Alexandre Leroux
Implements visit of tab and zone...
r212 }
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) {
Alexandre Leroux
Completes visit of tab and zone...
r214 impl->visitNodeLeave(
*zoneWidget, QObject::tr("Open in a new graph"),
[ var = impl->m_Variable, zoneWidget ]() { zoneWidget->createGraph(var); });
Alexandre Leroux
Implements visit of tab and zone...
r212 }
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 }