KTextEditor

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

KDE's Doxygen guidelines are available online.