Files
qt-creator/src/plugins/macros/actionmacrohandler.cpp
Lucie Gérard a7956df3ca Use SPDX license identifiers
Replace the current license disclaimer in files by
a SPDX-License-Identifier.

Task-number: QTBUG-67283
Change-Id: I708fd1f9f2b73d60f57cc3568646929117825813
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
2022-08-26 12:27:18 +00:00

86 lines
2.3 KiB
C++

// Copyright (C) 2016 Nicolas Arnaud-Cormos
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#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>
#include <coreplugin/icore.h>
#include <texteditor/texteditorconstants.h>
#include <QAction>
#include <QEvent>
using namespace Core;
using namespace Utils;
namespace Macros {
namespace Internal {
static const char EVENTNAME[] = "Action";
static quint8 ACTIONNAME = 0;
ActionMacroHandler::ActionMacroHandler()
{
connect(ActionManager::instance(), &ActionManager::commandAdded,
this, &ActionMacroHandler::addCommand);
// Register all existing scriptable actions
const QList<Command *> commands = ActionManager::commands();
for (Command *command : commands) {
if (command->isScriptable())
registerCommand(command->id());
}
}
bool ActionMacroHandler::canExecuteEvent(const MacroEvent &macroEvent)
{
return macroEvent.id() == EVENTNAME;
}
bool ActionMacroHandler::executeEvent(const MacroEvent &macroEvent)
{
QAction *action = ActionManager::command(Id::fromSetting(macroEvent.value(ACTIONNAME)))->action();
if (!action)
return false;
action->trigger();
return true;
}
void ActionMacroHandler::registerCommand(Id id)
{
if (!m_commandIds.contains(id)) {
m_commandIds.insert(id);
const Command *command = ActionManager::command(id);
if (QAction *action = command->action()) {
connect(action, &QAction::triggered, this, [this, id, command]() {
if (!isRecording())
return;
if (command->isScriptable(command->context())) {
MacroEvent e;
e.setId(EVENTNAME);
e.setValue(ACTIONNAME, id.toSetting());
addMacroEvent(e);
}
});
}
}
}
void ActionMacroHandler::addCommand(Id id)
{
const Command *command = ActionManager::command(id);
if (command->isScriptable())
registerCommand(id);
}
} // namespace Internal
} // namespace Macros