Added ability to snap rotation of items with Shift key in photo editor.

This commit is contained in:
23rd 2021-03-10 12:55:02 +03:00
parent 0b5044f064
commit 184d984336

View file

@ -16,6 +16,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Editor { namespace Editor {
namespace { namespace {
constexpr auto kSnapAngle = 45.;
auto Normalized(float64 angle) { auto Normalized(float64 angle) {
return angle return angle
+ ((std::abs(angle) < 360) ? 0 : (-360 * (angle < 0 ? -1 : 1))); + ((std::abs(angle) < 360) ? 0 : (-360 * (angle < 0 ? -1 : 1)));
@ -107,17 +109,20 @@ void ItemBase::paint(
void ItemBase::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { void ItemBase::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
if (isHandling()) { if (isHandling()) {
const auto mousePos = event->pos(); const auto mousePos = event->pos();
const auto shift = event->modifiers().testFlag(Qt::ShiftModifier);
const auto isLeft = (_handle == HandleType::Left); const auto isLeft = (_handle == HandleType::Left);
// Resize. if (!shift) {
const auto p = isLeft ? (mousePos * -1) : mousePos; // Resize.
const auto dx = int(2.0 * p.x()); const auto p = isLeft ? (mousePos * -1) : mousePos;
const auto dy = int(2.0 * p.y()); const auto dx = int(2.0 * p.x());
prepareGeometryChange(); const auto dy = int(2.0 * p.y());
_horizontalSize = std::clamp( prepareGeometryChange();
(dx > dy ? dx : dy), _horizontalSize = std::clamp(
st::photoEditorItemMinSize, (dx > dy ? dx : dy),
st::photoEditorItemMaxSize); st::photoEditorItemMinSize,
updateVerticalSize(); st::photoEditorItemMaxSize);
updateVerticalSize();
}
// Rotate. // Rotate.
const auto origin = mapToScene(boundingRect().center()); const auto origin = mapToScene(boundingRect().center());
@ -126,7 +131,9 @@ void ItemBase::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
const auto diff = pos - origin; const auto diff = pos - origin;
const auto angle = Normalized((isLeft ? 180 : 0) const auto angle = Normalized((isLeft ? 180 : 0)
+ (std::atan2(diff.y(), diff.x()) * 180 / M_PI)); + (std::atan2(diff.y(), diff.x()) * 180 / M_PI));
setRotation(angle); setRotation(shift
? (std::round(angle / kSnapAngle) * kSnapAngle) // Snap rotation.
: angle);
} else { } else {
QGraphicsItem::mouseMoveEvent(event); QGraphicsItem::mouseMoveEvent(event);
} }