Added ability to create items in photo editor with different ratios.

This commit is contained in:
23rd 2021-03-09 18:12:23 +03:00
parent e05343d721
commit 274b66f74b
3 changed files with 32 additions and 10 deletions

View file

@ -51,7 +51,7 @@ ItemBase::ItemBase(std::shared_ptr<float64> zPtr, int size, int x, int y)
Qt::DashLine, Qt::DashLine,
Qt::SquareCap, Qt::SquareCap,
Qt::RoundJoin) Qt::RoundJoin)
, _size(size) { , _horizontalSize(size) {
setFlags(QGraphicsItem::ItemIsMovable setFlags(QGraphicsItem::ItemIsMovable
| QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsSelectable
| QGraphicsItem::ItemIsFocusable); | QGraphicsItem::ItemIsFocusable);
@ -64,7 +64,9 @@ QRectF ItemBase::boundingRect() const {
} }
QRectF ItemBase::innerRect() 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( void ItemBase::paint(
@ -96,10 +98,11 @@ void ItemBase::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
const auto dx = int(2.0 * p.x()); const auto dx = int(2.0 * p.x());
const auto dy = int(2.0 * p.y()); const auto dy = int(2.0 * p.y());
prepareGeometryChange(); prepareGeometryChange();
_size = std::clamp( _horizontalSize = std::clamp(
(dx > dy ? dx : dy), (dx > dy ? dx : dy),
st::photoEditorItemMinSize, st::photoEditorItemMinSize,
st::photoEditorItemMaxSize); st::photoEditorItemMaxSize);
updateVerticalSize();
// Rotate. // Rotate.
const auto origin = mapToScene(boundingRect().center()); const auto origin = mapToScene(boundingRect().center());
@ -149,7 +152,7 @@ int ItemBase::type() const {
QRectF ItemBase::rightHandleRect() const { QRectF ItemBase::rightHandleRect() const {
return QRectF( return QRectF(
(_size / 2) - (_handleSize / 2), (_horizontalSize / 2) - (_handleSize / 2),
0 - (_handleSize / 2), 0 - (_handleSize / 2),
_handleSize, _handleSize,
_handleSize); _handleSize);
@ -157,7 +160,7 @@ QRectF ItemBase::rightHandleRect() const {
QRectF ItemBase::leftHandleRect() const { QRectF ItemBase::leftHandleRect() const {
return QRectF( return QRectF(
(-_size / 2) - (_handleSize / 2), (-_horizontalSize / 2) - (_handleSize / 2),
0 - (_handleSize / 2), 0 - (_handleSize / 2),
_handleSize, _handleSize,
_handleSize); _handleSize);
@ -167,8 +170,17 @@ bool ItemBase::isHandling() const {
return _handle != HandleType::None; return _handle != HandleType::None;
} }
int ItemBase::size() const { float64 ItemBase::size() const {
return _size; return _horizontalSize;
}
void ItemBase::updateVerticalSize() {
_verticalSize = _horizontalSize * _aspectRatio;
}
void ItemBase::setAspectRatio(float64 aspectRatio) {
_aspectRatio = aspectRatio;
updateVerticalSize();
} }
ItemBase::HandleType ItemBase::handleType(const QPointF &pos) const { ItemBase::HandleType ItemBase::handleType(const QPointF &pos) const {

View file

@ -48,13 +48,17 @@ protected:
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
QRectF innerRect() const; QRectF innerRect() const;
int size() const; float64 size() const;
float64 horizontalSize() const;
float64 verticalSize() const;
void setAspectRatio(float64 aspectRatio);
private: private:
HandleType handleType(const QPointF &pos) const; HandleType handleType(const QPointF &pos) const;
QRectF rightHandleRect() const; QRectF rightHandleRect() const;
QRectF leftHandleRect() const; QRectF leftHandleRect() const;
bool isHandling() const; bool isHandling() const;
void updateVerticalSize();
const std::shared_ptr<float64> _lastZ; const std::shared_ptr<float64> _lastZ;
const int _handleSize; const int _handleSize;
@ -63,7 +67,9 @@ private:
const QPen _selectPenInactive; const QPen _selectPenInactive;
const QPen _handlePen; const QPen _handlePen;
int _size; float64 _horizontalSize = 0;
float64 _verticalSize = 0;
float64 _aspectRatio = 1.0;
HandleType _handle = HandleType::None; HandleType _handle = HandleType::None;
}; };

View file

@ -37,6 +37,11 @@ ItemSticker::ItemSticker(
return; return;
} }
const auto updateThumbnail = [=] { const auto updateThumbnail = [=] {
const auto guard = gsl::finally([&] {
setAspectRatio(_pixmap.isNull()
? 1.0
: (_pixmap.height() / float64(_pixmap.width())));
});
if (stickerData->animated) { if (stickerData->animated) {
_lottie.player = ChatHelpers::LottiePlayerFromDocument( _lottie.player = ChatHelpers::LottiePlayerFromDocument(
_mediaView.get(), _mediaView.get(),
@ -73,7 +78,6 @@ ItemSticker::ItemSticker(
update(); update();
} }
}, _loadingLifetime); }, _loadingLifetime);
return;
} }
} }