##// END OF EJS Templates
Adds icon for 'Plot' menu
Alexandre Leroux -
r322:e195db267c87
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
@@ -1,59 +1,60
1 1 #ifndef SCIQLOP_MENUBUILDER_H
2 2 #define SCIQLOP_MENUBUILDER_H
3 3
4 4 #include <QLoggingCategory>
5 5 #include <QMenu>
6 6 #include <QStack>
7 7
8 8 Q_DECLARE_LOGGING_CATEGORY(LOG_MenuBuilder)
9 9
10 10 /// Helper assigned to build a hierarchical menu
11 11 class MenuBuilder {
12 12 public:
13 13 /**
14 14 * Ctor
15 15 * @param menu the parent menu
16 16 */
17 17 explicit MenuBuilder(QMenu *menu);
18 18
19 19 /**
20 20 * Adds action to the current menu
21 21 * @param actionName the name of the action
22 22 * @param actionFunction the function that will be executed when the action is triggered
23 23 */
24 24 template <typename ActionFun>
25 25 void addAction(const QString &actionName, ActionFun actionFunction);
26 26
27 27 /**
28 28 * Adds a new menu to the current menu
29 29 * @param name the name of the menu
30 * @param icon the icon of the menu (can be null)
30 31 */
31 void addMenu(const QString &name);
32 void addMenu(const QString &name, const QIcon &icon = {});
32 33
33 34 /// Adds a separator to the current menu. The separator is added only if the menu already
34 35 /// contains entries
35 36 void addSeparator();
36 37
37 38 /// Closes the current menu
38 39 void closeMenu();
39 40
40 41 private:
41 42 /// @return the current menu (i.e. the top menu of the stack), nullptr if there is no menu
42 43 QMenu *currentMenu() const;
43 44
44 45 /// Stack of all menus currently opened
45 46 QStack<QMenu *> m_Menus{};
46 47 };
47 48
48 49 template <typename ActionFun>
49 50 void MenuBuilder::addAction(const QString &actionName, ActionFun actionFunction)
50 51 {
51 52 if (auto currMenu = currentMenu()) {
52 53 currMenu->addAction(actionName, actionFunction);
53 54 }
54 55 else {
55 56 qCCritical(LOG_MenuBuilder()) << QObject::tr("No current menu to attach the action");
56 57 }
57 58 }
58 59
59 60 #endif // SCIQLOP_MENUBUILDER_H
@@ -1,8 +1,9
1 1 <RCC>
2 2 <qresource prefix="/">
3 3 <file>icones/delete.png</file>
4 4 <file>icones/openInspector.png</file>
5 5 <file>icones/next.png</file>
6 <file>icones/plot.png</file>
6 7 <file>icones/previous.png</file>
7 8 </qresource>
8 9 </RCC>
@@ -1,146 +1,146
1 1 #include "Visualization/operations/GenerateVariableMenuOperation.h"
2 2 #include "Visualization/operations/MenuBuilder.h"
3 3
4 4 #include "Visualization/VisualizationGraphWidget.h"
5 5 #include "Visualization/VisualizationTabWidget.h"
6 6 #include "Visualization/VisualizationZoneWidget.h"
7 7
8 8 #include <Variable/Variable.h>
9 9
10 10 #include <QMenu>
11 11 #include <QStack>
12 12
13 13 Q_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation, "GenerateVariableMenuOperation")
14 14
15 15 struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate {
16 16 explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable)
17 17 : m_Variable{variable}, m_MenuBuilder{menu}
18 18 {
19 19 }
20 20
21 21 void visitRootEnter()
22 22 {
23 23 // Creates the root menu
24 m_MenuBuilder.addMenu(QObject::tr("Plot"));
24 m_MenuBuilder.addMenu(QObject::tr("Plot"), QIcon{":/icones/plot.png"});
25 25 }
26 26
27 27 void visitRootLeave()
28 28 {
29 29 // Closes the root menu
30 30 m_MenuBuilder.closeMenu();
31 31 }
32 32
33 33 void visitNodeEnter(const IVisualizationWidget &container)
34 34 {
35 35 // Opens a new menu associated to the node
36 36 m_MenuBuilder.addMenu(container.name());
37 37 }
38 38
39 39 template <typename ActionFun>
40 40 void visitNodeLeave(const IVisualizationWidget &container, const QString &actionName,
41 41 ActionFun actionFunction)
42 42 {
43 43 if (m_Variable && container.canDrop(*m_Variable)) {
44 44 m_MenuBuilder.addSeparator();
45 45 m_MenuBuilder.addAction(actionName, actionFunction);
46 46 }
47 47
48 48 // Closes the menu associated to the node
49 49 m_MenuBuilder.closeMenu();
50 50 }
51 51
52 52 template <typename ActionFun>
53 53 void visitLeaf(const IVisualizationWidget &container, const QString &actionName,
54 54 ActionFun actionFunction)
55 55 {
56 56 if (m_Variable && container.canDrop(*m_Variable)) {
57 57 m_MenuBuilder.addAction(actionName, actionFunction);
58 58 }
59 59 }
60 60
61 61 std::shared_ptr<Variable> m_Variable;
62 62 MenuBuilder m_MenuBuilder;
63 63 };
64 64
65 65 GenerateVariableMenuOperation::GenerateVariableMenuOperation(QMenu *menu,
66 66 std::shared_ptr<Variable> variable)
67 67 : impl{spimpl::make_unique_impl<GenerateVariableMenuOperationPrivate>(menu, variable)}
68 68 {
69 69 }
70 70
71 71 void GenerateVariableMenuOperation::visitEnter(VisualizationWidget *widget)
72 72 {
73 73 // VisualizationWidget is not intended to accommodate a variable
74 74 Q_UNUSED(widget)
75 75
76 76 impl->visitRootEnter();
77 77 }
78 78
79 79 void GenerateVariableMenuOperation::visitLeave(VisualizationWidget *widget)
80 80 {
81 81 // VisualizationWidget is not intended to accommodate a variable
82 82 Q_UNUSED(widget)
83 83
84 84 impl->visitRootLeave();
85 85 }
86 86
87 87 void GenerateVariableMenuOperation::visitEnter(VisualizationTabWidget *tabWidget)
88 88 {
89 89 if (tabWidget) {
90 90 impl->visitNodeEnter(*tabWidget);
91 91 }
92 92 else {
93 93 qCCritical(LOG_GenerateVariableMenuOperation(),
94 94 "Can't visit enter VisualizationTabWidget : the widget is null");
95 95 }
96 96 }
97 97
98 98 void GenerateVariableMenuOperation::visitLeave(VisualizationTabWidget *tabWidget)
99 99 {
100 100 if (tabWidget) {
101 101 impl->visitNodeLeave(
102 102 *tabWidget, QObject::tr("Open in a new zone"),
103 103 [ var = impl->m_Variable, tabWidget ]() { tabWidget->createZone(var); });
104 104 }
105 105 else {
106 106 qCCritical(LOG_GenerateVariableMenuOperation(),
107 107 "Can't visit leave VisualizationTabWidget : the widget is null");
108 108 }
109 109 }
110 110
111 111 void GenerateVariableMenuOperation::visitEnter(VisualizationZoneWidget *zoneWidget)
112 112 {
113 113 if (zoneWidget) {
114 114 impl->visitNodeEnter(*zoneWidget);
115 115 }
116 116 else {
117 117 qCCritical(LOG_GenerateVariableMenuOperation(),
118 118 "Can't visit enter VisualizationZoneWidget : the widget is null");
119 119 }
120 120 }
121 121
122 122 void GenerateVariableMenuOperation::visitLeave(VisualizationZoneWidget *zoneWidget)
123 123 {
124 124 if (zoneWidget) {
125 125 impl->visitNodeLeave(
126 126 *zoneWidget, QObject::tr("Open in a new graph"),
127 127 [ var = impl->m_Variable, zoneWidget ]() { zoneWidget->createGraph(var); });
128 128 }
129 129 else {
130 130 qCCritical(LOG_GenerateVariableMenuOperation(),
131 131 "Can't visit leave VisualizationZoneWidget : the widget is null");
132 132 }
133 133 }
134 134
135 135 void GenerateVariableMenuOperation::visit(VisualizationGraphWidget *graphWidget)
136 136 {
137 137 if (graphWidget) {
138 138 impl->visitLeaf(
139 139 *graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()),
140 140 [ var = impl->m_Variable, graphWidget ]() { graphWidget->addVariableUsingGraph(var); });
141 141 }
142 142 else {
143 143 qCCritical(LOG_GenerateVariableMenuOperation(),
144 144 "Can't visit VisualizationGraphWidget : the widget is null");
145 145 }
146 146 }
@@ -1,55 +1,55
1 1 #include "Visualization/operations/MenuBuilder.h"
2 2
3 3 Q_LOGGING_CATEGORY(LOG_MenuBuilder, "MenuBuilder")
4 4
5 5 MenuBuilder::MenuBuilder(QMenu *menu)
6 6 {
7 7 if (menu) {
8 8 m_Menus.push(menu);
9 9 }
10 10 else {
11 11 qCCritical(LOG_MenuBuilder()) << QObject::tr("No parent menu has been defined");
12 12 }
13 13 }
14 14
15 void MenuBuilder::addMenu(const QString &name)
15 void MenuBuilder::addMenu(const QString &name, const QIcon &icon)
16 16 {
17 17 if (auto currMenu = currentMenu()) {
18 m_Menus.push(currMenu->addMenu(name));
18 m_Menus.push(currMenu->addMenu(icon, name));
19 19 }
20 20 else {
21 21 qCCritical(LOG_MenuBuilder()) << QObject::tr("No current menu to attach the new menu");
22 22 }
23 23 }
24 24
25 25 void MenuBuilder::addSeparator()
26 26 {
27 27 if (auto currMenu = currentMenu()) {
28 28 if (!currMenu->isEmpty()) {
29 29 currMenu->addSeparator();
30 30 }
31 31 }
32 32 else {
33 33 qCCritical(LOG_MenuBuilder()) << QObject::tr("No current menu to attach the separator");
34 34 }
35 35 }
36 36
37 37 void MenuBuilder::closeMenu()
38 38 {
39 39 if (!m_Menus.isEmpty()) {
40 40 if (auto closedMenu = m_Menus.pop()) {
41 41 // Purge menu : if the closed menu has no entries, we remove it from its parent (the
42 42 // current menu)
43 43 if (auto currMenu = currentMenu()) {
44 44 if (closedMenu->isEmpty()) {
45 45 currMenu->removeAction(closedMenu->menuAction());
46 46 }
47 47 }
48 48 }
49 49 }
50 50 }
51 51
52 52 QMenu *MenuBuilder::currentMenu() const
53 53 {
54 54 return !m_Menus.isEmpty() ? m_Menus.top() : nullptr;
55 55 }
General Comments 0
You need to be logged in to leave comments. Login now