vlivepreviewhelper.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #ifndef VLIVEPREVIEWHELPER_H
  2. #define VLIVEPREVIEWHELPER_H
  3. #include <QObject>
  4. #include <QTextDocument>
  5. #include "hgmarkdownhighlighter.h"
  6. #include "vpreviewmanager.h"
  7. class VEditor;
  8. class VDocument;
  9. class VGraphvizHelper;
  10. class VPlantUMLHelper;
  11. class VMathJaxPreviewHelper;
  12. class CodeBlockPreviewInfo
  13. {
  14. public:
  15. CodeBlockPreviewInfo();
  16. explicit CodeBlockPreviewInfo(const VCodeBlock &p_cb);
  17. void clearImageData();
  18. void updateNonContent(const QTextDocument *p_doc, const VCodeBlock &p_cb);
  19. void updateInplacePreview(const VEditor *p_editor, const QTextDocument *p_doc);
  20. VCodeBlock &codeBlock()
  21. {
  22. return m_codeBlock;
  23. }
  24. const VCodeBlock &codeBlock() const
  25. {
  26. return m_codeBlock;
  27. }
  28. void setCodeBlock(const VCodeBlock &p_cb)
  29. {
  30. m_codeBlock = p_cb;
  31. clearImageData();
  32. }
  33. bool inplacePreviewReady() const
  34. {
  35. return !m_inplacePreview.isNull();
  36. }
  37. bool hasImageData() const
  38. {
  39. return !m_imgData.isEmpty();
  40. }
  41. const QString &imageData() const
  42. {
  43. return m_imgData;
  44. }
  45. bool hasImageDataBa() const
  46. {
  47. return !m_imgDataBa.isEmpty();
  48. }
  49. const QByteArray &imageDataBa() const
  50. {
  51. return m_imgDataBa;
  52. }
  53. const QString &imageFormat() const
  54. {
  55. return m_imgFormat;
  56. }
  57. void setImageData(const QString &p_format, const QString &p_data)
  58. {
  59. m_imgDataBa.clear();
  60. m_imgFormat = p_format;
  61. m_imgData = p_data;
  62. }
  63. void setImageDataBa(const QString &p_format, const QByteArray &p_data)
  64. {
  65. m_imgData.clear();
  66. m_imgFormat = p_format;
  67. m_imgDataBa = p_data;
  68. }
  69. const QSharedPointer<VImageToPreview> inplacePreview() const
  70. {
  71. return m_inplacePreview;
  72. }
  73. private:
  74. static int getImageIndex()
  75. {
  76. static int index = 0;
  77. return ++index;
  78. }
  79. VCodeBlock m_codeBlock;
  80. QString m_imgData;
  81. QByteArray m_imgDataBa;
  82. QString m_imgFormat;
  83. QSharedPointer<VImageToPreview> m_inplacePreview;
  84. };
  85. // Manage live preview and inplace preview.
  86. class VLivePreviewHelper : public QObject
  87. {
  88. Q_OBJECT
  89. public:
  90. VLivePreviewHelper(VEditor *p_editor,
  91. VDocument *p_document,
  92. QObject *p_parent = nullptr);
  93. void updateLivePreview();
  94. void setLivePreviewEnabled(bool p_enabled);
  95. void setInplacePreviewEnabled(bool p_enabled);
  96. bool isPreviewEnabled() const;
  97. public slots:
  98. void updateCodeBlocks(const QVector<VCodeBlock> &p_codeBlocks);
  99. void webAsyncResultReady(int p_id, const QString &p_lang, const QString &p_result);
  100. signals:
  101. void inplacePreviewCodeBlockUpdated(const QVector<QSharedPointer<VImageToPreview> > &p_images);
  102. private slots:
  103. void handleCursorPositionChanged();
  104. void localAsyncResultReady(int p_id, const QString &p_format, const QString &p_result);
  105. void mathjaxPreviewResultReady(int p_identitifer,
  106. int p_id,
  107. const QString &p_format,
  108. const QByteArray &p_data);
  109. private:
  110. bool isPreviewLang(const QString &p_lang) const;
  111. // Get image data for this code block for inplace preview.
  112. void processForInplacePreview(int p_idx);
  113. // Emit signal to update inplace preview.
  114. void updateInplacePreview();
  115. // Sorted by m_startBlock in ascending order.
  116. QVector<CodeBlockPreviewInfo> m_codeBlocks;
  117. VEditor *m_editor;
  118. VDocument *m_document;
  119. QTextDocument *m_doc;
  120. // Current previewed code block index in m_codeBlocks.
  121. int m_cbIndex;
  122. bool m_flowchartEnabled;
  123. bool m_mermaidEnabled;
  124. int m_plantUMLMode;
  125. bool m_graphvizEnabled;
  126. bool m_mathjaxEnabled;
  127. bool m_livePreviewEnabled;
  128. bool m_inplacePreviewEnabled;
  129. VGraphvizHelper *m_graphvizHelper;
  130. VPlantUMLHelper *m_plantUMLHelper;
  131. VMathJaxPreviewHelper *m_mathJaxHelper;
  132. // Identification for VMathJaxPreviewHelper.
  133. int m_mathJaxID;
  134. };
  135. inline bool VLivePreviewHelper::isPreviewEnabled() const
  136. {
  137. return m_inplacePreviewEnabled || m_livePreviewEnabled;
  138. }
  139. #endif // VLIVEPREVIEWHELPER_H