vdocument.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef VDOCUMENT_H
  2. #define VDOCUMENT_H
  3. #include <QObject>
  4. #include <QString>
  5. #include "vwordcountinfo.h"
  6. class VFile;
  7. class VPlantUMLHelper;
  8. class VGraphvizHelper;
  9. class VDocument : public QObject
  10. {
  11. Q_OBJECT
  12. Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
  13. Q_PROPERTY(QString toc MEMBER m_toc NOTIFY tocChanged)
  14. Q_PROPERTY(QString html MEMBER m_html NOTIFY htmlChanged)
  15. public:
  16. // @p_file could be NULL.
  17. VDocument(const VFile *p_file, QObject *p_parent = 0);
  18. QString getToc();
  19. // Scroll to @anchor in the web.
  20. // @anchor is the id without '#', like "toc_1". If empty, will scroll to top.
  21. void scrollToAnchor(const QString &anchor);
  22. void setHtml(const QString &html);
  23. // Request to highlight a segment text.
  24. // Use p_id to identify the result.
  25. void highlightTextAsync(const QString &p_text, int p_id, int p_timeStamp);
  26. // Request to convert @p_text to HTML.
  27. void textToHtmlAsync(int p_identitifer,
  28. int p_id,
  29. int p_timeStamp,
  30. const QString &p_text,
  31. bool p_inlineStyle);
  32. void setFile(const VFile *p_file);
  33. bool isReadyToHighlight() const;
  34. bool isReadyToTextToHtml() const;
  35. // Request to get the HTML content.
  36. void getHtmlContentAsync();
  37. const VWordCountInfo &getWordCountInfo() const;
  38. // Whether change to preview mode.
  39. void setPreviewEnabled(bool p_enabled);
  40. // @p_livePreview: if true, display the result in the preview-div; otherwise,
  41. // call previewCodeBlockCB() to pass back the result.
  42. // Only for online parser.
  43. void previewCodeBlock(int p_id,
  44. const QString &p_lang,
  45. const QString &p_text,
  46. bool p_livePreview);
  47. // Set the content of the preview.
  48. void setPreviewContent(const QString &p_lang, const QString &p_html);
  49. int registerIdentifier();
  50. public slots:
  51. // Will be called in the HTML side
  52. // @toc: the HTML of the TOC.
  53. // @baseLevel: the base level of @toc, starting from 1. It is the top level
  54. // in the @toc.
  55. void setToc(const QString &toc, int baseLevel);
  56. // When the Web view has been scrolled, it will signal current header anchor.
  57. // Empty @anchor to indicate an invalid header.
  58. // The header does not begins with '#'.
  59. void setHeader(const QString &anchor);
  60. void setLog(const QString &p_log);
  61. void keyPressEvent(int p_key, bool p_ctrl, bool p_shift);
  62. void updateText();
  63. void highlightTextCB(const QString &p_html, int p_id, int p_timeStamp);
  64. void noticeReadyToHighlightText();
  65. void textToHtmlCB(int p_identitifer, int p_id, int p_timeStamp, const QString &p_html);
  66. void noticeReadyToTextToHtml();
  67. // Web-side handle logics (MathJax etc.) is finished.
  68. // But the page may not finish loading, such as images.
  69. void finishLogics();
  70. void htmlContentCB(const QString &p_head,
  71. const QString &p_style,
  72. const QString &p_body);
  73. void updateWordCountInfo(int p_wordCount,
  74. int p_charWithoutSpacesCount,
  75. int p_charWithSpacesCount);
  76. // Web-side call this to process PlantUML locally.
  77. void processPlantUML(int p_id, const QString &p_format, const QString &p_text);
  78. // Web-side call this to process Graphviz locally.
  79. void processGraphviz(int p_id, const QString &p_format, const QString &p_text);
  80. void previewCodeBlockCB(int p_id, const QString &p_lang, const QString &p_html);
  81. signals:
  82. void textChanged(const QString &text);
  83. void tocChanged(const QString &toc);
  84. void requestScrollToAnchor(const QString &anchor);
  85. // @anchor is the id of that anchor, without '#'.
  86. void headerChanged(const QString &anchor);
  87. void htmlChanged(const QString &html);
  88. void logChanged(const QString &p_log);
  89. void keyPressed(int p_key, bool p_ctrl, bool p_shift);
  90. void requestHighlightText(const QString &p_text, int p_id, int p_timeStamp);
  91. void textHighlighted(const QString &p_html, int p_id, int p_timeStamp);
  92. void readyToHighlightText();
  93. void logicsFinished();
  94. void requestTextToHtml(int p_identitifer,
  95. int p_id,
  96. int p_timeStamp,
  97. const QString &p_text,
  98. bool p_inlineStyle);
  99. void textToHtmlFinished(int p_identitifer, int p_id, int p_timeStamp, const QString &p_html);
  100. void requestHtmlContent();
  101. void htmlContentFinished(const QString &p_headContent,
  102. const QString &p_styleContent,
  103. const QString &p_bodyContent);
  104. void wordCountInfoUpdated();
  105. void plantUMLResultReady(int p_id,
  106. unsigned long long p_timeStamp,
  107. const QString &p_format,
  108. const QString &p_result);
  109. void graphvizResultReady(int p_id,
  110. unsigned long long p_timeStamp,
  111. const QString &p_format,
  112. const QString &p_result);
  113. void requestPreviewEnabled(bool p_enabled);
  114. void requestPreviewCodeBlock(int p_id,
  115. const QString &p_lang,
  116. const QString &p_text,
  117. bool p_livePreview);
  118. void requestSetPreviewContent(const QString &p_lang, const QString &p_html);
  119. void codeBlockPreviewReady(int p_id, const QString &p_lang, const QString &p_html);
  120. private:
  121. QString m_toc;
  122. QString m_header;
  123. // m_text does NOT contain actual content.
  124. QString m_text;
  125. // When using Hoedown, m_html will contain the html content.
  126. QString m_html;
  127. const VFile *m_file;
  128. // Whether the web side is ready to handle highlight text request.
  129. bool m_readyToHighlight;
  130. // Whether the web side is ready to convert text to html.
  131. bool m_readyToTextToHtml;
  132. VWordCountInfo m_wordCountInfo;
  133. VPlantUMLHelper *m_plantUMLHelper;
  134. VGraphvizHelper *m_graphvizHelper;
  135. int m_nextID;
  136. };
  137. inline bool VDocument::isReadyToHighlight() const
  138. {
  139. return m_readyToHighlight;
  140. }
  141. inline bool VDocument::isReadyToTextToHtml() const
  142. {
  143. return m_readyToTextToHtml;
  144. }
  145. inline const VWordCountInfo &VDocument::getWordCountInfo() const
  146. {
  147. return m_wordCountInfo;
  148. }
  149. inline int VDocument::registerIdentifier()
  150. {
  151. return ++m_nextID;
  152. }
  153. #endif // VDOCUMENT_H