vmdedit.h 3.1 KB

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