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 
0012 // Qt
0013 #include <QHash>
0014 #include <QObject>
0015 #include <QTimer>
0016 
0017 namespace KTextEditor
0018 {
0019 class Document;
0020 }
0021 namespace KParts
0022 {
0023 class ReadOnlyPart;
0024 }
0025 class QAction;
0026 class QKeySequence;
0027 class QLabel;
0028 class QTemporaryFile;
0029 
0030 namespace KTextEditorPreview
0031 {
0032 /**
0033  * Wrapper around a KPart which handles feeding it the content of a text document
0034  *
0035  * The class creates a KPart from the service description passed in the constructor
0036  * and then takes care for feeding the content of the currently set text document
0037  * to the KPart for preview in the target format, both on changes in the document
0038  * or when a new document is set.
0039  *
0040  * The content is pushed via the KParts stream API, if the KPart instance
0041  * supports it, or as fallback via the filesystem, using a QTemporaryFile instance.
0042  * Updates are squashed via a timer, to reduce load.
0043  */
0044 class KPartView : public QObject
0045 {
0046     Q_OBJECT
0047 
0048 public:
0049     /**
0050      * Constructor
0051      *
0052      * @param service the description of the KPart which should be used, may not be a nullptr
0053      * @param parent the object taking ownership, can be a nullptr
0054      */
0055     KPartView(const KPluginMetaData &service, QObject *parent);
0056     ~KPartView() override;
0057 
0058     /**
0059      * Returns the widget object, ownership is not transferred.
0060      */
0061     QWidget *widget() const;
0062 
0063     KParts::ReadOnlyPart *kPart() const;
0064 
0065     /**
0066      * Sets the current document whose content should be previewed by the KPart.
0067      *
0068      * @param document the document or, if there is none to preview, a nullptr
0069      */
0070     void setDocument(KTextEditor::Document *document);
0071 
0072     /**
0073      * Returns the current document whose content is previewed by the KPart.
0074      *
0075      * @return current document or, if there is none, a nullptr
0076      */
0077     KTextEditor::Document *document() const;
0078 
0079     /**
0080      * Sets whether the preview should be updating automatically on document changes or not.
0081      *
0082      * @param autoUpdating whether the preview should be updating automatically on document changes or not
0083      */
0084     void setAutoUpdating(bool autoUpdating);
0085 
0086     /**
0087      * Returns @c true if the preview is updating automatically on document changes, @c false otherwise.
0088      */
0089     bool isAutoUpdating() const;
0090 
0091     /**
0092      * Update preview to current document content.
0093      */
0094     void updatePreview();
0095 
0096 protected:
0097     bool eventFilter(QObject *object, QEvent *event) override;
0098 
0099 private:
0100     void triggerUpdatePreview();
0101     void handleOpenUrlRequest(const QUrl &url);
0102 
0103 private:
0104     QLabel *m_errorLabel = nullptr;
0105     KParts::ReadOnlyPart *m_part = nullptr;
0106     KTextEditor::Document *m_document = nullptr;
0107 
0108     bool m_autoUpdating = true;
0109     bool m_previewDirty = true;
0110     QTimer m_updateSquashingTimerFast;
0111     QTimer m_updateSquashingTimerSlow;
0112     QTemporaryFile *m_bufferFile = nullptr;
0113     QHash<QKeySequence, QAction *> m_shortcuts;
0114 };
0115 
0116 }