Debian Codegraph @

src/Gui/MenuManager.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <QApplication>
25 #include <QMenu>
26 #include <QMenuBar>
27
28
29 #include "MenuManager.h"
30 #include "Application.h"
31 #include "Command.h"
32 #include "MainWindow.h"
33
34
35 using namespace Gui;
36
37
38 MenuItem::MenuItem() = default;
39
40 MenuItem::MenuItem(MenuItem* item)
41 {
42 if (item) {
43 item->appendItem(this);
44 }
45 }
46
47 MenuItem::~MenuItem()
48 {
49 clear();
50 }
51
52 void MenuItem::setCommand(const std::string& name)
53 {
54 _name = name;
55 }
56
57 std::string MenuItem::command() const
58 {
59 return _name;
60 }
61
62 bool MenuItem::hasItems() const
63 {
64 return !_items.isEmpty();
65 }
66
67 MenuItem* MenuItem::findItem(const std::string& name)
68 {
69 if (_name == name) {
70 return this;
71 }
72 else {
73 for (auto& item : _items) {
74 if (item->_name == name) {
75 return item;
76 }
77 }
78 }
79
80 return nullptr;
81 }
82
83 MenuItem* MenuItem::findParentOf(const std::string& name)
84 {
85 for (auto& item : _items) {
86 if (item->_name == name) {
87 return this;
88 }
89 }
90
91 for (auto& item : _items) {
92 if (item->findParentOf(name)) {
93 return item;
94 }
95 }
96
97 return nullptr;
98 }
99
100 MenuItem* MenuItem::copy() const
101 {
102 auto root = new MenuItem;
103 root->setCommand(command());
104
105 for (auto& item : _items) {
106 root->appendItem(item->copy());
107 }
108
109 return root;
110 }
111
112 uint MenuItem::count() const
113 {
114 return _items.count();
115 }
116
117 void MenuItem::appendItem(MenuItem* item)
118 {
119 _items.push_back(item);
120 }
121
122 bool MenuItem::insertItem(MenuItem* before, MenuItem* item)
123 {
124 int pos = _items.indexOf(before);
125 if (pos != -1) {
126 _items.insert(pos, item);
127 return true;
128 }
129
130 return false;
131 }
132
133 MenuItem* MenuItem::afterItem(MenuItem* item) const
134 {
135 int pos = _items.indexOf(item);
136 if (pos < 0 || pos + 1 == _items.size()) {
137 return nullptr;
138 }
139 return _items.at(pos + 1);
140 }
141
142 void MenuItem::removeItem(MenuItem* item)
143 {
144 int pos = _items.indexOf(item);
145 if (pos != -1) {
146 _items.removeAt(pos);
147 }
148 }
149
150 void MenuItem::clear()
151 {
152 for (auto& item : _items) {
153 delete item;
154 }
155 _items.clear();
156 }
157
158 MenuItem& MenuItem::operator<<(const std::string& command)
159 {
160 auto item = new MenuItem(this);
161 item->setCommand(command);
162 return *this;
163 }
164
165 MenuItem& MenuItem::operator<<(MenuItem* item)
166 {
167 appendItem(item);
168 return *this;
169 }
170
171 QList<MenuItem*> MenuItem::getItems() const
172 {
173 return _items;
174 }
175
176 // -----------------------------------------------------------
177
178 MenuManager* MenuManager::_instance = nullptr;
179
180 MenuManager* MenuManager::getInstance()
181 {
182 if (!_instance) {
183 _instance = new MenuManager;
184 }
185 return _instance;
186 }
187
188 void MenuManager::destruct()
189 {
190 delete _instance;
191 _instance = nullptr;
192 }
193
194 MenuManager::MenuManager() = default;
195
196 MenuManager::~MenuManager() = default;
197
198 void MenuManager::setup(MenuItem* menuItems) const
199 {
200 if (!menuItems) {
201 return; // empty menu bar
202 }
203
204 QMenuBar* menuBar = getMainWindow()->menuBar();
205
206 // By right, it should be fine for more than one command action having the
207 // same shortcut but in different workbench. It should not require manual
208 // conflict resolving in this case, as the action in an inactive workbench
209 // is expected to be inactive as well, or else user may experience
210 // seemingly random shortcut miss firing based on the order he/she
211 // switches workbenches. In fact, this may be considered as an otherwise
212 // difficult to implement feature of context aware shortcut, where a
213 // specific shortcut can activate different actions under different
214 // workbenches.
215 //
216 // This works as expected for action adding to a toolbar. As Qt will ignore
217 // actions inside an invisible toolbar. However, Qt refuse to do the same
218 // for actions in a hidden menu action of a menu bar. This is very likely a
219 // Qt bug, as the behavior does not seem to conform to Qt's documentation
220 // of Qt::ShortcutContext.
221 //
222 // Clearing the menu bar, and recreate it every time when switching
223 // workbench with only the active actions can solve this problem.
224 menuBar->clear();
225
226 QList<QAction*> actions = menuBar->actions();
227 for (auto& item : menuItems->getItems()) {
228 // search for the menu action
229 QAction* action = findAction(actions, QString::fromLatin1(item->command().c_str()));
230 if (!action) {
231 // There must be not more than one separator in the menu bar, so
232 // we can safely remove it if available and append it at the end
233 if (item->command() == "Separator") {
234 action = menuBar->addSeparator();
235 action->setObjectName(QLatin1String("Separator"));
236 }
237 else {
238 // create a new menu
239 std::string menuName = item->command();
240 QMenu* menu = menuBar->addMenu(QApplication::translate("Workbench", menuName.c_str()));
241 action = menu->menuAction();
242 menu->setObjectName(QString::fromLatin1(menuName.c_str()));
243 action->setObjectName(QString::fromLatin1(menuName.c_str()));
244 }
245
246 // set the menu user data
247 action->setData(QString::fromLatin1(item->command().c_str()));
248 }
249 else {
250 // put the menu at the end
251 menuBar->removeAction(action);
252 menuBar->addAction(action);
253 action->setVisible(true);
254 int index = actions.indexOf(action);
255 actions.removeAt(index);
256 }
257
258 // flll up the menu
259 if (!action->isSeparator()) {
260 setup(item, action->menu());
261 }
262 }
263
264 // hide all menus which we don't need for the moment
265 for (auto& action : actions) {
266 action->setVisible(false);
267 }
268
269 // enable update again
270 // menuBar->setUpdatesEnabled(true);
271 }
272
273 void MenuManager::setup(MenuItem* item, QMenu* menu) const
274 {
275 CommandManager& mgr = Application::Instance->commandManager();
276 QList<QAction*> actions = menu->actions();
277 for (auto& item : item->getItems()) {
278 // search for the menu item
279 QList<QAction*> used_actions
280 = findActions(actions, QString::fromLatin1(item->command().c_str()));
281 if (used_actions.isEmpty()) {
282 if (item->command() == "Separator") {
283 QAction* action = menu->addSeparator();
284 action->setObjectName(QLatin1String("Separator"));
285 action->setData(QLatin1String("Separator"));
286 used_actions.append(action);
287 }
288 else {
289 if (item->hasItems()) {
290 // Creste a submenu
291 std::string menuName = item->command();
292 QMenu* submenu = menu->addMenu(
293 QApplication::translate("Workbench", menuName.c_str())
294 );
295 QAction* action = submenu->menuAction();
296 submenu->setObjectName(QString::fromLatin1(item->command().c_str()));
297 action->setObjectName(QString::fromLatin1(item->command().c_str()));
298 // set the menu user data
299 action->setData(QString::fromLatin1(item->command().c_str()));
300 used_actions.append(action);
301 }
302 else {
303 // A command can have more than one QAction
304 int count = menu->actions().count();
305 // Check if action was added successfully
306 if (mgr.addTo(item->command().c_str(), menu)) {
307 QList<QAction*> acts = menu->actions();
308 for (int i = count; i < acts.count(); i++) {
309 QAction* act = acts[i];
310 // set the menu user data
311 act->setData(QString::fromLatin1(item->command().c_str()));
312 used_actions.append(act);
313 }
314 }
315 }
316 }
317 }
318 else {
319 for (auto& action : used_actions) {
320 // put the menu item at the end
321 menu->removeAction(action);
322 menu->addAction(action);
323 int index = actions.indexOf(action);
324 actions.removeAt(index);
325 }
326 }
327
328 // fill up the submenu
329 if (item->hasItems()) {
330 setup(item, used_actions.front()->menu());
331 }
332 }
333
334 // remove all menu items which we don't need for the moment
335 for (auto& action : actions) {
336 menu->removeAction(action);
337 }
338 }
339
340 void MenuManager::retranslate() const
341 {
342 QMenuBar* menuBar = getMainWindow()->menuBar();
343 for (auto& action : menuBar->actions()) {
344 if (action->menu()) {
345 retranslate(action->menu());
346 }
347 }
348 }
349
350 void MenuManager::retranslate(QMenu* menu) const
351 {
352 // Note: Here we search for all menus and submenus to retranslate their
353 // titles. To ease the translation for each menu the native name is set
354 // as user data. However, there are special menus that are created by
355 // actions for which the name of the according command name is set. For
356 // such menus we have to use the command's menu text instead. Examples
357 // for such actions are Std_RecentFiles, Std_Workbench or Std_FreezeViews.
358 CommandManager& mgr = Application::Instance->commandManager();
359 QByteArray menuName = menu->menuAction()->data().toByteArray();
360 Command* cmd = mgr.getCommandByName(menuName);
361 if (cmd) {
362 menu->setTitle(QApplication::translate(cmd->className(), cmd->getMenuText()));
363 }
364 else {
365 menu->setTitle(QApplication::translate("Workbench", (const char*)menuName));
366 }
367 for (auto& action : menu->actions()) {
368 if (action->menu()) {
369 retranslate(action->menu());
370 }
371 }
372 }
373
374 QAction* MenuManager::findAction(const QList<QAction*>& acts, const QString& item) const
375 {
376 for (auto& action : acts) {
377 if (action->data().toString() == item) {
378 return action;
379 }
380 }
381
382 return nullptr; // no item with the user data found
383 }
384
385 QList<QAction*> MenuManager::findActions(const QList<QAction*>& acts, const QString& item) const
386 {
387 // It is possible that the user text of several actions match with 'item'.
388 // But for the first match all following actions must match. For example
389 // the Std_WindowsMenu command provides several actions with the same user
390 // name.
391 bool first_match = false;
392 QList<QAction*> used;
393 for (auto& action : acts) {
394 if (action->data().toString() == item) {
395 used.append(action);
396 first_match = true;
397 // get only one separator per request
398 if (item == QLatin1String("Separator")) {
399 break;
400 }
401 }
402 else if (first_match) {
403 break;
404 }
405 }
406
407 return used;
408 }
409
410 void MenuManager::setupContextMenu(MenuItem* item, QMenu& menu) const
411 {
412 setup(item, &menu);
413 }