Fixed item handlers size when opening photo editor with modified scene.

This commit is contained in:
23rd 2021-07-07 02:10:13 +03:00
parent 5bbf3a329d
commit fe8eae09c4
7 changed files with 56 additions and 41 deletions

View file

@ -157,6 +157,7 @@ void Paint::applyTransform(QRect geometry, int angle, bool flipped) {
.flipped = flipped,
.zoom = size.width() / float64(_scene->sceneRect().width()),
};
_scene->updateZoom(_transform.zoom);
}
std::shared_ptr<Scene> Paint::saveScene() const {
@ -291,7 +292,7 @@ ItemBase::Data Paint::itemBaseData() const {
const auto x = s.width() / 2;
const auto y = s.height() / 2;
return ItemBase::Data{
.zoomValue = _transform.zoom.value(),
.initialZoom = _transform.zoom,
.zPtr = _scene->lastZ(),
.size = size,
.x = x,

View file

@ -62,7 +62,7 @@ private:
struct {
int angle = 0;
bool flipped = false;
rpl::variable<float64> zoom = 0.;
float64 zoom = 0.;
} _transform;
std::vector<SavedItem> _previousItems;

View file

@ -139,6 +139,14 @@ std::shared_ptr<float64> Scene::lastZ() const {
return _lastZ;
}
void Scene::updateZoom(float64 zoom) {
for (const auto &item : items()) {
if (item->type() >= ItemBase::Type) {
static_cast<ItemBase*>(item.get())->updateZoom(zoom);
}
}
}
Scene::~Scene() {
// Prevent destroying by scene of all items.
QGraphicsScene::removeItem(_canvas.get());

View file

@ -42,6 +42,8 @@ public:
[[nodiscard]] std::shared_ptr<float64> lastZ() const;
void updateZoom(float64 zoom);
void cancelDrawing();
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

View file

@ -51,8 +51,7 @@ void NumberedItem::setNumber(int number) {
ItemBase::ItemBase(Data data)
: _lastZ(data.zPtr)
, _imageSize(data.imageSize)
, _horizontalSize(data.size)
, _zoom(std::move(data.zoomValue)) {
, _horizontalSize(data.size) {
setFlags(QGraphicsItem::ItemIsMovable
| QGraphicsItem::ItemIsSelectable
| QGraphicsItem::ItemIsFocusable);
@ -61,37 +60,7 @@ ItemBase::ItemBase(Data data)
setZValue((*_lastZ)++);
setFlip(data.flipped);
setRotation(data.rotation);
const auto &handleSize = st::photoEditorItemHandleSize;
_zoom.value(
) | rpl::start_with_next([=](float64 zoom) {
_scaledHandleSize = handleSize / 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));
}, _lifetime);
updateZoom(data.initialZoom);
}
QRectF ItemBase::boundingRect() const {
@ -243,8 +212,9 @@ void ItemBase::actionDelete() {
void ItemBase::actionDuplicate() {
if (const auto s = static_cast<Scene*>(scene())) {
const auto zoom = st::photoEditorItemHandleSize / _scaledHandleSize;
const auto newItem = duplicate(Data{
.zoomValue = _zoom.value(),
.initialZoom = zoom,
.zPtr = _lastZ,
.size = int(_horizontalSize),
.x = int(scenePos().x() + _horizontalSize / 3),
@ -348,6 +318,39 @@ void ItemBase::setFlip(bool value) {
}
}
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));
}
void ItemBase::performFlip() {
}

View file

@ -35,9 +35,10 @@ private:
class ItemBase : public NumberedItem {
public:
enum { Type = UserType + 2 };
struct Data {
rpl::producer<float64> zoomValue;
float64 initialZoom = 0.;
std::shared_ptr<float64> zPtr;
int size = 0;
int x = 0;
@ -53,9 +54,12 @@ public:
QPainter *p,
const QStyleOptionGraphicsItem *option,
QWidget *widget) override;
int type() const override;
bool flipped() const;
void setFlip(bool value);
void updateZoom(float64 zoom);
protected:
enum HandleType {
None,
@ -119,9 +123,6 @@ private:
bool _flipped = false;
rpl::variable<float64> _zoom;
rpl::lifetime _lifetime;
};
} // namespace Editor

View file

@ -21,7 +21,7 @@ namespace Editor {
class ItemSticker : public ItemBase {
public:
enum { Type = ItemBase::Type + 2 };
enum { Type = ItemBase::Type + 1 };
ItemSticker(
not_null<DocumentData*> document,