2021-02-08 07:01:15 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "editor/editor_crop.h"
|
|
|
|
|
2022-12-05 12:18:10 +00:00
|
|
|
#include "ui/userpic_view.h"
|
2021-02-22 03:25:13 +00:00
|
|
|
#include "styles/style_editor.h"
|
2022-11-03 12:29:22 +00:00
|
|
|
#include "styles/style_basic.h"
|
|
|
|
#include "styles/style_dialogs.h"
|
2021-02-08 07:01:15 +00:00
|
|
|
|
|
|
|
namespace Editor {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kETL = Qt::TopEdge | Qt::LeftEdge;
|
|
|
|
constexpr auto kETR = Qt::TopEdge | Qt::RightEdge;
|
|
|
|
constexpr auto kEBL = Qt::BottomEdge | Qt::LeftEdge;
|
|
|
|
constexpr auto kEBR = Qt::BottomEdge | Qt::RightEdge;
|
|
|
|
constexpr auto kEAll = Qt::TopEdge
|
|
|
|
| Qt::LeftEdge
|
|
|
|
| Qt::BottomEdge
|
|
|
|
| Qt::RightEdge;
|
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
std::tuple<int, int, int, int> RectEdges(const QRectF &r) {
|
2021-02-08 07:01:15 +00:00
|
|
|
return { r.left(), r.top(), r.left() + r.width(), r.top() + r.height() };
|
|
|
|
}
|
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
QPoint PointOfEdge(Qt::Edges e, const QRectF &r) {
|
2021-02-08 07:01:15 +00:00
|
|
|
switch(e) {
|
|
|
|
case kETL: return QPoint(r.x(), r.y());
|
|
|
|
case kETR: return QPoint(r.x() + r.width(), r.y());
|
|
|
|
case kEBL: return QPoint(r.x(), r.y() + r.height());
|
|
|
|
case kEBR: return QPoint(r.x() + r.width(), r.y() + r.height());
|
|
|
|
default: return QPoint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
QSizeF FlipSizeByRotation(const QSizeF &size, int angle) {
|
|
|
|
return (((angle / 90) % 2) == 1) ? size.transposed() : size;
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:01:15 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Crop::Crop(
|
|
|
|
not_null<Ui::RpWidget*> parent,
|
|
|
|
const PhotoModifications &modifications,
|
2021-02-21 07:28:37 +00:00
|
|
|
const QSize &imageSize,
|
|
|
|
EditorData data)
|
2021-02-08 07:01:15 +00:00
|
|
|
: RpWidget(parent)
|
2021-02-22 03:25:13 +00:00
|
|
|
, _pointSize(st::photoEditorCropPointSize)
|
2021-02-08 07:01:15 +00:00
|
|
|
, _pointSizeH(_pointSize / 2.)
|
|
|
|
, _innerMargins(QMarginsF(_pointSizeH, _pointSizeH, _pointSizeH, _pointSizeH)
|
|
|
|
.toMargins())
|
|
|
|
, _offset(_innerMargins.left(), _innerMargins.top())
|
2021-02-18 06:37:12 +00:00
|
|
|
, _edgePointMargins(_pointSizeH, _pointSizeH, -_pointSizeH, -_pointSizeH)
|
|
|
|
, _imageSize(imageSize)
|
2021-02-21 07:28:37 +00:00
|
|
|
, _data(std::move(data))
|
2021-02-18 06:37:12 +00:00
|
|
|
, _cropOriginal(modifications.crop.isValid()
|
|
|
|
? modifications.crop
|
|
|
|
: QRectF(QPoint(), _imageSize))
|
|
|
|
, _angle(modifications.angle)
|
2021-02-21 07:28:37 +00:00
|
|
|
, _flipped(modifications.flipped)
|
|
|
|
, _keepAspectRatio(_data.keepAspectRatio) {
|
2021-02-08 07:01:15 +00:00
|
|
|
|
|
|
|
setMouseTracking(true);
|
|
|
|
|
|
|
|
paintRequest(
|
|
|
|
) | rpl::start_with_next([=] {
|
2022-09-16 20:23:27 +00:00
|
|
|
auto p = QPainter(this);
|
2021-02-08 07:01:15 +00:00
|
|
|
|
|
|
|
p.fillPath(_painterPath, st::photoCropFadeBg);
|
|
|
|
paintPoints(p);
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
void Crop::applyTransform(
|
|
|
|
const QRect &geometry,
|
|
|
|
int angle,
|
|
|
|
bool flipped,
|
|
|
|
const QSizeF &scaledImageSize) {
|
2021-02-08 07:01:15 +00:00
|
|
|
if (geometry.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setGeometry(geometry);
|
2021-02-18 06:37:12 +00:00
|
|
|
_innerRect = QRectF(_offset, FlipSizeByRotation(scaledImageSize, angle));
|
|
|
|
_ratio.w = scaledImageSize.width() / float64(_imageSize.width());
|
|
|
|
_ratio.h = scaledImageSize.height() / float64(_imageSize.height());
|
|
|
|
_flipped = flipped;
|
|
|
|
_angle = angle;
|
2021-02-08 07:01:15 +00:00
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
const auto cropHolder = QRectF(QPointF(), scaledImageSize);
|
|
|
|
const auto cropHolderCenter = cropHolder.center();
|
2021-02-08 07:01:15 +00:00
|
|
|
|
2021-10-19 13:00:21 +00:00
|
|
|
auto matrix = QTransform()
|
2021-02-18 06:37:12 +00:00
|
|
|
.translate(cropHolderCenter.x(), cropHolderCenter.y())
|
|
|
|
.scale(flipped ? -1 : 1, 1)
|
|
|
|
.rotate(angle)
|
|
|
|
.translate(-cropHolderCenter.x(), -cropHolderCenter.y());
|
2021-02-08 07:01:15 +00:00
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
const auto cropHolderRotated = matrix.mapRect(cropHolder);
|
2021-02-08 07:01:15 +00:00
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
auto cropPaint = matrix
|
|
|
|
.scale(_ratio.w, _ratio.h)
|
|
|
|
.mapRect(_cropOriginal)
|
|
|
|
.translated(
|
|
|
|
-cropHolderRotated.x() + _offset.x(),
|
|
|
|
-cropHolderRotated.y() + _offset.y());
|
2021-02-08 07:01:15 +00:00
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
// Check boundaries.
|
2021-02-22 03:25:13 +00:00
|
|
|
const auto min = float64(st::photoEditorCropMinSize);
|
2021-02-18 06:37:12 +00:00
|
|
|
if ((cropPaint.width() < min) || (cropPaint.height() < min)) {
|
|
|
|
cropPaint.setWidth(std::max(min, cropPaint.width()));
|
|
|
|
cropPaint.setHeight(std::max(min, cropPaint.height()));
|
2021-02-08 07:01:15 +00:00
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
const auto p = cropPaint.center().toPoint();
|
|
|
|
setCropPaint(std::move(cropPaint));
|
2021-02-08 07:01:15 +00:00
|
|
|
|
|
|
|
computeDownState(p);
|
|
|
|
performMove(p);
|
|
|
|
clearDownState();
|
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
convertCropPaintToOriginal();
|
|
|
|
} else {
|
|
|
|
setCropPaint(std::move(cropPaint));
|
|
|
|
}
|
2021-02-08 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 20:23:27 +00:00
|
|
|
void Crop::paintPoints(QPainter &p) {
|
2021-02-08 07:01:15 +00:00
|
|
|
p.save();
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(st::photoCropPointFg);
|
|
|
|
for (const auto &r : ranges::views::values(_edges)) {
|
|
|
|
p.drawRect(r);
|
|
|
|
}
|
|
|
|
p.restore();
|
|
|
|
}
|
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
void Crop::setCropPaint(QRectF &&rect) {
|
|
|
|
_cropPaint = std::move(rect);
|
|
|
|
|
2021-02-08 07:01:15 +00:00
|
|
|
updateEdges();
|
|
|
|
|
|
|
|
_painterPath.clear();
|
|
|
|
_painterPath.addRect(_innerRect);
|
2021-02-21 07:28:37 +00:00
|
|
|
if (_data.cropType == EditorData::CropType::Ellipse) {
|
|
|
|
_painterPath.addEllipse(_cropPaint);
|
2022-11-03 12:29:22 +00:00
|
|
|
} else if (_data.cropType == EditorData::CropType::RoundedRect) {
|
|
|
|
const auto radius = std::min(_cropPaint.width(), _cropPaint.height())
|
2022-12-05 12:18:10 +00:00
|
|
|
* Ui::ForumUserpicRadiusMultiplier();
|
2022-11-03 12:29:22 +00:00
|
|
|
_painterPath.addRoundedRect(_cropPaint, radius, radius);
|
2021-02-21 07:28:37 +00:00
|
|
|
} else {
|
|
|
|
_painterPath.addRect(_cropPaint);
|
|
|
|
}
|
2021-02-08 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
void Crop::convertCropPaintToOriginal() {
|
2021-10-19 13:00:21 +00:00
|
|
|
const auto cropHolder = QTransform()
|
2021-02-18 06:37:12 +00:00
|
|
|
.scale(_ratio.w, _ratio.h)
|
|
|
|
.mapRect(QRectF(QPointF(), FlipSizeByRotation(_imageSize, _angle)));
|
|
|
|
const auto cropHolderCenter = cropHolder.center();
|
|
|
|
|
2021-10-19 13:00:21 +00:00
|
|
|
const auto matrix = QTransform()
|
2021-02-18 06:37:12 +00:00
|
|
|
.translate(cropHolderCenter.x(), cropHolderCenter.y())
|
|
|
|
.rotate(-_angle)
|
|
|
|
.scale((_flipped ? -1 : 1) * 1. / _ratio.w, 1. / _ratio.h)
|
|
|
|
.translate(-cropHolderCenter.x(), -cropHolderCenter.y());
|
|
|
|
|
|
|
|
const auto cropHolderRotated = matrix.mapRect(cropHolder);
|
|
|
|
|
|
|
|
_cropOriginal = matrix
|
|
|
|
.mapRect(QRectF(_cropPaint).translated(-_offset))
|
|
|
|
.translated(
|
|
|
|
-cropHolderRotated.x(),
|
|
|
|
-cropHolderRotated.y());
|
2021-02-08 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Crop::updateEdges() {
|
|
|
|
const auto &s = _pointSize;
|
|
|
|
const auto &m = _edgePointMargins;
|
2021-02-18 06:37:12 +00:00
|
|
|
const auto &r = _cropPaint;
|
2021-02-08 07:01:15 +00:00
|
|
|
for (const auto &e : { kETL, kETR, kEBL, kEBR }) {
|
|
|
|
_edges[e] = QRectF(PointOfEdge(e, r), QSize(s, s)) + m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Edges Crop::mouseState(const QPoint &p) {
|
|
|
|
for (const auto &[e, r] : _edges) {
|
|
|
|
if (r.contains(p)) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 06:37:12 +00:00
|
|
|
if (_cropPaint.contains(p)) {
|
2021-02-08 07:01:15 +00:00
|
|
|
return kEAll;
|
|
|
|
}
|
|
|
|
return Qt::Edges();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Crop::mousePressEvent(QMouseEvent *e) {
|
|
|
|
computeDownState(e->pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Crop::mouseReleaseEvent(QMouseEvent *e) {
|
|
|
|
clearDownState();
|
2021-02-18 06:37:12 +00:00
|
|
|
convertCropPaintToOriginal();
|
2021-02-08 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Crop::computeDownState(const QPoint &p) {
|
|
|
|
const auto edge = mouseState(p);
|
|
|
|
const auto &inner = _innerRect;
|
2021-02-18 06:37:12 +00:00
|
|
|
const auto &crop = _cropPaint;
|
2024-01-09 17:32:18 +00:00
|
|
|
const auto &[iLeft, iTop, iRight, iBottom] = RectEdges(inner);
|
|
|
|
const auto &[cLeft, cTop, cRight, cBottom] = RectEdges(crop);
|
2021-02-08 07:01:15 +00:00
|
|
|
_down = InfoAtDown{
|
|
|
|
.rect = crop,
|
|
|
|
.edge = edge,
|
|
|
|
.point = (p - PointOfEdge(edge, crop)),
|
2021-02-21 11:51:32 +00:00
|
|
|
.cropRatio = (_cropOriginal.width() / _cropOriginal.height()),
|
2021-02-08 07:01:15 +00:00
|
|
|
.borders = InfoAtDown::Borders{
|
|
|
|
.left = iLeft - cLeft,
|
|
|
|
.right = iRight - cRight,
|
|
|
|
.top = iTop - cTop,
|
|
|
|
.bottom = iBottom - cBottom,
|
|
|
|
}
|
|
|
|
};
|
2021-02-21 11:51:32 +00:00
|
|
|
if (_keepAspectRatio && (edge != kEAll)) {
|
|
|
|
const auto hasLeft = (edge & Qt::LeftEdge);
|
|
|
|
const auto hasTop = (edge & Qt::TopEdge);
|
|
|
|
|
|
|
|
const auto xSign = hasLeft ? -1 : 1;
|
|
|
|
const auto ySign = hasTop ? -1 : 1;
|
|
|
|
|
|
|
|
auto &xSide = (hasLeft ? _down.borders.left : _down.borders.right);
|
|
|
|
auto &ySide = (hasTop ? _down.borders.top : _down.borders.bottom);
|
|
|
|
|
|
|
|
const auto min = std::abs(std::min(xSign * xSide, ySign * ySide));
|
|
|
|
const auto xIsMin = ((xSign * xSide) < (ySign * ySide));
|
|
|
|
xSide = xSign * min;
|
|
|
|
ySide = ySign * min;
|
|
|
|
if (!xIsMin) {
|
|
|
|
xSide *= _down.cropRatio;
|
|
|
|
} else {
|
|
|
|
ySide /= _down.cropRatio;
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Crop::clearDownState() {
|
|
|
|
_down = InfoAtDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Crop::performCrop(const QPoint &pos) {
|
|
|
|
const auto &crop = _down.rect;
|
|
|
|
const auto &pressedEdge = _down.edge;
|
|
|
|
const auto hasLeft = (pressedEdge & Qt::LeftEdge);
|
|
|
|
const auto hasTop = (pressedEdge & Qt::TopEdge);
|
|
|
|
const auto hasRight = (pressedEdge & Qt::RightEdge);
|
|
|
|
const auto hasBottom = (pressedEdge & Qt::BottomEdge);
|
|
|
|
const auto diff = [&] {
|
2021-02-21 11:51:32 +00:00
|
|
|
auto diff = pos - PointOfEdge(pressedEdge, crop) - _down.point;
|
|
|
|
const auto xFactor = hasLeft ? 1 : -1;
|
|
|
|
const auto yFactor = hasTop ? 1 : -1;
|
2021-02-08 07:01:15 +00:00
|
|
|
const auto &borders = _down.borders;
|
2021-02-21 11:51:32 +00:00
|
|
|
const auto &cropRatio = _down.cropRatio;
|
|
|
|
if (_keepAspectRatio) {
|
|
|
|
const auto diffSign = xFactor * yFactor;
|
|
|
|
diff = (cropRatio != 1.)
|
|
|
|
? QPoint(diff.x(), (1. / cropRatio) * diff.x() * diffSign)
|
|
|
|
// For square/circle.
|
|
|
|
: ((diff.x() * xFactor) < (diff.y() * yFactor))
|
|
|
|
? QPoint(diff.x(), diff.x() * diffSign)
|
|
|
|
: QPoint(diff.y() * diffSign, diff.y());
|
|
|
|
}
|
2021-02-08 07:01:15 +00:00
|
|
|
|
2021-02-22 03:25:13 +00:00
|
|
|
const auto &minSize = st::photoEditorCropMinSize;
|
2021-07-09 09:44:25 +00:00
|
|
|
const auto xMin = xFactor * int(crop.width() - minSize);
|
|
|
|
// const auto xMin = int(xFactor * crop.width()
|
|
|
|
// - xFactor * minSize * ((cropRatio > 1.) ? cropRatio : 1.));
|
|
|
|
const auto yMin = yFactor * int(crop.height() - minSize);
|
|
|
|
// const auto yMin = int(yFactor * crop.height()
|
|
|
|
// - yFactor * minSize * ((cropRatio < 1.) ? (1. / cropRatio) : 1.));
|
2021-02-08 07:01:15 +00:00
|
|
|
|
|
|
|
const auto x = std::clamp(
|
|
|
|
diff.x(),
|
2021-02-21 11:51:32 +00:00
|
|
|
hasLeft ? borders.left : xMin,
|
|
|
|
hasLeft ? xMin : borders.right);
|
2021-02-08 07:01:15 +00:00
|
|
|
const auto y = std::clamp(
|
|
|
|
diff.y(),
|
2021-02-21 11:51:32 +00:00
|
|
|
hasTop ? borders.top : yMin,
|
|
|
|
hasTop ? yMin : borders.bottom);
|
2021-02-08 07:01:15 +00:00
|
|
|
return QPoint(x, y);
|
|
|
|
}();
|
2021-02-18 06:37:12 +00:00
|
|
|
setCropPaint(crop - QMargins(
|
2021-02-08 07:01:15 +00:00
|
|
|
hasLeft ? diff.x() : 0,
|
|
|
|
hasTop ? diff.y() : 0,
|
|
|
|
hasRight ? -diff.x() : 0,
|
|
|
|
hasBottom ? -diff.y() : 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Crop::performMove(const QPoint &pos) {
|
|
|
|
const auto &inner = _down.rect;
|
|
|
|
const auto &b = _down.borders;
|
|
|
|
const auto diffX = std::clamp(pos.x() - _down.point.x(), b.left, b.right);
|
|
|
|
const auto diffY = std::clamp(pos.y() - _down.point.y(), b.top, b.bottom);
|
2021-02-18 06:37:12 +00:00
|
|
|
setCropPaint(inner.translated(diffX, diffY));
|
2021-02-08 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Crop::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
const auto pos = e->pos();
|
|
|
|
const auto pressedEdge = _down.edge;
|
|
|
|
|
|
|
|
if (pressedEdge) {
|
|
|
|
if (pressedEdge == kEAll) {
|
|
|
|
performMove(pos);
|
|
|
|
} else if (pressedEdge) {
|
|
|
|
performCrop(pos);
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto edge = pressedEdge ? pressedEdge : mouseState(pos);
|
|
|
|
|
|
|
|
const auto cursor = ((edge == kETL) || (edge == kEBR))
|
|
|
|
? style::cur_sizefdiag
|
|
|
|
: ((edge == kETR) || (edge == kEBL))
|
|
|
|
? style::cur_sizebdiag
|
|
|
|
: (edge == kEAll)
|
|
|
|
? style::cur_sizeall
|
|
|
|
: style::cur_default;
|
|
|
|
setCursor(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
style::margins Crop::cropMargins() const {
|
|
|
|
return _innerMargins;
|
|
|
|
}
|
|
|
|
|
2021-02-18 06:37:12 +00:00
|
|
|
QRect Crop::saveCropRect() {
|
2021-02-20 07:52:48 +00:00
|
|
|
const auto savedCrop = _cropOriginal.toRect();
|
|
|
|
return (!savedCrop.topLeft().isNull() || (savedCrop.size() != _imageSize))
|
|
|
|
? savedCrop
|
|
|
|
: QRect();
|
2021-02-08 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Editor
|