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 | */ | ||||
23 | | ||||
24 | #ifndef KTEXTEDITOR_EXTERNALTOOLS_H | ||||
25 | #define KTEXTEDITOR_EXTERNALTOOLS_H | ||||
26 | | ||||
27 | #include "ui_configwidget.h" | ||||
28 | | ||||
29 | #include <KTextEditor/Application> | ||||
30 | #include <KTextEditor/Command> | ||||
31 | #include <KTextEditor/ConfigPage> | ||||
32 | #include <KTextEditor/MainWindow> | ||||
33 | #include <KTextEditor/Plugin> | ||||
34 | | ||||
35 | #include <KActionMenu> | ||||
36 | #include <KMacroExpander> | ||||
37 | #include <QDialog> | ||||
38 | | ||||
39 | #include <QHash> | ||||
40 | #include <QPixmap> | ||||
41 | | ||||
42 | class KateExternalToolsPlugin; | ||||
43 | | ||||
44 | /** | ||||
45 | * The external tools action | ||||
46 | * This action creates a menu, in which each item will launch a process | ||||
47 | * with the provided arguments, which may include the following macros: | ||||
48 | * - %URLS: the URLs of all open documents. | ||||
49 | * - %URL: The URL of the active document. | ||||
50 | * - %filedir: The directory of the current document, if that is a local file. | ||||
51 | * - %selection: The selection of the active document. | ||||
52 | * - %text: The text of the active document. | ||||
53 | * - %line: The line number of the cursor in the active view. | ||||
54 | * - %column: The column of the cursor in the active view. | ||||
55 | * | ||||
56 | * Each item has the following properties: | ||||
57 | * - Name: The friendly text for the menu | ||||
58 | * - Exec: The command to execute, including arguments. | ||||
59 | * - TryExec: the name of the executable, if not available, the | ||||
60 | * item will not be displayed. | ||||
61 | * - MimeTypes: An optional list of mimetypes. The item will be disabled or | ||||
62 | * hidden if the current file is not of one of the indicated types. | ||||
63 | * | ||||
64 | */ | ||||
65 | class KateExternalToolsMenuAction : public KActionMenu | ||||
66 | { | ||||
67 | friend class KateExternalToolAction; | ||||
68 | | ||||
69 | Q_OBJECT | ||||
70 | public: | ||||
71 | KateExternalToolsMenuAction(const QString& text, class KActionCollection* collection, QObject* parent, | ||||
72 | class KTextEditor::MainWindow* mw = nullptr); | ||||
73 | virtual ~KateExternalToolsMenuAction(); | ||||
74 | | ||||
75 | /** | ||||
76 | * This will load all the confiured services. | ||||
77 | */ | ||||
78 | void reload(); | ||||
79 | | ||||
80 | class KActionCollection* actionCollection() { return m_actionCollection; } | ||||
81 | | ||||
82 | private Q_SLOTS: | ||||
83 | void slotViewChanged(KTextEditor::View* view); | ||||
84 | | ||||
85 | private: | ||||
86 | class KActionCollection* m_actionCollection; | ||||
87 | KTextEditor::MainWindow* mainwindow; // for the actions to access view/doc managers | ||||
88 | }; | ||||
89 | | ||||
90 | /** | ||||
91 | * This Action contains a KateExternalTool | ||||
92 | */ | ||||
93 | class KateExternalToolAction : public QAction, public KWordMacroExpander | ||||
94 | { | ||||
95 | Q_OBJECT | ||||
96 | public: | ||||
97 | KateExternalToolAction(QObject* parent, class KateExternalTool* t); | ||||
98 | ~KateExternalToolAction(); | ||||
99 | | ||||
100 | protected: | ||||
101 | bool expandMacro(const QString& str, QStringList& ret) override; | ||||
102 | | ||||
103 | private Q_SLOTS: | ||||
104 | void slotRun(); | ||||
105 | | ||||
106 | public: | ||||
107 | class KateExternalTool* tool; | ||||
108 | }; | ||||
109 | | ||||
110 | /** | ||||
111 | * This class defines a single external tool. | ||||
112 | */ | ||||
113 | class KateExternalTool | ||||
114 | { | ||||
115 | public: | ||||
116 | explicit KateExternalTool(const QString& name = QString(), const QString& command = QString(), | ||||
117 | const QString& icon = QString(), const QString& tryexec = QString(), | ||||
118 | const QStringList& mimetypes = QStringList(), const QString& acname = QString(), | ||||
119 | const QString& cmdname = QString(), int save = 0); | ||||
120 | ~KateExternalTool() {} | ||||
121 | | ||||
122 | QString name; ///< The name used in the menu. | ||||
123 | QString command; ///< The command to execute. | ||||
124 | QString icon; ///< the icon to use in the menu. | ||||
125 | QString tryexec; ///< The name or path of the executable. | ||||
126 | QStringList mimetypes; ///< Optional list of mimetypes for which this action is valid. | ||||
127 | bool hasexec; ///< This is set by the constructor by calling checkExec(), if a | ||||
128 | ///< value is present. | ||||
129 | QString acname; ///< The name for the action. This is generated first time the | ||||
130 | ///< action is is created. | ||||
131 | QString cmdname; ///< The name for the commandline. | ||||
132 | int save; ///< We can save documents prior to activating the tool command: 0 = | ||||
133 | ///< nothing, 1 = current document, 2 = all documents. | ||||
134 | | ||||
135 | /** | ||||
136 | * @return true if mimetypes is empty, or the @p mimetype matches. | ||||
137 | */ | ||||
138 | bool valid(const QString& mimetype) const; | ||||
139 | /** | ||||
140 | * @return true if "tryexec" exists and has the executable bit set, or is | ||||
141 | * empty. | ||||
142 | * This is run at least once, and the tool is disabled if it fails. | ||||
143 | */ | ||||
144 | bool checkExec(); | ||||
145 | | ||||
146 | private: | ||||
147 | QString m_exec; ///< The fully qualified path of the executable. | ||||
148 | }; | ||||
149 | | ||||
150 | /** | ||||
151 | * The config widget. | ||||
152 | * The config widget allows the user to view a list of services of the type | ||||
153 | * "Kate/ExternalTool" and add, remove or edit them. | ||||
154 | */ | ||||
155 | class KateExternalToolsConfigWidget : public KTextEditor::ConfigPage, public Ui::ExternalToolsConfigWidget | ||||
156 | { | ||||
157 | Q_OBJECT | ||||
158 | public: | ||||
159 | KateExternalToolsConfigWidget(QWidget* parent, KateExternalToolsPlugin* plugin); | ||||
160 | virtual ~KateExternalToolsConfigWidget(); | ||||
161 | | ||||
162 | QString name() const override; | ||||
163 | QString fullName() const override; | ||||
164 | QIcon icon() const override; | ||||
165 | | ||||
166 | public Q_SLOTS: | ||||
167 | void apply() override; | ||||
168 | void reset() override; | ||||
169 | void defaults() override { reset(); } // double sigh | ||||
170 | | ||||
171 | private Q_SLOTS: | ||||
172 | void slotNew(); | ||||
173 | void slotEdit(); | ||||
174 | void slotRemove(); | ||||
175 | void slotInsertSeparator(); | ||||
176 | | ||||
177 | void slotMoveUp(); | ||||
178 | void slotMoveDown(); | ||||
179 | | ||||
180 | void slotSelectionChanged(); | ||||
181 | | ||||
182 | private: | ||||
183 | QPixmap blankIcon(); | ||||
184 | | ||||
185 | QStringList m_removed; | ||||
186 | | ||||
187 | class KConfig* config; | ||||
188 | | ||||
189 | bool m_changed; | ||||
190 | KateExternalToolsPlugin* m_plugin; | ||||
191 | }; | ||||
192 | | ||||
193 | /** | ||||
194 | * A Singleton class for invoking external tools with the view command line | ||||
195 | */ | ||||
196 | class KateExternalToolsCommand : public KTextEditor::Command | ||||
197 | { | ||||
198 | public: | ||||
199 | KateExternalToolsCommand(KateExternalToolsPlugin* plugin); | ||||
200 | virtual ~KateExternalToolsCommand() {} | ||||
201 | void reload(); | ||||
202 | | ||||
203 | public: | ||||
204 | // const QStringList& cmds() override; // FIXME | ||||
205 | bool exec(KTextEditor::View* view, const QString& cmd, QString& msg, | ||||
206 | const KTextEditor::Range& range = KTextEditor::Range::invalid()) override; | ||||
207 | bool help(KTextEditor::View* view, const QString& cmd, QString& msg) override; | ||||
208 | | ||||
209 | private: | ||||
210 | QStringList m_list; | ||||
211 | QHash<QString, QString> m_map; | ||||
212 | QHash<QString, QString> m_name; | ||||
213 | KateExternalToolsPlugin* m_plugin; | ||||
214 | }; | ||||
215 | | ||||
216 | /** | ||||
217 | * A Dialog to edit a single KateExternalTool object | ||||
218 | */ | ||||
219 | class KateExternalToolServiceEditor : public QDialog | ||||
220 | { | ||||
221 | Q_OBJECT | ||||
222 | | ||||
223 | public: | ||||
224 | explicit KateExternalToolServiceEditor(KateExternalTool* tool = nullptr, QWidget* parent = nullptr); | ||||
225 | | ||||
226 | class QLineEdit *leName, *leExecutable, *leMimetypes, *leCmdLine; | ||||
227 | class QTextEdit* teCommand; | ||||
228 | class KIconButton* btnIcon; | ||||
229 | class QComboBox* cmbSave; | ||||
230 | | ||||
231 | private Q_SLOTS: | ||||
232 | /** | ||||
233 | * Run when the OK button is clicked, to ensure critical values are provided. | ||||
234 | */ | ||||
235 | void slotOKClicked(); | ||||
236 | /** | ||||
237 | * show a mimetype chooser dialog | ||||
238 | */ | ||||
239 | void showMTDlg(); | ||||
240 | | ||||
241 | private: | ||||
242 | KateExternalTool* tool; | ||||
243 | }; | ||||
244 | | ||||
245 | #endif // KTEXTEDITOR_EXTERNALTOOLS_H | ||||
246 | | ||||
247 | // kate: space-indent on; indent-width 4; replace-tabs on; |