Changeset View
Changeset View
Standalone View
Standalone View
addons/externaltools/externaltools.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 | // TODO | ||||
21 | // Icons | ||||
22 | // Direct shortcut setting | ||||
23 | #include "externaltools.h" | ||||
24 | #include "externaltoolsplugin.h" | ||||
25 | #include "kateexternaltool.h" | ||||
26 | | ||||
27 | #include <KTextEditor/MainWindow> | ||||
28 | #include <KTextEditor/Document> | ||||
29 | #include <KTextEditor/View> | ||||
30 | | ||||
31 | #include <KActionCollection> | ||||
32 | #include <KConfig> | ||||
33 | #include <KConfigGroup> | ||||
34 | #include <KSharedConfig> | ||||
35 | #include <QStandardPaths> | ||||
36 | #include <QMenu> | ||||
37 | | ||||
38 | // BEGIN KateExternalToolsMenuAction | ||||
39 | KateExternalToolsMenuAction::KateExternalToolsMenuAction(const QString& text, KActionCollection* collection, | ||||
40 | KateExternalToolsPlugin* plugin, KTextEditor::MainWindow* mw) | ||||
41 | : KActionMenu(text, mw) | ||||
42 | , m_plugin(plugin) | ||||
43 | , m_mainwindow(mw) | ||||
44 | , m_actionCollection(collection) | ||||
45 | { | ||||
46 | reload(); | ||||
47 | | ||||
48 | // track active view to adapt enabled tool actions | ||||
49 | connect(mw, &KTextEditor::MainWindow::viewChanged, this, &KateExternalToolsMenuAction::slotViewChanged); | ||||
50 | } | ||||
51 | | ||||
52 | KateExternalToolsMenuAction::~KateExternalToolsMenuAction() {} | ||||
53 | | ||||
54 | void KateExternalToolsMenuAction::reload() | ||||
55 | { | ||||
56 | // clear action collection | ||||
57 | bool needs_readd = (m_actionCollection->takeAction(this) != nullptr); | ||||
58 | m_actionCollection->clear(); | ||||
59 | if (needs_readd) | ||||
60 | m_actionCollection->addAction(QStringLiteral("tools_external"), this); | ||||
61 | menu()->clear(); | ||||
62 | | ||||
63 | // create tool actions | ||||
64 | for (auto tool : m_plugin->tools()) { | ||||
65 | if (tool->hasexec) { | ||||
66 | auto a = new QAction(tool->name, this); | ||||
67 | a->setIcon(QIcon::fromTheme(tool->icon)); | ||||
68 | a->setData(QVariant::fromValue(tool)); | ||||
69 | | ||||
70 | connect(a, &QAction::triggered, [this, a]() { | ||||
71 | m_plugin->runTool(*a->data().value<KateExternalTool*>(), m_mainwindow->activeView()); | ||||
72 | }); | ||||
73 | | ||||
74 | m_actionCollection->addAction(tool->actionName, a); | ||||
75 | addAction(a); | ||||
76 | } | ||||
77 | } | ||||
78 | | ||||
79 | // load shortcuts | ||||
80 | KSharedConfig::Ptr pConfig = KSharedConfig::openConfig(QStringLiteral("externaltools"), KConfig::NoGlobals, | ||||
81 | QStandardPaths::ApplicationsLocation); | ||||
82 | KConfigGroup config(pConfig, "Global"); | ||||
83 | config = KConfigGroup(pConfig, "Shortcuts"); | ||||
84 | m_actionCollection->readSettings(&config); | ||||
85 | slotViewChanged(m_mainwindow->activeView()); | ||||
86 | } | ||||
87 | | ||||
88 | void KateExternalToolsMenuAction::slotViewChanged(KTextEditor::View* view) | ||||
89 | { | ||||
90 | // no active view, oh oh | ||||
91 | if (!view) { | ||||
92 | return; | ||||
93 | } | ||||
94 | | ||||
95 | // try to enable/disable to match current mime type | ||||
96 | const QString mimeType = view->document()->mimeType(); | ||||
97 | foreach (QAction* action, m_actionCollection->actions()) { | ||||
98 | if (action && action->data().value<KateExternalTool*>()) { | ||||
99 | auto tool = action->data().value<KateExternalTool*>(); | ||||
100 | const bool toolActive = tool->mimetypes.isEmpty() || tool->mimetypes.contains(mimeType); | ||||
101 | action->setEnabled(toolActive); | ||||
102 | } | ||||
103 | } | ||||
104 | } | ||||
105 | // END KateExternalToolsMenuAction | ||||
106 | | ||||
107 | // kate: space-indent on; indent-width 4; replace-tabs on; |