vmdtab.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #ifndef VMDTAB_H
  2. #define VMDTAB_H
  3. #include <QString>
  4. #include <QPointer>
  5. #include "vedittab.h"
  6. #include "vconstants.h"
  7. #include "vmarkdownconverter.h"
  8. #include "vconfigmanager.h"
  9. class VWebView;
  10. class QStackedLayout;
  11. class VDocument;
  12. class VMdEditor;
  13. class VInsertSelector;
  14. class QTimer;
  15. class VMdTab : public VEditTab
  16. {
  17. Q_OBJECT
  18. public:
  19. VMdTab(VFile *p_file, VEditArea *p_editArea, OpenFileMode p_mode, QWidget *p_parent = 0);
  20. // Close current tab.
  21. // @p_forced: if true, discard the changes.
  22. bool closeFile(bool p_forced) Q_DECL_OVERRIDE;
  23. // Enter read mode.
  24. // Will prompt user to save the changes.
  25. void readFile() Q_DECL_OVERRIDE;
  26. // Save file.
  27. bool saveFile() Q_DECL_OVERRIDE;
  28. bool isModified() const Q_DECL_OVERRIDE;
  29. // Scroll to @p_header.
  30. void scrollToHeader(const VHeaderPointer &p_header) Q_DECL_OVERRIDE;
  31. void insertImage() Q_DECL_OVERRIDE;
  32. void insertLink() Q_DECL_OVERRIDE;
  33. // Search @p_text in current note.
  34. void findText(const QString &p_text, uint p_options, bool p_peek,
  35. bool p_forward = true) Q_DECL_OVERRIDE;
  36. // Replace @p_text with @p_replaceText in current note.
  37. void replaceText(const QString &p_text, uint p_options,
  38. const QString &p_replaceText, bool p_findNext) Q_DECL_OVERRIDE;
  39. void replaceTextAll(const QString &p_text, uint p_options,
  40. const QString &p_replaceText) Q_DECL_OVERRIDE;
  41. QString getSelectedText() const Q_DECL_OVERRIDE;
  42. void clearSearchedWordHighlight() Q_DECL_OVERRIDE;
  43. VWebView *getWebViewer() const;
  44. VMdEditor *getEditor() const;
  45. MarkdownConverterType getMarkdownConverterType() const;
  46. void requestUpdateVimStatus() Q_DECL_OVERRIDE;
  47. // Insert decoration markers or decorate selected text.
  48. void decorateText(TextDecoration p_decoration, int p_level = -1) Q_DECL_OVERRIDE;
  49. // Create a filled VEditTabInfo.
  50. VEditTabInfo fetchTabInfo(VEditTabInfo::InfoType p_type = VEditTabInfo::InfoType::All) const Q_DECL_OVERRIDE;
  51. // Enable or disable heading sequence.
  52. void enableHeadingSequence(bool p_enabled);
  53. bool isHeadingSequenceEnabled() const;
  54. // Evaluate magic words.
  55. void evaluateMagicWords() Q_DECL_OVERRIDE;
  56. void applySnippet(const VSnippet *p_snippet) Q_DECL_OVERRIDE;
  57. void applySnippet() Q_DECL_OVERRIDE;
  58. void reload() Q_DECL_OVERRIDE;
  59. void handleFileOrDirectoryChange(bool p_isFile, UpdateAction p_act) Q_DECL_OVERRIDE;
  60. public slots:
  61. // Enter edit mode.
  62. void editFile() Q_DECL_OVERRIDE;
  63. protected:
  64. void writeBackupFile() Q_DECL_OVERRIDE;
  65. private slots:
  66. // Update m_outline according to @p_tocHtml for read mode.
  67. void updateOutlineFromHtml(const QString &p_tocHtml);
  68. // Update m_outline accroding to @p_headers for edit mode.
  69. void updateOutlineFromHeaders(const QVector<VTableOfContentItem> &p_headers);
  70. // Web viewer requests to update current header.
  71. // @p_anchor is the anchor of the header, like "toc_1".
  72. void updateCurrentHeader(const QString &p_anchor);
  73. // Editor requests to update current header.
  74. void updateCurrentHeader(int p_blockNumber);
  75. // Handle key press event in Web view.
  76. void handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift);
  77. // m_editor requests to save changes and enter read mode.
  78. void saveAndRead();
  79. // m_editor requests to discard changes and enter read mode.
  80. void discardAndRead();
  81. // Restore from m_infoToRestore.
  82. void restoreFromTabInfo();
  83. private:
  84. enum TabReady { None = 0, ReadMode = 0x1, EditMode = 0x2 };
  85. // Setup UI.
  86. void setupUI();
  87. // Show the file content in read mode.
  88. void showFileReadMode();
  89. // Show the file content in edit mode.
  90. void showFileEditMode();
  91. // Setup Markdown viewer.
  92. void setupMarkdownViewer();
  93. // Setup Markdown editor.
  94. void setupMarkdownEditor();
  95. // Use VMarkdownConverter (hoedown) to generate the Web view.
  96. void viewWebByConverter();
  97. // Scroll Web view to given header.
  98. // Return true if scroll was made.
  99. bool scrollWebViewToHeader(const VHeaderPointer &p_header);
  100. bool scrollEditorToHeader(const VHeaderPointer &p_header);
  101. // Scroll web/editor to given header.
  102. // Return true if scroll was made.
  103. bool scrollToHeaderInternal(const VHeaderPointer &p_header);
  104. // Search text in Web view.
  105. void findTextInWebView(const QString &p_text, uint p_options, bool p_peek,
  106. bool p_forward);
  107. // Called to zoom in/out content.
  108. void zoom(bool p_zoomIn, qreal p_step = 0.25) Q_DECL_OVERRIDE;
  109. // Zoom Web View.
  110. void zoomWebPage(bool p_zoomIn, qreal p_step = 0.25);
  111. // Focus the proper child widget.
  112. void focusChild() Q_DECL_OVERRIDE;
  113. // Get the markdown editor. If not init yet, init and return it.
  114. VMdEditor *getEditor();
  115. // Restore from @p_fino.
  116. // Return true if succeed.
  117. bool restoreFromTabInfo(const VEditTabInfo &p_info) Q_DECL_OVERRIDE;
  118. // Prepare insert selector with snippets.
  119. VInsertSelector *prepareSnippetSelector(QWidget *p_parent = nullptr);
  120. // Called once read or edit mode is ready.
  121. void tabIsReady(TabReady p_mode);
  122. // Check if there exists backup file from previous session.
  123. // Return true if we could continue.
  124. bool checkPreviousBackupFile();
  125. // updateStatus() with only cursor position information.
  126. void updateCursorStatus();
  127. void textToHtmlViaWebView(const QString &p_text);
  128. VMdEditor *m_editor;
  129. VWebView *m_webViewer;
  130. VDocument *m_document;
  131. MarkdownConverterType m_mdConType;
  132. // Whether heading sequence is enabled.
  133. bool m_enableHeadingSequence;
  134. QStackedLayout *m_stacks;
  135. // Timer to write backup file when content has been changed.
  136. QTimer *m_backupTimer;
  137. bool m_backupFileChecked;
  138. // Used to scroll to the header of edit mode in read mode.
  139. VHeaderPointer m_headerFromEditMode;
  140. };
  141. inline VMdEditor *VMdTab::getEditor()
  142. {
  143. if (m_editor) {
  144. return m_editor;
  145. } else {
  146. setupMarkdownEditor();
  147. return m_editor;
  148. }
  149. }
  150. inline VMdEditor *VMdTab::getEditor() const
  151. {
  152. return m_editor;
  153. }
  154. #endif // VMDTAB_H