Changeset View
Changeset View
Standalone View
Standalone View
addons/externaltools/kateexternaltoolsview.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright 2019 Dominik Haumann <dhaumann@kde.org> | ||||
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 | #include "kateexternaltoolsview.h" | ||||
21 | #include "externaltoolsplugin.h" | ||||
22 | #include "kateexternaltool.h" | ||||
23 | #include "ui_toolview.h" | ||||
24 | | ||||
25 | #include <KTextEditor/MainWindow> | ||||
26 | #include <KTextEditor/Document> | ||||
27 | #include <KTextEditor/View> | ||||
28 | | ||||
29 | #include <KActionCollection> | ||||
30 | #include <KAuthorized> | ||||
31 | #include <KConfig> | ||||
32 | #include <KConfigGroup> | ||||
33 | #include <KSharedConfig> | ||||
34 | #include <QStandardPaths> | ||||
35 | #include <QMenu> | ||||
36 | #include <KXMLGUIFactory> | ||||
37 | #include <KLocalizedString> | ||||
38 | | ||||
39 | #include <QToolButton> | ||||
40 | #include <QKeyEvent> | ||||
41 | #include <QFontDatabase> | ||||
42 | #include <QTextDocument> | ||||
43 | | ||||
44 | #include <map> | ||||
45 | #include <vector> | ||||
46 | | ||||
47 | | ||||
48 | // BEGIN KateExternalToolsMenuAction | ||||
49 | KateExternalToolsMenuAction::KateExternalToolsMenuAction(const QString& text, KActionCollection* collection, | ||||
50 | KateExternalToolsPlugin* plugin, KTextEditor::MainWindow* mw) | ||||
51 | : KActionMenu(text, mw) | ||||
52 | , m_plugin(plugin) | ||||
53 | , m_mainwindow(mw) | ||||
54 | , m_actionCollection(collection) | ||||
55 | { | ||||
56 | reload(); | ||||
57 | | ||||
58 | // track active view to adapt enabled tool actions | ||||
59 | connect(mw, &KTextEditor::MainWindow::viewChanged, this, &KateExternalToolsMenuAction::slotViewChanged); | ||||
60 | } | ||||
61 | | ||||
62 | KateExternalToolsMenuAction::~KateExternalToolsMenuAction() = default; | ||||
63 | | ||||
64 | void KateExternalToolsMenuAction::reload() | ||||
65 | { | ||||
66 | // clear action collection | ||||
67 | bool needs_readd = (m_actionCollection->takeAction(this) != nullptr); | ||||
68 | m_actionCollection->clear(); | ||||
69 | if (needs_readd) | ||||
70 | m_actionCollection->addAction(QStringLiteral("tools_external"), this); | ||||
71 | menu()->clear(); | ||||
72 | | ||||
73 | // create tool actions | ||||
74 | std::map<QString, KActionMenu*> categories; | ||||
75 | std::vector<QAction*> uncategorizedActions; | ||||
76 | | ||||
77 | // first add categorized actions, such that the submenus appear at the top | ||||
78 | for (auto tool : m_plugin->tools()) { | ||||
79 | if (tool->hasexec) { | ||||
80 | auto a = new QAction(tool->name, this); | ||||
81 | a->setIcon(QIcon::fromTheme(tool->icon)); | ||||
82 | a->setData(QVariant::fromValue(tool)); | ||||
83 | | ||||
84 | connect(a, &QAction::triggered, [this, a]() { | ||||
85 | m_plugin->runTool(*a->data().value<KateExternalTool*>(), m_mainwindow->activeView()); | ||||
86 | }); | ||||
87 | | ||||
88 | m_actionCollection->addAction(tool->actionName, a); | ||||
89 | if (!tool->category.isEmpty()) { | ||||
90 | auto categoryMenu = categories[tool->category]; | ||||
91 | if (!categoryMenu) { | ||||
92 | categoryMenu = new KActionMenu(tool->category, this); | ||||
93 | categories[tool->category] = categoryMenu; | ||||
94 | addAction(categoryMenu); | ||||
95 | } | ||||
96 | categoryMenu->addAction(a); | ||||
97 | } else { | ||||
98 | uncategorizedActions.push_back(a); | ||||
99 | } | ||||
100 | } | ||||
101 | } | ||||
102 | | ||||
103 | // now add uncategorized actions below | ||||
104 | for (auto uncategorizedAction : uncategorizedActions) { | ||||
105 | addAction(uncategorizedAction); | ||||
106 | } | ||||
107 | | ||||
108 | // load shortcuts | ||||
109 | KSharedConfig::Ptr pConfig = KSharedConfig::openConfig(QStringLiteral("externaltools"), KConfig::NoGlobals, | ||||
110 | QStandardPaths::ApplicationsLocation); | ||||
111 | KConfigGroup config(pConfig, "Global"); | ||||
112 | config = KConfigGroup(pConfig, "Shortcuts"); | ||||
113 | m_actionCollection->readSettings(&config); | ||||
114 | slotViewChanged(m_mainwindow->activeView()); | ||||
115 | } | ||||
116 | | ||||
117 | void KateExternalToolsMenuAction::slotViewChanged(KTextEditor::View* view) | ||||
118 | { | ||||
119 | // no active view, oh oh | ||||
120 | if (!view) { | ||||
121 | return; | ||||
122 | } | ||||
123 | | ||||
124 | // try to enable/disable to match current mime type | ||||
125 | const QString mimeType = view->document()->mimeType(); | ||||
126 | foreach (QAction* action, m_actionCollection->actions()) { | ||||
127 | if (action && action->data().value<KateExternalTool*>()) { | ||||
128 | auto tool = action->data().value<KateExternalTool*>(); | ||||
129 | action->setEnabled(tool->matchesMimetype(mimeType)); | ||||
130 | } | ||||
131 | } | ||||
132 | } | ||||
133 | // END KateExternalToolsMenuAction | ||||
134 | | ||||
135 | | ||||
136 | | ||||
137 | | ||||
138 | | ||||
139 | // BEGIN KateExternalToolsPluginView | ||||
140 | KateExternalToolsPluginView::KateExternalToolsPluginView(KTextEditor::MainWindow* mainWindow, | ||||
141 | KateExternalToolsPlugin* plugin) | ||||
142 | : QObject(mainWindow) | ||||
143 | , m_plugin(plugin) | ||||
144 | , m_mainWindow(mainWindow) | ||||
145 | , m_outputDoc(new QTextDocument(this)) | ||||
146 | , m_statusDoc(new QTextDocument(this)) | ||||
147 | { | ||||
148 | m_plugin->registerPluginView(this); | ||||
149 | | ||||
150 | KXMLGUIClient::setComponentName(QLatin1String("externaltools"), i18n("External Tools")); | ||||
151 | setXMLFile(QLatin1String("ui.rc")); | ||||
152 | | ||||
153 | if (KAuthorized::authorizeAction(QStringLiteral("shell_access"))) { | ||||
154 | m_externalToolsMenu = new KateExternalToolsMenuAction(i18n("External Tools"), actionCollection(), plugin, mainWindow); | ||||
155 | actionCollection()->addAction(QStringLiteral("tools_external"), m_externalToolsMenu); | ||||
156 | m_externalToolsMenu->setWhatsThis(i18n("Launch external helper applications")); | ||||
157 | } | ||||
158 | | ||||
159 | mainWindow->guiFactory()->addClient(this); | ||||
160 | | ||||
161 | // ESC should close & hide ToolView | ||||
162 | connect(m_mainWindow, &KTextEditor::MainWindow::unhandledShortcutOverride, [this](QEvent* event) { | ||||
163 | auto keyEvent = static_cast<QKeyEvent *>(event); | ||||
164 | if (keyEvent->key() == Qt::Key_Escape && keyEvent->modifiers() == Qt::NoModifier) { | ||||
165 | deleteToolView(); | ||||
166 | } | ||||
167 | }); | ||||
168 | } | ||||
169 | | ||||
170 | KateExternalToolsPluginView::~KateExternalToolsPluginView() | ||||
171 | { | ||||
172 | m_plugin->unregisterPluginView(this); | ||||
173 | | ||||
174 | m_mainWindow->guiFactory()->removeClient(this); | ||||
175 | | ||||
176 | deleteToolView(); | ||||
177 | | ||||
178 | delete m_externalToolsMenu; | ||||
179 | m_externalToolsMenu = nullptr; | ||||
180 | } | ||||
181 | | ||||
182 | void KateExternalToolsPluginView::rebuildMenu() | ||||
183 | { | ||||
184 | if (m_externalToolsMenu) { | ||||
185 | KXMLGUIFactory* f = factory(); | ||||
186 | f->removeClient(this); | ||||
187 | reloadXML(); | ||||
188 | m_externalToolsMenu->reload(); | ||||
189 | f->addClient(this); | ||||
190 | } | ||||
191 | } | ||||
192 | | ||||
193 | KTextEditor::MainWindow* KateExternalToolsPluginView::mainWindow() const | ||||
194 | { | ||||
195 | return m_mainWindow; | ||||
196 | } | ||||
197 | | ||||
198 | void KateExternalToolsPluginView::createToolView() | ||||
199 | { | ||||
200 | if (!m_toolView) { | ||||
201 | m_toolView = mainWindow()->createToolView(m_plugin, QStringLiteral("ktexteditor_plugin_externaltools"), | ||||
202 | KTextEditor::MainWindow::Bottom, QIcon::fromTheme(QStringLiteral("system-run")), i18n("External Tools")); | ||||
203 | | ||||
204 | m_ui = new Ui::ToolView(); | ||||
205 | m_ui->setupUi(m_toolView); | ||||
206 | | ||||
207 | // set the documents | ||||
208 | m_ui->teOutput->setDocument(m_outputDoc); | ||||
209 | m_ui->teStatus->setDocument(m_statusDoc); | ||||
210 | | ||||
211 | // use fixed font for displaying status and output text | ||||
212 | const auto fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); | ||||
213 | m_ui->teOutput->setFont(fixedFont); | ||||
214 | m_ui->teStatus->setFont(fixedFont); | ||||
215 | | ||||
216 | // close button to delete tool view | ||||
217 | auto btnClose = new QToolButton(); | ||||
218 | btnClose->setAutoRaise(true); | ||||
219 | btnClose->setIcon(QIcon::fromTheme(QStringLiteral("tab-close"))); | ||||
220 | connect(btnClose, &QToolButton::clicked, this, &KateExternalToolsPluginView::deleteToolView); | ||||
221 | m_ui->tabWidget->setCornerWidget(btnClose); | ||||
222 | } | ||||
223 | } | ||||
224 | | ||||
225 | void KateExternalToolsPluginView::showToolView(ToolViewFocus tab) | ||||
226 | { | ||||
227 | createToolView(); | ||||
228 | | ||||
229 | if (tab == ToolViewFocus::OutputTab) { | ||||
230 | m_ui->tabWidget->setCurrentWidget(m_ui->tabOutput); | ||||
231 | } else { | ||||
232 | m_ui->tabWidget->setCurrentWidget(m_ui->tabStatus); | ||||
233 | } | ||||
234 | | ||||
235 | mainWindow()->showToolView(m_toolView); | ||||
236 | } | ||||
237 | | ||||
238 | void KateExternalToolsPluginView::clearToolView() | ||||
239 | { | ||||
240 | m_outputDoc->clear(); | ||||
241 | m_statusDoc->clear(); | ||||
242 | } | ||||
243 | | ||||
244 | void KateExternalToolsPluginView::addToolStatus(const QString& message) | ||||
245 | { | ||||
246 | QTextCursor cursor(m_statusDoc); | ||||
247 | cursor.movePosition(QTextCursor::End); | ||||
248 | cursor.insertText(message); | ||||
249 | cursor.insertText(QStringLiteral("\n")); | ||||
250 | } | ||||
251 | | ||||
252 | void KateExternalToolsPluginView::setOutputData(const QString& data) | ||||
253 | { | ||||
254 | QTextCursor cursor(m_outputDoc); | ||||
255 | cursor.movePosition(QTextCursor::End); | ||||
256 | cursor.insertText(data); | ||||
257 | } | ||||
258 | | ||||
259 | void KateExternalToolsPluginView::deleteToolView() | ||||
260 | { | ||||
261 | if (m_toolView) { | ||||
262 | delete m_ui; | ||||
263 | m_ui = nullptr; | ||||
264 | | ||||
265 | delete m_toolView; | ||||
266 | m_toolView = nullptr; | ||||
267 | } | ||||
268 | } | ||||
269 | // END KateExternalToolsPluginView | ||||
270 | | ||||
271 | // kate: space-indent on; indent-width 4; replace-tabs on; |