File indexing completed on 2024-04-28 05:49:02

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Friedrich W. H. Kossebau <kossebau@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 // KF
0010 #include <KPluginMetaData>
0011 #include <KXMLGUIBuilder>
0012 // Qt
0013 #include <QPointer>
0014 #include <QStackedWidget>
0015 
0016 #include <optional>
0017 
0018 class KTextEditorPreviewPlugin;
0019 
0020 namespace KTextEditor
0021 {
0022 class Document;
0023 class MainWindow;
0024 class View;
0025 }
0026 class KXMLGUIFactory;
0027 class KToggleAction;
0028 class KConfigGroup;
0029 
0030 class QWidgetAction;
0031 class QMenu;
0032 
0033 namespace KTextEditorPreview
0034 {
0035 class KPartView;
0036 
0037 /**
0038  * The actual widget shown in the toolview.
0039  *
0040  * It either shows a label "No preview available."
0041  * or the widget of the KPart currently used to preview
0042  * the selected document in its target format.
0043  *
0044  * The preview can be locked to a document by checking off the
0045  * lock action. If locked the currently shown document will not
0046  * be changed if another view is activated, unless the document
0047  * itself is closed, where then the label is shown instead.
0048  */
0049 class PreviewWidget : public QStackedWidget, public KXMLGUIBuilder
0050 {
0051     Q_OBJECT
0052 
0053 public:
0054     /**
0055      * Constructor
0056      *
0057      * @param core the plugin object
0058      * @param mainWindow the main window with all the texteditor views
0059      * @param parent widget object taking the ownership
0060      */
0061     PreviewWidget(KTextEditor::MainWindow *mainWindow, QWidget *parent);
0062     ~PreviewWidget() override;
0063 
0064     void readSessionConfig(const KConfigGroup &configGroup);
0065     void writeSessionConfig(KConfigGroup &configGroup) const;
0066 
0067 public: // KXMLGUIBuilder API
0068     QWidget *createContainer(QWidget *parent, int index, const QDomElement &element, QAction *&containerAction) override;
0069     void removeContainer(QWidget *container, QWidget *parent, QDomElement &element, QAction *containerAction) override;
0070 
0071 protected:
0072     void showEvent(QShowEvent *event) override;
0073     void hideEvent(QHideEvent *event) override;
0074 
0075 private Q_SLOTS:
0076     /**
0077      * Update the widget to the currently active view.
0078      *
0079      * If the document lock is set, this will not change the preview.
0080      * Otherwise the preview will switch to the document of the now active view
0081      * and show a preview for that, unless there is no matching kpart found.
0082      * In that case, or if there is no active view, a label will be shown with
0083      * "No preview available".
0084      *
0085      * @param view the view or, if there is none, a nullptr
0086      */
0087     void setTextEditorView(KTextEditor::View *view);
0088     void resetTextEditorView(KTextEditor::Document *document);
0089     void unsetDocument(KTextEditor::Document *document);
0090 
0091 private:
0092     void toggleDocumentLocking(bool locked);
0093     void toggleAutoUpdating(bool autoRefreshing);
0094     void updatePreview();
0095     void showAboutKPartPlugin();
0096     void clearMenu();
0097     static std::optional<KPluginMetaData> findPreviewPart(const QStringList mimeTypes);
0098 
0099 private:
0100     KToggleAction *m_lockAction;
0101     KToggleAction *m_autoUpdateAction;
0102     QAction *m_updateAction;
0103     QWidgetAction *m_kPartMenuAction;
0104     QMenu *m_kPartMenu;
0105     QAction *m_aboutKPartAction;
0106 
0107     KTextEditor::MainWindow *const m_mainWindow;
0108 
0109     KTextEditor::Document *m_previewedTextEditorDocument = nullptr;
0110     KTextEditor::View *m_previewedTextEditorView = nullptr;
0111     QString m_currentServiceId;
0112     QString m_currentMode;
0113     QPointer<KPartView> m_partView;
0114     KXMLGUIFactory *m_xmlGuiFactory;
0115 };
0116 
0117 }