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-04-10 14:16:37 +00:00
|
|
|
const auto kDuplicateSequence = QKeySequence("ctrl+d");
|
|
|
|
const auto kFlipSequence = QKeySequence("ctrl+s");
|
|
|
|
const auto kDeleteSequence = QKeySequence("delete");
|
|
|
|
|
2021-07-06 21:13:40 +00:00
|
|
|
constexpr auto kMinSizeRatio = 0.05;
|
|
|
|
constexpr auto kMaxSizeRatio = 1.00;
|
|
|
|
|
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-07-07 21:51:58 +00:00
|
|
|
NumberedItem::Status NumberedItem::status() const {
|
|
|
|
return _status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NumberedItem::isNormalStatus() const {
|
|
|
|
return _status == Status::Normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NumberedItem::isUndidStatus() const {
|
|
|
|
return _status == Status::Undid;
|
|
|
|
}
|
|
|
|
|
2021-07-08 12:29:04 +00:00
|
|
|
bool NumberedItem::isRemovedStatus() const {
|
|
|
|
return _status == Status::Removed;
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:25:04 +00:00
|
|
|
void NumberedItem::save(SaveState state) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumberedItem::restore(SaveState state) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NumberedItem::hasState(SaveState state) const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-07 21:51:58 +00:00
|
|
|
void NumberedItem::setStatus(Status status) {
|
|
|
|
if (status != _status) {
|
|
|
|
_status = status;
|
|
|
|
setVisible(status == Status::Normal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-04 12:57:07 +00:00
|
|
|
ItemBase::ItemBase(Data data)
|
|
|
|
: _lastZ(data.zPtr)
|
2021-07-06 21:13:40 +00:00
|
|
|
, _imageSize(data.imageSize)
|
2021-07-06 23:10:13 +00:00
|
|
|
, _horizontalSize(data.size) {
|
2021-02-23 06:58:25 +00:00
|
|
|
setFlags(QGraphicsItem::ItemIsMovable
|
|
|
|
| QGraphicsItem::ItemIsSelectable
|
|
|
|
| QGraphicsItem::ItemIsFocusable);
|
|
|
|
setAcceptHoverEvents(true);
|
2021-07-07 14:13:40 +00:00
|
|
|
applyData(data);
|
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
|
2021-09-27 08:13:57 +00:00
|
|
|
? (base::SafeRound(angle / kSnapAngle) * kSnapAngle)
|
2021-03-10 09:55:02 +00:00
|
|
|
: 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) {
|
2021-04-10 14:16:37 +00:00
|
|
|
if (scene()) {
|
2021-03-10 12:07:04 +00:00
|
|
|
scene()->clearSelection();
|
|
|
|
setSelected(true);
|
|
|
|
}
|
|
|
|
|
2021-04-10 14:16:37 +00:00
|
|
|
const auto add = [&](
|
|
|
|
auto base,
|
|
|
|
const QKeySequence &sequence,
|
|
|
|
Fn<void()> callback) {
|
|
|
|
// TODO: refactor.
|
|
|
|
const auto sequenceText = QChar('\t')
|
|
|
|
+ sequence.toString(QKeySequence::NativeText);
|
|
|
|
_menu->addAction(base(tr::now) + sequenceText, std::move(callback));
|
|
|
|
};
|
|
|
|
|
2021-03-10 12:07:04 +00:00
|
|
|
_menu = base::make_unique_q<Ui::PopupMenu>(nullptr);
|
2021-04-10 14:16:37 +00:00
|
|
|
add(
|
|
|
|
tr::lng_photo_editor_menu_delete,
|
|
|
|
kDeleteSequence,
|
|
|
|
[=] { actionDelete(); });
|
|
|
|
add(
|
|
|
|
tr::lng_photo_editor_menu_flip,
|
|
|
|
kFlipSequence,
|
|
|
|
[=] { actionFlip(); });
|
|
|
|
add(
|
|
|
|
tr::lng_photo_editor_menu_duplicate,
|
|
|
|
kDuplicateSequence,
|
|
|
|
[=] { actionDuplicate(); });
|
|
|
|
|
|
|
|
_menu->popup(event->screenPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::performForSelectedItems(Action action) {
|
|
|
|
if (const auto s = scene()) {
|
|
|
|
for (const auto item : s->selectedItems()) {
|
|
|
|
if (const auto base = static_cast<ItemBase*>(item)) {
|
|
|
|
(base->*action)();
|
|
|
|
}
|
2021-03-10 12:07:04 +00:00
|
|
|
}
|
2021-04-10 14:16:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::actionFlip() {
|
|
|
|
setFlip(!flipped());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::actionDelete() {
|
|
|
|
if (const auto s = static_cast<Scene*>(scene())) {
|
|
|
|
s->removeItem(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::actionDuplicate() {
|
|
|
|
if (const auto s = static_cast<Scene*>(scene())) {
|
2021-07-07 14:13:40 +00:00
|
|
|
auto data = generateData();
|
|
|
|
data.x += int(_horizontalSize / 3);
|
|
|
|
data.y += int(_verticalSize / 3);
|
|
|
|
const auto newItem = duplicate(std::move(data));
|
2021-04-10 14:16:37 +00:00
|
|
|
if (hasFocus()) {
|
|
|
|
newItem->setFocus();
|
2021-03-11 10:20:34 +00:00
|
|
|
}
|
2021-04-10 14:16:37 +00:00
|
|
|
const auto selected = isSelected();
|
|
|
|
newItem->setSelected(selected);
|
|
|
|
setSelected(false);
|
|
|
|
s->addItem(newItem);
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 12:07:04 +00:00
|
|
|
|
2021-05-06 18:23:04 +00:00
|
|
|
void ItemBase::keyPressEvent(QKeyEvent *e) {
|
|
|
|
if (e->key() == Qt::Key_Escape) {
|
|
|
|
if (const auto s = scene()) {
|
|
|
|
s->clearSelection();
|
|
|
|
s->clearFocus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handleActionKey(e);
|
2021-03-10 12:07:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 14:16:37 +00:00
|
|
|
void ItemBase::handleActionKey(not_null<QKeyEvent*> e) {
|
|
|
|
const auto matches = [&](const QKeySequence &sequence) {
|
|
|
|
const auto searchKey = (e->modifiers() | e->key())
|
|
|
|
& ~(Qt::KeypadModifier | Qt::GroupSwitchModifier);
|
|
|
|
const auto events = QKeySequence(searchKey);
|
|
|
|
return sequence.matches(events) == QKeySequence::ExactMatch;
|
|
|
|
};
|
|
|
|
if (matches(kDuplicateSequence)) {
|
|
|
|
performForSelectedItems(&ItemBase::actionDuplicate);
|
|
|
|
} else if (matches(kDeleteSequence)) {
|
|
|
|
performForSelectedItems(&ItemBase::actionDelete);
|
|
|
|
} else if (matches(kFlipSequence)) {
|
|
|
|
performForSelectedItems(&ItemBase::actionFlip);
|
|
|
|
}
|
2021-02-23 06:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2021-05-06 21:47:49 +00:00
|
|
|
const auto verticalSize = _horizontalSize * _aspectRatio;
|
|
|
|
_verticalSize = std::max(
|
|
|
|
verticalSize,
|
2021-07-06 21:13:40 +00:00
|
|
|
float64(_sizeLimits.min));
|
|
|
|
if (verticalSize < _sizeLimits.min) {
|
2021-05-06 21:47:49 +00:00
|
|
|
_horizontalSize = _verticalSize / _aspectRatio;
|
|
|
|
}
|
2021-03-09 15:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 23:10:13 +00:00
|
|
|
int ItemBase::type() const {
|
|
|
|
return ItemBase::Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::updateZoom(float64 zoom) {
|
|
|
|
_scaledHandleSize = st::photoEditorItemHandleSize / zoom;
|
|
|
|
_scaledInnerMargins = QMarginsF(
|
|
|
|
_scaledHandleSize,
|
|
|
|
_scaledHandleSize,
|
|
|
|
_scaledHandleSize,
|
|
|
|
_scaledHandleSize) * 0.5;
|
|
|
|
|
|
|
|
const auto maxSide = std::max(
|
|
|
|
_imageSize.width(),
|
|
|
|
_imageSize.height());
|
|
|
|
_sizeLimits = {
|
|
|
|
.min = int(maxSide * kMinSizeRatio),
|
|
|
|
.max = int(maxSide * kMaxSizeRatio),
|
|
|
|
};
|
|
|
|
_horizontalSize = std::clamp(
|
|
|
|
_horizontalSize,
|
|
|
|
float64(_sizeLimits.min),
|
|
|
|
float64(_sizeLimits.max));
|
|
|
|
updateVerticalSize();
|
|
|
|
|
|
|
|
updatePens(QPen(
|
|
|
|
QBrush(),
|
|
|
|
1 / zoom,
|
|
|
|
Qt::DashLine,
|
|
|
|
Qt::SquareCap,
|
|
|
|
Qt::RoundJoin));
|
|
|
|
}
|
|
|
|
|
2021-03-10 17:50:35 +00:00
|
|
|
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-07-07 14:13:40 +00:00
|
|
|
ItemBase::Data ItemBase::generateData() const {
|
|
|
|
return {
|
|
|
|
.initialZoom = (st::photoEditorItemHandleSize / _scaledHandleSize),
|
|
|
|
.zPtr = _lastZ,
|
|
|
|
.size = int(_horizontalSize),
|
|
|
|
.x = int(scenePos().x()),
|
|
|
|
.y = int(scenePos().y()),
|
|
|
|
.flipped = flipped(),
|
|
|
|
.rotation = int(rotation()),
|
|
|
|
.imageSize = _imageSize,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::applyData(const Data &data) {
|
|
|
|
// _lastZ is const.
|
|
|
|
// _imageSize is const.
|
|
|
|
_horizontalSize = data.size;
|
|
|
|
setPos(data.x, data.y);
|
|
|
|
setZValue((*_lastZ)++);
|
|
|
|
setFlip(data.flipped);
|
|
|
|
setRotation(data.rotation);
|
|
|
|
updateZoom(data.initialZoom);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:14:50 +00:00
|
|
|
void ItemBase::save(SaveState state) {
|
2021-07-08 12:29:04 +00:00
|
|
|
const auto z = zValue();
|
|
|
|
auto &saved = (state == SaveState::Keep) ? _keeped : _saved;
|
|
|
|
saved = {
|
|
|
|
.data = generateData(),
|
|
|
|
.zValue = z,
|
|
|
|
.status = status(),
|
|
|
|
};
|
2021-07-07 18:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::restore(SaveState state) {
|
2021-07-07 22:25:04 +00:00
|
|
|
if (!hasState(state)) {
|
2021-07-07 18:14:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-07-07 22:25:04 +00:00
|
|
|
const auto &saved = (state == SaveState::Keep) ? _keeped : _saved;
|
2021-07-07 18:14:50 +00:00
|
|
|
applyData(saved.data);
|
|
|
|
setZValue(saved.zValue);
|
2021-07-08 12:29:04 +00:00
|
|
|
setStatus(saved.status);
|
2021-07-07 18:14:50 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 22:25:04 +00:00
|
|
|
bool ItemBase::hasState(SaveState state) const {
|
|
|
|
const auto &saved = (state == SaveState::Keep) ? _keeped : _saved;
|
|
|
|
return saved.zValue;
|
|
|
|
}
|
|
|
|
|
2021-02-23 06:58:25 +00:00
|
|
|
} // namespace Editor
|