info_statistics_widget.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include "info/statistics/info_statistics_widget.h"
  8. #include "data/data_session.h"
  9. #include "data/data_stories.h"
  10. #include "info/info_controller.h"
  11. #include "info/info_memento.h"
  12. #include "info/statistics/info_statistics_inner_widget.h"
  13. #include "lang/lang_keys.h"
  14. #include "main/main_session.h"
  15. #include "ui/ui_utility.h"
  16. namespace Info::Statistics {
  17. Memento::Memento(not_null<Controller*> controller)
  18. : ContentMemento(controller->statisticsTag()) {
  19. }
  20. Memento::Memento(not_null<PeerData*> peer, FullMsgId contextId)
  21. : ContentMemento(Tag{ peer, contextId, {} }) {
  22. }
  23. Memento::Memento(not_null<PeerData*> peer, FullStoryId storyId)
  24. : ContentMemento(Tag{ peer, {}, storyId }) {
  25. }
  26. Memento::~Memento() = default;
  27. Section Memento::section() const {
  28. return Section(Section::Type::Statistics);
  29. }
  30. void Memento::setState(SavedState state) {
  31. _state = std::move(state);
  32. }
  33. SavedState Memento::state() {
  34. return base::take(_state);
  35. }
  36. object_ptr<ContentWidget> Memento::createWidget(
  37. QWidget *parent,
  38. not_null<Controller*> controller,
  39. const QRect &geometry) {
  40. auto result = object_ptr<Widget>(parent, controller);
  41. result->setInternalState(geometry, this);
  42. return result;
  43. }
  44. Widget::Widget(
  45. QWidget *parent,
  46. not_null<Controller*> controller)
  47. : ContentWidget(parent, controller)
  48. , _inner(setInnerWidget(
  49. object_ptr<InnerWidget>(
  50. this,
  51. controller,
  52. controller->statisticsTag().peer,
  53. controller->statisticsTag().contextId,
  54. controller->statisticsTag().storyId))) {
  55. _inner->showRequests(
  56. ) | rpl::start_with_next([=](InnerWidget::ShowRequest request) {
  57. if (request.history) {
  58. controller->showPeerHistory(
  59. request.history.peer,
  60. Window::SectionShow::Way::Forward,
  61. request.history.msg);
  62. } else if (request.info) {
  63. controller->showPeerInfo(request.info);
  64. } else if (request.messageStatistic || request.storyStatistic) {
  65. controller->showSection(Make(
  66. controller->statisticsTag().peer,
  67. request.messageStatistic,
  68. request.storyStatistic));
  69. } else if (const auto &s = request.story) {
  70. if (const auto peer = controller->session().data().peer(s.peer)) {
  71. controller->parentController()->openPeerStory(
  72. peer,
  73. s.story,
  74. { Data::StoriesContextSingle() });
  75. }
  76. }
  77. }, _inner->lifetime());
  78. _inner->scrollToRequests(
  79. ) | rpl::start_with_next([this](const Ui::ScrollToRequest &request) {
  80. scrollTo(request);
  81. }, _inner->lifetime());
  82. }
  83. bool Widget::showInternal(not_null<ContentMemento*> memento) {
  84. return false;
  85. }
  86. rpl::producer<QString> Widget::title() {
  87. return controller()->statisticsTag().contextId
  88. ? tr::lng_stats_message_title()
  89. : controller()->statisticsTag().storyId
  90. ? tr::lng_stats_story_title()
  91. : tr::lng_stats_title();
  92. }
  93. void Widget::setInternalState(
  94. const QRect &geometry,
  95. not_null<Memento*> memento) {
  96. setGeometry(geometry);
  97. Ui::SendPendingMoveResizeEvents(this);
  98. restoreState(memento);
  99. }
  100. rpl::producer<bool> Widget::desiredShadowVisibility() const {
  101. return rpl::single<bool>(true);
  102. }
  103. void Widget::showFinished() {
  104. _inner->showFinished();
  105. }
  106. std::shared_ptr<ContentMemento> Widget::doCreateMemento() {
  107. auto result = std::make_shared<Memento>(controller());
  108. saveState(result.get());
  109. return result;
  110. }
  111. void Widget::saveState(not_null<Memento*> memento) {
  112. memento->setScrollTop(scrollTopSave());
  113. _inner->saveState(memento);
  114. }
  115. void Widget::restoreState(not_null<Memento*> memento) {
  116. _inner->restoreState(memento);
  117. scrollTopRestore(memento->scrollTop());
  118. }
  119. std::shared_ptr<Info::Memento> Make(
  120. not_null<PeerData*> peer,
  121. FullMsgId contextId,
  122. FullStoryId storyId) {
  123. const auto memento = storyId
  124. ? std::make_shared<Memento>(peer, storyId)
  125. : std::make_shared<Memento>(peer, contextId);
  126. return std::make_shared<Info::Memento>(
  127. std::vector<std::shared_ptr<ContentMemento>>(1, std::move(memento)));
  128. }
  129. } // namespace Info::Statistics