forked from scribusproject/scribus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactionsearch.cpp
More file actions
77 lines (64 loc) · 1.94 KB
/
actionsearch.cpp
File metadata and controls
77 lines (64 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "actionsearch.h"
#include <QAction>
#include <QDebug>
#include <QList>
#include <QMenuBar>
#include <QMenu>
#include <QStringList>
ActionSearch::ActionSearch(QMenuBar *menuBar)
: menuBar{menuBar}
{
}
void ActionSearch::update()
{
m_actions.clear();
for (auto menuAction: menuBar->actions())
readMenuActions(menuAction->menu());
}
void ActionSearch::execute(const QString& actionName)
{
if (!m_actions.contains(actionName))
return;
QAction* action = m_actions[actionName];
if (!action->isEnabled())
return;
action->trigger();
}
void ActionSearch::readMenuActions(QMenu* menu)
{
// TODO: check why menu can be null
if (menu == nullptr)
return;
QStringList menus;
QMenu* currentMenu = menu;
while (currentMenu != nullptr)
{
QString title = currentMenu->title().replace("&", "");
menus.prepend(title);
currentMenu = dynamic_cast<QMenu*>(currentMenu->parentWidget());
}
QString menuName(menus.join(" > "));
for (auto action: menu->actions())
{
if (action->menu() != nullptr)
{
readMenuActions(action->menu());
continue;
}
QString actionName = action->text().replace("&", "");
if (actionName.isEmpty() || !action->isEnabled())
continue;
// TODO: we might want to have a multilevel menuName
if (!menuName.isEmpty())
actionName += " (" + menuName +")";
m_actions.insert(actionName, action);
}
}