##// END OF EJS Templates
Merge branch 'feature/VariableMenu1' into develop
Alexandre Leroux -
r217:b575895e8245 merge
parent child
Show More
@@ -0,0 +1,19
1 #ifndef SCIQLOP_IVARIABLECONTAINER_H
2 #define SCIQLOP_IVARIABLECONTAINER_H
3
4 class Variable;
5
6 /**
7 * @brief The IVariableContainer interface represents an UI object that can accommodate a variable
8 */
9 class IVariableContainer {
10
11 public:
12 virtual ~IVariableContainer() = default;
13
14 /// Checks if the container can handle the variable passed in parameter
15 virtual bool canDrop(const Variable &variable) const = 0;
16 };
17
18
19 #endif // SCIQLOP_IVARIABLECONTAINER_H
@@ -0,0 +1,42
1 #ifndef SCIQLOP_GENERATEVARIABLEMENUOPERATION_H
2 #define SCIQLOP_GENERATEVARIABLEMENUOPERATION_H
3
4 #include "Visualization/IVisualizationWidgetVisitor.h"
5
6 #include <Common/spimpl.h>
7
8 #include <QLoggingCategory>
9
10 class QMenu;
11 class Variable;
12
13 Q_DECLARE_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation)
14
15 /**
16 * @brief The GenerateVariableMenuOperation class defines an operation that traverses all of
17 * visualization widgets to determine which can accommodate a variable. The result of the operation
18 * is a menu that contains actions to add the variable into the containers.
19 */
20 class GenerateVariableMenuOperation : public IVisualizationWidgetVisitor {
21 public:
22 /**
23 * Ctor
24 * @param menu the menu to which to attach the generated menu
25 * @param variable the variable for which to generate the menu
26 */
27 explicit GenerateVariableMenuOperation(QMenu *menu, std::shared_ptr<Variable> variable);
28
29 void visitEnter(VisualizationWidget *widget) override final;
30 void visitLeave(VisualizationWidget *widget) override final;
31 void visitEnter(VisualizationTabWidget *tabWidget) override final;
32 void visitLeave(VisualizationTabWidget *tabWidget) override final;
33 void visitEnter(VisualizationZoneWidget *zoneWidget) override final;
34 void visitLeave(VisualizationZoneWidget *zoneWidget) override final;
35 void visit(VisualizationGraphWidget *graphWidget) override final;
36
37 private:
38 class GenerateVariableMenuOperationPrivate;
39 spimpl::unique_impl_ptr<GenerateVariableMenuOperationPrivate> impl;
40 };
41
42 #endif // SCIQLOP_GENERATEVARIABLEMENUOPERATION_H
@@ -0,0 +1,200
1 #include "Visualization/operations/GenerateVariableMenuOperation.h"
2
3 #include "Visualization/VisualizationGraphWidget.h"
4 #include "Visualization/VisualizationTabWidget.h"
5 #include "Visualization/VisualizationZoneWidget.h"
6
7 #include <Variable/Variable.h>
8
9 #include <QMenu>
10 #include <QStack>
11
12 Q_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation, "GenerateVariableMenuOperation")
13
14 namespace {
15
16 /// Helper assigned to build the hierarchical menu associated with a variable
17 struct MenuBuilder {
18 /**
19 * Ctor
20 * @param menu the parent menu
21 */
22 explicit MenuBuilder(QMenu *menu)
23 {
24 if (menu) {
25 m_Menus.push(menu);
26 }
27 else {
28 qCCritical(LOG_GenerateVariableMenuOperation())
29 << QObject::tr("No parent menu has been defined");
30 }
31 }
32
33 /**
34 * Adds action to the current menu
35 * @param actionName the name of the action
36 * @param actionFunction the function that will be executed when the action is triggered
37 */
38 template <typename ActionFun>
39 void addAction(const QString &actionName, ActionFun actionFunction)
40 {
41 if (auto currMenu = currentMenu()) {
42 currMenu->addAction(actionName, actionFunction);
43 }
44 else {
45 qCCritical(LOG_GenerateVariableMenuOperation())
46 << QObject::tr("No current menu to attach the action");
47 }
48 }
49
50 /**
51 * Adds a new menu to the current menu
52 * @param name the name of the menu
53 */
54 void addMenu(const QString &name)
55 {
56 if (auto currMenu = currentMenu()) {
57 m_Menus.push(currMenu->addMenu(name));
58 }
59 else {
60 qCCritical(LOG_GenerateVariableMenuOperation())
61 << QObject::tr("No current menu to attach the new menu");
62 }
63 }
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
80 /// Closes the current menu
81 void closeMenu()
82 {
83 if (!m_Menus.isEmpty()) {
84 if (auto closedMenu = m_Menus.pop()) {
85 // Purge menu : if the closed menu has no entries, we remove it from its parent (the
86 // current menu)
87 if (auto currMenu = currentMenu()) {
88 if (closedMenu->isEmpty()) {
89 currMenu->removeAction(closedMenu->menuAction());
90 }
91 }
92 }
93 }
94 }
95
96 /// Gets the current menu (i.e. the top menu of the stack)
97 QMenu *currentMenu() const { return !m_Menus.isEmpty() ? m_Menus.top() : nullptr; }
98
99 /// Stack of all menus currently opened
100 QStack<QMenu *> m_Menus{};
101 };
102
103 } // namespace
104
105 struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate {
106 explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable)
107 : m_Variable{variable}, m_MenuBuilder{menu}
108 {
109 }
110
111 void visitNodeEnter(const IVisualizationWidget &container)
112 {
113 // Opens a new menu associated to the node
114 m_MenuBuilder.addMenu(container.name());
115 }
116
117 template <typename ActionFun>
118 void visitNodeLeave(const IVisualizationWidget &container, const QString &actionName,
119 ActionFun actionFunction)
120 {
121 if (m_Variable && container.canDrop(*m_Variable)) {
122 m_MenuBuilder.addSeparator();
123 m_MenuBuilder.addAction(actionName, actionFunction);
124 }
125
126 // Closes the menu associated to the node
127 m_MenuBuilder.closeMenu();
128 }
129
130 template <typename ActionFun>
131 void visitLeaf(const IVisualizationWidget &container, const QString &actionName,
132 ActionFun actionFunction)
133 {
134 if (m_Variable && container.canDrop(*m_Variable)) {
135 m_MenuBuilder.addAction(actionName, actionFunction);
136 }
137 }
138
139 std::shared_ptr<Variable> m_Variable;
140 MenuBuilder m_MenuBuilder;
141 };
142
143 GenerateVariableMenuOperation::GenerateVariableMenuOperation(QMenu *menu,
144 std::shared_ptr<Variable> variable)
145 : impl{spimpl::make_unique_impl<GenerateVariableMenuOperationPrivate>(menu, variable)}
146 {
147 }
148
149 void GenerateVariableMenuOperation::visitEnter(VisualizationWidget *widget)
150 {
151 // VisualizationWidget is not intended to accommodate a variable
152 Q_UNUSED(widget)
153 }
154
155 void GenerateVariableMenuOperation::visitLeave(VisualizationWidget *widget)
156 {
157 // VisualizationWidget is not intended to accommodate a variable
158 Q_UNUSED(widget)
159 }
160
161 void GenerateVariableMenuOperation::visitEnter(VisualizationTabWidget *tabWidget)
162 {
163 if (tabWidget) {
164 impl->visitNodeEnter(*tabWidget);
165 }
166 }
167
168 void GenerateVariableMenuOperation::visitLeave(VisualizationTabWidget *tabWidget)
169 {
170 if (tabWidget) {
171 impl->visitNodeLeave(
172 *tabWidget, QObject::tr("Open in a new zone"),
173 [ var = impl->m_Variable, tabWidget ]() { tabWidget->createZone(var); });
174 }
175 }
176
177 void GenerateVariableMenuOperation::visitEnter(VisualizationZoneWidget *zoneWidget)
178 {
179 if (zoneWidget) {
180 impl->visitNodeEnter(*zoneWidget);
181 }
182 }
183
184 void GenerateVariableMenuOperation::visitLeave(VisualizationZoneWidget *zoneWidget)
185 {
186 if (zoneWidget) {
187 impl->visitNodeLeave(
188 *zoneWidget, QObject::tr("Open in a new graph"),
189 [ var = impl->m_Variable, zoneWidget ]() { zoneWidget->createGraph(var); });
190 }
191 }
192
193 void GenerateVariableMenuOperation::visit(VisualizationGraphWidget *graphWidget)
194 {
195 if (graphWidget) {
196 impl->visitLeaf(
197 *graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()),
198 [ var = impl->m_Variable, graphWidget ]() { graphWidget->addVariable(var); });
199 }
200 }
@@ -10,3 +10,7
10 # Ignore false positive relative to 'override' keyword
10 # Ignore false positive relative to 'override' keyword
11 .*IPSIS_S04_VARIABLE.*found: override
11 .*IPSIS_S04_VARIABLE.*found: override
12 .*IPSIS_S06.*found: override
12 .*IPSIS_S06.*found: override
13
14 # Ignore false positive relative to 'final' keyword
15 .*IPSIS_S04_VARIABLE.*found: final
16 .*IPSIS_S06.*found: final
@@ -1,21 +1,23
1 #ifndef SCIQLOP_IVISUALIZATIONWIDGET_H
1 #ifndef SCIQLOP_IVISUALIZATIONWIDGET_H
2 #define SCIQLOP_IVISUALIZATIONWIDGET_H
2 #define SCIQLOP_IVISUALIZATIONWIDGET_H
3
3
4 #include "Visualization/IVisualizationWidgetVisitor.h"
4 #include "Visualization/IVariableContainer.h"
5
5
6 #include <QString>
6 #include <QString>
7 #include <memory>
7 #include <memory>
8
8
9 class IVisualizationWidgetVisitor;
10
9 /**
11 /**
10 * @brief The IVisualizationWidget handles the visualization widget.
12 * @brief The IVisualizationWidget handles the visualization widget.
11 */
13 */
12 class IVisualizationWidget {
14 class IVisualizationWidget : public IVariableContainer {
13
15
14 public:
16 public:
15 virtual ~IVisualizationWidget() = default;
17 virtual ~IVisualizationWidget() = default;
16
18
17 /// Initializes the plugin
19 /// Initializes the plugin
18 virtual void accept(IVisualizationWidget *visitor) = 0;
20 virtual void accept(IVisualizationWidgetVisitor *visitor) = 0;
19 virtual void close() = 0;
21 virtual void close() = 0;
20 virtual QString name() const = 0;
22 virtual QString name() const = 0;
21 };
23 };
@@ -15,9 +15,12 class IVisualizationWidgetVisitor {
15 public:
15 public:
16 virtual ~IVisualizationWidgetVisitor() = default;
16 virtual ~IVisualizationWidgetVisitor() = default;
17
17
18 virtual void visit(VisualizationWidget *widget) = 0;
18 virtual void visitEnter(VisualizationWidget *widget) = 0;
19 virtual void visit(VisualizationTabWidget *tabWidget) = 0;
19 virtual void visitLeave(VisualizationWidget *widget) = 0;
20 virtual void visit(VisualizationZoneWidget *zoneWidget) = 0;
20 virtual void visitEnter(VisualizationTabWidget *tabWidget) = 0;
21 virtual void visitLeave(VisualizationTabWidget *tabWidget) = 0;
22 virtual void visitEnter(VisualizationZoneWidget *zoneWidget) = 0;
23 virtual void visitLeave(VisualizationZoneWidget *zoneWidget) = 0;
21 virtual void visit(VisualizationGraphWidget *graphWidget) = 0;
24 virtual void visit(VisualizationGraphWidget *graphWidget) = 0;
22 };
25 };
23
26
@@ -25,7 +25,8 public:
25 void addVariable(std::shared_ptr<Variable> variable);
25 void addVariable(std::shared_ptr<Variable> variable);
26
26
27 // IVisualizationWidget interface
27 // IVisualizationWidget interface
28 void accept(IVisualizationWidget *visitor) override;
28 void accept(IVisualizationWidgetVisitor *visitor) override;
29 bool canDrop(const Variable &variable) const override;
29 void close() override;
30 void close() override;
30 QString name() const;
31 QString name() const;
31
32
@@ -36,7 +36,8 public:
36 void removeZone(VisualizationZoneWidget *zone);
36 void removeZone(VisualizationZoneWidget *zone);
37
37
38 // IVisualizationWidget interface
38 // IVisualizationWidget interface
39 void accept(IVisualizationWidget *visitor) override;
39 void accept(IVisualizationWidgetVisitor *visitor) override;
40 bool canDrop(const Variable &variable) const override;
40 void close() override;
41 void close() override;
41 QString name() const override;
42 QString name() const override;
42
43
@@ -32,7 +32,8 public:
32 void removeTab(VisualizationTabWidget *tab);
32 void removeTab(VisualizationTabWidget *tab);
33
33
34 // IVisualizationWidget interface
34 // IVisualizationWidget interface
35 void accept(IVisualizationWidget *visitor) override;
35 void accept(IVisualizationWidgetVisitor *visitor) override;
36 bool canDrop(const Variable &variable) const override;
36 void close() override;
37 void close() override;
37 QString name() const;
38 QString name() const;
38
39
@@ -33,7 +33,8 public:
33 void removeGraph(VisualizationGraphWidget *graph);
33 void removeGraph(VisualizationGraphWidget *graph);
34
34
35 // IVisualizationWidget interface
35 // IVisualizationWidget interface
36 void accept(IVisualizationWidget *visitor) override;
36 void accept(IVisualizationWidgetVisitor *visitor) override;
37 bool canDrop(const Variable &variable) const override;
37 void close() override;
38 void close() override;
38 QString name() const override;
39 QString name() const override;
39
40
@@ -1,5 +1,6
1 #include "Visualization/VisualizationGraphWidget.h"
1 #include "Visualization/VisualizationGraphWidget.h"
2 #include "Visualization/GraphPlottablesFactory.h"
2 #include "Visualization/GraphPlottablesFactory.h"
3 #include "Visualization/IVisualizationWidgetVisitor.h"
3 #include "ui_VisualizationGraphWidget.h"
4 #include "ui_VisualizationGraphWidget.h"
4
5
5 #include <Variable/Variable.h>
6 #include <Variable/Variable.h>
@@ -56,9 +57,18 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
56 }
57 }
57 }
58 }
58
59
59 void VisualizationGraphWidget::accept(IVisualizationWidget *visitor)
60 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
60 {
61 {
61 // TODO: manage the visitor
62 if (visitor) {
63 visitor->visit(this);
64 }
65 }
66
67 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
68 {
69 /// @todo : for the moment, a graph can always accomodate a variable
70 Q_UNUSED(variable);
71 return true;
62 }
72 }
63
73
64 void VisualizationGraphWidget::close()
74 void VisualizationGraphWidget::close()
@@ -1,4 +1,5
1 #include "Visualization/VisualizationTabWidget.h"
1 #include "Visualization/VisualizationTabWidget.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
2 #include "ui_VisualizationTabWidget.h"
3 #include "ui_VisualizationTabWidget.h"
3
4
4 #include "Visualization/VisualizationZoneWidget.h"
5 #include "Visualization/VisualizationZoneWidget.h"
@@ -60,9 +61,30 void VisualizationTabWidget::removeZone(VisualizationZoneWidget *zone)
60 {
61 {
61 }
62 }
62
63
63 void VisualizationTabWidget::accept(IVisualizationWidget *visitor)
64 void VisualizationTabWidget::accept(IVisualizationWidgetVisitor *visitor)
64 {
65 {
65 // TODO: manage the visitor
66 if (visitor) {
67 visitor->visitEnter(this);
68
69 // Apply visitor to zone children
70 for (auto i = 0; i < layout()->count(); ++i) {
71 if (auto item = layout()->itemAt(i)) {
72 if (auto visualizationZoneWidget
73 = dynamic_cast<VisualizationZoneWidget *>(item->widget())) {
74 visualizationZoneWidget->accept(visitor);
75 }
76 }
77 }
78
79 visitor->visitLeave(this);
80 }
81 }
82
83 bool VisualizationTabWidget::canDrop(const Variable &variable) const
84 {
85 // A tab can always accomodate a variable
86 Q_UNUSED(variable);
87 return true;
66 }
88 }
67
89
68 void VisualizationTabWidget::close()
90 void VisualizationTabWidget::close()
@@ -1,4 +1,5
1 #include "Visualization/VisualizationWidget.h"
1 #include "Visualization/VisualizationWidget.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
2 #include "Visualization/VisualizationGraphWidget.h"
3 #include "Visualization/VisualizationGraphWidget.h"
3 #include "Visualization/VisualizationTabWidget.h"
4 #include "Visualization/VisualizationTabWidget.h"
4 #include "Visualization/VisualizationZoneWidget.h"
5 #include "Visualization/VisualizationZoneWidget.h"
@@ -81,9 +82,28 void VisualizationWidget::removeTab(VisualizationTabWidget *tab)
81 // lambda function (in the constructor)
82 // lambda function (in the constructor)
82 }
83 }
83
84
84 void VisualizationWidget::accept(IVisualizationWidget *visitor)
85 void VisualizationWidget::accept(IVisualizationWidgetVisitor *visitor)
85 {
86 {
86 // TODO: manage the visitor
87 if (visitor) {
88 visitor->visitEnter(this);
89
90 // Apply visitor for tab children
91 for (auto i = 0; i < ui->tabWidget->count(); ++i) {
92 if (auto visualizationTabWidget
93 = dynamic_cast<VisualizationTabWidget *>(ui->tabWidget->widget(i))) {
94 visualizationTabWidget->accept(visitor);
95 }
96 }
97
98 visitor->visitLeave(this);
99 }
100 }
101
102 bool VisualizationWidget::canDrop(const Variable &variable) const
103 {
104 // The main widget can never accomodate a variable
105 Q_UNUSED(variable);
106 return false;
87 }
107 }
88
108
89 void VisualizationWidget::close()
109 void VisualizationWidget::close()
@@ -1,4 +1,5
1 #include "Visualization/VisualizationZoneWidget.h"
1 #include "Visualization/VisualizationZoneWidget.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
2 #include "ui_VisualizationZoneWidget.h"
3 #include "ui_VisualizationZoneWidget.h"
3
4
4 #include "Visualization/VisualizationGraphWidget.h"
5 #include "Visualization/VisualizationGraphWidget.h"
@@ -54,9 +55,31 void VisualizationZoneWidget::removeGraph(VisualizationGraphWidget *graph)
54 {
55 {
55 }
56 }
56
57
57 void VisualizationZoneWidget::accept(IVisualizationWidget *visitor)
58 void VisualizationZoneWidget::accept(IVisualizationWidgetVisitor *visitor)
58 {
59 {
59 // TODO: manage the visitor
60 if (visitor) {
61 visitor->visitEnter(this);
62
63 // Apply visitor to graph children
64 auto layout = ui->visualizationZoneFrame->layout();
65 for (auto i = 0; i < layout->count(); ++i) {
66 if (auto item = layout->itemAt(i)) {
67 if (auto visualizationGraphWidget
68 = dynamic_cast<VisualizationGraphWidget *>(item->widget())) {
69 visualizationGraphWidget->accept(visitor);
70 }
71 }
72 }
73
74 visitor->visitLeave(this);
75 }
76 }
77
78 bool VisualizationZoneWidget::canDrop(const Variable &variable) const
79 {
80 // A tab can always accomodate a variable
81 Q_UNUSED(variable);
82 return true;
60 }
83 }
61
84
62 void VisualizationZoneWidget::close()
85 void VisualizationZoneWidget::close()
@@ -8,6 +8,7 SqpApplication\.h:\d+:.IPSIS_S04_VARIABLE.*found: sqpApp
8
8
9 # Ignore false positive relative to unnamed namespace
9 # Ignore false positive relative to unnamed namespace
10 DataSourceTreeWidgetItem\.cpp:\d+:.*IPSIS_F13.*
10 DataSourceTreeWidgetItem\.cpp:\d+:.*IPSIS_F13.*
11 GenerateVariableMenuOperation\.cpp:\d+:.*IPSIS_F13.*
11 VisualizationTabWidget\.cpp:\d+:.*IPSIS_F13.*
12 VisualizationTabWidget\.cpp:\d+:.*IPSIS_F13.*
12 VisualizationZoneWidget\.cpp:\d+:.*IPSIS_F13.*
13 VisualizationZoneWidget\.cpp:\d+:.*IPSIS_F13.*
13
14
General Comments 0
You need to be logged in to leave comments. Login now