mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Fixed item handlers size when opening photo editor with modified scene.
This commit is contained in:
parent
5bbf3a329d
commit
fe8eae09c4
7 changed files with 56 additions and 41 deletions
|
@ -157,6 +157,7 @@ void Paint::applyTransform(QRect geometry, int angle, bool flipped) {
|
||||||
.flipped = flipped,
|
.flipped = flipped,
|
||||||
.zoom = size.width() / float64(_scene->sceneRect().width()),
|
.zoom = size.width() / float64(_scene->sceneRect().width()),
|
||||||
};
|
};
|
||||||
|
_scene->updateZoom(_transform.zoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Scene> Paint::saveScene() const {
|
std::shared_ptr<Scene> Paint::saveScene() const {
|
||||||
|
@ -291,7 +292,7 @@ ItemBase::Data Paint::itemBaseData() const {
|
||||||
const auto x = s.width() / 2;
|
const auto x = s.width() / 2;
|
||||||
const auto y = s.height() / 2;
|
const auto y = s.height() / 2;
|
||||||
return ItemBase::Data{
|
return ItemBase::Data{
|
||||||
.zoomValue = _transform.zoom.value(),
|
.initialZoom = _transform.zoom,
|
||||||
.zPtr = _scene->lastZ(),
|
.zPtr = _scene->lastZ(),
|
||||||
.size = size,
|
.size = size,
|
||||||
.x = x,
|
.x = x,
|
||||||
|
|
|
@ -62,7 +62,7 @@ private:
|
||||||
struct {
|
struct {
|
||||||
int angle = 0;
|
int angle = 0;
|
||||||
bool flipped = false;
|
bool flipped = false;
|
||||||
rpl::variable<float64> zoom = 0.;
|
float64 zoom = 0.;
|
||||||
} _transform;
|
} _transform;
|
||||||
|
|
||||||
std::vector<SavedItem> _previousItems;
|
std::vector<SavedItem> _previousItems;
|
||||||
|
|
|
@ -139,6 +139,14 @@ std::shared_ptr<float64> Scene::lastZ() const {
|
||||||
return _lastZ;
|
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() {
|
Scene::~Scene() {
|
||||||
// Prevent destroying by scene of all items.
|
// Prevent destroying by scene of all items.
|
||||||
QGraphicsScene::removeItem(_canvas.get());
|
QGraphicsScene::removeItem(_canvas.get());
|
||||||
|
|
|
@ -42,6 +42,8 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] std::shared_ptr<float64> lastZ() const;
|
[[nodiscard]] std::shared_ptr<float64> lastZ() const;
|
||||||
|
|
||||||
|
void updateZoom(float64 zoom);
|
||||||
|
|
||||||
void cancelDrawing();
|
void cancelDrawing();
|
||||||
protected:
|
protected:
|
||||||
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
|
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
|
||||||
|
|
|
@ -51,8 +51,7 @@ void NumberedItem::setNumber(int number) {
|
||||||
ItemBase::ItemBase(Data data)
|
ItemBase::ItemBase(Data data)
|
||||||
: _lastZ(data.zPtr)
|
: _lastZ(data.zPtr)
|
||||||
, _imageSize(data.imageSize)
|
, _imageSize(data.imageSize)
|
||||||
, _horizontalSize(data.size)
|
, _horizontalSize(data.size) {
|
||||||
, _zoom(std::move(data.zoomValue)) {
|
|
||||||
setFlags(QGraphicsItem::ItemIsMovable
|
setFlags(QGraphicsItem::ItemIsMovable
|
||||||
| QGraphicsItem::ItemIsSelectable
|
| QGraphicsItem::ItemIsSelectable
|
||||||
| QGraphicsItem::ItemIsFocusable);
|
| QGraphicsItem::ItemIsFocusable);
|
||||||
|
@ -61,37 +60,7 @@ ItemBase::ItemBase(Data data)
|
||||||
setZValue((*_lastZ)++);
|
setZValue((*_lastZ)++);
|
||||||
setFlip(data.flipped);
|
setFlip(data.flipped);
|
||||||
setRotation(data.rotation);
|
setRotation(data.rotation);
|
||||||
|
updateZoom(data.initialZoom);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QRectF ItemBase::boundingRect() const {
|
QRectF ItemBase::boundingRect() const {
|
||||||
|
@ -243,8 +212,9 @@ void ItemBase::actionDelete() {
|
||||||
|
|
||||||
void ItemBase::actionDuplicate() {
|
void ItemBase::actionDuplicate() {
|
||||||
if (const auto s = static_cast<Scene*>(scene())) {
|
if (const auto s = static_cast<Scene*>(scene())) {
|
||||||
|
const auto zoom = st::photoEditorItemHandleSize / _scaledHandleSize;
|
||||||
const auto newItem = duplicate(Data{
|
const auto newItem = duplicate(Data{
|
||||||
.zoomValue = _zoom.value(),
|
.initialZoom = zoom,
|
||||||
.zPtr = _lastZ,
|
.zPtr = _lastZ,
|
||||||
.size = int(_horizontalSize),
|
.size = int(_horizontalSize),
|
||||||
.x = int(scenePos().x() + _horizontalSize / 3),
|
.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() {
|
void ItemBase::performFlip() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,9 +35,10 @@ private:
|
||||||
|
|
||||||
class ItemBase : public NumberedItem {
|
class ItemBase : public NumberedItem {
|
||||||
public:
|
public:
|
||||||
|
enum { Type = UserType + 2 };
|
||||||
|
|
||||||
struct Data {
|
struct Data {
|
||||||
rpl::producer<float64> zoomValue;
|
float64 initialZoom = 0.;
|
||||||
std::shared_ptr<float64> zPtr;
|
std::shared_ptr<float64> zPtr;
|
||||||
int size = 0;
|
int size = 0;
|
||||||
int x = 0;
|
int x = 0;
|
||||||
|
@ -53,9 +54,12 @@ public:
|
||||||
QPainter *p,
|
QPainter *p,
|
||||||
const QStyleOptionGraphicsItem *option,
|
const QStyleOptionGraphicsItem *option,
|
||||||
QWidget *widget) override;
|
QWidget *widget) override;
|
||||||
|
int type() const override;
|
||||||
|
|
||||||
bool flipped() const;
|
bool flipped() const;
|
||||||
void setFlip(bool value);
|
void setFlip(bool value);
|
||||||
|
|
||||||
|
void updateZoom(float64 zoom);
|
||||||
protected:
|
protected:
|
||||||
enum HandleType {
|
enum HandleType {
|
||||||
None,
|
None,
|
||||||
|
@ -119,9 +123,6 @@ private:
|
||||||
|
|
||||||
bool _flipped = false;
|
bool _flipped = false;
|
||||||
|
|
||||||
rpl::variable<float64> _zoom;
|
|
||||||
rpl::lifetime _lifetime;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Editor
|
} // namespace Editor
|
||||||
|
|
|
@ -21,7 +21,7 @@ namespace Editor {
|
||||||
|
|
||||||
class ItemSticker : public ItemBase {
|
class ItemSticker : public ItemBase {
|
||||||
public:
|
public:
|
||||||
enum { Type = ItemBase::Type + 2 };
|
enum { Type = ItemBase::Type + 1 };
|
||||||
|
|
||||||
ItemSticker(
|
ItemSticker(
|
||||||
not_null<DocumentData*> document,
|
not_null<DocumentData*> document,
|
||||||
|
|
Loading…
Add table
Reference in a new issue