Skip to content

Commit

Permalink
fix: geometry issue of preview windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ffiirree committed Sep 19, 2024
1 parent e3cafdf commit b441aba
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
30 changes: 21 additions & 9 deletions src/preview/image-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,22 @@ void ImageWindow::preview(const std::shared_ptr<QMimeData>& mimedata)
opacity_ = 1.0;
thumbnail_ = false;

setWindowOpacity(opacity_);

if (const auto pixmap = render(data_); pixmap) {
pixmap_ = pixmap.value();
texture_->present(pixmap_);

if (mimedata->hasFormat(clipboard::MIME_TYPE_POINT)) {
move(clipboard::deserialize<QPoint>(mimedata->data(clipboard::MIME_TYPE_POINT)));
}
else {
const auto screen = QGuiApplication::screenAt(QCursor::pos())
? QGuiApplication::screenAt(QCursor::pos())
: QGuiApplication::primaryScreen();
move(screen->geometry().center() -
QPoint{ pixmap_.size().width() / 2, pixmap_.size().height() / 2 });
}

resize(pixmap_.size());
}
Expand All @@ -72,20 +81,23 @@ void ImageWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
thumbnail_ = !thumbnail_;

QRect _geometry{};

if (thumbnail_) {
_geometry.setSize(THUMBNAIL_SIZE_);
_geometry.moveCenter(event->pos());
texture_->present(pixmap_.copy(_geometry));
QRect rect({ 0, 0 }, THUMBNAIL_SIZE_);
rect.moveCenter(event->globalPosition().toPoint());
const auto geo = rect.intersected(geometry());

texture_->present(grab(geo.translated((event->position() - event->globalPosition()).toPoint())));

thumb_offset_ = geometry().center() - geo.center();
setGeometry(geo);
}
else {
_geometry.setSize(pixmap_.size().scaled(pixmap_.size() * scale_, Qt::KeepAspectRatio));
auto geo = QRect({}, pixmap_.size().scaled(pixmap_.size() * scale_, Qt::KeepAspectRatio));
texture_->present(pixmap_);
}

_geometry.moveCenter(geometry().center());
setGeometry(_geometry);
geo.moveCenter(geometry().center() + thumb_offset_);
setGeometry(geo);
}
}

void ImageWindow::wheelEvent(QWheelEvent *event)
Expand Down
3 changes: 2 additions & 1 deletion src/preview/image-window.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public slots:
qreal scale_{ 1.0 };
qreal opacity_{ 1.0 };

QSize THUMBNAIL_SIZE_{ 125, 125 };
QSize THUMBNAIL_SIZE_{ 125, 125 };
QPoint thumb_offset_{};

QMenu *context_menu_{};
QAction *zoom_action_{};
Expand Down
1 change: 0 additions & 1 deletion src/snipping/canvas/graphicsitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <QAbstractTextDocumentLayout>
#include <QGraphicsScene>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
Expand Down
2 changes: 1 addition & 1 deletion src/snipping/canvas/graphicsitems.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class GraphicsEllipseleItem : public GraphicsItem

void push(const QPointF&) override;

bool invalid() const override { return geometry_.width() * geometry_.height() < 16; }
[[nodiscard]] bool invalid() const override { return geometry_.width() * geometry_.height() < 16; }

[[nodiscard]] ResizerLocation location(const QPointF&) const override;

Expand Down

0 comments on commit b441aba

Please sign in to comment.