Subversion Repositories Scribus

Rev

Rev 23163 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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