From f7fa36ca1daf0eec32fc3880852b96a7eee036ef Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 5 Feb 2021 10:08:20 +0300 Subject: [PATCH] Added photo painting to PhotoEditorContent. --- Telegram/SourceFiles/editor/photo_editor.cpp | 8 +++++- Telegram/SourceFiles/editor/photo_editor.h | 5 ++++ .../editor/photo_editor_content.cpp | 27 +++++++++++++++++++ .../SourceFiles/editor/photo_editor_content.h | 2 ++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Telegram/SourceFiles/editor/photo_editor.cpp b/Telegram/SourceFiles/editor/photo_editor.cpp index 0030351ab..5fde53f30 100644 --- a/Telegram/SourceFiles/editor/photo_editor.cpp +++ b/Telegram/SourceFiles/editor/photo_editor.cpp @@ -15,7 +15,13 @@ namespace Editor { PhotoEditor::PhotoEditor( not_null parent, std::shared_ptr photo) -: RpWidget(parent) { +: RpWidget(parent) +, _content(base::make_unique_q(this, photo)) +, _controls(base::make_unique_q(this)) { + sizeValue( + ) | rpl::start_with_next([=](const QSize &size) { + _content->resize(size); + }, lifetime()); } } // namespace Editor diff --git a/Telegram/SourceFiles/editor/photo_editor.h b/Telegram/SourceFiles/editor/photo_editor.h index 82b186893..7cd0f04b5 100644 --- a/Telegram/SourceFiles/editor/photo_editor.h +++ b/Telegram/SourceFiles/editor/photo_editor.h @@ -9,6 +9,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/rp_widget.h" +#include "base/unique_qptr.h" + namespace Editor { class PhotoEditorContent; @@ -22,6 +24,9 @@ public: private: + base::unique_qptr _content; + base::unique_qptr _controls; + }; } // namespace Editor diff --git a/Telegram/SourceFiles/editor/photo_editor_content.cpp b/Telegram/SourceFiles/editor/photo_editor_content.cpp index dd57403b0..e63bd0a92 100644 --- a/Telegram/SourceFiles/editor/photo_editor_content.cpp +++ b/Telegram/SourceFiles/editor/photo_editor_content.cpp @@ -13,6 +13,33 @@ PhotoEditorContent::PhotoEditorContent( not_null parent, std::shared_ptr photo) : RpWidget(parent) { + + sizeValue( + ) | rpl::start_with_next([=](const QSize &size) { + const auto imageSize = [&] { + const auto originalSize = photo->size() / cIntRetinaFactor(); + if ((originalSize.width() > size.width()) + && (originalSize.height() > size.height())) { + return originalSize.scaled( + size, + Qt::KeepAspectRatio); + } + return originalSize; + }(); + _imageRect = QRect( + QPoint( + (size.width() - imageSize.width()) / 2, + (size.height() - imageSize.height()) / 2), + imageSize); + }, lifetime()); + + paintRequest( + ) | rpl::start_with_next([=](const QRect &clip) { + Painter p(this); + + p.fillRect(clip, Qt::transparent); + p.drawPixmap(_imageRect, *photo, photo->rect()); + }, lifetime()); } } // namespace Editor diff --git a/Telegram/SourceFiles/editor/photo_editor_content.h b/Telegram/SourceFiles/editor/photo_editor_content.h index 80ce4678d..e9424c3b6 100644 --- a/Telegram/SourceFiles/editor/photo_editor_content.h +++ b/Telegram/SourceFiles/editor/photo_editor_content.h @@ -19,6 +19,8 @@ public: private: + QRect _imageRect; + }; } // namespace Editor