2021-02-23 06:58:25 +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
|
|
|
|
*/
|
2021-03-21 12:38:33 +00:00
|
|
|
#include "editor/scene/scene_item_base.h"
|
2021-02-23 06:58:25 +00:00
|
|
|
|
2021-03-21 12:38:33 +00:00
|
|
|
#include "editor/scene/scene.h"
|
2021-03-10 12:07:04 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "ui/widgets/popup_menu.h"
|
2021-02-23 06:58:25 +00:00
|
|
|
#include "styles/style_editor.h"
|
|
|
|
|
2021-03-10 12:07:04 +00:00
|
|
|
#include <QGraphicsScene>
|
2021-02-23 06:58:25 +00:00
|
|
|
#include <QGraphicsSceneHoverEvent>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
|
|
|
|
namespace Editor {
|
|
|
|
namespace {
|
|
|
|
|
2021-03-10 09:55:02 +00:00
|
|
|
constexpr auto kSnapAngle = 45.;
|
|
|
|
|
2021-02-23 06:58:25 +00:00
|
|
|
auto Normalized(float64 angle) {
|
|
|
|
return angle
|
|
|
|
+ ((std::abs(angle) < 360) ? 0 : (-360 * (angle < 0 ? -1 : 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2021-04-10 13:17:18 +00:00
|
|
|
|
|
|
|
int NumberedItem::type() const {
|
|
|
|
return NumberedItem::Type;
|
|
|
|
}
|
2021-02-23 06:58:25 +00:00
|
|
|
|
2021-03-14 09:44:49 +00:00
|
|
|
int NumberedItem::number() const {
|
|
|
|
return _number;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumberedItem::setNumber(int number) {
|
|
|
|
_number = number;
|
|
|
|
}
|
|
|
|
|
2021-03-10 07:45:42 +00:00
|
|
|
ItemBase::ItemBase(
|
|
|
|
rpl::producer<float64> zoomValue,
|
|
|
|
std::shared_ptr<float64> zPtr,
|
|
|
|
int size,
|
|
|
|
int x,
|
|
|
|
int y)
|
2021-02-23 06:58:25 +00:00
|
|
|
: _lastZ(zPtr)
|
2021-03-11 10:20:34 +00:00
|
|
|
, _horizontalSize(size)
|
|
|
|
, _zoom(std::move(zoomValue)) {
|
2021-02-23 06:58:25 +00:00
|
|
|
setFlags(QGraphicsItem::ItemIsMovable
|
|
|
|
| QGraphicsItem::ItemIsSelectable
|
|
|
|
| QGraphicsItem::ItemIsFocusable);
|
|
|
|
setAcceptHoverEvents(true);
|
|
|
|
setPos(x, y);
|
2021-03-11 10:20:34 +00:00
|
|
|
setZValue((*_lastZ)++);
|
2021-03-10 07:45:42 +00:00
|
|
|
|
|
|
|
const auto &handleSize = st::photoEditorItemHandleSize;
|
2021-03-11 10:20:34 +00:00
|
|
|
_zoom.value(
|
2021-03-10 07:45:42 +00:00
|
|
|
) | rpl::start_with_next([=](float64 zoom) {
|
|
|
|
_scaledHandleSize = handleSize / zoom;
|
|
|
|
_scaledInnerMargins = QMarginsF(
|
|
|
|
_scaledHandleSize,
|
|
|
|
_scaledHandleSize,
|
|
|
|
_scaledHandleSize,
|
|
|
|
_scaledHandleSize) * 0.5;
|
2021-03-21 17:22:22 +00:00
|
|
|
_sizeLimits = {
|
|
|
|
.min = int(st::photoEditorItemMinSize / zoom),
|
|
|
|
.max = int(st::photoEditorItemMaxSize / zoom),
|
|
|
|
};
|
2021-03-21 17:37:45 +00:00
|
|
|
|
|
|
|
updatePens(QPen(
|
|
|
|
QBrush(),
|
|
|
|
1 / zoom,
|
|
|
|
Qt::DashLine,
|
|
|
|
Qt::SquareCap,
|
|
|
|
Qt::RoundJoin));
|
2021-03-10 07:45:42 +00:00
|
|
|
}, _lifetime);
|
2021-02-23 06:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF ItemBase::boundingRect() const {
|
2021-03-10 07:45:42 +00:00
|
|
|
return innerRect() + _scaledInnerMargins;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF ItemBase::contentRect() const {
|
|
|
|
return innerRect() - _scaledInnerMargins;
|
2021-02-23 06:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF ItemBase::innerRect() const {
|
2021-03-09 15:12:23 +00:00
|
|
|
const auto &hSize = _horizontalSize;
|
|
|
|
const auto &vSize = _verticalSize;
|
|
|
|
return QRectF(-hSize / 2, -vSize / 2, hSize, vSize);
|
2021-02-23 06:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::paint(
|
|
|
|
QPainter *p,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
QWidget *) {
|
|
|
|
if (!(option->state & QStyle::State_Selected)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PainterHighQualityEnabler hq(*p);
|
2021-03-21 17:37:45 +00:00
|
|
|
const auto hasFocus = (option->state & QStyle::State_HasFocus);
|
|
|
|
p->setPen(hasFocus ? _pens.select : _pens.selectInactive);
|
2021-02-23 06:58:25 +00:00
|
|
|
p->drawRect(innerRect());
|
|
|
|
|
2021-03-21 17:37:45 +00:00
|
|
|
p->setPen(hasFocus ? _pens.handle : _pens.handleInactive);
|
2021-02-23 06:58:25 +00:00
|
|
|
p->setBrush(st::photoEditorItemBaseHandleFg);
|
|
|
|
p->drawEllipse(rightHandleRect());
|
|
|
|
p->drawEllipse(leftHandleRect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
if (isHandling()) {
|
|
|
|
const auto mousePos = event->pos();
|
2021-03-10 09:55:02 +00:00
|
|
|
const auto shift = event->modifiers().testFlag(Qt::ShiftModifier);
|
2021-02-23 06:58:25 +00:00
|
|
|
const auto isLeft = (_handle == HandleType::Left);
|
2021-03-10 09:55:02 +00:00
|
|
|
if (!shift) {
|
|
|
|
// Resize.
|
|
|
|
const auto p = isLeft ? (mousePos * -1) : mousePos;
|
|
|
|
const auto dx = int(2.0 * p.x());
|
|
|
|
const auto dy = int(2.0 * p.y());
|
|
|
|
prepareGeometryChange();
|
|
|
|
_horizontalSize = std::clamp(
|
|
|
|
(dx > dy ? dx : dy),
|
2021-03-21 17:22:22 +00:00
|
|
|
_sizeLimits.min,
|
|
|
|
_sizeLimits.max);
|
2021-03-10 09:55:02 +00:00
|
|
|
updateVerticalSize();
|
|
|
|
}
|
2021-02-23 06:58:25 +00:00
|
|
|
|
|
|
|
// Rotate.
|
|
|
|
const auto origin = mapToScene(boundingRect().center());
|
|
|
|
const auto pos = mapToScene(mousePos);
|
|
|
|
|
|
|
|
const auto diff = pos - origin;
|
|
|
|
const auto angle = Normalized((isLeft ? 180 : 0)
|
|
|
|
+ (std::atan2(diff.y(), diff.x()) * 180 / M_PI));
|
2021-03-10 09:55:02 +00:00
|
|
|
setRotation(shift
|
|
|
|
? (std::round(angle / kSnapAngle) * kSnapAngle) // Snap rotation.
|
|
|
|
: angle);
|
2021-02-23 06:58:25 +00:00
|
|
|
} else {
|
|
|
|
QGraphicsItem::mouseMoveEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
|
|
|
|
setCursor(isHandling()
|
|
|
|
? Qt::ClosedHandCursor
|
|
|
|
: (handleType(event->pos()) != HandleType::None) && isSelected()
|
|
|
|
? Qt::OpenHandCursor
|
|
|
|
: Qt::ArrowCursor);
|
|
|
|
QGraphicsItem::hoverMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::mousePressEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
setZValue((*_lastZ)++);
|
|
|
|
if (event->button() == Qt::LeftButton) {
|
|
|
|
_handle = handleType(event->pos());
|
2021-03-10 10:49:47 +00:00
|
|
|
}
|
|
|
|
if (isHandling()) {
|
|
|
|
setCursor(Qt::ClosedHandCursor);
|
2021-02-23 06:58:25 +00:00
|
|
|
} else {
|
|
|
|
QGraphicsItem::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
if ((event->button() == Qt::LeftButton) && isHandling()) {
|
|
|
|
_handle = HandleType::None;
|
|
|
|
} else {
|
|
|
|
QGraphicsItem::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:07:04 +00:00
|
|
|
void ItemBase::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
|
|
|
|
if (!isSelected() && scene()) {
|
|
|
|
scene()->clearSelection();
|
|
|
|
setSelected(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_menu = base::make_unique_q<Ui::PopupMenu>(nullptr);
|
2021-03-10 17:50:35 +00:00
|
|
|
_menu->addAction(tr::lng_photo_editor_menu_delete(tr::now), [=] {
|
2021-03-10 15:51:23 +00:00
|
|
|
if (const auto s = static_cast<Scene*>(scene())) {
|
|
|
|
s->removeItem(this);
|
2021-03-10 12:07:04 +00:00
|
|
|
}
|
|
|
|
});
|
2021-03-10 17:50:35 +00:00
|
|
|
_menu->addAction(tr::lng_photo_editor_menu_flip(tr::now), [=] {
|
|
|
|
setFlip(!flipped());
|
|
|
|
});
|
2021-03-11 10:20:34 +00:00
|
|
|
_menu->addAction(tr::lng_photo_editor_menu_duplicate(tr::now), [=] {
|
|
|
|
if (const auto s = static_cast<Scene*>(scene())) {
|
|
|
|
const auto newItem = duplicate(
|
|
|
|
_zoom.value(),
|
|
|
|
_lastZ,
|
|
|
|
_horizontalSize,
|
|
|
|
scenePos().x() + _horizontalSize / 3,
|
|
|
|
scenePos().y() + _verticalSize / 3);
|
|
|
|
newItem->setFlip(flipped());
|
|
|
|
newItem->setRotation(rotation());
|
|
|
|
s->clearSelection();
|
|
|
|
newItem->setSelected(true);
|
|
|
|
s->addItem(newItem);
|
|
|
|
}
|
|
|
|
});
|
2021-03-10 12:07:04 +00:00
|
|
|
|
|
|
|
_menu->popup(event->screenPos());
|
|
|
|
}
|
|
|
|
|
2021-02-23 06:58:25 +00:00
|
|
|
int ItemBase::type() const {
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF ItemBase::rightHandleRect() const {
|
|
|
|
return QRectF(
|
2021-03-10 07:45:42 +00:00
|
|
|
(_horizontalSize / 2) - (_scaledHandleSize / 2),
|
|
|
|
0 - (_scaledHandleSize / 2),
|
|
|
|
_scaledHandleSize,
|
|
|
|
_scaledHandleSize);
|
2021-02-23 06:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF ItemBase::leftHandleRect() const {
|
|
|
|
return QRectF(
|
2021-03-10 07:45:42 +00:00
|
|
|
(-_horizontalSize / 2) - (_scaledHandleSize / 2),
|
|
|
|
0 - (_scaledHandleSize / 2),
|
|
|
|
_scaledHandleSize,
|
|
|
|
_scaledHandleSize);
|
2021-02-23 06:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ItemBase::isHandling() const {
|
|
|
|
return _handle != HandleType::None;
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:12:23 +00:00
|
|
|
float64 ItemBase::size() const {
|
|
|
|
return _horizontalSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::updateVerticalSize() {
|
|
|
|
_verticalSize = _horizontalSize * _aspectRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::setAspectRatio(float64 aspectRatio) {
|
|
|
|
_aspectRatio = aspectRatio;
|
|
|
|
updateVerticalSize();
|
2021-02-23 06:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemBase::HandleType ItemBase::handleType(const QPointF &pos) const {
|
|
|
|
return rightHandleRect().contains(pos)
|
|
|
|
? HandleType::Right
|
|
|
|
: leftHandleRect().contains(pos)
|
|
|
|
? HandleType::Left
|
|
|
|
: HandleType::None;
|
|
|
|
}
|
|
|
|
|
2021-03-10 17:50:35 +00:00
|
|
|
bool ItemBase::flipped() const {
|
|
|
|
return _flipped;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::setFlip(bool value) {
|
|
|
|
if (_flipped != value) {
|
|
|
|
performFlip();
|
|
|
|
_flipped = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::performFlip() {
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:37:45 +00:00
|
|
|
void ItemBase::updatePens(QPen pen) {
|
|
|
|
_pens = {
|
|
|
|
.select = pen,
|
|
|
|
.selectInactive = pen,
|
|
|
|
.handle = pen,
|
|
|
|
.handleInactive = pen,
|
|
|
|
};
|
|
|
|
_pens.select.setColor(Qt::white);
|
|
|
|
_pens.selectInactive.setColor(Qt::gray);
|
|
|
|
_pens.handle.setColor(Qt::white);
|
|
|
|
_pens.handleInactive.setColor(Qt::gray);
|
|
|
|
_pens.handle.setStyle(Qt::SolidLine);
|
|
|
|
_pens.handleInactive.setStyle(Qt::SolidLine);
|
|
|
|
}
|
|
|
|
|
2021-02-23 06:58:25 +00:00
|
|
|
} // namespace Editor
|