ACF $AcfVersion:0$
CCommandTools.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QtGlobal>
7#if QT_VERSION >= 0x050000
8#include <QtWidgets/QMenu>
9#include <QtWidgets/QMenuBar>
10#include <QtWidgets/QToolBar>
11#else
12#include <QtGui/QMenu>
13#include <QtGui/QMenuBar>
14#include <QtGui/QToolBar>
15#endif
16
17#if QT_VERSION >= 0x050000 && QT_VERSION < 0x060000
18#include <QtWidgets/QAction>
19#include <QtWidgets/QActionGroup>
20#else
21#include <QtGui/QAction>
22#include <QtGui/QActionGroup>
23#endif
24
25// ACF includes
27
29
30
31namespace iqtgui
32{
33
34
39{
40public:
44 template <class MenuType>
45 static void CreateMenu(
46 const iqtgui::CHierarchicalCommand& command,
47 MenuType& result,
48 const QVector<int>& includedGroups = QVector<int>(),
49 const QVector<int>& excludedGroups = QVector<int>());
50
54 static int SetupToolbar(
55 const iqtgui::CHierarchicalCommand& command,
56 QToolBar& result,
57 int prevGroupId = ibase::ICommand::GI_NONE,
58 const QVector<int>& includedGroups = QVector<int>(),
59 const QVector<int>& excludedGroups = QVector<int>());
60
64 static int SetupContextMenu(
65 const iqtgui::CHierarchicalCommand& command,
66 QWidget& menuOwner,
67 int prevGroupId = ibase::ICommand::GI_NONE,
68 const QVector<int>& includedGroups = QVector<int>(),
69 const QVector<int>& excludedGroups = QVector<int>());
70
71private:
72 static bool IsCommandIncluded(
73 const iqtgui::CHierarchicalCommand& command,
74 const QVector<int>& includedGroups = QVector<int>(),
75 const QVector<int>& excludedGroups = QVector<int>());
76};
77
78
79// protected template methods
80
81template <class MenuType>
83 const iqtgui::CHierarchicalCommand& command,
84 MenuType& result,
85 const QVector<int>& includedGroups,
86 const QVector<int>& excludedGroups)
87{
88 int prevGroupId = ibase::ICommand::GI_NONE;
89
90 int childsCount = command.GetChildsCount();
91
92 QMap<int, QActionGroup*> groups;
93
94 for (int i = 0; i < childsCount; ++i){
95 QString text = command.text();
96 iqtgui::CHierarchicalCommand* hierarchicalPtr = const_cast<iqtgui::CHierarchicalCommand*>(
97 dynamic_cast<const iqtgui::CHierarchicalCommand*>(command.GetChild(i)));
98
99 if (hierarchicalPtr != NULL){
100 QString text2 = hierarchicalPtr->text();
101 int groupId = hierarchicalPtr->GetGroupId();
102 int flags = hierarchicalPtr->GetStaticFlags();
103
104 if ((groupId != prevGroupId) && (prevGroupId != ibase::ICommand::GI_NONE)){
105 result.addSeparator();
106 }
107
108 if (groupId != ibase::ICommand::GI_NONE){
109 prevGroupId = groupId;
110 }
111
112 if (hierarchicalPtr->GetChildsCount() > 0){
113 QMenu* newMenuPtr = new QMenu(&result);
114 newMenuPtr->setTitle(hierarchicalPtr->GetName());
115
116 CreateMenu<QMenu>(*hierarchicalPtr, *newMenuPtr);
117
118 result.addMenu(newMenuPtr);
119 }
120 else if ((flags & ibase::ICommand::CF_GLOBAL_MENU) != 0){
121 if (IsCommandIncluded(*hierarchicalPtr, includedGroups, excludedGroups)){
122 if ((flags & ibase::ICommand::CF_EXCLUSIVE) != 0){
123 QActionGroup*& groupPtr = groups[hierarchicalPtr->GetGroupId()];
124 if (groupPtr == NULL){
125 groupPtr = new QActionGroup(&result);
126 groupPtr->setExclusive(true);
127 }
128
129 groupPtr->addAction(hierarchicalPtr);
130 hierarchicalPtr->setCheckable(true);
131 }
132
133 result.addAction(hierarchicalPtr);
134 }
135 }
136 }
137 }
138}
139
140
141} // namespace iqtgui
142
143
144
145
@ CF_EXCLUSIVE
Enable exclusive selection of this command with other commands in the same node with the same group I...
Definition ICommand.h:63
@ CF_GLOBAL_MENU
Enable to use this command in global application menu.
Definition ICommand.h:38
virtual const QString & GetName() const override
Get the object name.
Helper class to manage menu and toolbar using CHierarchicalCommand.
static void CreateMenu(const iqtgui::CHierarchicalCommand &command, MenuType &result, const QVector< int > &includedGroups=QVector< int >(), const QVector< int > &excludedGroups=QVector< int >())
Create menu according to the given commands.
static int SetupToolbar(const iqtgui::CHierarchicalCommand &command, QToolBar &result, int prevGroupId=ibase::ICommand::GI_NONE, const QVector< int > &includedGroups=QVector< int >(), const QVector< int > &excludedGroups=QVector< int >())
Fill a toolbar with the commands.
static int SetupContextMenu(const iqtgui::CHierarchicalCommand &command, QWidget &menuOwner, int prevGroupId=ibase::ICommand::GI_NONE, const QVector< int > &includedGroups=QVector< int >(), const QVector< int > &excludedGroups=QVector< int >())
Fill a context menu of a widget with the commands.
Implementation of hierarchical command based on QAction from Qt.
virtual ibase::ICommand * GetChild(int index) const override
virtual int GetChildsCount() const override
virtual int GetStaticFlags() const override
virtual int GetGroupId() const override
#define NULL
Definition istd.h:74
Standard GUI specific interfaces and components based on Qt.