Changeset View
Changeset View
Standalone View
Standalone View
addons/externaltools/externaltools.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | This file is part of the Kate text editor of the KDE project. | ||||
3 | It describes a "external tools" action for kate and provides a dialog | ||||
4 | page to configure that. | ||||
5 | | ||||
6 | This library is free software; you can redistribute it and/or | ||||
7 | modify it under the terms of the GNU Library General Public | ||||
8 | License version 2 as published by the Free Software Foundation. | ||||
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 | Copyright (C) 2004 Anders Lund <anders@alweb.dk> | ||||
22 | Copyright (C) 2019 Dominik Haumann <dhaumann@kde.org> | ||||
23 | */ | ||||
24 | | ||||
25 | #ifndef KTEXTEDITOR_EXTERNALTOOLS_H | ||||
26 | #define KTEXTEDITOR_EXTERNALTOOLS_H | ||||
27 | | ||||
28 | #include "ui_configwidget.h" | ||||
29 | #include "ui_tooldialog.h" | ||||
30 | | ||||
31 | #include <KTextEditor/Application> | ||||
32 | #include <KTextEditor/Command> | ||||
33 | #include <KTextEditor/ConfigPage> | ||||
34 | #include <KTextEditor/MainWindow> | ||||
35 | #include <KTextEditor/Plugin> | ||||
36 | | ||||
37 | #include <KActionMenu> | ||||
38 | #include <KMacroExpander> | ||||
39 | #include <QDialog> | ||||
40 | | ||||
41 | #include <QHash> | ||||
42 | #include <QPixmap> | ||||
43 | #include <QProcess> | ||||
44 | | ||||
45 | class KateExternalToolsPlugin; | ||||
46 | class KateExternalTool; | ||||
47 | | ||||
48 | /** | ||||
49 | * The external tools action | ||||
50 | * This action creates a menu, in which each item will launch a process | ||||
51 | * with the provided arguments, which may include the following macros: | ||||
52 | * - %URLS: the URLs of all open documents. | ||||
53 | * - %URL: The URL of the active document. | ||||
54 | * - %filedir: The directory of the current document, if that is a local file. | ||||
55 | * - %selection: The selection of the active document. | ||||
56 | * - %text: The text of the active document. | ||||
57 | * - %line: The line number of the cursor in the active view. | ||||
58 | * - %column: The column of the cursor in the active view. | ||||
59 | * | ||||
60 | * Each item has the following properties: | ||||
61 | * - Name: The friendly text for the menu | ||||
62 | * - Exec: The command to execute, including arguments. | ||||
63 | * - TryExec: the name of the executable, if not available, the | ||||
64 | * item will not be displayed. | ||||
65 | * - MimeTypes: An optional list of mimetypes. The item will be disabled or | ||||
66 | * hidden if the current file is not of one of the indicated types. | ||||
67 | * | ||||
68 | */ | ||||
69 | class KateExternalToolsMenuAction : public KActionMenu | ||||
70 | { | ||||
71 | friend class KateExternalToolAction; | ||||
72 | | ||||
73 | Q_OBJECT | ||||
74 | public: | ||||
75 | KateExternalToolsMenuAction(const QString& text, class KActionCollection* collection, QObject* parent, | ||||
76 | class KTextEditor::MainWindow* mw = nullptr); | ||||
77 | virtual ~KateExternalToolsMenuAction(); | ||||
78 | | ||||
79 | /** | ||||
80 | * This will load all the configured services. | ||||
81 | */ | ||||
82 | void reload(); | ||||
83 | | ||||
84 | class KActionCollection* actionCollection() { return m_actionCollection; } | ||||
85 | | ||||
86 | private Q_SLOTS: | ||||
87 | void slotViewChanged(KTextEditor::View* view); | ||||
88 | | ||||
89 | private: | ||||
90 | class KActionCollection* m_actionCollection; | ||||
91 | KTextEditor::MainWindow* mainwindow; // for the actions to access view/doc managers | ||||
92 | }; | ||||
93 | | ||||
94 | /** | ||||
95 | * This Action contains a KateExternalTool | ||||
96 | */ | ||||
97 | class KateExternalToolAction : public QAction | ||||
98 | { | ||||
99 | Q_OBJECT | ||||
100 | public: | ||||
101 | KateExternalToolAction(QObject* parent, class KateExternalTool* t); | ||||
102 | ~KateExternalToolAction(); | ||||
103 | | ||||
104 | private Q_SLOTS: | ||||
105 | void slotRun(); | ||||
106 | | ||||
107 | public: | ||||
108 | class KateExternalTool* tool; | ||||
109 | }; | ||||
110 | | ||||
111 | /** | ||||
112 | * The config widget. | ||||
113 | * The config widget allows the user to view a list of services of the type | ||||
114 | * "Kate/ExternalTool" and add, remove or edit them. | ||||
115 | */ | ||||
116 | class KateExternalToolsConfigWidget : public KTextEditor::ConfigPage, public Ui::ExternalToolsConfigWidget | ||||
117 | { | ||||
118 | Q_OBJECT | ||||
119 | public: | ||||
120 | KateExternalToolsConfigWidget(QWidget* parent, KateExternalToolsPlugin* plugin); | ||||
121 | virtual ~KateExternalToolsConfigWidget(); | ||||
122 | | ||||
123 | QString name() const override; | ||||
124 | QString fullName() const override; | ||||
125 | QIcon icon() const override; | ||||
126 | | ||||
127 | public Q_SLOTS: | ||||
128 | void apply() override; | ||||
129 | void reset() override; | ||||
130 | void defaults() override { reset(); } // double sigh | ||||
131 | | ||||
132 | private Q_SLOTS: | ||||
133 | void slotNew(); | ||||
134 | void slotEdit(); | ||||
135 | void slotRemove(); | ||||
136 | void slotInsertSeparator(); | ||||
137 | | ||||
138 | void slotMoveUp(); | ||||
139 | void slotMoveDown(); | ||||
140 | | ||||
141 | void slotSelectionChanged(); | ||||
142 | | ||||
143 | private: | ||||
144 | QPixmap blankIcon(); | ||||
145 | | ||||
146 | QStringList m_removed; | ||||
147 | | ||||
148 | class KConfig* m_config = nullptr; | ||||
149 | bool m_changed = false; | ||||
150 | KateExternalToolsPlugin* m_plugin; | ||||
151 | }; | ||||
152 | | ||||
153 | /** | ||||
154 | * A Singleton class for invoking external tools with the view command line | ||||
155 | */ | ||||
156 | class KateExternalToolsCommand : public KTextEditor::Command | ||||
157 | { | ||||
158 | public: | ||||
159 | KateExternalToolsCommand(KateExternalToolsPlugin* plugin); | ||||
160 | virtual ~KateExternalToolsCommand() {} | ||||
161 | void reload(); | ||||
162 | | ||||
163 | public: | ||||
164 | // const QStringList& cmds() override; // FIXME | ||||
165 | bool exec(KTextEditor::View* view, const QString& cmd, QString& msg, | ||||
166 | const KTextEditor::Range& range = KTextEditor::Range::invalid()) override; | ||||
167 | bool help(KTextEditor::View* view, const QString& cmd, QString& msg) override; | ||||
168 | | ||||
169 | private: | ||||
170 | QStringList m_list; | ||||
171 | QHash<QString, QString> m_map; | ||||
172 | QHash<QString, QString> m_name; | ||||
173 | KateExternalToolsPlugin* m_plugin; | ||||
174 | }; | ||||
175 | | ||||
176 | /** | ||||
177 | * A Dialog to edit a single KateExternalTool object | ||||
178 | */ | ||||
179 | class KateExternalToolServiceEditor : public QDialog | ||||
180 | { | ||||
181 | Q_OBJECT | ||||
182 | | ||||
183 | public: | ||||
184 | explicit KateExternalToolServiceEditor(KateExternalTool* tool = nullptr, QWidget* parent = nullptr); | ||||
185 | | ||||
186 | private Q_SLOTS: | ||||
187 | /** | ||||
188 | * Run when the OK button is clicked, to ensure critical values are provided. | ||||
189 | */ | ||||
190 | void slotOKClicked(); | ||||
191 | | ||||
192 | /** | ||||
193 | * show a mimetype chooser dialog | ||||
194 | */ | ||||
195 | void showMTDlg(); | ||||
196 | | ||||
197 | public: | ||||
198 | Ui::ToolDialog * ui; | ||||
199 | private: | ||||
200 | KateExternalTool* tool; | ||||
201 | }; | ||||
202 | | ||||
203 | /** | ||||
204 | * Helper class to run a KateExternalTool. | ||||
205 | */ | ||||
206 | class ExternalToolRunner | ||||
207 | { | ||||
208 | public: | ||||
209 | ExternalToolRunner(KateExternalTool * tool); | ||||
210 | ExternalToolRunner(const ExternalToolRunner &) = delete; | ||||
211 | void operator=(const ExternalToolRunner &) = delete; | ||||
212 | | ||||
213 | ~ExternalToolRunner(); | ||||
214 | | ||||
215 | void run(); | ||||
216 | | ||||
217 | private: | ||||
218 | KateExternalTool * m_tool; | ||||
219 | QProcess * m_process = nullptr; | ||||
220 | }; | ||||
221 | | ||||
222 | #endif // KTEXTEDITOR_EXTERNALTOOLS_H | ||||
223 | | ||||
224 | // kate: space-indent on; indent-width 4; replace-tabs on; |