Fixed independence of item and scene transforms when adding.

This commit is contained in:
23rd 2021-03-21 20:00:56 +03:00
parent 832dd8d50c
commit c5c707f0fd
4 changed files with 31 additions and 7 deletions

View file

@ -112,11 +112,13 @@ Paint::Paint(
const auto y = s.height() / 2;
const auto item = std::make_shared<ItemSticker>(
document,
_zoom.value(),
_transform.zoom.value(),
_lastZ,
size,
x,
y);
item->setFlip(_transform.flipped);
item->setRotation(-_transform.angle);
_scene->addItem(item);
_scene->clearSelection();
}, lifetime());
@ -152,7 +154,11 @@ void Paint::applyTransform(QRect geometry, int angle, bool flipped) {
_view->setTransform(QTransform().scale(ratioW, ratioH).rotate(angle));
_view->setGeometry(QRect(QPoint(), size));
_zoom = size.width() / float64(_scene->sceneRect().width());
_transform = {
.angle = angle,
.flipped = flipped,
.zoom = size.width() / float64(_scene->sceneRect().width()),
};
}
std::shared_ptr<Scene> Paint::saveScene() const {

View file

@ -55,13 +55,18 @@ private:
const base::unique_qptr<QGraphicsView> _view;
const QSize _imageSize;
struct {
int angle = 0;
bool flipped = false;
rpl::variable<float64> zoom = 0.;
} _transform;
std::vector<SavedItem> _previousItems;
std::vector<std::shared_ptr<QGraphicsItem>> _itemsToRemove;
rpl::variable<bool> _hasUndo = true;
rpl::variable<bool> _hasRedo = true;
rpl::variable<float64> _zoom = 0.;
};

View file

@ -51,8 +51,8 @@ ItemSticker::ItemSticker(
Lottie::Quality::High);
_lottie.player->updates(
) | rpl::start_with_next([=] {
_pixmap = App::pixmapFromImageInPlace(
_lottie.player->frame());
updatePixmap(App::pixmapFromImageInPlace(
_lottie.player->frame()));
_lottie.player = nullptr;
_lottie.lifetime.destroy();
update();
@ -63,11 +63,12 @@ ItemSticker::ItemSticker(
if (!sticker) {
return false;
}
_pixmap = sticker->pixNoCache(
auto pixmap = sticker->pixNoCache(
sticker->width() * cIntRetinaFactor(),
sticker->height() * cIntRetinaFactor(),
Images::Option::Smooth);
_pixmap.setDevicePixelRatio(cRetinaFactor());
pixmap.setDevicePixelRatio(cRetinaFactor());
updatePixmap(std::move(pixmap));
return true;
};
if (!updateThumbnail()) {
@ -81,6 +82,15 @@ ItemSticker::ItemSticker(
}
}
void ItemSticker::updatePixmap(QPixmap &&pixmap) {
_pixmap = std::move(pixmap);
if (flipped()) {
performFlip();
} else {
update();
}
}
void ItemSticker::paint(
QPainter *p,
const QStyleOptionGraphicsItem *option,
@ -99,6 +109,7 @@ int ItemSticker::type() const {
void ItemSticker::performFlip() {
_pixmap = _pixmap.transformed(QTransform().scale(-1, 1));
update();
}
std::shared_ptr<ItemBase> ItemSticker::duplicate(

View file

@ -48,6 +48,8 @@ private:
const not_null<DocumentData*> _document;
const std::shared_ptr<Data::DocumentMedia> _mediaView;
void updatePixmap(QPixmap &&pixmap);
struct {
std::unique_ptr<Lottie::SinglePlayer> player;
rpl::lifetime lifetime;