vmdedit.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifndef VMDEDIT_H
  2. #define VMDEDIT_H
  3. #include "vedit.h"
  4. #include <QVector>
  5. #include <QString>
  6. #include "vtoc.h"
  7. class HGMarkdownHighlighter;
  8. class VMdEdit : public VEdit
  9. {
  10. Q_OBJECT
  11. public:
  12. VMdEdit(VFile *p_file, QWidget *p_parent = 0);
  13. void beginEdit() Q_DECL_OVERRIDE;
  14. void endEdit() Q_DECL_OVERRIDE;
  15. void saveFile() Q_DECL_OVERRIDE;
  16. void reloadFile() Q_DECL_OVERRIDE;
  17. // An image has been inserted.
  18. void imageInserted(const QString &p_name);
  19. // Scroll to m_headers[p_headerIndex].
  20. void scrollToHeader(int p_headerIndex);
  21. // Like toPlainText(), but remove special blocks containing images.
  22. QString toPlainTextWithoutImg() const;
  23. signals:
  24. void headersChanged(const QVector<VHeader> &headers);
  25. void curHeaderChanged(int p_lineNumber, int p_outlineIndex);
  26. void statusChanged();
  27. private slots:
  28. void generateEditOutline();
  29. void updateCurHeader();
  30. // Update block list containing image links.
  31. void updateImageBlocks(QSet<int> p_imageBlocks);
  32. protected:
  33. void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
  34. bool canInsertFromMimeData(const QMimeData *source) const Q_DECL_OVERRIDE;
  35. void insertFromMimeData(const QMimeData *source) Q_DECL_OVERRIDE;
  36. private:
  37. void updateFontAndPalette();
  38. void updateTabSettings();
  39. void initInitImages();
  40. void clearUnusedImages();
  41. // p_text[p_index] is QChar::ObjectReplacementCharacter. Remove the line containing it.
  42. // Returns the index of previous line's '\n'.
  43. int removeObjectReplacementLine(QString &p_text, int p_index) const;
  44. void previewImageOfBlock(int p_block);
  45. bool isImagePreviewBlock(int p_block);
  46. bool isImagePreviewBlock(QTextBlock p_block);
  47. // p_block is a image preview block. We need to update it with image.
  48. void updateImagePreviewBlock(int p_block, const QString &p_image);
  49. // Insert a block after @p_block to preview image @p_image.
  50. void insertImagePreviewBlock(int p_block, const QString &p_image);
  51. // Clean up un-referenced image preview block.
  52. void clearOrphanImagePreviewBlock();
  53. void removeBlock(QTextBlock p_block);
  54. bool isOrphanImagePreviewBlock(QTextBlock p_block);
  55. // Returns the image relative path (image/xxx.png) only when
  56. // there is one and only one image link.
  57. QString fetchImageToPreview(const QString &p_text);
  58. HGMarkdownHighlighter *m_mdHighlighter;
  59. QVector<QString> m_insertedImages;
  60. QVector<QString> m_initImages;
  61. bool m_expandTab;
  62. QString m_tabSpaces;
  63. QVector<VHeader> m_headers;
  64. };
  65. #endif // VMDEDIT_H