From cae97cb062b7eef5546c3213641c3e67f6960638 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 25 Jan 2023 19:54:03 +0300 Subject: [PATCH] Replaced gradient stops with gradient colors in EmojiUserpic of builder. --- .../info_userpic_emoji_builder_preview.cpp | 38 +++++++------------ .../info_userpic_emoji_builder_preview.h | 10 ++--- .../info_userpic_emoji_builder_widget.cpp | 9 ++++- 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_preview.cpp b/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_preview.cpp index 1aa4f9813b..40b110e694 100644 --- a/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_preview.cpp +++ b/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_preview.cpp @@ -12,8 +12,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_document_media.h" #include "data/data_session.h" #include "history/view/media/history_view_sticker_player.h" +#include "info/userpic/info_userpic_emoji_builder_common.h" #include "main/main_session.h" -#include "ui/painter.h" #include "ui/rect.h" namespace UserpicBuilder { @@ -78,11 +78,8 @@ void PreviewPainter::setDocument( }, _lifetime); } -void PreviewPainter::paintBackground(QPainter &p, const QBrush &brush) { - PainterHighQualityEnabler hq(p); - p.setPen(Qt::NoPen); - p.setBrush(brush); - p.drawEllipse(0, 0, _size, _size); +void PreviewPainter::paintBackground(QPainter &p, const QImage &image) { + p.drawImage(0, 0, image); } bool PreviewPainter::paintForeground(QPainter &p) { @@ -126,15 +123,10 @@ void EmojiUserpic::setDocument(not_null document) { } void EmojiUserpic::result(int size, Fn done) { - const auto colors = ranges::views::all( - _stops - ) | ranges::views::transform([](const QGradientStop &stop) { - return stop.second; - }) | ranges::to_vector; const auto painter = lifetime().make_state(size); // Reset to the first frame. painter->setDocument(_painter.document(), [=] { - auto background = Images::GenerateLinearGradient(Size(size), colors); + auto background = GenerateGradient(Size(size), _colors, false); auto p = QPainter(&background); while (true) { @@ -146,20 +138,16 @@ void EmojiUserpic::result(int size, Fn done) { }); } -void EmojiUserpic::setGradientStops(QGradientStops stops) { - if (_stops == stops) { +void EmojiUserpic::setGradientColors(std::vector colors) { + if (_colors == colors) { return; } - if (!_stops.empty()) { - auto gradient = QLinearGradient(0, 0, width() / 2., height()); - gradient.setStops(base::take(_stops)); - _previousBrush = QBrush(std::move(gradient)); + if (const auto colors = base::take(_colors); !colors.empty()) { + _previousImage = GenerateGradient(size(), colors); } - _stops = std::move(stops); + _colors = std::move(colors); { - auto gradient = QLinearGradient(0, 0, width() / 2., height()); - gradient.setStops(_stops); - _brush = QBrush(std::move(gradient)); + _image = GenerateGradient(size(), _colors); } if (_duration) { _animation.stop(); @@ -172,13 +160,13 @@ void EmojiUserpic::setGradientStops(QGradientStops stops) { void EmojiUserpic::paintEvent(QPaintEvent *event) { auto p = QPainter(this); - if (_animation.animating() && (_previousBrush != Qt::NoBrush)) { - _painter.paintBackground(p, _previousBrush); + if (_animation.animating() && !_previousImage.isNull()) { + _painter.paintBackground(p, _previousImage); p.setOpacity(_animation.value(1.)); } - _painter.paintBackground(p, _brush); + _painter.paintBackground(p, _image); p.setOpacity(1.); _painter.paintForeground(p); diff --git a/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_preview.h b/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_preview.h index 371e0109c7..4ceaa1dcee 100644 --- a/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_preview.h +++ b/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_preview.h @@ -33,7 +33,7 @@ public: not_null document, Fn updateCallback); - void paintBackground(QPainter &p, const QBrush &brush); + void paintBackground(QPainter &p, const QImage &image); bool paintForeground(QPainter &p); private: @@ -53,7 +53,7 @@ public: EmojiUserpic(not_null parent, const QSize &size); void result(int size, Fn done); - void setGradientStops(QGradientStops stops); + void setGradientColors(std::vector colors); void setDocument(not_null document); void setDuration(crl::time duration); @@ -63,9 +63,9 @@ protected: private: PreviewPainter _painter; - QBrush _previousBrush; - QBrush _brush; - QGradientStops _stops; + QImage _previousImage; + QImage _image; + std::vector _colors; crl::time _duration; Ui::Animations::Simple _animation; diff --git a/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_widget.cpp b/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_widget.cpp index ea95c88dc7..8d8e3f25a6 100644 --- a/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_widget.cpp +++ b/Telegram/SourceFiles/info/userpic/info_userpic_emoji_builder_widget.cpp @@ -277,7 +277,12 @@ not_null CreateUserpicBuilder( data.builderColorIndex); palette->stopsValue( ) | rpl::start_with_next([=](QGradientStops stops) { - preview->setGradientStops(std::move(stops)); + const auto colors = ranges::views::all( + stops + ) | ranges::views::transform([](const QGradientStop &stop) { + return stop.second; + }) | ranges::to_vector; + preview->setGradientColors(colors); }, preview->lifetime()); paletteBg->innerRectValue( ) | rpl::start_with_next([=](const QRect &r) { @@ -339,7 +344,7 @@ not_null CreateEmojiUserpic( ) | rpl::start_with_next([=](int index) { const auto c = Ui::EmptyUserpic::UserpicColor( Ui::EmptyUserpic::ColorIndex(index)); - widget->setGradientStops({ { 0, c.color1->c }, { 1, c.color2->c } }); + widget->setGradientColors({ c.color1->c, c.color2->c }); }, widget->lifetime()); return widget; }