actionmacrohandler.cpp 3.87 KB
Newer Older
1
2
/**************************************************************************
**
3
** Copyright (c) 2014 Nicolas Arnaud-Cormos
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
Eike Ziller's avatar
Eike Ziller committed
13
14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
25
26
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Tobias Hunger's avatar
Tobias Hunger committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30
31
32
33
34
35
36
37
38

#include "actionmacrohandler.h"
#include "macroevent.h"
#include "macro.h"

#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icontext.h>
39
40
#include <coreplugin/icore.h>
#include <coreplugin/id.h>
41

42
43
44
#include <texteditor/texteditorconstants.h>

#include <QAction>
45
46
#include <QEvent>
#include <QSignalMapper>
Friedemann Kleint's avatar
Friedemann Kleint committed
47

48
using namespace Core;
49

50
51
namespace Macros {
namespace Internal {
52
53
54
55
56
57
58

static const char EVENTNAME[] = "Action";
static quint8 ACTIONNAME = 0;

ActionMacroHandler::ActionMacroHandler():
    m_mapper(new QSignalMapper(this))
{
Robert Loehning's avatar
Robert Loehning committed
59
60
    connect(m_mapper, SIGNAL(mapped(QString)),
            this, SLOT(addActionEvent(QString)));
61

62
63
    connect(ActionManager::instance(), &ActionManager::commandAdded,
            this, &ActionMacroHandler::addCommand);
64
65

    // Register all existing scriptable actions
66
67
68
69
    QList<Command *> commands = ActionManager::commands();
    foreach (Command *command, commands) {
        if (command->isScriptable())
            registerCommand(command->id());
70
71
72
73
74
    }
}

bool ActionMacroHandler::canExecuteEvent(const MacroEvent &macroEvent)
{
75
    return macroEvent.id() == EVENTNAME;
76
77
78
79
}

bool ActionMacroHandler::executeEvent(const MacroEvent &macroEvent)
{
80
    QAction *action = ActionManager::command(Id::fromSetting(macroEvent.value(ACTIONNAME)))->action();
81
82
83
84
85
86
87
    if (!action)
        return false;

    action->trigger();
    return true;
}

88
void ActionMacroHandler::addActionEvent(const QString &name)
89
90
91
92
{
    if (!isRecording())
        return;

93
94
95
    const Id id = Id::fromString(name);
    const Command *command = ActionManager::command(id);
    if (command->isScriptable(command->context())) {
96
97
        MacroEvent e;
        e.setId(EVENTNAME);
98
        e.setValue(ACTIONNAME, id.toSetting());
99
100
101
102
        addMacroEvent(e);
    }
}

103
void ActionMacroHandler::registerCommand(Id id)
104
105
106
{
    if (!m_commandIds.contains(id)) {
        m_commandIds.insert(id);
107
108
        const Command *command = ActionManager::command(id);
        if (QAction *action = command->action()) {
109
            connect(action, SIGNAL(triggered()), m_mapper, SLOT(map()));
110
            m_mapper->setMapping(action, id.toString());
111
112
113
114
115
            return;
        }
    }
}

116
void ActionMacroHandler::addCommand(Id id)
117
{
118
119
    const Command *command = ActionManager::command(id);
    if (command->isScriptable())
120
121
        registerCommand(id);
}
122
123
124

} // namespace Internal
} // namespace Macros