diff --git a/cockatrice/src/interface/widgets/cards/card_info_picture_widget.cpp b/cockatrice/src/interface/widgets/cards/card_info_picture_widget.cpp index fc1b9ebe2..85d210e1a 100644 --- a/cockatrice/src/interface/widgets/cards/card_info_picture_widget.cpp +++ b/cockatrice/src/interface/widgets/cards/card_info_picture_widget.cpp @@ -39,10 +39,6 @@ CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool _hoverT setMouseTracking(true); } - enlargedPixmapWidget = new CardInfoPictureEnlargedWidget(this->window()); - enlargedPixmapWidget->hide(); - connect(this, &QObject::destroyed, enlargedPixmapWidget, &CardInfoPictureEnlargedWidget::deleteLater); - hoverTimer = new QTimer(this); hoverTimer->setSingleShot(true); connect(hoverTimer, &QTimer::timeout, this, &CardInfoPictureWidget::showEnlargedPixmap); @@ -277,7 +273,7 @@ void CardInfoPictureWidget::leaveEvent(QEvent *event) if (hoverToZoomEnabled) { hoverTimer->stop(); - enlargedPixmapWidget->hide(); + destroyEnlargedPixmapWidget(); } if (raiseOnEnter) { @@ -294,7 +290,7 @@ void CardInfoPictureWidget::moveEvent(QMoveEvent *event) QWidget::moveEvent(event); hoverTimer->stop(); - enlargedPixmapWidget->hide(); + destroyEnlargedPixmapWidget(); if (animation->state() == QAbstractAnimation::Running) { return; @@ -310,7 +306,7 @@ void CardInfoPictureWidget::mouseMoveEvent(QMouseEvent *event) { QWidget::mouseMoveEvent(event); - if (hoverToZoomEnabled && enlargedPixmapWidget->isVisible()) { + if (hoverToZoomEnabled && enlargedPixmapWidget && enlargedPixmapWidget->isVisible()) { const QPoint cursorPos = QCursor::pos(); const QRect screenGeometry = QGuiApplication::screenAt(cursorPos)->geometry(); const QSize widgetSize = enlargedPixmapWidget->size(); @@ -344,7 +340,7 @@ void CardInfoPictureWidget::mousePressEvent(QMouseEvent *event) void CardInfoPictureWidget::hideEvent(QHideEvent *event) { - enlargedPixmapWidget->hide(); + destroyEnlargedPixmapWidget(); QWidget::hideEvent(event); } @@ -444,12 +440,19 @@ QMenu *CardInfoPictureWidget::createAddToOpenDeckMenu() * If card information is available, the enlarged pixmap is loaded, positioned near the cursor, * and displayed. */ -void CardInfoPictureWidget::showEnlargedPixmap() const +void CardInfoPictureWidget::showEnlargedPixmap() { if (!exactCard) { return; } + // Lazy creation of the enlarged widget + if (!enlargedPixmapWidget) { + enlargedPixmapWidget = new CardInfoPictureEnlargedWidget(const_cast(this)->window()); + enlargedPixmapWidget->hide(); + connect(this, &QObject::destroyed, enlargedPixmapWidget, &CardInfoPictureEnlargedWidget::deleteLater); + } + const QSize enlargedSize(static_cast(size().width() * 2), static_cast(size().width() * aspectRatio * 2)); enlargedPixmapWidget->setCardPixmap(exactCard, enlargedSize); @@ -460,7 +463,6 @@ void CardInfoPictureWidget::showEnlargedPixmap() const 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; } @@ -472,3 +474,11 @@ void CardInfoPictureWidget::showEnlargedPixmap() const enlargedPixmapWidget->show(); } + +void CardInfoPictureWidget::destroyEnlargedPixmapWidget() +{ + if (enlargedPixmapWidget) { + enlargedPixmapWidget->deleteLater(); + enlargedPixmapWidget = nullptr; + } +} diff --git a/cockatrice/src/interface/widgets/cards/card_info_picture_widget.h b/cockatrice/src/interface/widgets/cards/card_info_picture_widget.h index 980d700f6..c9dbc47ab 100644 --- a/cockatrice/src/interface/widgets/cards/card_info_picture_widget.h +++ b/cockatrice/src/interface/widgets/cards/card_info_picture_widget.h @@ -63,7 +63,8 @@ protected: { return resizedPixmap; } - void showEnlargedPixmap() const; + void showEnlargedPixmap(); + void destroyEnlargedPixmapWidget(); private: ExactCard exactCard;