2021-03-03 09:24:35 +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.h"
|
2021-03-03 09:24:35 +00:00
|
|
|
|
2021-03-21 12:38:33 +00:00
|
|
|
#include "editor/scene/scene_item_canvas.h"
|
|
|
|
#include "editor/scene/scene_item_line.h"
|
|
|
|
#include "editor/scene/scene_item_sticker.h"
|
2021-03-03 09:24:35 +00:00
|
|
|
#include "ui/rp_widget.h"
|
|
|
|
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
|
|
|
|
namespace Editor {
|
2021-03-08 16:31:48 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-07-07 21:51:45 +00:00
|
|
|
using ItemPtr = std::shared_ptr<NumberedItem>;
|
2021-03-10 15:51:23 +00:00
|
|
|
|
2021-03-08 16:31:48 +00:00
|
|
|
bool SkipMouseEvent(not_null<QGraphicsSceneMouseEvent*> event) {
|
|
|
|
return event->isAccepted() || (event->button() == Qt::RightButton);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2021-03-03 09:24:35 +00:00
|
|
|
|
|
|
|
Scene::Scene(const QRectF &rect)
|
|
|
|
: QGraphicsScene(rect)
|
2021-07-06 22:31:43 +00:00
|
|
|
, _canvas(std::make_shared<ItemCanvas>())
|
|
|
|
, _lastZ(std::make_shared<float64>(9000.)) {
|
2021-03-10 15:51:23 +00:00
|
|
|
QGraphicsScene::addItem(_canvas.get());
|
2021-03-08 16:31:48 +00:00
|
|
|
_canvas->clearPixmap();
|
|
|
|
|
|
|
|
_canvas->grabContentRequests(
|
|
|
|
) | rpl::start_with_next([=](ItemCanvas::Content &&content) {
|
2021-03-10 15:51:23 +00:00
|
|
|
const auto item = std::make_shared<ItemLine>(
|
|
|
|
std::move(content.pixmap));
|
2021-03-08 16:31:48 +00:00
|
|
|
item->setPos(content.position);
|
|
|
|
addItem(item);
|
|
|
|
_canvas->setZValue(++_lastLineZ);
|
2021-03-03 09:24:35 +00:00
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
2021-05-08 15:02:41 +00:00
|
|
|
void Scene::cancelDrawing() {
|
|
|
|
_canvas->cancelDrawing();
|
|
|
|
}
|
|
|
|
|
2021-10-04 04:46:12 +00:00
|
|
|
void Scene::addItem(ItemPtr item) {
|
2021-03-10 15:51:23 +00:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-14 09:44:49 +00:00
|
|
|
item->setNumber(_itemNumber++);
|
2021-03-10 15:51:23 +00:00
|
|
|
QGraphicsScene::addItem(item.get());
|
|
|
|
_items.push_back(std::move(item));
|
2021-03-08 16:31:48 +00:00
|
|
|
_addsItem.fire({});
|
2021-03-14 09:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:51:23 +00:00
|
|
|
void Scene::removeItem(not_null<QGraphicsItem*> item) {
|
|
|
|
const auto it = ranges::find_if(_items, [&](const ItemPtr &i) {
|
|
|
|
return i.get() == item;
|
|
|
|
});
|
|
|
|
if (it == end(_items)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
removeItem(*it);
|
|
|
|
}
|
|
|
|
|
2021-07-08 12:29:04 +00:00
|
|
|
void Scene::removeItem(const ItemPtr &item) {
|
|
|
|
item->setStatus(NumberedItem::Status::Removed);
|
2021-07-04 14:22:57 +00:00
|
|
|
_removesItem.fire({});
|
2021-03-10 15:51:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 09:24:35 +00:00
|
|
|
void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
QGraphicsScene::mousePressEvent(event);
|
2021-03-08 16:31:48 +00:00
|
|
|
if (SkipMouseEvent(event)) {
|
2021-03-03 09:24:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-08 16:31:48 +00:00
|
|
|
_canvas->handleMousePressEvent(event);
|
2021-03-03 09:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
QGraphicsScene::mouseReleaseEvent(event);
|
2021-03-08 16:31:48 +00:00
|
|
|
if (SkipMouseEvent(event)) {
|
2021-03-03 09:24:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-08 16:31:48 +00:00
|
|
|
_canvas->handleMouseReleaseEvent(event);
|
2021-03-03 09:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
QGraphicsScene::mouseMoveEvent(event);
|
2021-03-08 16:31:48 +00:00
|
|
|
if (SkipMouseEvent(event)) {
|
2021-03-03 09:24:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-08 16:31:48 +00:00
|
|
|
_canvas->handleMouseMoveEvent(event);
|
2021-03-03 09:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::applyBrush(const QColor &color, float size) {
|
2021-03-08 16:31:48 +00:00
|
|
|
_canvas->applyBrush(color, size);
|
2021-03-03 09:24:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 16:31:48 +00:00
|
|
|
rpl::producer<> Scene::addsItem() const {
|
|
|
|
return _addsItem.events();
|
2021-07-04 14:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Scene::removesItem() const {
|
|
|
|
return _removesItem.events();
|
2021-03-08 16:31:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:51:23 +00:00
|
|
|
std::vector<ItemPtr> Scene::items(
|
|
|
|
Qt::SortOrder order) const {
|
|
|
|
auto copyItems = _items;
|
2021-03-14 09:44:49 +00:00
|
|
|
|
2021-03-10 15:51:23 +00:00
|
|
|
ranges::sort(copyItems, [&](ItemPtr a, ItemPtr b) {
|
2021-07-07 21:51:45 +00:00
|
|
|
const auto numA = a->number();
|
|
|
|
const auto numB = b->number();
|
2021-03-14 09:44:49 +00:00
|
|
|
return (order == Qt::AscendingOrder) ? (numA < numB) : (numA > numB);
|
|
|
|
});
|
|
|
|
|
2021-03-10 15:51:23 +00:00
|
|
|
return copyItems;
|
2021-03-14 09:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 22:31:43 +00:00
|
|
|
std::shared_ptr<float64> Scene::lastZ() const {
|
|
|
|
return _lastZ;
|
|
|
|
}
|
|
|
|
|
2021-07-06 23:10:13 +00:00
|
|
|
void Scene::updateZoom(float64 zoom) {
|
|
|
|
for (const auto &item : items()) {
|
|
|
|
if (item->type() >= ItemBase::Type) {
|
|
|
|
static_cast<ItemBase*>(item.get())->updateZoom(zoom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 21:57:40 +00:00
|
|
|
bool Scene::hasUndo() const {
|
|
|
|
return ranges::any_of(_items, &NumberedItem::isNormalStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scene::hasRedo() const {
|
|
|
|
return ranges::any_of(_items, &NumberedItem::isUndidStatus);
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:03:00 +00:00
|
|
|
void Scene::performUndo() {
|
|
|
|
const auto filtered = items(Qt::DescendingOrder);
|
|
|
|
|
|
|
|
const auto it = ranges::find_if(filtered, &NumberedItem::isNormalStatus);
|
|
|
|
if (it != filtered.end()) {
|
|
|
|
(*it)->setStatus(NumberedItem::Status::Undid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::performRedo() {
|
|
|
|
const auto filtered = items(Qt::AscendingOrder);
|
|
|
|
|
|
|
|
const auto it = ranges::find_if(filtered, &NumberedItem::isUndidStatus);
|
|
|
|
if (it != filtered.end()) {
|
|
|
|
(*it)->setStatus(NumberedItem::Status::Normal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 12:29:04 +00:00
|
|
|
void Scene::removeIf(Fn<bool(const ItemPtr &)> proj) {
|
|
|
|
auto copy = std::vector<ItemPtr>();
|
|
|
|
for (const auto &item : _items) {
|
|
|
|
const auto toRemove = proj(item);
|
|
|
|
if (toRemove) {
|
|
|
|
// Scene loses ownership of an item.
|
|
|
|
// It seems for some reason this line causes a crash. =(
|
|
|
|
// QGraphicsScene::removeItem(item.get());
|
|
|
|
} else {
|
|
|
|
copy.push_back(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_items = std::move(copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::clearRedoList() {
|
|
|
|
for (const auto &item : _items) {
|
|
|
|
if (item->isUndidStatus()) {
|
|
|
|
item->setStatus(NumberedItem::Status::Removed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::save(SaveState state) {
|
|
|
|
removeIf([](const ItemPtr &item) {
|
|
|
|
return item->isRemovedStatus()
|
|
|
|
&& !item->hasState(SaveState::Keep)
|
|
|
|
&& !item->hasState(SaveState::Save);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto &item : _items) {
|
|
|
|
item->save(state);
|
|
|
|
}
|
|
|
|
clearSelection();
|
|
|
|
cancelDrawing();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::restore(SaveState state) {
|
|
|
|
removeIf([=](const ItemPtr &item) {
|
|
|
|
return !item->hasState(state);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto &item : _items) {
|
|
|
|
item->restore(state);
|
|
|
|
}
|
|
|
|
clearSelection();
|
|
|
|
cancelDrawing();
|
|
|
|
}
|
|
|
|
|
2021-03-03 09:24:35 +00:00
|
|
|
Scene::~Scene() {
|
2021-03-10 15:51:23 +00:00
|
|
|
// Prevent destroying by scene of all items.
|
|
|
|
QGraphicsScene::removeItem(_canvas.get());
|
|
|
|
for (const auto &item : items()) {
|
|
|
|
// Scene loses ownership of an item.
|
|
|
|
QGraphicsScene::removeItem(item.get());
|
|
|
|
}
|
2021-03-03 09:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Editor
|