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 | // TODO | ||||
21 | // Icons | ||||
22 | // Direct shortcut setting | ||||
23 | #include "kateexternaltoolsview.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 <KAuthorized> | ||||
33 | #include <KConfig> | ||||
34 | #include <KConfigGroup> | ||||
35 | #include <KSharedConfig> | ||||
36 | #include <QStandardPaths> | ||||
37 | #include <QMenu> | ||||
38 | #include <KXMLGUIFactory> | ||||
39 | #include <KLocalizedString> | ||||
40 | | ||||
41 | #include <map> | ||||
42 | #include <vector> | ||||
43 | | ||||
44 | | ||||
45 | // BEGIN KateExternalToolsMenuAction | ||||
46 | KateExternalToolsMenuAction::KateExternalToolsMenuAction(const QString& text, KActionCollection* collection, | ||||
47 | KateExternalToolsPlugin* plugin, KTextEditor::MainWindow* mw) | ||||
48 | : KActionMenu(text, mw) | ||||
49 | , m_plugin(plugin) | ||||
50 | , m_mainwindow(mw) | ||||
51 | , m_actionCollection(collection) | ||||
52 | { | ||||
53 | reload(); | ||||
54 | | ||||
55 | // track active view to adapt enabled tool actions | ||||
56 | connect(mw, &KTextEditor::MainWindow::viewChanged, this, &KateExternalToolsMenuAction::slotViewChanged); | ||||
57 | } | ||||
58 | | ||||
59 | KateExternalToolsMenuAction::~KateExternalToolsMenuAction() = default; | ||||
60 | | ||||
61 | void KateExternalToolsMenuAction::reload() | ||||
62 | { | ||||
63 | // clear action collection | ||||
64 | bool needs_readd = (m_actionCollection->takeAction(this) != nullptr); | ||||
65 | m_actionCollection->clear(); | ||||
66 | if (needs_readd) | ||||
67 | m_actionCollection->addAction(QStringLiteral("tools_external"), this); | ||||
68 | menu()->clear(); | ||||
69 | | ||||
70 | // create tool actions | ||||
71 | std::map<QString, KActionMenu*> categories; | ||||
72 | std::vector<QAction*> uncategorizedActions; | ||||
73 | | ||||
74 | // first add categorized actions, such that the submenus appear at the top | ||||
75 | for (auto tool : m_plugin->tools()) { | ||||
76 | if (tool->hasexec) { | ||||
77 | auto a = new QAction(tool->name, this); | ||||
78 | a->setIcon(QIcon::fromTheme(tool->icon)); | ||||
79 | a->setData(QVariant::fromValue(tool)); | ||||
80 | | ||||
81 | connect(a, &QAction::triggered, [this, a]() { | ||||
82 | m_plugin->runTool(*a->data().value<KateExternalTool*>(), m_mainwindow->activeView()); | ||||
83 | }); | ||||
84 | | ||||
85 | m_actionCollection->addAction(tool->actionName, a); | ||||
86 | if (!tool->category.isEmpty()) { | ||||
87 | auto categoryMenu = categories[tool->category]; | ||||
88 | if (!categoryMenu) { | ||||
89 | categoryMenu = new KActionMenu(tool->category, this); | ||||
90 | categories[tool->category] = categoryMenu; | ||||
91 | addAction(categoryMenu); | ||||
92 | } | ||||
93 | categoryMenu->addAction(a); | ||||
94 | } else { | ||||
95 | uncategorizedActions.push_back(a); | ||||
96 | } | ||||
97 | } | ||||
98 | } | ||||
99 | | ||||
100 | // now add uncategorized actions below | ||||
101 | for (auto uncategorizedAction : uncategorizedActions) { | ||||
102 | addAction(uncategorizedAction); | ||||
103 | } | ||||
104 | | ||||
105 | // load shortcuts | ||||
106 | KSharedConfig::Ptr pConfig = KSharedConfig::openConfig(QStringLiteral("externaltools"), KConfig::NoGlobals, | ||||
107 | QStandardPaths::ApplicationsLocation); | ||||
108 | KConfigGroup config(pConfig, "Global"); | ||||
109 | config = KConfigGroup(pConfig, "Shortcuts"); | ||||
110 | m_actionCollection->readSettings(&config); | ||||
111 | slotViewChanged(m_mainwindow->activeView()); | ||||
112 | } | ||||
113 | | ||||
114 | void KateExternalToolsMenuAction::slotViewChanged(KTextEditor::View* view) | ||||
115 | { | ||||
116 | // no active view, oh oh | ||||
117 | if (!view) { | ||||
118 | return; | ||||
119 | } | ||||
120 | | ||||
121 | // try to enable/disable to match current mime type | ||||
122 | const QString mimeType = view->document()->mimeType(); | ||||
123 | foreach (QAction* action, m_actionCollection->actions()) { | ||||
124 | if (action && action->data().value<KateExternalTool*>()) { | ||||
125 | auto tool = action->data().value<KateExternalTool*>(); | ||||
126 | action->setEnabled(tool->matchesMimetype(mimeType)); | ||||
127 | } | ||||
128 | } | ||||
129 | } | ||||
130 | // END KateExternalToolsMenuAction | ||||
131 | | ||||
132 | | ||||
133 | | ||||
134 | | ||||
135 | | ||||
136 | // BEGIN KateExternalToolsPluginView | ||||
137 | KateExternalToolsPluginView::KateExternalToolsPluginView(KTextEditor::MainWindow* mainWindow, | ||||
138 | KateExternalToolsPlugin* plugin) | ||||
139 | : QObject(mainWindow) | ||||
140 | , m_plugin(plugin) | ||||
141 | , m_mainWindow(mainWindow) | ||||
142 | { | ||||
143 | KXMLGUIClient::setComponentName(QLatin1String("externaltools"), i18n("External Tools")); | ||||
144 | setXMLFile(QLatin1String("ui.rc")); | ||||
145 | | ||||
146 | if (KAuthorized::authorizeAction(QStringLiteral("shell_access"))) { | ||||
147 | m_externalToolsMenu = new KateExternalToolsMenuAction(i18n("External Tools"), actionCollection(), plugin, mainWindow); | ||||
148 | actionCollection()->addAction(QStringLiteral("tools_external"), m_externalToolsMenu); | ||||
149 | m_externalToolsMenu->setWhatsThis(i18n("Launch external helper applications")); | ||||
150 | } | ||||
151 | | ||||
152 | mainWindow->guiFactory()->addClient(this); | ||||
153 | } | ||||
154 | | ||||
155 | void KateExternalToolsPluginView::rebuildMenu() | ||||
156 | { | ||||
157 | if (m_externalToolsMenu) { | ||||
158 | KXMLGUIFactory* f = factory(); | ||||
159 | f->removeClient(this); | ||||
160 | reloadXML(); | ||||
161 | m_externalToolsMenu->reload(); | ||||
162 | f->addClient(this); | ||||
163 | } | ||||
164 | } | ||||
165 | | ||||
166 | KateExternalToolsPluginView::~KateExternalToolsPluginView() | ||||
167 | { | ||||
168 | m_mainWindow->guiFactory()->removeClient(this); | ||||
169 | | ||||
170 | delete m_externalToolsMenu; | ||||
171 | m_externalToolsMenu = nullptr; | ||||
172 | } | ||||
173 | | ||||
174 | KTextEditor::MainWindow* KateExternalToolsPluginView::mainWindow() const | ||||
175 | { | ||||
176 | return m_mainWindow; | ||||
177 | } | ||||
178 | // END KateExternalToolsPluginView | ||||
179 | | ||||
180 | // kate: space-indent on; indent-width 4; replace-tabs on; |