CCommandTools.h
Go to the documentation of this file.
1 #ifndef iqtgui_CCommandTools_included
2 #define iqtgui_CCommandTools_included
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 #include <QtWidgets/QAction>
12 #include <QtWidgets/QActionGroup>
13 #else
14 #include <QtGui/QMenu>
15 #include <QtGui/QMenuBar>
16 #include <QtGui/QToolBar>
17 #include <QtGui/QAction>
18 #include <QtGui/QActionGroup>
19 #endif
20 
21 // ACF includes
23 
25 
26 
27 namespace iqtgui
28 {
29 
30 
35 {
36 public:
40  template <class MenuType>
41  static void CreateMenu(const iqtgui::CHierarchicalCommand& command, MenuType& result);
42 
46  static int SetupToolbar(
47  const iqtgui::CHierarchicalCommand& command,
48  QToolBar& result,
49  int prevGroupId = ibase::ICommand::GI_NONE);
50 
54  static int SetupContextMenu(
55  const iqtgui::CHierarchicalCommand& command,
56  QWidget& menuOwner,
57  int prevGroupId = ibase::ICommand::GI_NONE);
58 };
59 
60 
61 // protected template methods
62 
63 template <class MenuType>
64 void CCommandTools::CreateMenu(const iqtgui::CHierarchicalCommand& command, MenuType& result)
65 {
66  int prevGroupId = ibase::ICommand::GI_NONE;
67 
68  int childsCount = command.GetChildsCount();
69 
70  QMap<int, QActionGroup*> groups;
71 
72  for (int i = 0; i < childsCount; ++i){
73  QString text = command.text();
74  iqtgui::CHierarchicalCommand* hierarchicalPtr = const_cast<iqtgui::CHierarchicalCommand*>(
75  dynamic_cast<const iqtgui::CHierarchicalCommand*>(command.GetChild(i)));
76 
77  if (hierarchicalPtr != NULL){
78  QString text2 = hierarchicalPtr->text();
79  int groupId = hierarchicalPtr->GetGroupId();
80  int flags = hierarchicalPtr->GetStaticFlags();
81 
82  if ((groupId != prevGroupId) && (prevGroupId != ibase::ICommand::GI_NONE)){
83  result.addSeparator();
84  }
85 
86  if (groupId != ibase::ICommand::GI_NONE){
87  prevGroupId = groupId;
88  }
89 
90  if (hierarchicalPtr->GetChildsCount() > 0){
91  QMenu* newMenuPtr = new QMenu(&result);
92  newMenuPtr->setTitle(hierarchicalPtr->GetName());
93 
94  CreateMenu<QMenu>(*hierarchicalPtr, *newMenuPtr);
95 
96  result.addMenu(newMenuPtr);
97  }
98  else if ((flags & ibase::ICommand::CF_GLOBAL_MENU) != 0){
99  if ((flags & ibase::ICommand::CF_EXCLUSIVE) != 0){
100  QActionGroup*& groupPtr = groups[hierarchicalPtr->GetGroupId()];
101  if (groupPtr == NULL){
102  groupPtr = new QActionGroup(&result);
103  groupPtr->setExclusive(true);
104  }
105 
106  groupPtr->addAction(hierarchicalPtr);
107  hierarchicalPtr->setCheckable(true);
108  }
109 
110  result.addAction(hierarchicalPtr);
111  }
112  }
113  }
114 }
115 
116 
117 } // namespace iqtgui
118 
119 
120 #endif // !iqtgui_CCommandTools_included
121 
122 
Implementation of hierarchical command based on QAction from Qt.
static int SetupContextMenu(const iqtgui::CHierarchicalCommand &command, QWidget &menuOwner, int prevGroupId=ibase::ICommand::GI_NONE)
Fill a context menu of a widget with the commands.
Enable to use this command in global application menu.
Definition: ICommand.h:38
Helper class to manage menu and toolbar using CHierarchicalCommand.
Definition: CCommandTools.h:34
virtual const QString & GetName() const
Get the object name.
#define NULL
Definition: istd.h:64
virtual ibase::ICommand * GetChild(int index) const
Get child value.
virtual int GetChildsCount() const
Get number of childs.
virtual int GetGroupId() const
static int SetupToolbar(const iqtgui::CHierarchicalCommand &command, QToolBar &result, int prevGroupId=ibase::ICommand::GI_NONE)
Fill a toolbar with the commands.
static void CreateMenu(const iqtgui::CHierarchicalCommand &command, MenuType &result)
Create menu according to the given commands.
Definition: CCommandTools.h:64
virtual int GetStaticFlags() const
Enable exclusive selection of this command with other commands in the same node with the same group I...
Definition: ICommand.h:63

© 2007-2017 Witold Gantzke and Kirill Lepskiy