2022-08-19 15:59:36 +02:00
|
|
|
// 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
|
2010-12-20 10:35:30 +01:00
|
|
|
|
|
|
|
|
#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>
|
2013-01-15 13:52:34 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2013-01-15 13:52:34 +01:00
|
|
|
#include <texteditor/texteditorconstants.h>
|
|
|
|
|
|
|
|
|
|
#include <QAction>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QEvent>
|
2011-01-20 14:03:07 +01:00
|
|
|
|
2013-01-15 13:52:34 +01:00
|
|
|
using namespace Core;
|
2020-06-26 13:59:38 +02:00
|
|
|
using namespace Utils;
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2013-01-15 13:52:34 +01:00
|
|
|
namespace Macros {
|
|
|
|
|
namespace Internal {
|
2010-12-20 10:35:30 +01:00
|
|
|
|
|
|
|
|
static const char EVENTNAME[] = "Action";
|
|
|
|
|
static quint8 ACTIONNAME = 0;
|
|
|
|
|
|
2016-05-29 00:20:16 +03:00
|
|
|
ActionMacroHandler::ActionMacroHandler()
|
2010-12-20 10:35:30 +01:00
|
|
|
{
|
2014-11-05 08:58:24 +01:00
|
|
|
connect(ActionManager::instance(), &ActionManager::commandAdded,
|
|
|
|
|
this, &ActionMacroHandler::addCommand);
|
2010-12-20 10:35:30 +01:00
|
|
|
|
|
|
|
|
// Register all existing scriptable actions
|
2022-06-02 14:23:03 +02:00
|
|
|
const QList<Command *> commands = ActionManager::commands();
|
|
|
|
|
for (Command *command : commands) {
|
2013-01-15 13:52:34 +01:00
|
|
|
if (command->isScriptable())
|
|
|
|
|
registerCommand(command->id());
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ActionMacroHandler::canExecuteEvent(const MacroEvent ¯oEvent)
|
|
|
|
|
{
|
2013-01-15 13:52:34 +01:00
|
|
|
return macroEvent.id() == EVENTNAME;
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ActionMacroHandler::executeEvent(const MacroEvent ¯oEvent)
|
|
|
|
|
{
|
2013-01-15 13:52:34 +01:00
|
|
|
QAction *action = ActionManager::command(Id::fromSetting(macroEvent.value(ACTIONNAME)))->action();
|
2010-12-20 10:35:30 +01:00
|
|
|
if (!action)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
action->trigger();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 13:52:34 +01:00
|
|
|
void ActionMacroHandler::registerCommand(Id id)
|
2010-12-20 10:35:30 +01:00
|
|
|
{
|
|
|
|
|
if (!m_commandIds.contains(id)) {
|
|
|
|
|
m_commandIds.insert(id);
|
2013-01-15 13:52:34 +01:00
|
|
|
const Command *command = ActionManager::command(id);
|
|
|
|
|
if (QAction *action = command->action()) {
|
2016-05-29 00:20:16 +03:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-05 08:58:24 +01:00
|
|
|
void ActionMacroHandler::addCommand(Id id)
|
2010-12-20 10:35:30 +01:00
|
|
|
{
|
2013-01-15 13:52:34 +01:00
|
|
|
const Command *command = ActionManager::command(id);
|
|
|
|
|
if (command->isScriptable())
|
2010-12-20 10:35:30 +01:00
|
|
|
registerCommand(id);
|
|
|
|
|
}
|
2013-01-15 13:52:34 +01:00
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Macros
|