info_media_widget.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/media/info_media_widget.h"
  8. #include "info/media/info_media_inner_widget.h"
  9. #include "info/info_controller.h"
  10. #include "main/main_session.h"
  11. #include "ui/widgets/scroll_area.h"
  12. #include "ui/search_field_controller.h"
  13. #include "ui/ui_utility.h"
  14. #include "data/data_peer.h"
  15. #include "data/data_user.h"
  16. #include "data/data_channel.h"
  17. #include "data/data_forum_topic.h"
  18. #include "lang/lang_keys.h"
  19. #include "styles/style_info.h"
  20. namespace Info::Media {
  21. std::optional<int> TypeToTabIndex(Type type) {
  22. switch (type) {
  23. case Type::Photo: return 0;
  24. case Type::Video: return 1;
  25. case Type::File: return 2;
  26. }
  27. return std::nullopt;
  28. }
  29. Type TabIndexToType(int index) {
  30. switch (index) {
  31. case 0: return Type::Photo;
  32. case 1: return Type::Video;
  33. case 2: return Type::File;
  34. }
  35. Unexpected("Index in Info::Media::TabIndexToType()");
  36. }
  37. Memento::Memento(not_null<Controller*> controller)
  38. : Memento(
  39. (controller->peer()
  40. ? controller->peer()
  41. : controller->storiesPeer()
  42. ? controller->storiesPeer()
  43. : controller->parentController()->session().user()),
  44. controller->topic(),
  45. controller->migratedPeerId(),
  46. (controller->section().type() == Section::Type::Downloads
  47. ? Type::File
  48. : controller->section().type() == Section::Type::Stories
  49. ? Type::PhotoVideo
  50. : controller->section().mediaType())) {
  51. }
  52. Memento::Memento(not_null<PeerData*> peer, PeerId migratedPeerId, Type type)
  53. : Memento(peer, nullptr, migratedPeerId, type) {
  54. }
  55. Memento::Memento(not_null<Data::ForumTopic*> topic, Type type)
  56. : Memento(topic->channel(), topic, PeerId(), type) {
  57. }
  58. Memento::Memento(
  59. not_null<PeerData*> peer,
  60. Data::ForumTopic *topic,
  61. PeerId migratedPeerId,
  62. Type type)
  63. : ContentMemento(peer, topic, migratedPeerId)
  64. , _type(type) {
  65. _searchState.query.type = type;
  66. _searchState.query.peerId = peer->id;
  67. _searchState.query.topicRootId = topic ? topic->rootId() : 0;
  68. _searchState.query.migratedPeerId = migratedPeerId;
  69. if (migratedPeerId) {
  70. _searchState.migratedList = Storage::SparseIdsList();
  71. }
  72. }
  73. Section Memento::section() const {
  74. return Section(_type);
  75. }
  76. object_ptr<ContentWidget> Memento::createWidget(
  77. QWidget *parent,
  78. not_null<Controller*> controller,
  79. const QRect &geometry) {
  80. auto result = object_ptr<Widget>(
  81. parent,
  82. controller);
  83. result->setInternalState(geometry, this);
  84. return result;
  85. }
  86. Widget::Widget(QWidget *parent, not_null<Controller*> controller)
  87. : ContentWidget(parent, controller) {
  88. _inner = setInnerWidget(object_ptr<InnerWidget>(
  89. this,
  90. controller));
  91. _inner->setScrollHeightValue(scrollHeightValue());
  92. _inner->scrollToRequests(
  93. ) | rpl::start_with_next([this](Ui::ScrollToRequest request) {
  94. scrollTo(request);
  95. }, _inner->lifetime());
  96. }
  97. rpl::producer<SelectedItems> Widget::selectedListValue() const {
  98. return _inner->selectedListValue();
  99. }
  100. void Widget::selectionAction(SelectionAction action) {
  101. _inner->selectionAction(action);
  102. }
  103. rpl::producer<QString> Widget::title() {
  104. if (controller()->key().peer()->sharedMediaInfo() && isStackBottom()) {
  105. return tr::lng_profile_shared_media();
  106. }
  107. switch (controller()->section().mediaType()) {
  108. case Section::MediaType::Photo:
  109. return tr::lng_media_type_photos();
  110. case Section::MediaType::GIF:
  111. return tr::lng_media_type_gifs();
  112. case Section::MediaType::Video:
  113. return tr::lng_media_type_videos();
  114. case Section::MediaType::MusicFile:
  115. return tr::lng_media_type_songs();
  116. case Section::MediaType::File:
  117. return tr::lng_media_type_files();
  118. case Section::MediaType::RoundVoiceFile:
  119. return tr::lng_media_type_audios();
  120. case Section::MediaType::Link:
  121. return tr::lng_media_type_links();
  122. case Section::MediaType::RoundFile:
  123. return tr::lng_media_type_rounds();
  124. }
  125. Unexpected("Bad media type in Info::TitleValue()");
  126. }
  127. void Widget::setIsStackBottom(bool isStackBottom) {
  128. ContentWidget::setIsStackBottom(isStackBottom);
  129. _inner->setIsStackBottom(isStackBottom);
  130. }
  131. bool Widget::showInternal(not_null<ContentMemento*> memento) {
  132. if (!controller()->validateMementoPeer(memento)) {
  133. return false;
  134. }
  135. if (const auto mediaMemento = dynamic_cast<Memento*>(memento.get())) {
  136. if (_inner->showInternal(mediaMemento)) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. void Widget::setInternalState(
  143. const QRect &geometry,
  144. not_null<Memento*> memento) {
  145. setGeometry(geometry);
  146. Ui::SendPendingMoveResizeEvents(this);
  147. restoreState(memento);
  148. }
  149. std::shared_ptr<ContentMemento> Widget::doCreateMemento() {
  150. auto result = std::make_shared<Memento>(controller());
  151. saveState(result.get());
  152. return result;
  153. }
  154. void Widget::saveState(not_null<Memento*> memento) {
  155. _inner->saveState(memento);
  156. }
  157. void Widget::restoreState(not_null<Memento*> memento) {
  158. _inner->restoreState(memento);
  159. }
  160. } // namespace Info::Media