File indexing completed on 2024-04-14 15:17:41

0001 /********************************************************************************
0002   Copyright (C) 2011-2022 by Michel Ludwig (michel.ludwig@kdemail.net)
0003  ********************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or modify  *
0008  *   it under the terms of the GNU General Public License as published by  *
0009  *   the Free Software Foundation; either version 2 of the License, or     *
0010  *   (at your option) any later version.                                   *
0011  *                                                                         *
0012  ***************************************************************************/
0013 
0014 #ifndef LIVEPREVIEW_H
0015 #define LIVEPREVIEW_H
0016 
0017 #include "documentinfo.h"
0018 #include "kileinfo.h"
0019 #include "kileproject.h"
0020 #include "kiletool.h"
0021 #include "editorextension.h"
0022 #include "widgets/previewwidget.h"
0023 
0024 #include <QHash>
0025 #include <QObject>
0026 #include <QPointer>
0027 #include <QString>
0028 #include <QStringList>
0029 
0030 #include <KLed>
0031 #include <KToggleAction>
0032 #include <QTemporaryDir>
0033 
0034 namespace KileDocument {
0035 class TextInfo;
0036 }
0037 
0038 namespace KileTool
0039 {
0040 
0041 class LivePreviewManager : public QObject
0042 {
0043     Q_OBJECT
0044 
0045 public:
0046     // has to be instatiated after the view manager only!
0047     LivePreviewManager(KileInfo *ki, KActionCollection *ac);
0048     ~LivePreviewManager();
0049 
0050     // live preview won't be run in 'boot up' mode, which is enabled by default
0051     void disableBootUpMode();
0052 
0053     void readConfig(KConfig *config);
0054     void writeConfig();
0055     static void readLivePreviewStatusSettings(KConfigGroup &configGroup, LivePreviewUserStatusHandler *handler);
0056     static void writeLivePreviewStatusSettings(KConfigGroup &configGroup, LivePreviewUserStatusHandler *handler);
0057 
0058     void compilePreview(KileDocument::LaTeXInfo *info, KTextEditor::View *view);
0059     void showPreviewCompileIfNecessary(KileDocument::LaTeXInfo *info, KTextEditor::View *view);
0060 
0061     bool isLivePreviewActive() const;
0062     bool isLivePreviewPossible() const;
0063 
0064     bool isLivePreviewEnabledForCurrentDocument();
0065     void setLivePreviewEnabledForCurrentDocument(bool b);
0066 
0067     void buildLivePreviewMenu(KConfig *config);
0068 
0069     QString getPreviewFile() const;
0070     inline QUrl getPreviewFileURL() const {
0071         return QUrl::fromLocalFile(getPreviewFile());
0072     }
0073 
0074 Q_SIGNALS:
0075     void livePreviewSuccessful();
0076     void livePreviewRunning();
0077     void livePreviewStopped(); // disabled or stopped
0078 
0079 public Q_SLOTS:
0080     void handleTextChanged(KTextEditor::Document *doc);
0081     void handleDocumentSavedOrUploaded(KTextEditor::Document *doc, bool savedAs);
0082 
0083     void handleMasterDocumentChanged();
0084 
0085     void recompileLivePreview();
0086     void refreshLivePreview();
0087 
0088     void showCursorPositionInDocumentViewer();
0089 
0090 private Q_SLOTS:
0091     void handleDocumentModificationTimerTimeout();
0092 
0093     // TextInfo* object due to the signal 'aboutToBeDestroyed(KileDocument::TextInfo*)'
0094     void removeLaTeXInfo(KileDocument::TextInfo *info);
0095     void removeProject(KileProject *project);
0096 
0097     void toolDestroyed();
0098     void toolDone(KileTool::Base *base, int i, bool childToolSpawned);
0099     void childToolDone(KileTool::Base *base, int i, bool childToolSpawned);
0100 
0101     void handleTextViewActivated(KTextEditor::View *view, bool clearPreview = true, bool forceCompilation = false);
0102     void handleTextViewClosed(KTextEditor::View *view, bool wasActiveView);
0103 
0104     void handleDocumentOpened(KileDocument::TextInfo *info);
0105 
0106     void handleProjectOpened(KileProject *project);
0107     void handleProjectItemAdded(KileProject *project, KileProjectItem *item);
0108     void handleProjectItemRemoved(KileProject *project, KileProjectItem *item);
0109 
0110     void handleDocumentSavedAs(KTextEditor::View*, KileDocument::TextInfo*);
0111 
0112     void handleSpawnedChildTool(KileTool::Base *parent, KileTool::Base *child);
0113 
0114     void previewForCurrentDocumentActionTriggered(bool b);
0115 
0116     void livePreviewToolActionTriggered();
0117 
0118 private:
0119     class PreviewInformation;
0120 
0121     KileInfo *m_ki;
0122     bool m_bootUpMode;
0123     QPointer<KLed> m_previewStatusLed;
0124     KToggleAction *m_previewForCurrentDocumentAction;
0125     QAction *m_recompileLivePreviewAction;
0126     QTimer *m_ledBlinkingTimer, *m_documentChangedTimer;
0127 
0128     QHash<QString, QString> m_runningPathToPreviewPathHash;
0129     QHash<QString, QString> m_runningPreviewPathToPathHash;
0130     QString m_runningPreviewFile;
0131     KileDocument::LaTeXInfo *m_runningLaTeXInfo;
0132     KTextEditor::View *m_runningTextView;
0133     KileProject *m_runningProject;
0134     PreviewInformation *m_runningPreviewInformation;
0135     QHash<KileDocument::TextInfo*, QByteArray> m_runningTextHash;
0136 
0137     PreviewInformation *m_shownPreviewInformation;
0138 
0139     QHash<KileDocument::LaTeXInfo*, PreviewInformation*> m_latexInfoToPreviewInformationHash;
0140     QHash<KileProject*, PreviewInformation*> m_projectToPreviewInformationHash;
0141     PreviewInformation *m_masterDocumentPreviewInformation;
0142 
0143     // all the members required to handle tool actions for live preview
0144     QHash<ToolConfigPair, QAction *> m_livePreviewToolToActionHash;
0145     QHash<QAction *, ToolConfigPair> m_actionToLivePreviewToolHash;
0146     QActionGroup *m_livePreviewToolActionGroup;
0147     std::list<QAction*> m_livePreviewToolActionList;
0148 
0149     PreviewInformation* findPreviewInformation(KileDocument::TextInfo *textInfo, KileProject* *locatedProject = Q_NULLPTR,
0150             LivePreviewUserStatusHandler* *userStatusHandler = Q_NULLPTR,
0151             LaTeXOutputHandler* *latexOutputHandler = Q_NULLPTR);
0152     bool isCurrentDocumentOrProject(KTextEditor::Document *doc);
0153 
0154     void updatePreviewInformationAfterCompilationFinished();
0155 
0156     void displayErrorMessage(const QString &text, bool clearFirst = false);
0157 
0158     void createActions(KActionCollection *ac);
0159     void populateViewerControlToolBar();
0160     void synchronizeViewWithCursor(KileDocument::TextInfo *info, KTextEditor::View *view,
0161                                    const KTextEditor::Cursor& newPosition,
0162                                    bool calledFromCursorPositionChange = false);
0163 
0164     void stopAndClearPreview();
0165 
0166     void showPreviewDisabled();
0167     void showPreviewRunning();
0168     void showPreviewFailed();
0169     void showPreviewSuccessful();
0170     void showPreviewOutOfDate();
0171 
0172     void stopLivePreview();
0173     void clearLivePreview();
0174 
0175     void deleteAllLivePreviewInformation();
0176 
0177     void handleProjectItemAdditionOrRemoval(KileProject *project, KileProjectItem *item);
0178 
0179     void fillTextHashForMasterDocument(QHash<KileDocument::TextInfo*, QByteArray> &textHash);
0180 
0181     void disablePreview();
0182 
0183     void clearRunningLivePreviewInformation();
0184 
0185     void updateLivePreviewToolActions(LivePreviewUserStatusHandler *statusHandler);
0186     void setLivePreviewToolActionsEnabled(bool b);
0187 
0188     bool ensureDocumentIsOpenInViewer(PreviewInformation *previewInformation, bool *hadToOpen = Q_NULLPTR);
0189     void reloadDocumentInViewer();
0190 
0191 };
0192 
0193 }
0194 
0195 #endif