2021-02-23 15:35:40 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
2021-03-21 12:38:33 +00:00
|
|
|
#include "editor/scene/scene_item_sticker.h"
|
2021-02-23 15:35:40 +00:00
|
|
|
|
|
|
|
#include "chat_helpers/stickers_lottie.h"
|
|
|
|
#include "data/data_document.h"
|
|
|
|
#include "data/data_document_media.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "lottie/lottie_common.h"
|
|
|
|
#include "lottie/lottie_single_player.h"
|
|
|
|
#include "main/main_session.h"
|
2021-05-07 14:33:53 +00:00
|
|
|
#include "ui/ui_utility.h"
|
2021-02-23 15:35:40 +00:00
|
|
|
#include "styles/style_editor.h"
|
|
|
|
|
|
|
|
namespace Editor {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ItemSticker::ItemSticker(
|
|
|
|
not_null<DocumentData*> document,
|
2021-07-04 12:57:07 +00:00
|
|
|
ItemBase::Data data)
|
|
|
|
: ItemBase(std::move(data))
|
2021-02-23 15:35:40 +00:00
|
|
|
, _document(document)
|
2021-03-10 07:45:42 +00:00
|
|
|
, _mediaView(_document->createMediaView()) {
|
2021-02-23 15:35:40 +00:00
|
|
|
const auto stickerData = document->sticker();
|
|
|
|
if (!stickerData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto updateThumbnail = [=] {
|
2021-03-09 15:12:23 +00:00
|
|
|
const auto guard = gsl::finally([&] {
|
2022-01-25 11:26:19 +00:00
|
|
|
if (_pixmap.isNull()) {
|
|
|
|
setAspectRatio(1.);
|
|
|
|
}
|
2021-03-09 15:12:23 +00:00
|
|
|
});
|
2022-01-19 14:45:51 +00:00
|
|
|
if (stickerData->isLottie()) {
|
2021-02-23 15:35:40 +00:00
|
|
|
_lottie.player = ChatHelpers::LottiePlayerFromDocument(
|
|
|
|
_mediaView.get(),
|
|
|
|
ChatHelpers::StickerLottieSize::MessageHistory,
|
|
|
|
QSize(kStickerSideSize, kStickerSideSize)
|
|
|
|
* cIntRetinaFactor(),
|
|
|
|
Lottie::Quality::High);
|
|
|
|
_lottie.player->updates(
|
|
|
|
) | rpl::start_with_next([=] {
|
2021-05-07 14:33:53 +00:00
|
|
|
updatePixmap(Ui::PixmapFromImage(
|
2021-03-21 17:00:56 +00:00
|
|
|
_lottie.player->frame()));
|
2021-02-23 15:35:40 +00:00
|
|
|
_lottie.player = nullptr;
|
|
|
|
_lottie.lifetime.destroy();
|
|
|
|
update();
|
|
|
|
}, _lottie.lifetime);
|
|
|
|
return true;
|
2022-01-25 11:26:19 +00:00
|
|
|
} else if (stickerData->isWebm()
|
|
|
|
&& !_document->dimensions.isEmpty()) {
|
|
|
|
const auto callback = [=](::Media::Clip::Notification) {
|
|
|
|
const auto size = _document->dimensions;
|
|
|
|
if (_webm && _webm->ready() && !_webm->started()) {
|
|
|
|
_webm->start({ .frame = size, .keepAlpha = true });
|
|
|
|
}
|
|
|
|
if (_webm && _webm->started()) {
|
|
|
|
updatePixmap(_webm->current(
|
|
|
|
{ .frame = size, .keepAlpha = true },
|
|
|
|
0));
|
|
|
|
_webm = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
_webm = ::Media::Clip::MakeReader(
|
|
|
|
_mediaView->owner()->location(),
|
|
|
|
_mediaView->bytes(),
|
|
|
|
callback);
|
|
|
|
return true;
|
2021-02-23 15:35:40 +00:00
|
|
|
}
|
|
|
|
const auto sticker = _mediaView->getStickerLarge();
|
|
|
|
if (!sticker) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-21 12:31:39 +00:00
|
|
|
const auto ratio = style::DevicePixelRatio();
|
|
|
|
auto pixmap = sticker->pixNoCache(sticker->size() * ratio);
|
|
|
|
pixmap.setDevicePixelRatio(ratio);
|
2021-03-21 17:00:56 +00:00
|
|
|
updatePixmap(std::move(pixmap));
|
2021-02-23 15:35:40 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
if (!updateThumbnail()) {
|
|
|
|
_document->owner().session().downloaderTaskFinished(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
if (updateThumbnail()) {
|
|
|
|
_loadingLifetime.destroy();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}, _loadingLifetime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:00:56 +00:00
|
|
|
void ItemSticker::updatePixmap(QPixmap &&pixmap) {
|
|
|
|
_pixmap = std::move(pixmap);
|
|
|
|
if (flipped()) {
|
|
|
|
performFlip();
|
|
|
|
} else {
|
|
|
|
update();
|
|
|
|
}
|
2022-01-25 11:26:19 +00:00
|
|
|
if (!_pixmap.isNull()) {
|
|
|
|
setAspectRatio(_pixmap.height() / float64(_pixmap.width()));
|
|
|
|
}
|
2021-03-21 17:00:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 15:35:40 +00:00
|
|
|
void ItemSticker::paint(
|
|
|
|
QPainter *p,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
QWidget *w) {
|
2021-03-10 07:45:42 +00:00
|
|
|
p->drawPixmap(contentRect().toRect(), _pixmap);
|
2021-02-23 15:35:40 +00:00
|
|
|
ItemBase::paint(p, option, w);
|
|
|
|
}
|
|
|
|
|
2021-10-04 04:46:12 +00:00
|
|
|
not_null<DocumentData*> ItemSticker::sticker() const {
|
|
|
|
return _document;
|
2021-03-14 09:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ItemSticker::type() const {
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
2021-03-10 17:50:35 +00:00
|
|
|
void ItemSticker::performFlip() {
|
|
|
|
_pixmap = _pixmap.transformed(QTransform().scale(-1, 1));
|
2021-03-21 17:00:56 +00:00
|
|
|
update();
|
2021-03-10 17:50:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 12:57:07 +00:00
|
|
|
std::shared_ptr<ItemBase> ItemSticker::duplicate(ItemBase::Data data) const {
|
|
|
|
return std::make_shared<ItemSticker>(_document, std::move(data));
|
2021-03-11 10:20:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 15:35:40 +00:00
|
|
|
} // namespace Editor
|