From 72eee553f4b191fc4c1ae64f253572193dcee281 Mon Sep 17 00:00:00 2001 From: Gauwal Date: Tue, 4 Nov 2025 13:19:19 +0100 Subject: [PATCH] Isolating pin badge init to it's own method + adding comments --- .../printing_selector_card_overlay_widget.cpp | 92 ++++++++++++------- .../printing_selector_card_overlay_widget.h | 1 + 2 files changed, 59 insertions(+), 34 deletions(-) diff --git a/cockatrice/src/interface/widgets/printing_selector/printing_selector_card_overlay_widget.cpp b/cockatrice/src/interface/widgets/printing_selector/printing_selector_card_overlay_widget.cpp index 6cbf66fbd..5a26e0505 100644 --- a/cockatrice/src/interface/widgets/printing_selector/printing_selector_card_overlay_widget.cpp +++ b/cockatrice/src/interface/widgets/printing_selector/printing_selector_card_overlay_widget.cpp @@ -56,40 +56,7 @@ PrintingSelectorCardOverlayWidget::PrintingSelectorCardOverlayWidget(QWidget *pa cardInfoPicture->setCard(_rootCard); mainLayout->addWidget(cardInfoPicture); - // Pin badge shown when this printing is pinned (hidden by default) - pinBadge = new QLabel(this); - pinBadge->setObjectName(QStringLiteral("printingSelectorPinBadge")); - - bool pinLoaded = false; - QImageReader pinReader(QStringLiteral("theme:icons/pin")); - - if (pinReader.canRead()) { - const QSize targetSize(64, 64); - const qreal dpr = pinBadge->devicePixelRatioF(); - const QSize rasterSize(qMax(1, qCeil(targetSize.width() * dpr)), qMax(1, qCeil(targetSize.height() * dpr))); - pinReader.setScaledSize(rasterSize); - const QImage pinImage = pinReader.read(); - if (!pinImage.isNull()) { - QPixmap pinPix = QPixmap::fromImage(pinImage); - pinPix.setDevicePixelRatio(dpr); - pinBadge->setPixmap(pinPix); - pinBadge->setFixedSize(targetSize); - pinBadge->setStyleSheet(QStringLiteral("background: transparent;")); - pinLoaded = true; - } - } - - if (!pinLoaded) { - // visible fallback so you can see the badge even without the SVG - pinBadge->setText(QStringLiteral("PIN")); - pinBadge->setAlignment(Qt::AlignCenter); - pinBadge->setFixedSize(24, 12); - pinBadge->setStyleSheet("background: yellow; color: black; border: 1px solid red;"); - } - - pinBadge->setAttribute(Qt::WA_TransparentForMouseEvents); - pinBadge->setVisible(false); - pinBadge->raise(); + initializePinBadge(); // Update when this overlay emits cardPreferenceChanged or when size/scale changes connect(this, &PrintingSelectorCardOverlayWidget::cardPreferenceChanged, this, @@ -196,14 +163,17 @@ void PrintingSelectorCardOverlayWidget::updatePinBadgeVisibility() if (!pinBadge || !cardInfoPicture) return; + // Query the persisted preference override to decide whether this printing is pinned. const auto &preferredProviderId = SettingsCache::instance().cardOverrides().getCardPreferenceOverride(rootCard.getName()); const auto &cardProviderId = rootCard.getPrinting().getUuid(); const bool isPinned = (!preferredProviderId.isEmpty() && preferredProviderId == cardProviderId); + // Toggle the badge once; the pixmap was already rasterized in initializePinBadge(). pinBadge->setVisible(isPinned); if (isPinned) { + // Keep a small margin that scales with the card size to avoid obscuring stuff. const int margin = qMax(3, int(cardInfoPicture->width() * 0.03)); int x = qMax(0, cardInfoPicture->width() - pinBadge->width() - margin); int y = margin * 3; @@ -287,3 +257,57 @@ void PrintingSelectorCardOverlayWidget::customMenu(QPoint point) } menu.exec(this->mapToGlobal(point)); } + +/** + * @brief Initializes the pin badge overlay and loads its icon with DPI-aware rasterization. + * + * The icon is rasterized once using the label's device pixel ratio so it stays crisp on HiDPI + * displays. The resulting pixmap is cached on the QLabel and simply shown/hidden when needed. + * If the SVG cannot be read, a textual fallback badge is created instead. + */ +void PrintingSelectorCardOverlayWidget::initializePinBadge() +{ + if (!pinBadge) { + // construct the overlay label once + pinBadge = new QLabel(this); + pinBadge->setObjectName(QStringLiteral("printingSelectorPinBadge")); + } else { + // Clear any previous pixmap / style in case we reinitialize for a DPR change. + pinBadge->clear(); + pinBadge->setStyleSheet(QString()); + } + + bool pinLoaded = false; + QImageReader pinReader(QStringLiteral("theme:icons/pin")); + + if (pinReader.canRead()) { + // Rasterize a 64×64 logical icon so it has a consistent size regardless of card scaling. + const QSize targetSize(64, 64); + const qreal dpr = pinBadge->devicePixelRatioF(); + const QSize rasterSize(qMax(1, qCeil(targetSize.width() * dpr)), qMax(1, qCeil(targetSize.height() * dpr))); + pinReader.setScaledSize(rasterSize); + const QImage pinImage = pinReader.read(); + if (!pinImage.isNull()) { + // Tag the pixmap with the same DPR so Qt renders it at the correct physical size. + QPixmap pinPix = QPixmap::fromImage(pinImage); + pinPix.setDevicePixelRatio(dpr); + pinBadge->setPixmap(pinPix); + pinBadge->setFixedSize(targetSize); + pinBadge->setStyleSheet(QStringLiteral("background: transparent;")); + pinLoaded = true; + } + } + + if (!pinLoaded) { + // Fall back to a text badge when the SVG cannot be decoded (e.g., missing theme resource). + pinBadge->setText(QStringLiteral("PIN")); + pinBadge->setAlignment(Qt::AlignCenter); + pinBadge->setFixedSize(24, 12); + pinBadge->setStyleSheet(QStringLiteral("background: yellow; color: black; border: 1px solid red;")); + } + + // The overlay is mouse-transparent and hidden until we know the card is pinned. + pinBadge->setAttribute(Qt::WA_TransparentForMouseEvents); + pinBadge->setVisible(false); + pinBadge->raise(); +} diff --git a/cockatrice/src/interface/widgets/printing_selector/printing_selector_card_overlay_widget.h b/cockatrice/src/interface/widgets/printing_selector/printing_selector_card_overlay_widget.h index 2606d068b..46fb49b6c 100644 --- a/cockatrice/src/interface/widgets/printing_selector/printing_selector_card_overlay_widget.h +++ b/cockatrice/src/interface/widgets/printing_selector/printing_selector_card_overlay_widget.h @@ -46,6 +46,7 @@ private slots: void updatePinBadgeVisibility(); private: + void initializePinBadge(); CardInfoPictureWidget *cardInfoPicture; AllZonesCardAmountWidget *allZonesCardAmountWidget; QLabel *pinBadge = nullptr;