KTextEditor

katescriptaction.cpp
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2010-2018 Dominik Haumann <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "katescriptaction.h"
22 #include "kateabstractinputmode.h"
23 #include "katecmd.h"
24 #include "katecommandlinescript.h"
25 #include "katedocument.h"
26 #include "kateglobal.h"
27 #include "katepartdebug.h"
28 #include "katescriptmanager.h"
29 #include "kateview.h"
30 #include "kateviewhelpers.h"
31 
32 #include <QJsonObject>
33 
34 #include <KActionCollection>
35 #include <KLocalizedString>
36 #include <KXMLGUIFactory>
37 
38 // BEGIN KateScriptAction
39 KateScriptAction::KateScriptAction(const QString &cmd, const QJsonObject &action, KTextEditor::ViewPrivate *view)
40  : QAction(i18nc("Script command name", action.value(QStringLiteral("name")).toString().toUtf8().data()), view)
41  , m_view(view)
42  , m_command(cmd)
43  , m_interactive(action.value(QStringLiteral("interactive")).toBool())
44 {
45  const QString icon = action.value(QStringLiteral("icon")).toString();
46  if (!icon.isEmpty()) {
47  setIcon(QIcon::fromTheme(icon));
48  }
49 
50  connect(this, SIGNAL(triggered(bool)), this, SLOT(exec()));
51 }
52 
53 KateScriptAction::~KateScriptAction()
54 {
55 }
56 
57 void KateScriptAction::exec()
58 {
59  if (m_interactive) {
60  m_view->currentInputMode()->launchInteractiveCommand(m_command + QLatin1Char(' '));
61  } else {
62  KTextEditor::Command *p = KateCmd::self()->queryCommand(m_command);
63  if (p) {
64  QString msg;
65  p->exec(m_view, m_command, msg);
66  }
67  }
68 }
69 // END KateScriptAction
70 
71 // BEGIN KateScriptActionMenu
72 KateScriptActionMenu::KateScriptActionMenu(KTextEditor::ViewPrivate *view, const QString &text)
73  : KActionMenu(QIcon::fromTheme(QStringLiteral("code-context")), text, view)
74  , m_view(view)
75 {
76  repopulate();
77  setDelayed(false);
78 
79  // on script-reload signal, repopulate script menu
80  connect(KTextEditor::EditorPrivate::self()->scriptManager(), SIGNAL(reloaded()), this, SLOT(repopulate()));
81 }
82 
83 KateScriptActionMenu::~KateScriptActionMenu()
84 {
85  cleanup();
86 }
87 
88 void KateScriptActionMenu::cleanup()
89 {
90  // delete menus and actions for real
91  qDeleteAll(m_menus);
92  m_menus.clear();
93 
94  qDeleteAll(m_actions);
95  m_actions.clear();
96 }
97 
99 {
100  // if the view is already hooked into the GUI, first remove it
101  // now and add it later, so that the changes we do here take effect
102  KXMLGUIFactory *viewFactory = m_view->factory();
103  if (viewFactory) {
104  viewFactory->removeClient(m_view);
105  }
106 
107  // remove existing menu actions
108  cleanup();
109 
110  // now add all command line script commands
113  for (KateCommandLineScript *script : scripts) {
117  const auto &actions = script->commandHeader().actions();
118  for (const auto &value : actions) {
122  const auto action = value.toObject();
123 
127  const QString cmd = action.value(QStringLiteral("function")).toString();
128 
129  // show in a category submenu?
130  QMenu *m = menu();
131  QString category = action.value(QStringLiteral("category")).toString();
132  if (!category.isEmpty()) {
133  category = i18nc("Script command category", category.toUtf8().data());
134  m = menus[category];
135  if (!m) {
136  m = menu()->addMenu(category);
137  menus.insert(category, m);
138  m_menus.append(m);
139  }
140  }
141 
142  // create action + add to menu
143  QAction *a = new KateScriptAction(cmd, action, m_view);
144  m->addAction(a);
145  m_view->actionCollection()->addAction(QLatin1String("tools_scripts_") + cmd, a);
146  const QString shortcut = action.value(QStringLiteral("shortcut")).toString();
147  if (!shortcut.isEmpty()) {
148  m_view->actionCollection()->setDefaultShortcut(a, shortcut);
149  }
150 
151  m_actions.append(a);
152  }
153  }
154 
155  // finally add the view to the xml factory again, if it initially was there
156  if (viewFactory) {
157  viewFactory->addClient(m_view);
158  }
159 }
160 
161 // END KateScriptActionMenu
QHash::iterator insert(const Key &key, const T &value)
void removeClient(KXMLGUIClient *client)
void addClient(KXMLGUIClient *client)
QAction * addAction(const QString &text)
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.
char * toString(const T &value)
KateScriptManager * scriptManager()
Global script collection.
Definition: kateglobal.h:301
char * data()
QJsonValue value(const QString &key) const const
QIcon fromTheme(const QString &name)
static KTextEditor::EditorPrivate * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:373
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-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:00:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.