Added ability to duplicate items in photo editor.

This commit is contained in:
23rd 2021-03-11 13:20:34 +03:00
parent 808c9e3d2c
commit 049945a9b9
5 changed files with 47 additions and 4 deletions

View file

@ -117,7 +117,6 @@ Paint::Paint(
size,
x,
y);
item->setZValue((*_lastZ)++);
_scene->addItem(item);
_scene->clearSelection();
}, lifetime());

View file

@ -56,16 +56,17 @@ ItemBase::ItemBase(
Qt::DashLine,
Qt::SquareCap,
Qt::RoundJoin)
, _horizontalSize(size) {
, _horizontalSize(size)
, _zoom(std::move(zoomValue)) {
setFlags(QGraphicsItem::ItemIsMovable
| QGraphicsItem::ItemIsSelectable
| QGraphicsItem::ItemIsFocusable);
setAcceptHoverEvents(true);
setPos(x, y);
setZValue((*_lastZ)++);
const auto &handleSize = st::photoEditorItemHandleSize;
std::move(
zoomValue
_zoom.value(
) | rpl::start_with_next([=](float64 zoom) {
_scaledHandleSize = handleSize / zoom;
_scaledInnerMargins = QMarginsF(
@ -187,6 +188,21 @@ void ItemBase::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
_menu->addAction(tr::lng_photo_editor_menu_flip(tr::now), [=] {
setFlip(!flipped());
});
_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);
}
});
_menu->popup(event->screenPos());
}

View file

@ -70,6 +70,12 @@ protected:
void setAspectRatio(float64 aspectRatio);
virtual void performFlip();
virtual std::shared_ptr<ItemBase> duplicate(
rpl::producer<float64> zoomValue,
std::shared_ptr<float64> zPtr,
int size,
int x,
int y) const = 0;
private:
HandleType handleType(const QPointF &pos) const;
QRectF rightHandleRect() const;
@ -94,6 +100,7 @@ private:
bool _flipped = false;
rpl::variable<float64> _zoom;
rpl::lifetime _lifetime;
};

View file

@ -101,4 +101,19 @@ void ItemSticker::performFlip() {
_pixmap = _pixmap.transformed(QTransform().scale(-1, 1));
}
std::shared_ptr<ItemBase> ItemSticker::duplicate(
rpl::producer<float64> zoomValue,
std::shared_ptr<float64> zPtr,
int size,
int x,
int y) const {
return std::make_shared<ItemSticker>(
_document,
std::move(zoomValue),
std::move(zPtr),
size,
x,
y);
}
} // namespace Editor

View file

@ -38,6 +38,12 @@ public:
int type() const override;
protected:
void performFlip() override;
std::shared_ptr<ItemBase> duplicate(
rpl::producer<float64> zoomValue,
std::shared_ptr<float64> zPtr,
int size,
int x,
int y) const override;
private:
const not_null<DocumentData*> _document;
const std::shared_ptr<Data::DocumentMedia> _mediaView;