profile_block_peer_list.h 3.4 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 "profile/profile_block_widget.h"
  9. #include "ui/userpic_view.h"
  10. namespace Ui {
  11. class RippleAnimation;
  12. class PopupMenu;
  13. } // namespace Ui
  14. namespace style {
  15. struct PeerListItem;
  16. } // namespace style
  17. namespace Profile {
  18. class PeerListWidget : public BlockWidget {
  19. public:
  20. PeerListWidget(QWidget *parent, PeerData *peer, const QString &title, const style::PeerListItem &st, const QString &removeText);
  21. struct Item {
  22. explicit Item(not_null<PeerData*> peer);
  23. ~Item();
  24. const not_null<PeerData*> peer;
  25. Ui::PeerUserpicView userpic;
  26. Ui::Text::String name;
  27. QString statusText;
  28. bool statusHasOnlineColor = false;
  29. std::optional<QString> rank;
  30. int rankWidth = 0;
  31. bool hasRemoveLink = false;
  32. std::unique_ptr<Ui::RippleAnimation> ripple;
  33. };
  34. int getListTop() const {
  35. return contentTop();
  36. }
  37. int getListLeft() const;
  38. const std::vector<Item*> &items() const {
  39. return _items;
  40. }
  41. int itemsCount() const {
  42. return _items.size();
  43. }
  44. // Does not take ownership of item.
  45. void addItem(Item *item) {
  46. if (!item) return;
  47. _items.push_back(item);
  48. }
  49. void clearItems() {
  50. _items.clear();
  51. }
  52. void reserveItemsForSize(int size) {
  53. _items.reserve(size);
  54. }
  55. template <typename Predicate>
  56. void sortItems(Predicate predicate) {
  57. std::sort(_items.begin(), _items.end(), std::move(predicate));
  58. }
  59. void setPreloadMoreCallback(Fn<void()> callback) {
  60. _preloadMoreCallback = std::move(callback);
  61. }
  62. void setSelectedCallback(Fn<void(PeerData*)> callback) {
  63. _selectedCallback = std::move(callback);
  64. }
  65. void setRemovedCallback(Fn<void(PeerData*)> callback) {
  66. _removedCallback = std::move(callback);
  67. }
  68. void setUpdateItemCallback(Fn<void(Item*)> callback) {
  69. _updateItemCallback = std::move(callback);
  70. }
  71. protected:
  72. int resizeGetHeight(int newWidth) override;
  73. void visibleTopBottomUpdated(
  74. int visibleTop,
  75. int visibleBottom) override;
  76. void paintItemRect(Painter &p, int x, int y, int w, int h) const;
  77. void refreshVisibility();
  78. void paintContents(Painter &p) override;
  79. void mouseMoveEvent(QMouseEvent *e) override;
  80. void mousePressEvent(QMouseEvent *e) override;
  81. void mouseReleaseEvent(QMouseEvent *e) override;
  82. void enterEventHook(QEnterEvent *e) override;
  83. void enterFromChildEvent(QEvent *e, QWidget *child) override {
  84. enterEventHook(nullptr);
  85. }
  86. void leaveEventHook(QEvent *e) override;
  87. void leaveToChildEvent(QEvent *e, QWidget *child) override {
  88. leaveEventHook(e);
  89. }
  90. private:
  91. void mousePressReleased(Qt::MouseButton button);
  92. void updateSelection();
  93. void setSelected(int selected, bool selectedRemove);
  94. void repaintSelectedRow();
  95. void repaintRow(int index);
  96. void preloadPhotos();
  97. int rowWidth() const;
  98. void paintItem(Painter &p, int x, int y, Item *item, bool selected, bool selectedRemove);
  99. const style::PeerListItem &_st;
  100. Fn<void()> _preloadMoreCallback;
  101. Fn<void(PeerData*)> _selectedCallback;
  102. Fn<void(PeerData*)> _removedCallback;
  103. Fn<void(Item*)> _updateItemCallback;
  104. std::vector<Item*> _items;
  105. int _visibleTop = 0;
  106. int _visibleBottom = 0;
  107. int _selected = -1;
  108. int _pressed = -1;
  109. Qt::MouseButton _pressButton = Qt::LeftButton;
  110. bool _selectedRemove = false;
  111. bool _pressedRemove = false;
  112. QPoint _mousePosition;
  113. QString _removeText;
  114. int _removeWidth = 0;
  115. };
  116. } // namespace Profile