[CardInfoPictureEnlargedWidget] Fix DPR scaling. (#6382)

This commit is contained in:
BruebachL 2025-11-30 13:03:18 +01:00 committed by GitHub
parent eab4d435f8
commit 2b64e65f45
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -33,10 +33,13 @@ CardInfoPictureEnlargedWidget::CardInfoPictureEnlargedWidget(QWidget *parent) :
*/ */
void CardInfoPictureEnlargedWidget::loadPixmap(const QSize &size) void CardInfoPictureEnlargedWidget::loadPixmap(const QSize &size)
{ {
// Handle DPI scaling
qreal dpr = devicePixelRatio(); // Get the actual scaling factor
QSize availableSize = size * dpr; // Convert to physical pixel size
if (card) { if (card) {
CardPictureLoader::getPixmap(enlargedPixmap, card, size); CardPictureLoader::getPixmap(enlargedPixmap, card, availableSize);
} else { } else {
CardPictureLoader::getCardBackPixmap(enlargedPixmap, size); CardPictureLoader::getCardBackPixmap(enlargedPixmap, availableSize);
} }
pixmapDirty = false; pixmapDirty = false;
} }
@ -77,25 +80,35 @@ void CardInfoPictureEnlargedWidget::paintEvent(QPaintEvent *event)
loadPixmap(size()); loadPixmap(size());
} }
// Scale the size of the pixmap to fit the widget while maintaining the aspect ratio qreal dpr = enlargedPixmap.devicePixelRatio();
QSize scaledSize = enlargedPixmap.size().scaled(size().width(), size().height(), Qt::KeepAspectRatio);
// Calculate the position to center the scaled pixmap QSize logicalPixmapSize(enlargedPixmap.width() / dpr, enlargedPixmap.height() / dpr);
QPoint topLeft{(width() - scaledSize.width()) / 2, (height() - scaledSize.height()) / 2};
// Define the radius for rounded corners // Scale the pixmap to fit the widget (logical → logical)
// Adjust the radius as needed for rounded corners QSize scaledLogicalSize = logicalPixmapSize.scaled(size(), Qt::KeepAspectRatio);
qreal radius = SettingsCache::instance().getRoundCardCorners() ? 0.05 * scaledSize.width() : 0.;
// Convert scaled logical size → physical size for scaled()
QSize scaledPhysicalSize = scaledLogicalSize * dpr;
// Pixmap scaled in PHYSICAL pixels
QPixmap finalPixmap = enlargedPixmap.scaled(scaledPhysicalSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
finalPixmap.setDevicePixelRatio(dpr);
// Center inside widget
QPoint topLeft{(width() - scaledLogicalSize.width()) / 2, (height() - scaledLogicalSize.height()) / 2};
// Rounded corner radius based on logical width
qreal radius = SettingsCache::instance().getRoundCardCorners() ? 0.05 * scaledLogicalSize.width() : 0.0;
QStylePainter painter(this); QStylePainter painter(this);
// Fill the background with transparent color to ensure rounded corners are rendered properly // Fill the background with transparent color to ensure rounded corners are rendered properly
painter.fillRect(rect(), Qt::transparent); // Use the transparent background painter.fillRect(rect(), Qt::transparent); // Use the transparent background
QPainterPath shape; QPainterPath shape;
shape.addRoundedRect(QRect(topLeft, scaledSize), radius, radius); shape.addRoundedRect(QRect(topLeft, scaledLogicalSize), radius, radius);
painter.setClipPath(shape); // Set the clipping path painter.setClipPath(shape); // Set the clipping path
// Draw the pixmap scaled to the calculated size // Draw the pixmap scaled to the calculated size
painter.drawItemPixmap(QRect(topLeft, scaledSize), Qt::AlignCenter, painter.drawPixmap(QRect(topLeft, scaledLogicalSize), finalPixmap);
enlargedPixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
} }