##// END OF EJS Templates
Completes visit of tab and zone...
Alexandre Leroux -
r199:aa91a995c6e9
parent child
Show More
@@ -1,167 +1,192
1 #include "Visualization/operations/GenerateVariableMenuOperation.h"
1 #include "Visualization/operations/GenerateVariableMenuOperation.h"
2
2
3 #include "Visualization/VisualizationGraphWidget.h"
3 #include "Visualization/VisualizationGraphWidget.h"
4 #include "Visualization/VisualizationTabWidget.h"
4 #include "Visualization/VisualizationTabWidget.h"
5 #include "Visualization/VisualizationZoneWidget.h"
5 #include "Visualization/VisualizationZoneWidget.h"
6
6
7 #include <Variable/Variable.h>
7 #include <Variable/Variable.h>
8
8
9 #include <QMenu>
9 #include <QMenu>
10 #include <QStack>
10 #include <QStack>
11
11
12 Q_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation, "GenerateVariableMenuOperation")
12 Q_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation, "GenerateVariableMenuOperation")
13
13
14 namespace {
14 namespace {
15
15
16 /// Helper assigned to build the hierarchical menu associated with a variable
16 /// Helper assigned to build the hierarchical menu associated with a variable
17 struct MenuBuilder {
17 struct MenuBuilder {
18 /**
18 /**
19 * Ctor
19 * Ctor
20 * @param menu the parent menu
20 * @param menu the parent menu
21 */
21 */
22 explicit MenuBuilder(QMenu *menu)
22 explicit MenuBuilder(QMenu *menu)
23 {
23 {
24 if (menu) {
24 if (menu) {
25 m_Menus.push(menu);
25 m_Menus.push(menu);
26 }
26 }
27 else {
27 else {
28 qCCritical(LOG_GenerateVariableMenuOperation())
28 qCCritical(LOG_GenerateVariableMenuOperation())
29 << QObject::tr("No parent menu has been defined");
29 << QObject::tr("No parent menu has been defined");
30 }
30 }
31 }
31 }
32
32
33 /**
33 /**
34 * Adds action to the current menu
34 * Adds action to the current menu
35 * @param actionName the name of the action
35 * @param actionName the name of the action
36 * @param actionFunction the function that will be executed when the action is triggered
36 * @param actionFunction the function that will be executed when the action is triggered
37 */
37 */
38 template <typename ActionFun>
38 template <typename ActionFun>
39 void addAction(const QString &actionName, ActionFun actionFunction)
39 void addAction(const QString &actionName, ActionFun actionFunction)
40 {
40 {
41 if (auto currMenu = currentMenu()) {
41 if (auto currMenu = currentMenu()) {
42 currMenu->addAction(actionName, actionFunction);
42 currMenu->addAction(actionName, actionFunction);
43 }
43 }
44 else {
44 else {
45 qCCritical(LOG_GenerateVariableMenuOperation())
45 qCCritical(LOG_GenerateVariableMenuOperation())
46 << QObject::tr("No current menu to attach the action");
46 << QObject::tr("No current menu to attach the action");
47 }
47 }
48 }
48 }
49
49
50 /**
50 /**
51 * Adds a new menu to the current menu
51 * Adds a new menu to the current menu
52 * @param name the name of the menu
52 * @param name the name of the menu
53 */
53 */
54 void addMenu(const QString &name)
54 void addMenu(const QString &name)
55 {
55 {
56 if (auto currMenu = currentMenu()) {
56 if (auto currMenu = currentMenu()) {
57 m_Menus.push(currMenu->addMenu(name));
57 m_Menus.push(currMenu->addMenu(name));
58 }
58 }
59 else {
59 else {
60 qCCritical(LOG_GenerateVariableMenuOperation())
60 qCCritical(LOG_GenerateVariableMenuOperation())
61 << QObject::tr("No current menu to attach the new menu");
61 << QObject::tr("No current menu to attach the new menu");
62 }
62 }
63 }
63 }
64
64
65 /// Adds a separator to the current menu. The separator is added only if the menu already
66 /// contains entries
67 void addSeparator()
68 {
69 if (auto currMenu = currentMenu()) {
70 if (!currMenu->isEmpty()) {
71 currMenu->addSeparator();
72 }
73 }
74 else {
75 qCCritical(LOG_GenerateVariableMenuOperation())
76 << QObject::tr("No current menu to attach the separator");
77 }
78 }
79
65 /// Closes the current menu
80 /// Closes the current menu
66 void closeMenu()
81 void closeMenu()
67 {
82 {
68 if (!m_Menus.isEmpty()) {
83 if (!m_Menus.isEmpty()) {
69 m_Menus.pop();
84 m_Menus.pop();
70 }
85 }
71 }
86 }
72
87
73 /// Gets the current menu (i.e. the top menu of the stack)
88 /// Gets the current menu (i.e. the top menu of the stack)
74 QMenu *currentMenu() const { return !m_Menus.isEmpty() ? m_Menus.top() : nullptr; }
89 QMenu *currentMenu() const { return !m_Menus.isEmpty() ? m_Menus.top() : nullptr; }
75
90
76 /// Stack of all menus currently opened
91 /// Stack of all menus currently opened
77 QStack<QMenu *> m_Menus{};
92 QStack<QMenu *> m_Menus{};
78 };
93 };
79
94
80 } // namespace
95 } // namespace
81
96
82 struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate {
97 struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate {
83 explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable)
98 explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable)
84 : m_Variable{variable}, m_MenuBuilder{menu}
99 : m_Variable{variable}, m_MenuBuilder{menu}
85 {
100 {
86 }
101 }
87
102
88 void visitNodeEnter(const IVisualizationWidget &container)
103 void visitNodeEnter(const IVisualizationWidget &container)
89 {
104 {
90 // Opens a new menu associated to the node
105 // Opens a new menu associated to the node
91 m_MenuBuilder.addMenu(container.name());
106 m_MenuBuilder.addMenu(container.name());
92 }
107 }
93
108
94 template <typename ActionFun>
109 template <typename ActionFun>
95 void visitNodeLeave()
110 void visitNodeLeave(const IVisualizationWidget &container, const QString &actionName,
111 ActionFun actionFunction)
96 {
112 {
113 if (m_Variable && container.canDrop(*m_Variable)) {
114 m_MenuBuilder.addSeparator();
115 m_MenuBuilder.addAction(actionName, actionFunction);
116 }
117
97 // Closes the menu associated to the node
118 // Closes the menu associated to the node
98 m_MenuBuilder.closeMenu();
119 m_MenuBuilder.closeMenu();
99 }
120 }
100
121
101 template <typename ActionFun>
122 template <typename ActionFun>
102 void visitLeaf(const IVisualizationWidget &container, const QString &actionName,
123 void visitLeaf(const IVisualizationWidget &container, const QString &actionName,
103 ActionFun actionFunction)
124 ActionFun actionFunction)
104 {
125 {
105 if (m_Variable && container.canDrop(*m_Variable)) {
126 if (m_Variable && container.canDrop(*m_Variable)) {
106 m_MenuBuilder.addAction(actionName, actionFunction);
127 m_MenuBuilder.addAction(actionName, actionFunction);
107 }
128 }
108 }
129 }
109
130
110 std::shared_ptr<Variable> m_Variable;
131 std::shared_ptr<Variable> m_Variable;
111 MenuBuilder m_MenuBuilder;
132 MenuBuilder m_MenuBuilder;
112 };
133 };
113
134
114 GenerateVariableMenuOperation::GenerateVariableMenuOperation(QMenu *menu,
135 GenerateVariableMenuOperation::GenerateVariableMenuOperation(QMenu *menu,
115 std::shared_ptr<Variable> variable)
136 std::shared_ptr<Variable> variable)
116 : impl{spimpl::make_unique_impl<GenerateVariableMenuOperationPrivate>(menu, variable)}
137 : impl{spimpl::make_unique_impl<GenerateVariableMenuOperationPrivate>(menu, variable)}
117 {
138 {
118 }
139 }
119
140
120 void GenerateVariableMenuOperation::visitEnter(VisualizationWidget *widget)
141 void GenerateVariableMenuOperation::visitEnter(VisualizationWidget *widget)
121 {
142 {
122 // VisualizationWidget is not intended to accommodate a variable
143 // VisualizationWidget is not intended to accommodate a variable
123 Q_UNUSED(widget)
144 Q_UNUSED(widget)
124 }
145 }
125
146
126 void GenerateVariableMenuOperation::visitLeave(VisualizationWidget *widget)
147 void GenerateVariableMenuOperation::visitLeave(VisualizationWidget *widget)
127 {
148 {
128 // VisualizationWidget is not intended to accommodate a variable
149 // VisualizationWidget is not intended to accommodate a variable
129 Q_UNUSED(widget)
150 Q_UNUSED(widget)
130 }
151 }
131
152
132 void GenerateVariableMenuOperation::visitEnter(VisualizationTabWidget *tabWidget)
153 void GenerateVariableMenuOperation::visitEnter(VisualizationTabWidget *tabWidget)
133 {
154 {
134 if (tabWidget) {
155 if (tabWidget) {
135 impl->visitNodeEnter(*tabWidget);
156 impl->visitNodeEnter(*tabWidget);
136 }
157 }
137 }
158 }
138
159
139 void GenerateVariableMenuOperation::visitLeave(VisualizationTabWidget *tabWidget)
160 void GenerateVariableMenuOperation::visitLeave(VisualizationTabWidget *tabWidget)
140 {
161 {
141 if (tabWidget) {
162 if (tabWidget) {
142 impl->visitNodeLeave();
163 impl->visitNodeLeave(
164 *tabWidget, QObject::tr("Open in a new zone"),
165 [ var = impl->m_Variable, tabWidget ]() { tabWidget->createZone(var); });
143 }
166 }
144 }
167 }
145
168
146 void GenerateVariableMenuOperation::visitEnter(VisualizationZoneWidget *zoneWidget)
169 void GenerateVariableMenuOperation::visitEnter(VisualizationZoneWidget *zoneWidget)
147 {
170 {
148 if (zoneWidget) {
171 if (zoneWidget) {
149 impl->visitNodeEnter(*zoneWidget);
172 impl->visitNodeEnter(*zoneWidget);
150 }
173 }
151 }
174 }
152
175
153 void GenerateVariableMenuOperation::visitLeave(VisualizationZoneWidget *zoneWidget)
176 void GenerateVariableMenuOperation::visitLeave(VisualizationZoneWidget *zoneWidget)
154 {
177 {
155 if (zoneWidget) {
178 if (zoneWidget) {
156 impl->visitNodeLeave();
179 impl->visitNodeLeave(
180 *zoneWidget, QObject::tr("Open in a new graph"),
181 [ var = impl->m_Variable, zoneWidget ]() { zoneWidget->createGraph(var); });
157 }
182 }
158 }
183 }
159
184
160 void GenerateVariableMenuOperation::visit(VisualizationGraphWidget *graphWidget)
185 void GenerateVariableMenuOperation::visit(VisualizationGraphWidget *graphWidget)
161 {
186 {
162 if (graphWidget) {
187 if (graphWidget) {
163 impl->visitLeaf(
188 impl->visitLeaf(
164 *graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()),
189 *graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()),
165 [ var = impl->m_Variable, graphWidget ]() { graphWidget->addVariable(var); });
190 [ var = impl->m_Variable, graphWidget ]() { graphWidget->addVariable(var); });
166 }
191 }
167 }
192 }
General Comments 0
You need to be logged in to leave comments. Login now