KTextEditor

katescriptaction.cpp
1/*
2 SPDX-FileCopyrightText: 2010-2018 Dominik Haumann <dhaumann@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "katescriptaction.h"
8#include "kateabstractinputmode.h"
9#include "katecmd.h"
10#include "katecmds.h"
11#include "katedocument.h"
12#include "kateglobal.h"
13#include "katepartdebug.h"
14#include "katescriptmanager.h"
15#include "kateview.h"
16#include "kateviewhelpers.h"
17
18#include <QJsonObject>
19#include <QMenu>
20
21#include <KActionCollection>
22#include <KLocalizedString>
23#include <KXMLGUIFactory>
24
25// BEGIN KateScriptAction
26KateScriptAction::KateScriptAction(const QString &cmd, const QJsonObject &action, KTextEditor::ViewPrivate *view)
27 : QAction(i18nc("Script command name", action.value(QStringLiteral("name")).toString().toUtf8().data()), view)
28 , m_view(view)
29 , m_command(cmd)
30 , m_interactive(action.value(QStringLiteral("interactive")).toBool())
31{
32 const QString icon = action.value(QStringLiteral("icon")).toString();
33 if (!icon.isEmpty()) {
34 setIcon(QIcon::fromTheme(icon));
35 }
36
37 connect(this, &KateScriptAction::triggered, this, &KateScriptAction::exec);
38}
39
40void KateScriptAction::exec()
41{
42 if (m_interactive) {
43 m_view->currentInputMode()->launchInteractiveCommand(m_command + QLatin1Char(' '));
44 } else {
45 KTextEditor::Command *p = KateCmd::self()->queryCommand(m_command);
46 if (p) {
47 QString msg;
48 p->exec(m_view, m_command, msg);
49 }
50 }
51}
52// END KateScriptAction
53
54// BEGIN KateScriptActionMenu
55KateScriptActionMenu::KateScriptActionMenu(KTextEditor::ViewPrivate *view, const QString &text)
56 : KActionMenu(QIcon::fromTheme(QStringLiteral("code-context")), text, view)
57 , m_view(view)
58{
59 repopulate();
60 setPopupMode(QToolButton::InstantPopup);
61
62 // on script-reload signal, repopulate script menu
63 connect(KTextEditor::EditorPrivate::self()->scriptManager(), &KateScriptManager::reloaded, this, &KateScriptActionMenu::repopulate);
64}
65
66KateScriptActionMenu::~KateScriptActionMenu()
67{
68 cleanup();
69}
70
71void KateScriptActionMenu::cleanup()
72{
73 // delete menus and actions for real
74 qDeleteAll(m_menus);
75 m_menus.clear();
76
77 qDeleteAll(m_actions);
78 m_actions.clear();
79}
80
81void KateScriptActionMenu::repopulate()
82{
83 // if the view is already hooked into the GUI, first remove it
84 // now and add it later, so that the changes we do here take effect
85 KXMLGUIFactory *viewFactory = m_view->factory();
86 if (viewFactory) {
87 viewFactory->removeClient(m_view);
88 }
89
90 // remove existing menu actions
91 cleanup();
92
93 // now add all command line script commands
96 for (KateCommandLineScript *script : scripts) {
97 // traverse actions
98 const auto &actions = script->commandHeader().actions();
99 for (const auto &value : actions) {
100 // action is a value
101 const auto action = value.toObject();
102
103 // get command
104 const QString cmd = action.value(QStringLiteral("function")).toString();
105
106 // show in a category submenu?
107 QMenu *m = menu();
108 QString category = action.value(QStringLiteral("category")).toString();
109 if (!category.isEmpty()) {
110 m = menus[category];
111 if (!m) {
112 m = menu()->addMenu(i18nc("Script command category", category.toUtf8().data()));
113 menus.insert(category, m);
114 m_menus.append(m);
115 m_view->actionCollection()->addAction(QLatin1String("tools_scripts_") + category, m->menuAction());
116 }
117 }
118
119 // create action + add to menu
120 QAction *a = new KateScriptAction(cmd, action, m_view);
121 m->addAction(a);
122 m_view->actionCollection()->addAction(QLatin1String("tools_scripts_") + cmd, a);
123 const QString shortcut = action.value(QStringLiteral("shortcut")).toString();
124 if (!shortcut.isEmpty()) {
126 }
127
128 m_actions.append(a);
129 }
130 }
131
132 KateCommands::EditingCommands editingCmds;
133 QMenu *m = m_view->actionCollection()->action(QStringLiteral("tools_scripts_Editing"))->menu();
134 for (const auto &cmd : editingCmds.allCommands()) {
135 auto a = new QAction(cmd.name);
136 connect(a, &QAction::triggered, this, [cmd = cmd.cmd, this] {
137 KTextEditor::Command *p = KateCmd::self()->queryCommand(cmd);
138 if (p) {
139 QString msg;
140 p->exec(m_view, cmd, msg);
141 }
142 });
143 m->addAction(a);
144 m_actions.append(a);
145 m_view->actionCollection()->addAction(QLatin1String("tools_scripts_") + cmd.cmd, a);
146 }
147
148 // finally add the view to the xml factory again, if it initially was there
149 if (viewFactory) {
150 viewFactory->addClient(m_view);
151 }
152}
153
154// END KateScriptActionMenu
Q_INVOKABLE QAction * action(const QString &name) const
QAction * addAction(const QString &name, const QObject *receiver=nullptr, const char *member=nullptr)
static void setDefaultShortcut(QAction *action, const QKeySequence &shortcut)
An Editor command line command.
virtual bool exec(KTextEditor::View *view, const QString &cmd, QString &msg, const KTextEditor::Range &range=KTextEditor::Range::invalid())=0
Execute the command for the given view and cmd string.
KateScriptManager * scriptManager()
Global script collection.
Definition kateglobal.h:264
static KTextEditor::EditorPrivate * self()
Kate Part Internal stuff ;)
virtual KActionCollection * actionCollection() const
KXMLGUIFactory * factory() const
void removeClient(KXMLGUIClient *client)
void addClient(KXMLGUIClient *client)
A specialized class for scripts that are of type ScriptType::Indentation.
KateScriptAction is an action that executes a commandline-script if triggered.
void reloaded()
this signal is emitted when all scripts are deleted and reloaded again.
const QList< KateCommandLineScript * > & commandLineScripts()
Get all scripts available in the command line.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
Category category(StandardShortcut id)
QAction(QObject *parent)
QMenu * menu() const const
void triggered(bool checked)
iterator insert(const Key &key, const T &value)
QJsonValue value(QLatin1StringView key) const const
QString toString() const const
void append(QList< T > &&value)
void clear()
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * addMenu(QMenu *menu)
QAction * menuAction() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:11:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.