File indexing completed on 2024-05-05 05:51:23

0001 /* This file is part of the KDE project
0002  *
0003  *  SPDX-FileCopyrightText: 2019 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 #pragma once
0008 
0009 namespace KTextEditor
0010 {
0011 class MainWindow;
0012 }
0013 namespace KTextEditor
0014 {
0015 class Document;
0016 class View;
0017 }
0018 
0019 #include <KActionMenu>
0020 #include <KXMLGUIClient>
0021 
0022 #include <QPointer>
0023 
0024 class QTextDocument;
0025 
0026 class KActionCollection;
0027 class KateExternalToolsPlugin;
0028 class KateExternalTool;
0029 
0030 namespace Ui
0031 {
0032 class ToolView;
0033 }
0034 
0035 /**
0036  * Menu action that displays all KateExternalTool in a submenu.
0037  * Enables/disables the tool actions whenever the view changes, depending on the mimetype.
0038  */
0039 class KateExternalToolsMenuAction : public KActionMenu
0040 {
0041     Q_OBJECT
0042 public:
0043     KateExternalToolsMenuAction(const QString &text,
0044                                 KActionCollection *collection,
0045                                 KateExternalToolsPlugin *plugin,
0046                                 class KTextEditor::MainWindow *mw = nullptr);
0047     ~KateExternalToolsMenuAction() override;
0048 
0049     /**
0050      * This will load all the configured services.
0051      */
0052     void reload();
0053 
0054     KActionCollection *actionCollection() const
0055     {
0056         return m_actionCollection;
0057     }
0058 
0059 private Q_SLOTS:
0060     /**
0061      * Called whenever the current view changed.
0062      * Calls updateActionState() for the corresponding document.
0063      */
0064     void slotViewChanged(KTextEditor::View *view);
0065 
0066     /**
0067      * Required to enable/disable the tools that depend on specific mimetypes.
0068      */
0069     void updateActionState(KTextEditor::Document *activeDoc);
0070 
0071     /**
0072      * Triggered via Tools > External Tools > Configure...
0073      */
0074     void showConfigPage();
0075 
0076 private:
0077     KateExternalToolsPlugin *m_plugin;
0078     KTextEditor::MainWindow *m_mainwindow; // for the actions to access view/doc managers
0079     KActionCollection *m_actionCollection;
0080     QMetaObject::Connection m_docUrlChangedConnection;
0081 };
0082 
0083 class KateExternalToolsPluginView : public QObject, public KXMLGUIClient
0084 {
0085     Q_OBJECT
0086 
0087 public:
0088     /**
0089      * Constructor.
0090      */
0091     KateExternalToolsPluginView(KTextEditor::MainWindow *mainWindow, KateExternalToolsPlugin *plugin);
0092 
0093     /**
0094      * Virtual destructor.
0095      */
0096     ~KateExternalToolsPluginView() override;
0097 
0098     /**
0099      * Returns the associated mainWindow
0100      */
0101     KTextEditor::MainWindow *mainWindow() const;
0102 
0103 public Q_SLOTS:
0104     /**
0105      * Called by the plugin view to reload the menu
0106      */
0107     void rebuildMenu();
0108 
0109     /**
0110      * Creates the tool view. If already existing, does nothing.
0111      */
0112     void createToolView();
0113 
0114     /**
0115      * Shows the tool view. The toolview will be created, if not yet existing.
0116      */
0117     void showToolView();
0118 
0119     /**
0120      * Clears the toolview data. If no toolview is around, nothing happens.
0121      */
0122     void clearToolView();
0123 
0124     /**
0125      * Sets the output data to data;
0126      */
0127     void setOutputData(const QString &data);
0128 
0129     /**
0130      * Deletes the tool view, if existing.
0131      */
0132     void deleteToolView();
0133 
0134     /**
0135      * On Escape, hide tool view.
0136      */
0137     void handleEsc(QEvent *event);
0138 
0139 private Q_SLOTS:
0140     void slotViewChanged(KTextEditor::View *v);
0141     void onDocumentSaved(KTextEditor::Document *doc);
0142     void onDocumentAboutToSave(KTextEditor::Document *doc);
0143 
0144 private:
0145     KateExternalToolsPlugin *m_plugin;
0146     KTextEditor::MainWindow *m_mainWindow;
0147     KateExternalToolsMenuAction *m_externalToolsMenu = nullptr;
0148     QWidget *m_toolView = nullptr;
0149     Ui::ToolView *m_ui = nullptr;
0150     QTextDocument *m_outputDoc = nullptr;
0151     QPointer<KTextEditor::View> m_currentView;
0152 };
0153 
0154 // kate: space-indent on; indent-width 4; replace-tabs on;