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

KDE's Doxygen guidelines are available online.