1 /* This file is part of the KDE project
2  *
3  *  SPDX-FileCopyrightText: 2019 Dominik Haumann <dhaumann@kde.org>
4  *
5  *  SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 #ifndef KTEXTEDITOR_EXTERNALTOOLS_H
8 #define KTEXTEDITOR_EXTERNALTOOLS_H
9 
10 namespace KTextEditor
11 {
12 class MainWindow;
13 }
14 namespace KTextEditor
15 {
16 class Document;
17 class View;
18 }
19 
20 #include <KActionMenu>
21 #include <KMacroExpander>
22 #include <KXMLGUIClient>
23 
24 class QTextDocument;
25 
26 class KActionCollection;
27 class KateExternalToolsPlugin;
28 class KateExternalTool;
29 
30 namespace Ui
31 {
32 class ToolView;
33 }
34 
35 /**
36  * Menu action that displays all KateExternalTool in a submenu.
37  * Enables/disables the tool actions whenever the view changes, depending on the mimetype.
38  */
39 class KateExternalToolsMenuAction : public KActionMenu
40 {
41     Q_OBJECT
42 public:
43     KateExternalToolsMenuAction(const QString &text,
44                                 KActionCollection *collection,
45                                 KateExternalToolsPlugin *plugin,
46                                 class KTextEditor::MainWindow *mw = nullptr);
47     ~KateExternalToolsMenuAction() override;
48 
49     /**
50      * This will load all the configured services.
51      */
52     void reload();
53 
actionCollection()54     KActionCollection *actionCollection() const
55     {
56         return m_actionCollection;
57     }
58 
59 private Q_SLOTS:
60     /**
61      * Called whenever the current view changed.
62      * Calls updateActionState() for the corresponding document.
63      */
64     void slotViewChanged(KTextEditor::View *view);
65 
66     /**
67      * Required to enable/disable the tools that depend on specific mimetypes.
68      */
69     void updateActionState(KTextEditor::Document *activeDoc);
70 
71     /**
72      * Triggered via Tools > External Tools > Configure...
73      */
74     void showConfigPage();
75 
76 private:
77     KateExternalToolsPlugin *m_plugin;
78     KTextEditor::MainWindow *m_mainwindow; // for the actions to access view/doc managers
79     KActionCollection *m_actionCollection;
80     QMetaObject::Connection m_docUrlChangedConnection;
81 };
82 
83 class KateExternalToolsPluginView : public QObject, public KXMLGUIClient
84 {
85     Q_OBJECT
86 
87 public:
88     /**
89      * Constructor.
90      */
91     KateExternalToolsPluginView(KTextEditor::MainWindow *mainWindow, KateExternalToolsPlugin *plugin);
92 
93     /**
94      * Virtual destructor.
95      */
96     ~KateExternalToolsPluginView() override;
97 
98     /**
99      * Returns the associated mainWindow
100      */
101     KTextEditor::MainWindow *mainWindow() const;
102 
103 public Q_SLOTS:
104     /**
105      * Called by the plugin view to reload the menu
106      */
107     void rebuildMenu();
108 
109     /**
110      * Creates the tool view. If already existing, does nothing.
111      */
112     void createToolView();
113 
114     /**
115      * Shows the tool view. The toolview will be created, if not yet existing.
116      */
117     void showToolView();
118 
119     /**
120      * Clears the toolview data. If no toolview is around, nothing happens.
121      */
122     void clearToolView();
123 
124     /**
125      * Sets the output data to data;
126      */
127     void setOutputData(const QString &data);
128 
129     /**
130      * Deletes the tool view, if existing.
131      */
132     void deleteToolView();
133 
134     /**
135      * On Escape, hide tool view.
136      */
137     void handleEsc(QEvent *event);
138 
139 Q_SIGNALS:
140     /**
141      * Signal for outgoing message, the host application will handle them!
142      * Will only be handled inside the main windows of this plugin view.
143      * @param message outgoing message we send to the host application
144      */
145     void message(const QVariantMap &message);
146 
147 private:
148     KateExternalToolsPlugin *m_plugin;
149     KTextEditor::MainWindow *m_mainWindow;
150     KateExternalToolsMenuAction *m_externalToolsMenu = nullptr;
151     QWidget *m_toolView = nullptr;
152     Ui::ToolView *m_ui = nullptr;
153     QTextDocument *m_outputDoc = nullptr;
154 };
155 
156 #endif // KTEXTEDITOR_EXTERNALTOOLS_H
157 
158 // kate: space-indent on; indent-width 4; replace-tabs on;
159