attach_album_preview.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "ui/rp_widget.h"
  9. #include "ui/chat/attach/attach_send_files_way.h"
  10. #include "base/timer.h"
  11. namespace style {
  12. struct ComposeControls;
  13. } // namespace style
  14. namespace Ui {
  15. struct PreparedFile;
  16. struct GroupMediaLayout;
  17. class AlbumThumbnail;
  18. class PopupMenu;
  19. class AlbumPreview final : public RpWidget {
  20. public:
  21. AlbumPreview(
  22. QWidget *parent,
  23. const style::ComposeControls &st,
  24. gsl::span<Ui::PreparedFile> items,
  25. SendFilesWay way,
  26. Fn<bool(int, AttachActionType)> actionAllowed);
  27. ~AlbumPreview();
  28. void setSendWay(SendFilesWay way);
  29. [[nodiscard]] base::flat_set<int> collectSpoileredIndices();
  30. [[nodiscard]] bool canHaveSpoiler(int index) const;
  31. void toggleSpoilers(bool enabled);
  32. [[nodiscard]] std::vector<int> takeOrder();
  33. [[nodiscard]] rpl::producer<int> thumbDeleted() const {
  34. return _thumbDeleted.events();
  35. }
  36. [[nodiscard]] rpl::producer<int> thumbChanged() const {
  37. return _thumbChanged.events();
  38. }
  39. [[nodiscard]] rpl::producer<int> thumbModified() const {
  40. return _thumbModified.events();
  41. }
  42. [[nodiscard]] rpl::producer<int> thumbEditCoverRequested() const {
  43. return _thumbEditCoverRequested.events();
  44. }
  45. [[nodiscard]] rpl::producer<int> thumbClearCoverRequested() const {
  46. return _thumbClearCoverRequested.events();
  47. }
  48. [[nodiscard]] rpl::producer<> orderUpdated() const {
  49. return _orderUpdated.events();
  50. }
  51. [[nodiscard]] QImage generatePriceTagBackground() const;
  52. protected:
  53. void paintEvent(QPaintEvent *e) override;
  54. void mousePressEvent(QMouseEvent *e) override;
  55. void mouseMoveEvent(QMouseEvent *e) override;
  56. void mouseReleaseEvent(QMouseEvent *e) override;
  57. private:
  58. int countLayoutHeight(
  59. const std::vector<GroupMediaLayout> &layout) const;
  60. std::vector<GroupMediaLayout> generateOrderedLayout() const;
  61. std::vector<int> defaultOrder(int count = -1) const;
  62. void prepareThumbs(gsl::span<Ui::PreparedFile> items);
  63. void updateSizeAnimated(const std::vector<GroupMediaLayout> &layout);
  64. void updateSize();
  65. void updateFileRows();
  66. AlbumThumbnail *thumbUnderCursor();
  67. void deleteThumbByIndex(int index);
  68. void changeThumbByIndex(int index);
  69. void modifyThumbByIndex(int index);
  70. void thumbButtonsCallback(
  71. not_null<AlbumThumbnail*> thumb,
  72. AttachButtonType type);
  73. void switchToDrag();
  74. void paintAlbum(Painter &p) const;
  75. void paintPhotos(Painter &p, QRect clip) const;
  76. void paintFiles(Painter &p, QRect clip) const;
  77. void applyCursor(style::cursor cursor);
  78. int contentLeft() const;
  79. int contentTop() const;
  80. AlbumThumbnail *findThumb(QPoint position) const;
  81. not_null<AlbumThumbnail*> findClosestThumb(QPoint position) const;
  82. void updateSuggestedDrag(QPoint position);
  83. int orderIndex(not_null<AlbumThumbnail*> thumb) const;
  84. void cancelDrag();
  85. void finishDrag();
  86. void showContextMenu(not_null<AlbumThumbnail*> thumb, QPoint position);
  87. const style::ComposeControls &_st;
  88. SendFilesWay _sendWay;
  89. Fn<bool(int, AttachActionType)> _actionAllowed;
  90. style::cursor _cursor = style::cur_default;
  91. std::vector<int> _order;
  92. std::vector<QSize> _itemsShownDimensions;
  93. std::vector<std::unique_ptr<AlbumThumbnail>> _thumbs;
  94. int _thumbsHeight = 0;
  95. int _photosHeight = 0;
  96. int _filesHeight = 0;
  97. bool _hasMixedFileHeights = false;
  98. AlbumThumbnail *_draggedThumb = nullptr;
  99. AlbumThumbnail *_suggestedThumb = nullptr;
  100. AlbumThumbnail *_paintedAbove = nullptr;
  101. AlbumThumbnail *_pressedThumb = nullptr;
  102. QPoint _draggedStartPosition;
  103. base::Timer _dragTimer;
  104. AttachButtonType _pressedButtonType = AttachButtonType::None;
  105. rpl::event_stream<int> _thumbDeleted;
  106. rpl::event_stream<int> _thumbChanged;
  107. rpl::event_stream<int> _thumbModified;
  108. rpl::event_stream<int> _thumbEditCoverRequested;
  109. rpl::event_stream<int> _thumbClearCoverRequested;
  110. rpl::event_stream<> _orderUpdated;
  111. base::unique_qptr<PopupMenu> _menu;
  112. mutable Animations::Simple _thumbsHeightAnimation;
  113. mutable Animations::Simple _shrinkAnimation;
  114. mutable Animations::Simple _finishDragAnimation;
  115. };
  116. } // namespace Ui