Transorm in MenuBuilder class with .h and .cpp
@@ -5,9 +5,13 | |||||
5 |
|
5 | |||
6 | #include <Common/spimpl.h> |
|
6 | #include <Common/spimpl.h> | |
7 |
|
7 | |||
|
8 | #include <QLoggingCategory> | |||
|
9 | ||||
8 | class QMenu; |
|
10 | class QMenu; | |
9 | class Variable; |
|
11 | class Variable; | |
10 |
|
12 | |||
|
13 | Q_DECLARE_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation) | |||
|
14 | ||||
11 | /** |
|
15 | /** | |
12 | * @brief The GenerateVariableMenuOperation class defines an operation that traverses all of |
|
16 | * @brief The GenerateVariableMenuOperation class defines an operation that traverses all of | |
13 | * visualization widgets to determine which can accommodate a variable. The result of the operation |
|
17 | * visualization widgets to determine which can accommodate a variable. The result of the operation |
@@ -7,14 +7,69 | |||||
7 | #include <Variable/Variable.h> |
|
7 | #include <Variable/Variable.h> | |
8 |
|
8 | |||
9 | #include <QMenu> |
|
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 a new menu to the current menu | |||
|
35 | * @param name the name of the menu | |||
|
36 | */ | |||
|
37 | void addMenu(const QString &name) | |||
|
38 | { | |||
|
39 | if (auto currMenu = currentMenu()) { | |||
|
40 | m_Menus.push(currMenu->addMenu(name)); | |||
|
41 | } | |||
|
42 | else { | |||
|
43 | qCCritical(LOG_GenerateVariableMenuOperation()) | |||
|
44 | << QObject::tr("No current menu to attach the new menu"); | |||
|
45 | } | |||
|
46 | } | |||
|
47 | ||||
|
48 | /// Closes the current menu | |||
|
49 | void closeMenu() | |||
|
50 | { | |||
|
51 | if (!m_Menus.isEmpty()) { | |||
|
52 | m_Menus.pop(); | |||
|
53 | } | |||
|
54 | } | |||
|
55 | ||||
|
56 | /// Gets the current menu (i.e. the top menu of the stack) | |||
|
57 |
QMenu *currentMenu() const { return !m_Menus.isEmpty() ? m_Menus.top() : nullptr; }
Add in the doc that nullptr can be a normal result that is different from m_Menus.top() = nullptr |
|||
|
58 | ||||
|
59 | /// Stack of all menus currently opened | |||
|
60 | QStack<QMenu *> m_Menus{}; | |||
|
61 | }; | |||
|
62 | ||||
|
63 | } // namespace | |||
10 |
|
64 | |||
11 | struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate { |
|
65 | struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate { | |
12 | explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable) |
|
66 | explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable) | |
13 | : m_Variable{variable} |
|
67 | : m_Variable{variable}, m_MenuBuilder{menu} | |
14 | { |
|
68 | { | |
15 | } |
|
69 | } | |
16 |
|
70 | |||
17 | std::shared_ptr<Variable> m_Variable; |
|
71 | std::shared_ptr<Variable> m_Variable; | |
|
72 | MenuBuilder m_MenuBuilder; | |||
18 | }; |
|
73 | }; | |
19 |
|
74 | |||
20 | GenerateVariableMenuOperation::GenerateVariableMenuOperation(QMenu *menu, |
|
75 | GenerateVariableMenuOperation::GenerateVariableMenuOperation(QMenu *menu, |
General Comments 0
You need to be logged in to leave comments.
Login now