From fe8eae09c4b612f6c3491dcab36194407cf31914 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 7 Jul 2021 02:10:13 +0300 Subject: [PATCH] Fixed item handlers size when opening photo editor with modified scene. --- Telegram/SourceFiles/editor/editor_paint.cpp | 3 +- Telegram/SourceFiles/editor/editor_paint.h | 2 +- Telegram/SourceFiles/editor/scene/scene.cpp | 8 +++ Telegram/SourceFiles/editor/scene/scene.h | 2 + .../editor/scene/scene_item_base.cpp | 71 ++++++++++--------- .../editor/scene/scene_item_base.h | 9 +-- .../editor/scene/scene_item_sticker.h | 2 +- 7 files changed, 56 insertions(+), 41 deletions(-) diff --git a/Telegram/SourceFiles/editor/editor_paint.cpp b/Telegram/SourceFiles/editor/editor_paint.cpp index 0344e69a1..410f9ab53 100644 --- a/Telegram/SourceFiles/editor/editor_paint.cpp +++ b/Telegram/SourceFiles/editor/editor_paint.cpp @@ -157,6 +157,7 @@ void Paint::applyTransform(QRect geometry, int angle, bool flipped) { .flipped = flipped, .zoom = size.width() / float64(_scene->sceneRect().width()), }; + _scene->updateZoom(_transform.zoom); } std::shared_ptr Paint::saveScene() const { @@ -291,7 +292,7 @@ ItemBase::Data Paint::itemBaseData() const { const auto x = s.width() / 2; const auto y = s.height() / 2; return ItemBase::Data{ - .zoomValue = _transform.zoom.value(), + .initialZoom = _transform.zoom, .zPtr = _scene->lastZ(), .size = size, .x = x, diff --git a/Telegram/SourceFiles/editor/editor_paint.h b/Telegram/SourceFiles/editor/editor_paint.h index 259d3f99d..d6d566412 100644 --- a/Telegram/SourceFiles/editor/editor_paint.h +++ b/Telegram/SourceFiles/editor/editor_paint.h @@ -62,7 +62,7 @@ private: struct { int angle = 0; bool flipped = false; - rpl::variable zoom = 0.; + float64 zoom = 0.; } _transform; std::vector _previousItems; diff --git a/Telegram/SourceFiles/editor/scene/scene.cpp b/Telegram/SourceFiles/editor/scene/scene.cpp index 184778c9a..865c1c8b6 100644 --- a/Telegram/SourceFiles/editor/scene/scene.cpp +++ b/Telegram/SourceFiles/editor/scene/scene.cpp @@ -139,6 +139,14 @@ std::shared_ptr Scene::lastZ() const { return _lastZ; } +void Scene::updateZoom(float64 zoom) { + for (const auto &item : items()) { + if (item->type() >= ItemBase::Type) { + static_cast(item.get())->updateZoom(zoom); + } + } +} + Scene::~Scene() { // Prevent destroying by scene of all items. QGraphicsScene::removeItem(_canvas.get()); diff --git a/Telegram/SourceFiles/editor/scene/scene.h b/Telegram/SourceFiles/editor/scene/scene.h index 168069fb7..820515f9f 100644 --- a/Telegram/SourceFiles/editor/scene/scene.h +++ b/Telegram/SourceFiles/editor/scene/scene.h @@ -42,6 +42,8 @@ public: [[nodiscard]] std::shared_ptr lastZ() const; + void updateZoom(float64 zoom); + void cancelDrawing(); protected: void mousePressEvent(QGraphicsSceneMouseEvent *event) override; diff --git a/Telegram/SourceFiles/editor/scene/scene_item_base.cpp b/Telegram/SourceFiles/editor/scene/scene_item_base.cpp index c701362a0..0ce01646f 100644 --- a/Telegram/SourceFiles/editor/scene/scene_item_base.cpp +++ b/Telegram/SourceFiles/editor/scene/scene_item_base.cpp @@ -51,8 +51,7 @@ void NumberedItem::setNumber(int number) { ItemBase::ItemBase(Data data) : _lastZ(data.zPtr) , _imageSize(data.imageSize) -, _horizontalSize(data.size) -, _zoom(std::move(data.zoomValue)) { +, _horizontalSize(data.size) { setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable); @@ -61,37 +60,7 @@ ItemBase::ItemBase(Data data) setZValue((*_lastZ)++); setFlip(data.flipped); setRotation(data.rotation); - - const auto &handleSize = st::photoEditorItemHandleSize; - _zoom.value( - ) | rpl::start_with_next([=](float64 zoom) { - _scaledHandleSize = handleSize / zoom; - _scaledInnerMargins = QMarginsF( - _scaledHandleSize, - _scaledHandleSize, - _scaledHandleSize, - _scaledHandleSize) * 0.5; - - const auto maxSide = std::max( - _imageSize.width(), - _imageSize.height()); - _sizeLimits = { - .min = int(maxSide * kMinSizeRatio), - .max = int(maxSide * kMaxSizeRatio), - }; - _horizontalSize = std::clamp( - _horizontalSize, - float64(_sizeLimits.min), - float64(_sizeLimits.max)); - updateVerticalSize(); - - updatePens(QPen( - QBrush(), - 1 / zoom, - Qt::DashLine, - Qt::SquareCap, - Qt::RoundJoin)); - }, _lifetime); + updateZoom(data.initialZoom); } QRectF ItemBase::boundingRect() const { @@ -243,8 +212,9 @@ void ItemBase::actionDelete() { void ItemBase::actionDuplicate() { if (const auto s = static_cast(scene())) { + const auto zoom = st::photoEditorItemHandleSize / _scaledHandleSize; const auto newItem = duplicate(Data{ - .zoomValue = _zoom.value(), + .initialZoom = zoom, .zPtr = _lastZ, .size = int(_horizontalSize), .x = int(scenePos().x() + _horizontalSize / 3), @@ -348,6 +318,39 @@ void ItemBase::setFlip(bool value) { } } +int ItemBase::type() const { + return ItemBase::Type; +} + +void ItemBase::updateZoom(float64 zoom) { + _scaledHandleSize = st::photoEditorItemHandleSize / zoom; + _scaledInnerMargins = QMarginsF( + _scaledHandleSize, + _scaledHandleSize, + _scaledHandleSize, + _scaledHandleSize) * 0.5; + + const auto maxSide = std::max( + _imageSize.width(), + _imageSize.height()); + _sizeLimits = { + .min = int(maxSide * kMinSizeRatio), + .max = int(maxSide * kMaxSizeRatio), + }; + _horizontalSize = std::clamp( + _horizontalSize, + float64(_sizeLimits.min), + float64(_sizeLimits.max)); + updateVerticalSize(); + + updatePens(QPen( + QBrush(), + 1 / zoom, + Qt::DashLine, + Qt::SquareCap, + Qt::RoundJoin)); +} + void ItemBase::performFlip() { } diff --git a/Telegram/SourceFiles/editor/scene/scene_item_base.h b/Telegram/SourceFiles/editor/scene/scene_item_base.h index c7b4f11a1..48f9760a5 100644 --- a/Telegram/SourceFiles/editor/scene/scene_item_base.h +++ b/Telegram/SourceFiles/editor/scene/scene_item_base.h @@ -35,9 +35,10 @@ private: class ItemBase : public NumberedItem { public: + enum { Type = UserType + 2 }; struct Data { - rpl::producer zoomValue; + float64 initialZoom = 0.; std::shared_ptr zPtr; int size = 0; int x = 0; @@ -53,9 +54,12 @@ public: QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget) override; + int type() const override; bool flipped() const; void setFlip(bool value); + + void updateZoom(float64 zoom); protected: enum HandleType { None, @@ -119,9 +123,6 @@ private: bool _flipped = false; - rpl::variable _zoom; - rpl::lifetime _lifetime; - }; } // namespace Editor diff --git a/Telegram/SourceFiles/editor/scene/scene_item_sticker.h b/Telegram/SourceFiles/editor/scene/scene_item_sticker.h index ac3d2f8f2..e8bb3078a 100644 --- a/Telegram/SourceFiles/editor/scene/scene_item_sticker.h +++ b/Telegram/SourceFiles/editor/scene/scene_item_sticker.h @@ -21,7 +21,7 @@ namespace Editor { class ItemSticker : public ItemBase { public: - enum { Type = ItemBase::Type + 2 }; + enum { Type = ItemBase::Type + 1 }; ItemSticker( not_null document,