vimagepreviewer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef VIMAGEPREVIEWER_H
  2. #define VIMAGEPREVIEWER_H
  3. #include <QObject>
  4. #include <QString>
  5. #include <QTextBlock>
  6. #include <QHash>
  7. class VMdEdit;
  8. class QTimer;
  9. class QTextDocument;
  10. class VFile;
  11. class VDownloader;
  12. class VImagePreviewer : public QObject
  13. {
  14. Q_OBJECT
  15. public:
  16. explicit VImagePreviewer(VMdEdit *p_edit, int p_timeToPreview);
  17. void disableImagePreview();
  18. void enableImagePreview();
  19. bool isPreviewEnabled();
  20. bool isImagePreviewBlock(const QTextBlock &p_block);
  21. QImage fetchCachedImageFromPreviewBlock(QTextBlock &p_block);
  22. // Clear the m_imageCache and all the preview blocks.
  23. // Then re-preview all the blocks.
  24. void refresh();
  25. void update();
  26. private slots:
  27. void timerTimeout();
  28. void handleContentChange(int p_position, int p_charsRemoved, int p_charsAdded);
  29. void imageDownloaded(const QByteArray &p_data, const QString &p_url);
  30. private:
  31. struct ImageInfo
  32. {
  33. ImageInfo(const QString &p_name, int p_width)
  34. : m_name(p_name), m_width(p_width)
  35. {
  36. }
  37. QString m_name;
  38. int m_width;
  39. };
  40. void previewImages();
  41. bool isValidImagePreviewBlock(QTextBlock &p_block);
  42. // Fetch the image link's URL if there is only one link.
  43. QString fetchImageUrlToPreview(const QString &p_text);
  44. // Fetch teh image's full path if there is only one image link.
  45. QString fetchImagePathToPreview(const QString &p_text);
  46. // Try to preview the image of @p_block.
  47. // Return the next block to process.
  48. QTextBlock previewImageOfOneBlock(QTextBlock &p_block);
  49. // Insert a new block to preview image.
  50. QTextBlock insertImagePreviewBlock(QTextBlock &p_block, const QString &p_imagePath);
  51. // @p_block is the image block. Update it to preview @p_imagePath.
  52. void updateImagePreviewBlock(QTextBlock &p_block, const QString &p_imagePath);
  53. void removeBlock(QTextBlock &p_block);
  54. // Corrupted image preview block: ObjectReplacementCharacter mixed with other
  55. // non-space characters.
  56. // Remove the ObjectReplacementCharacter chars.
  57. void clearCorruptedImagePreviewBlock(QTextBlock &p_block);
  58. void clearAllImagePreviewBlocks();
  59. QTextImageFormat fetchFormatFromPreviewBlock(QTextBlock &p_block);
  60. QString fetchImagePathFromPreviewBlock(QTextBlock &p_block);
  61. void updateFormatInPreviewBlock(QTextBlock &p_block,
  62. const QTextImageFormat &p_format);
  63. // Look up m_imageCache to get the resource name in QTextDocument's cache.
  64. // If there is none, insert it.
  65. QString imageCacheResourceName(const QString &p_imagePath);
  66. QString imagePathToCacheResourceName(const QString &p_imagePath);
  67. // Return true if and only if there is update.
  68. bool updateImageWidth(QTextImageFormat &p_format);
  69. VMdEdit *m_edit;
  70. QTextDocument *m_document;
  71. VFile *m_file;
  72. QTimer *m_timer;
  73. bool m_enablePreview;
  74. bool m_isPreviewing;
  75. bool m_requestCearBlocks;
  76. bool m_requestRefreshBlocks;
  77. bool m_updatePending;
  78. // Map from image full path to QUrl identifier in the QTextDocument's cache.
  79. QHash<QString, ImageInfo> m_imageCache;;
  80. VDownloader *m_downloader;
  81. // The preview width.
  82. int m_imageWidth;
  83. static const int c_minImageWidth;
  84. };
  85. #endif // VIMAGEPREVIEWER_H