From 274b66f74bd7400046d32117ca022c6fa72276af Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 9 Mar 2021 18:12:23 +0300 Subject: [PATCH] Added ability to create items in photo editor with different ratios. --- .../SourceFiles/editor/scene_item_base.cpp | 26 ++++++++++++++----- Telegram/SourceFiles/editor/scene_item_base.h | 10 +++++-- .../SourceFiles/editor/scene_item_sticker.cpp | 6 ++++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Telegram/SourceFiles/editor/scene_item_base.cpp b/Telegram/SourceFiles/editor/scene_item_base.cpp index 50b006bb5..ca19bc93f 100644 --- a/Telegram/SourceFiles/editor/scene_item_base.cpp +++ b/Telegram/SourceFiles/editor/scene_item_base.cpp @@ -51,7 +51,7 @@ ItemBase::ItemBase(std::shared_ptr zPtr, int size, int x, int y) Qt::DashLine, Qt::SquareCap, Qt::RoundJoin) -, _size(size) { +, _horizontalSize(size) { setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable); @@ -64,7 +64,9 @@ QRectF ItemBase::boundingRect() const { } QRectF ItemBase::innerRect() const { - return QRectF(-_size / 2, -_size / 2, _size, _size); + const auto &hSize = _horizontalSize; + const auto &vSize = _verticalSize; + return QRectF(-hSize / 2, -vSize / 2, hSize, vSize); } void ItemBase::paint( @@ -96,10 +98,11 @@ void ItemBase::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { const auto dx = int(2.0 * p.x()); const auto dy = int(2.0 * p.y()); prepareGeometryChange(); - _size = std::clamp( + _horizontalSize = std::clamp( (dx > dy ? dx : dy), st::photoEditorItemMinSize, st::photoEditorItemMaxSize); + updateVerticalSize(); // Rotate. const auto origin = mapToScene(boundingRect().center()); @@ -149,7 +152,7 @@ int ItemBase::type() const { QRectF ItemBase::rightHandleRect() const { return QRectF( - (_size / 2) - (_handleSize / 2), + (_horizontalSize / 2) - (_handleSize / 2), 0 - (_handleSize / 2), _handleSize, _handleSize); @@ -157,7 +160,7 @@ QRectF ItemBase::rightHandleRect() const { QRectF ItemBase::leftHandleRect() const { return QRectF( - (-_size / 2) - (_handleSize / 2), + (-_horizontalSize / 2) - (_handleSize / 2), 0 - (_handleSize / 2), _handleSize, _handleSize); @@ -167,8 +170,17 @@ bool ItemBase::isHandling() const { return _handle != HandleType::None; } -int ItemBase::size() const { - return _size; +float64 ItemBase::size() const { + return _horizontalSize; +} + +void ItemBase::updateVerticalSize() { + _verticalSize = _horizontalSize * _aspectRatio; +} + +void ItemBase::setAspectRatio(float64 aspectRatio) { + _aspectRatio = aspectRatio; + updateVerticalSize(); } ItemBase::HandleType ItemBase::handleType(const QPointF &pos) const { diff --git a/Telegram/SourceFiles/editor/scene_item_base.h b/Telegram/SourceFiles/editor/scene_item_base.h index 51676e70b..cd835012f 100644 --- a/Telegram/SourceFiles/editor/scene_item_base.h +++ b/Telegram/SourceFiles/editor/scene_item_base.h @@ -48,13 +48,17 @@ protected: void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; QRectF innerRect() const; - int size() const; + float64 size() const; + float64 horizontalSize() const; + float64 verticalSize() const; + void setAspectRatio(float64 aspectRatio); private: HandleType handleType(const QPointF &pos) const; QRectF rightHandleRect() const; QRectF leftHandleRect() const; bool isHandling() const; + void updateVerticalSize(); const std::shared_ptr _lastZ; const int _handleSize; @@ -63,7 +67,9 @@ private: const QPen _selectPenInactive; const QPen _handlePen; - int _size; + float64 _horizontalSize = 0; + float64 _verticalSize = 0; + float64 _aspectRatio = 1.0; HandleType _handle = HandleType::None; }; diff --git a/Telegram/SourceFiles/editor/scene_item_sticker.cpp b/Telegram/SourceFiles/editor/scene_item_sticker.cpp index c92b19a29..66c1b8d2c 100644 --- a/Telegram/SourceFiles/editor/scene_item_sticker.cpp +++ b/Telegram/SourceFiles/editor/scene_item_sticker.cpp @@ -37,6 +37,11 @@ ItemSticker::ItemSticker( return; } const auto updateThumbnail = [=] { + const auto guard = gsl::finally([&] { + setAspectRatio(_pixmap.isNull() + ? 1.0 + : (_pixmap.height() / float64(_pixmap.width()))); + }); if (stickerData->animated) { _lottie.player = ChatHelpers::LottiePlayerFromDocument( _mediaView.get(), @@ -73,7 +78,6 @@ ItemSticker::ItemSticker( update(); } }, _loadingLifetime); - return; } }