From 890aacaeeeae15d15fa776f086d9ba1aa8de117c Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 8 Feb 2019 16:43:31 +0300 Subject: [PATCH] Allow to blur wallpapers from file. --- .../boxes/background_preview_box.cpp | 6 ++--- Telegram/SourceFiles/data/data_session.cpp | 19 ++++++++------ .../SourceFiles/settings/settings_chat.cpp | 25 ++++++------------- .../window/themes/window_theme.cpp | 24 +++++++++++++----- .../SourceFiles/window/themes/window_theme.h | 5 ++-- 5 files changed, 42 insertions(+), 37 deletions(-) diff --git a/Telegram/SourceFiles/boxes/background_preview_box.cpp b/Telegram/SourceFiles/boxes/background_preview_box.cpp index 142e9ea94..02008930a 100644 --- a/Telegram/SourceFiles/boxes/background_preview_box.cpp +++ b/Telegram/SourceFiles/boxes/background_preview_box.cpp @@ -511,9 +511,6 @@ void BackgroundPreviewBox::paintEvent(QPaintEvent *e) { void BackgroundPreviewBox::paintImage(Painter &p, TimeMs ms) { Expects(!_scaled.isNull()); - if (_paper.isPattern() && _paper.document() && _full.isNull()) { - return; - } const auto master = _paper.isPattern() ? std::clamp(_paper.patternIntensity() / 100., 0., 1.) : 1.; @@ -695,7 +692,8 @@ std::optional BackgroundPreviewBox::patternBackgroundColor() const { void BackgroundPreviewBox::checkLoadedDocument() { const auto document = _paper.document(); - if (!document + if (!_full.isNull() + || !document || !document->loaded(DocumentData::FilePathResolveChecked) || _generating) { return; diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 3c28aa5bb..43e04c1a3 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -13,6 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/application.h" #include "core/crash_reports.h" // for CrashReports::SetAnnotation #include "ui/image/image.h" +#include "ui/image/image_source.h" // for Images::LocalFileSource #include "export/export_controller.h" #include "export/view/export_view_panel_controller.h" #include "window/notifications_manager.h" @@ -3067,10 +3068,11 @@ void Session::setWallpapers(const QVector &data, int32 hash) { _wallpapers.reserve(data.size() + 2); _wallpapers.push_back(Data::Legacy1DefaultWallPaper()); - _wallpapers.back().setLocalImageAsThumbnail(Images::Create( - qsl(":/gui/art/bg_initial.jpg"), - "JPG" - ).get()); + _wallpapers.back().setLocalImageAsThumbnail(std::make_shared( + std::make_unique( + qsl(":/gui/art/bg_initial.jpg"), + QByteArray(), + "JPG"))); for (const auto &paper : data) { paper.match([&](const MTPDwallPaper &paper) { if (const auto parsed = Data::WallPaper::Create(paper)) { @@ -3083,10 +3085,11 @@ void Session::setWallpapers(const QVector &data, int32 hash) { Data::IsDefaultWallPaper); if (defaultFound == end(_wallpapers)) { _wallpapers.push_back(Data::DefaultWallPaper()); - _wallpapers.back().setLocalImageAsThumbnail(Images::Create( - qsl(":/gui/arg/bg.jpg"), - "JPG" - ).get()); + _wallpapers.back().setLocalImageAsThumbnail(std::make_shared( + std::make_unique( + qsl(":/gui/arg/bg.jpg"), + QByteArray(), + "JPG"))); } } diff --git a/Telegram/SourceFiles/settings/settings_chat.cpp b/Telegram/SourceFiles/settings/settings_chat.cpp index 5fb0938f9..5734bff87 100644 --- a/Telegram/SourceFiles/settings/settings_chat.cpp +++ b/Telegram/SourceFiles/settings/settings_chat.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/auto_download_box.h" #include "boxes/stickers_box.h" #include "boxes/background_box.h" +#include "boxes/background_preview_box.h" #include "boxes/download_path_box.h" #include "boxes/local_storage_box.h" #include "ui/wrap/vertical_layout.h" @@ -22,6 +23,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/effects/radial_animation.h" #include "ui/toast/toast.h" #include "ui/image/image.h" +#include "ui/image/image_source.h" #include "lang/lang_keys.h" #include "window/themes/window_theme_editor.h" #include "window/themes/window_theme.h" @@ -395,24 +397,13 @@ void ChooseFromFile(not_null parent) { : App::readImage(result.remoteContent); if (image.isNull() || image.width() <= 0 || image.height() <= 0) { return; - } else if (image.width() > 4096 * image.height()) { - image = image.copy( - (image.width() - 4096 * image.height()) / 2, - 0, - 4096 * image.height(), - image.height()); - } else if (image.height() > 4096 * image.width()) { - image = image.copy( - 0, - (image.height() - 4096 * image.width()) / 2, - image.width(), - 4096 * image.width()); } - - Window::Theme::Background()->set( - Data::CustomWallPaper(), - std::move(image)); - Window::Theme::Background()->setTile(false); + auto local = Data::CustomWallPaper(); + local.setLocalImageAsThumbnail(std::make_shared( + std::make_unique( + std::move(image), + "JPG"))); + Ui::show(Box(local)); }; FileDialog::GetOpenPath( parent.get(), diff --git a/Telegram/SourceFiles/window/themes/window_theme.cpp b/Telegram/SourceFiles/window/themes/window_theme.cpp index 0fd44db8d..01f8d7680 100644 --- a/Telegram/SourceFiles/window/themes/window_theme.cpp +++ b/Telegram/SourceFiles/window/themes/window_theme.cpp @@ -105,13 +105,13 @@ QString StringFromColor(QColor color) { WallPaper::WallPaper(WallPaperId id) : _id(id) { } -void WallPaper::setLocalImageAsThumbnail(not_null image) { +void WallPaper::setLocalImageAsThumbnail(std::shared_ptr image) { Expects(IsDefaultWallPaper(*this) || IsLegacy1DefaultWallPaper(*this) || IsCustomWallPaper(*this)); Expects(_thumbnail == nullptr); - _thumbnail = image; + _thumbnail = std::move(image); } WallPaperId WallPaper::id() const { @@ -127,7 +127,11 @@ DocumentData *WallPaper::document() const { } Image *WallPaper::thumbnail() const { - return _thumbnail; + return _thumbnail + ? _thumbnail.get() + : _document + ? _document->thumbnail() + : nullptr; } bool WallPaper::isPattern() const { @@ -195,6 +199,9 @@ void WallPaper::loadThumbnail() const { if (_thumbnail) { _thumbnail->load(fileOrigin()); } + if (_document) { + _document->loadThumbnail(fileOrigin()); + } } void WallPaper::loadDocument() const { @@ -288,6 +295,12 @@ WallPaper WallPaper::withParamsFrom(const WallPaper &other) const { return result; } +WallPaper WallPaper::withoutImageData() const { + auto result = *this; + result._thumbnail = nullptr; + return result; +} + std::optional WallPaper::Create(const MTPWallPaper &data) { return data.match([](const MTPDwallPaper &data) { return Create(data); @@ -307,7 +320,6 @@ std::optional WallPaper::Create(const MTPDwallPaper &data) { result._flags = data.vflags.v; result._slug = qs(data.vslug); result._document = document; - result._thumbnail = document->thumbnail(); if (data.has_settings()) { const auto isPattern = ((result._flags & Flag::f_pattern) != 0); data.vsettings.match([&](const MTPDwallPaperSettings &data) { @@ -988,7 +1000,7 @@ void ChatBackground::set(const Data::WallPaper &paper, QImage image) { } } else if (Data::IsDefaultWallPaper(_paper) || (!_paper.backgroundColor() && image.isNull())) { - setPaper(Data::DefaultWallPaper()); + setPaper(Data::DefaultWallPaper().withParamsFrom(_paper)); image.load(qsl(":/gui/art/bg.jpg")); } Local::writeBackground( @@ -1082,7 +1094,7 @@ void ChatBackground::preparePixmaps(QImage image) { } void ChatBackground::setPaper(const Data::WallPaper &paper) { - _paper = paper; + _paper = paper.withoutImageData(); } bool ChatBackground::adjustPaletteRequired() { diff --git a/Telegram/SourceFiles/window/themes/window_theme.h b/Telegram/SourceFiles/window/themes/window_theme.h index 54ec47e1f..9fb49b7ce 100644 --- a/Telegram/SourceFiles/window/themes/window_theme.h +++ b/Telegram/SourceFiles/window/themes/window_theme.h @@ -17,7 +17,7 @@ class WallPaper { public: explicit WallPaper(WallPaperId id); - void setLocalImageAsThumbnail(not_null image); + void setLocalImageAsThumbnail(std::shared_ptr image); [[nodiscard]] WallPaperId id() const; [[nodiscard]] std::optional backgroundColor() const; @@ -43,6 +43,7 @@ public: [[nodiscard]] WallPaper withPatternIntensity(int intensity) const; [[nodiscard]] WallPaper withBackgroundColor(QColor color) const; [[nodiscard]] WallPaper withParamsFrom(const WallPaper &other) const; + [[nodiscard]] WallPaper withoutImageData() const; [[nodiscard]] static std::optional Create( const MTPWallPaper &data); @@ -75,7 +76,7 @@ private: int _intensity = kDefaultIntensity; DocumentData *_document = nullptr; - Image *_thumbnail = nullptr; + std::shared_ptr _thumbnail; };