diff --git a/cockatrice/src/client/ui/widgets/cards/card_info_picture_widget.cpp b/cockatrice/src/client/ui/widgets/cards/card_info_picture_widget.cpp index 592766939..84b12374f 100644 --- a/cockatrice/src/client/ui/widgets/cards/card_info_picture_widget.cpp +++ b/cockatrice/src/client/ui/widgets/cards/card_info_picture_widget.cpp @@ -248,10 +248,24 @@ void CardInfoPictureWidget::leaveEvent(QEvent *event) void CardInfoPictureWidget::mouseMoveEvent(QMouseEvent *event) { QWidget::mouseMoveEvent(event); + if (hoverToZoomEnabled && enlargedPixmapWidget->isVisible()) { - const QPointF cursorPos = QCursor::pos(); - enlargedPixmapWidget->move(QPoint(static_cast(cursorPos.x()) + enlargedPixmapOffset, - static_cast(cursorPos.y()) + enlargedPixmapOffset)); + const QPoint cursorPos = QCursor::pos(); + const QRect screenGeometry = QGuiApplication::screenAt(cursorPos)->geometry(); + 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); } } @@ -353,9 +367,22 @@ void CardInfoPictureWidget::showEnlargedPixmap() const static_cast(size().width() * aspectRatio * scaleFactor)); enlargedPixmapWidget->setCardPixmap(info, enlargedSize); - const QPointF cursorPos = QCursor::pos(); - enlargedPixmapWidget->move(static_cast(cursorPos.x()) + enlargedPixmapOffset, - static_cast(cursorPos.y()) + enlargedPixmapOffset); + const QPoint cursorPos = QCursor::pos(); + const QRect screenGeometry = QGuiApplication::screenAt(cursorPos)->geometry(); + 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(); }