Correctly size hint card_info_picture_widget, adjust scaleFactor default and correctly parent hover-to-zoom scaled picture. (#5812)

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2025-04-09 17:16:34 +02:00 committed by GitHub
parent 4a68d9d3ea
commit e808d030db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 11 deletions

View file

@ -9,6 +9,7 @@
#include <QMenu> #include <QMenu>
#include <QMouseEvent> #include <QMouseEvent>
#include <QScreen>
#include <QStylePainter> #include <QStylePainter>
#include <QWidget> #include <QWidget>
#include <utility> #include <utility>
@ -37,7 +38,7 @@ CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool hoverTo
setMouseTracking(true); setMouseTracking(true);
} }
enlargedPixmapWidget = new CardInfoPictureEnlargedWidget(this); enlargedPixmapWidget = new CardInfoPictureEnlargedWidget(this->window());
enlargedPixmapWidget->hide(); enlargedPixmapWidget->hide();
hoverTimer = new QTimer(this); hoverTimer = new QTimer(this);
@ -210,7 +211,8 @@ void CardInfoPictureWidget::paintEvent(QPaintEvent *event)
*/ */
QSize CardInfoPictureWidget::sizeHint() const QSize CardInfoPictureWidget::sizeHint() const
{ {
return {static_cast<int>(baseWidth * scaleFactor), static_cast<int>(baseHeight * scaleFactor)}; return {static_cast<int>(baseWidth * scaleFactor / 100.0),
static_cast<int>(baseWidth * scaleFactor / 100.0 * aspectRatio)};
} }
/** /**
@ -254,10 +256,24 @@ void CardInfoPictureWidget::leaveEvent(QEvent *event)
void CardInfoPictureWidget::mouseMoveEvent(QMouseEvent *event) void CardInfoPictureWidget::mouseMoveEvent(QMouseEvent *event)
{ {
QWidget::mouseMoveEvent(event); QWidget::mouseMoveEvent(event);
if (hoverToZoomEnabled && enlargedPixmapWidget->isVisible()) { if (hoverToZoomEnabled && enlargedPixmapWidget->isVisible()) {
const QPointF cursorPos = QCursor::pos(); const QPoint cursorPos = QCursor::pos();
enlargedPixmapWidget->move(QPoint(static_cast<int>(cursorPos.x()) + enlargedPixmapOffset, const QRect screenGeometry = QGuiApplication::screenAt(cursorPos)->geometry();
static_cast<int>(cursorPos.y()) + enlargedPixmapOffset)); const QSize widgetSize = enlargedPixmapWidget->size();
int newX = cursorPos.x() + enlargedPixmapOffset;
int newY = cursorPos.y() + enlargedPixmapOffset;
// Adjust if out of bounds
if (newX + widgetSize.width() > screenGeometry.right()) {
newX = cursorPos.x() - widgetSize.width() - enlargedPixmapOffset;
}
if (newY + widgetSize.height() > screenGeometry.bottom()) {
newY = cursorPos.y() - widgetSize.height() - enlargedPixmapOffset;
}
enlargedPixmapWidget->move(newX, newY);
} }
} }
@ -370,13 +386,25 @@ void CardInfoPictureWidget::showEnlargedPixmap() const
return; return;
} }
const QSize enlargedSize(static_cast<int>(size().width() * scaleFactor), const QSize enlargedSize(static_cast<int>(size().width() * 2), static_cast<int>(size().width() * aspectRatio * 2));
static_cast<int>(size().width() * aspectRatio * scaleFactor));
enlargedPixmapWidget->setCardPixmap(info, enlargedSize); enlargedPixmapWidget->setCardPixmap(info, enlargedSize);
const QPointF cursorPos = QCursor::pos(); const QPoint cursorPos = QCursor::pos();
enlargedPixmapWidget->move(static_cast<int>(cursorPos.x()) + enlargedPixmapOffset, const QRect screenGeometry = QGuiApplication::screenAt(cursorPos)->geometry();
static_cast<int>(cursorPos.y()) + enlargedPixmapOffset); const QSize widgetSize = enlargedPixmapWidget->size();
int newX = cursorPos.x() + enlargedPixmapOffset;
int newY = cursorPos.y() + enlargedPixmapOffset;
// Adjust if out of bounds
if (newX + widgetSize.width() > screenGeometry.right()) {
newX = cursorPos.x() - widgetSize.width() - enlargedPixmapOffset;
}
if (newY + widgetSize.height() > screenGeometry.bottom()) {
newY = cursorPos.y() - widgetSize.height() - enlargedPixmapOffset;
}
enlargedPixmapWidget->move(newX, newY);
enlargedPixmapWidget->show(); enlargedPixmapWidget->show();
} }

View file

@ -60,7 +60,7 @@ private:
qreal aspectRatio = magicTheGatheringCardAspectRatio; qreal aspectRatio = magicTheGatheringCardAspectRatio;
int baseWidth = 200; int baseWidth = 200;
int baseHeight = 200; int baseHeight = 200;
double scaleFactor = 1.5; double scaleFactor = 100;
QPixmap resizedPixmap; QPixmap resizedPixmap;
bool pixmapDirty; bool pixmapDirty;
bool hoverToZoomEnabled; bool hoverToZoomEnabled;