From d1b6cf1fae3d505bcd343f6991fc71ad3e03a14f Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 20 Feb 2021 10:52:48 +0300 Subject: [PATCH] Added draft menu to EditCaptionBox to open photo editor. --- .../SourceFiles/boxes/edit_caption_box.cpp | 45 +++++++++++++++++++ Telegram/SourceFiles/boxes/send_files_box.cpp | 33 +++----------- Telegram/SourceFiles/editor/editor_crop.cpp | 5 ++- Telegram/SourceFiles/editor/editor_paint.cpp | 6 ++- .../editor/photo_editor_common.cpp | 2 +- .../editor/photo_editor_layer_widget.cpp | 39 +++++++++++++++- .../editor/photo_editor_layer_widget.h | 12 +++++ 7 files changed, 111 insertions(+), 31 deletions(-) diff --git a/Telegram/SourceFiles/boxes/edit_caption_box.cpp b/Telegram/SourceFiles/boxes/edit_caption_box.cpp index b7b20aa91..103c771ad 100644 --- a/Telegram/SourceFiles/boxes/edit_caption_box.cpp +++ b/Telegram/SourceFiles/boxes/edit_caption_box.cpp @@ -64,6 +64,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_chat_helpers.h" #include "styles/style_chat.h" +#include "editor/photo_editor_layer_widget.h" + #include namespace { @@ -402,6 +404,47 @@ EditCaptionBox::EditCaptionBox( ) | rpl::start_with_next([=] { closeBox(); }, lifetime()); + + AddPhotoEditorMenu(this, [=, _controller = controller] { + const auto previewWidth = st::sendMediaPreviewSize; + if (!_preparedList.files.empty()) { + Editor::OpenWithPreparedFile( + this, + controller, + &_preparedList.files.front(), + previewWidth, + [=] { updateEditPreview(); }); + } else { + auto callback = [=](const Editor::PhotoModifications &mods) { + if (!mods) { + return; + } + auto copy = computeImage()->original(); + _preparedList = Storage::PrepareMediaFromImage( + std::move(copy), + QByteArray(), + previewWidth); + + using ImageInfo = Ui::PreparedFileInformation::Image; + auto &file = _preparedList.files.front(); + const auto image = std::get_if( + &file.information->media); + + image->modifications = mods; + Storage::UpdateImageDetails(file, previewWidth); + updateEditPreview(); + }; + const auto fileImage = std::make_shared(*computeImage()); + controller->showLayer( + std::make_unique( + this, + &controller->window(), + fileImage, + Editor::PhotoModifications(), + std::move(callback)), + Ui::LayerOption::KeepOther); + } + }); } EditCaptionBox::~EditCaptionBox() = default; @@ -1048,6 +1091,8 @@ void EditCaptionBox::save() { action.options = options; action.replaceMediaOf = item->fullId().msg; + Storage::ApplyModifications(_preparedList); + _controller->session().api().editMedia( std::move(_preparedList), (!_asFile && _photo) ? SendMediaType::Photo : SendMediaType::File, diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp index 7fb81f588..a2bad0e79 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.cpp +++ b/Telegram/SourceFiles/boxes/send_files_box.cpp @@ -621,33 +621,12 @@ void SendFilesBox::pushBlock(int from, int till) { block.itemModifyRequest( ) | rpl::start_with_next([=, controller = _controller](int index) { - auto &file = _list.files[index]; - if (file.type != Ui::PreparedFile::Type::Photo) { - return; - } - using ImageInfo = Ui::PreparedFileInformation::Image; - const auto image = std::get_if(&file.information->media); - if (!image) { - return; - } - - auto callback = [=](const Editor::PhotoModifications &mods) { - image->modifications = mods; - Storage::UpdateImageDetails( - _list.files[index], - st::sendMediaPreviewSize); - refreshAllAfterChanges(from); - }; - auto copy = image->data; - const auto fileImage = std::make_shared(std::move(copy)); - controller->showLayer( - std::make_unique( - this, - &controller->window(), - fileImage, - image->modifications, - std::move(callback)), - Ui::LayerOption::KeepOther); + Editor::OpenWithPreparedFile( + this, + controller, + &_list.files[index], + st::sendMediaPreviewSize, + [=] { refreshAllAfterChanges(from); }); }, widget->lifetime()); } diff --git a/Telegram/SourceFiles/editor/editor_crop.cpp b/Telegram/SourceFiles/editor/editor_crop.cpp index 57e324350..0d72e8d08 100644 --- a/Telegram/SourceFiles/editor/editor_crop.cpp +++ b/Telegram/SourceFiles/editor/editor_crop.cpp @@ -294,7 +294,10 @@ style::margins Crop::cropMargins() const { } QRect Crop::saveCropRect() { - return _cropOriginal.toRect(); + const auto savedCrop = _cropOriginal.toRect(); + return (!savedCrop.topLeft().isNull() || (savedCrop.size() != _imageSize)) + ? savedCrop + : QRect(); } } // namespace Editor diff --git a/Telegram/SourceFiles/editor/editor_paint.cpp b/Telegram/SourceFiles/editor/editor_paint.cpp index e52017e09..39ef644f2 100644 --- a/Telegram/SourceFiles/editor/editor_paint.cpp +++ b/Telegram/SourceFiles/editor/editor_paint.cpp @@ -166,7 +166,11 @@ void Paint::initDrawing() { } std::shared_ptr Paint::saveScene() const { - return _scene; + return _scene->items().empty() + ? nullptr + : ranges::none_of(_scene->items(), &QGraphicsItem::isVisible) + ? nullptr + : _scene; } void Paint::cancel() { diff --git a/Telegram/SourceFiles/editor/photo_editor_common.cpp b/Telegram/SourceFiles/editor/photo_editor_common.cpp index cdc9278d5..b5c49c933 100644 --- a/Telegram/SourceFiles/editor/photo_editor_common.cpp +++ b/Telegram/SourceFiles/editor/photo_editor_common.cpp @@ -33,7 +33,7 @@ QImage ImageModified(QImage image, const PhotoModifications &mods) { } bool PhotoModifications::empty() const { - return !angle && !flipped && !crop.isValid(); + return !angle && !flipped && !crop.isValid() && !paint; } PhotoModifications::operator bool() const { diff --git a/Telegram/SourceFiles/editor/photo_editor_layer_widget.cpp b/Telegram/SourceFiles/editor/photo_editor_layer_widget.cpp index 0db66252d..58b6fccd2 100644 --- a/Telegram/SourceFiles/editor/photo_editor_layer_widget.cpp +++ b/Telegram/SourceFiles/editor/photo_editor_layer_widget.cpp @@ -8,9 +8,46 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "editor/photo_editor_layer_widget.h" #include "editor/photo_editor.h" +#include "storage/storage_media_prepare.h" +#include "ui/chat/attach/attach_prepare.h" +#include "window/window_session_controller.h" namespace Editor { +void OpenWithPreparedFile( + not_null parent, + not_null controller, + not_null file, + int previewWidth, + Fn &&doneCallback) { + + if (file->type != Ui::PreparedFile::Type::Photo) { + return; + } + using ImageInfo = Ui::PreparedFileInformation::Image; + const auto image = std::get_if(&file->information->media); + if (!image) { + return; + } + + auto callback = [=, done = std::move(doneCallback)]( + const PhotoModifications &mods) { + image->modifications = mods; + Storage::UpdateImageDetails(*file, previewWidth); + done(); + }; + auto copy = image->data; + const auto fileImage = std::make_shared(std::move(copy)); + controller->showLayer( + std::make_unique( + parent, + &controller->window(), + fileImage, + image->modifications, + std::move(callback)), + Ui::LayerOption::KeepOther); +} + LayerWidget::LayerWidget( not_null parent, not_null window, @@ -38,7 +75,7 @@ LayerWidget::LayerWidget( _content->doneRequests( ) | rpl::start_with_next([=, done = std::move(doneCallback)]( const PhotoModifications &mods) { - doneCallback(mods); + done(mods); closeLayer(); }, lifetime()); diff --git a/Telegram/SourceFiles/editor/photo_editor_layer_widget.h b/Telegram/SourceFiles/editor/photo_editor_layer_widget.h index ece6f151f..8d6842791 100644 --- a/Telegram/SourceFiles/editor/photo_editor_layer_widget.h +++ b/Telegram/SourceFiles/editor/photo_editor_layer_widget.h @@ -13,12 +13,24 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "editor/photo_editor_common.h" #include "ui/image/image.h" +namespace Ui { +struct PreparedFile; +} // namespace Ui + namespace Window { class Controller; +class SessionController; } // namespace Window namespace Editor { +void OpenWithPreparedFile( + not_null parent, + not_null controller, + not_null file, + int previewWidth, + Fn &&doneCallback); + class PhotoEditor; class LayerWidget : public Ui::LayerWidget {