Skip to content

Commit b441aba

Browse files
committed
fix: geometry issue of preview windows
1 parent e3cafdf commit b441aba

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/preview/image-window.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ void ImageWindow::preview(const std::shared_ptr<QMimeData>& mimedata)
4545
opacity_ = 1.0;
4646
thumbnail_ = false;
4747

48+
setWindowOpacity(opacity_);
49+
4850
if (const auto pixmap = render(data_); pixmap) {
4951
pixmap_ = pixmap.value();
5052
texture_->present(pixmap_);
5153

5254
if (mimedata->hasFormat(clipboard::MIME_TYPE_POINT)) {
5355
move(clipboard::deserialize<QPoint>(mimedata->data(clipboard::MIME_TYPE_POINT)));
5456
}
57+
else {
58+
const auto screen = QGuiApplication::screenAt(QCursor::pos())
59+
? QGuiApplication::screenAt(QCursor::pos())
60+
: QGuiApplication::primaryScreen();
61+
move(screen->geometry().center() -
62+
QPoint{ pixmap_.size().width() / 2, pixmap_.size().height() / 2 });
63+
}
5564

5665
resize(pixmap_.size());
5766
}
@@ -72,20 +81,23 @@ void ImageWindow::mouseDoubleClickEvent(QMouseEvent *event)
7281
{
7382
thumbnail_ = !thumbnail_;
7483

75-
QRect _geometry{};
76-
7784
if (thumbnail_) {
78-
_geometry.setSize(THUMBNAIL_SIZE_);
79-
_geometry.moveCenter(event->pos());
80-
texture_->present(pixmap_.copy(_geometry));
85+
QRect rect({ 0, 0 }, THUMBNAIL_SIZE_);
86+
rect.moveCenter(event->globalPosition().toPoint());
87+
const auto geo = rect.intersected(geometry());
88+
89+
texture_->present(grab(geo.translated((event->position() - event->globalPosition()).toPoint())));
90+
91+
thumb_offset_ = geometry().center() - geo.center();
92+
setGeometry(geo);
8193
}
8294
else {
83-
_geometry.setSize(pixmap_.size().scaled(pixmap_.size() * scale_, Qt::KeepAspectRatio));
95+
auto geo = QRect({}, pixmap_.size().scaled(pixmap_.size() * scale_, Qt::KeepAspectRatio));
8496
texture_->present(pixmap_);
85-
}
8697

87-
_geometry.moveCenter(geometry().center());
88-
setGeometry(_geometry);
98+
geo.moveCenter(geometry().center() + thumb_offset_);
99+
setGeometry(geo);
100+
}
89101
}
90102

91103
void ImageWindow::wheelEvent(QWheelEvent *event)

src/preview/image-window.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public slots:
4747
qreal scale_{ 1.0 };
4848
qreal opacity_{ 1.0 };
4949

50-
QSize THUMBNAIL_SIZE_{ 125, 125 };
50+
QSize THUMBNAIL_SIZE_{ 125, 125 };
51+
QPoint thumb_offset_{};
5152

5253
QMenu *context_menu_{};
5354
QAction *zoom_action_{};

src/snipping/canvas/graphicsitems.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <QAbstractTextDocumentLayout>
88
#include <QGraphicsScene>
99
#include <QGraphicsSceneHoverEvent>
10-
#include <QGraphicsSceneMouseEvent>
1110
#include <QKeyEvent>
1211
#include <QPainter>
1312
#include <QStyleOptionGraphicsItem>

src/snipping/canvas/graphicsitems.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class GraphicsEllipseleItem : public GraphicsItem
274274

275275
void push(const QPointF&) override;
276276

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

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

0 commit comments

Comments
 (0)