scene_item_sticker.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "editor/scene/scene_item_sticker.h"
  8. #include "chat_helpers/stickers_lottie.h"
  9. #include "data/data_document.h"
  10. #include "data/data_document_media.h"
  11. #include "data/data_session.h"
  12. #include "lottie/lottie_common.h"
  13. #include "lottie/lottie_single_player.h"
  14. #include "main/main_session.h"
  15. #include "ui/ui_utility.h"
  16. #include "styles/style_editor.h"
  17. namespace Editor {
  18. namespace {
  19. } // namespace
  20. ItemSticker::ItemSticker(
  21. not_null<DocumentData*> document,
  22. ItemBase::Data data)
  23. : ItemBase(std::move(data))
  24. , _document(document)
  25. , _mediaView(_document->createMediaView()) {
  26. const auto stickerData = document->sticker();
  27. if (!stickerData) {
  28. return;
  29. }
  30. const auto updateThumbnail = [=] {
  31. const auto guard = gsl::finally([&] {
  32. if (_image.isNull()) {
  33. setAspectRatio(1.);
  34. }
  35. });
  36. if (stickerData->isLottie()) {
  37. _lottie.player = ChatHelpers::LottiePlayerFromDocument(
  38. _mediaView.get(),
  39. ChatHelpers::StickerLottieSize::MessageHistory,
  40. QSize(kStickerSideSize, kStickerSideSize)
  41. * style::DevicePixelRatio(),
  42. Lottie::Quality::High);
  43. _lottie.player->updates(
  44. ) | rpl::start_with_next([=] {
  45. updatePixmap(_lottie.player->frame());
  46. _lottie.player = nullptr;
  47. _lottie.lifetime.destroy();
  48. update();
  49. }, _lottie.lifetime);
  50. return true;
  51. } else if (stickerData->isWebm()
  52. && !_document->dimensions.isEmpty()) {
  53. const auto callback = [=](::Media::Clip::Notification) {
  54. const auto size = _document->dimensions;
  55. if (_webm && _webm->ready() && !_webm->started()) {
  56. _webm->start({ .frame = size, .keepAlpha = true });
  57. }
  58. if (_webm && _webm->started()) {
  59. updatePixmap(_webm->current(
  60. { .frame = size, .keepAlpha = true },
  61. 0));
  62. _webm = nullptr;
  63. }
  64. };
  65. _webm = ::Media::Clip::MakeReader(
  66. _mediaView->owner()->location(),
  67. _mediaView->bytes(),
  68. callback);
  69. return true;
  70. }
  71. const auto sticker = _mediaView->getStickerLarge();
  72. if (!sticker) {
  73. return false;
  74. }
  75. const auto ratio = style::DevicePixelRatio();
  76. auto pixmap = sticker->pixNoCache(sticker->size() * ratio);
  77. pixmap.setDevicePixelRatio(ratio);
  78. updatePixmap(pixmap.toImage());
  79. return true;
  80. };
  81. if (!updateThumbnail()) {
  82. _document->owner().session().downloaderTaskFinished(
  83. ) | rpl::start_with_next([=] {
  84. if (updateThumbnail()) {
  85. _loadingLifetime.destroy();
  86. update();
  87. }
  88. }, _loadingLifetime);
  89. }
  90. }
  91. void ItemSticker::updatePixmap(QImage &&image) {
  92. _image = std::move(image);
  93. if (flipped()) {
  94. performFlip();
  95. } else {
  96. update();
  97. }
  98. if (!_image.isNull()) {
  99. setAspectRatio(_image.height() / float64(_image.width()));
  100. }
  101. }
  102. void ItemSticker::paint(
  103. QPainter *p,
  104. const QStyleOptionGraphicsItem *option,
  105. QWidget *w) {
  106. const auto rect = contentRect();
  107. const auto imageSize = QSizeF(_image.size() / style::DevicePixelRatio())
  108. .scaled(rect.size(), Qt::KeepAspectRatio);
  109. const auto resultRect = QRectF(rect.topLeft(), imageSize).translated(
  110. (rect.width() - imageSize.width()) / 2.,
  111. (rect.height() - imageSize.height()) / 2.);
  112. p->drawImage(resultRect, _image);
  113. ItemBase::paint(p, option, w);
  114. }
  115. not_null<DocumentData*> ItemSticker::sticker() const {
  116. return _document;
  117. }
  118. int ItemSticker::type() const {
  119. return Type;
  120. }
  121. void ItemSticker::performFlip() {
  122. _image = _image.transformed(QTransform().scale(-1, 1));
  123. update();
  124. }
  125. std::shared_ptr<ItemBase> ItemSticker::duplicate(ItemBase::Data data) const {
  126. return std::make_shared<ItemSticker>(_document, std::move(data));
  127. }
  128. } // namespace Editor