[CardInfoPictureWidget] Refactor constant fields to static const (#6575)

* [CardInfoPictureWidget] Refactor constant fields to static const

* rename constants

* reformat

* comment out unused
This commit is contained in:
RickyRister 2026-01-27 17:50:19 -08:00 committed by GitHub
parent 165c4ddd2a
commit 5cc5767c87
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 26 deletions

View file

@ -15,6 +15,18 @@
#include <libcockatrice/card/relation/card_relation.h> #include <libcockatrice/card/relation/card_relation.h>
#include <utility> #include <utility>
static constexpr qreal MTG_CARD_ASPECT_RATIO = 1.396;
// static constexpr qreal YUGIOH_CARD_ASPECT_RATIO = 1.457;
static constexpr qreal ASPECT_RATIO = MTG_CARD_ASPECT_RATIO;
static constexpr int BASE_WIDTH = 200;
static constexpr int BASE_HEIGHT = 200;
static constexpr int ENLARGED_PIXMAP_OFFSET = 10;
static constexpr int HOVER_ACTIVATE_THRESHOLD_MS = 500;
static constexpr int ANIMATION_OFFSET = 10; // Adjust this for how much the widget moves up
/** /**
* @class CardInfoPictureWidget * @class CardInfoPictureWidget
* @brief Widget that displays an enlarged image of a card, loading the image based on the card's info or showing a * @brief Widget that displays an enlarged image of a card, loading the image based on the card's info or showing a
@ -34,7 +46,7 @@
CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool _hoverToZoomEnabled, const bool _raiseOnEnter) CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool _hoverToZoomEnabled, const bool _raiseOnEnter)
: QWidget(parent), pixmapDirty(true), hoverToZoomEnabled(_hoverToZoomEnabled), raiseOnEnter(_raiseOnEnter) : QWidget(parent), pixmapDirty(true), hoverToZoomEnabled(_hoverToZoomEnabled), raiseOnEnter(_raiseOnEnter)
{ {
setMinimumHeight(baseHeight); setMinimumHeight(BASE_HEIGHT);
if (hoverToZoomEnabled) { if (hoverToZoomEnabled) {
setMouseTracking(true); setMouseTracking(true);
} }
@ -52,7 +64,7 @@ CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool _hoverT
animation->setEasingCurve(QEasingCurve::OutQuad); animation->setEasingCurve(QEasingCurve::OutQuad);
animation->setStartValue(originalPos); animation->setStartValue(originalPos);
animation->setEndValue(originalPos - QPoint(0, animationOffset)); animation->setEndValue(originalPos - QPoint(0, ANIMATION_OFFSET));
connect(&SettingsCache::instance(), &SettingsCache::roundCardCornersChanged, this, [this](bool _roundCardCorners) { connect(&SettingsCache::instance(), &SettingsCache::roundCardCornersChanged, this, [this](bool _roundCardCorners) {
Q_UNUSED(_roundCardCorners); Q_UNUSED(_roundCardCorners);
@ -119,8 +131,8 @@ void CardInfoPictureWidget::resizeEvent(QResizeEvent *event)
*/ */
void CardInfoPictureWidget::setScaleFactor(const int scale) void CardInfoPictureWidget::setScaleFactor(const int scale)
{ {
const int newWidth = baseWidth * scale / 100; const int newWidth = BASE_WIDTH * scale / 100;
const int newHeight = static_cast<int>(newWidth * aspectRatio); const int newHeight = static_cast<int>(newWidth * ASPECT_RATIO);
scaleFactor = scale; scaleFactor = scale;
@ -226,8 +238,8 @@ void CardInfoPictureWidget::paintEvent(QPaintEvent *event)
*/ */
QSize CardInfoPictureWidget::sizeHint() const QSize CardInfoPictureWidget::sizeHint() const
{ {
return {static_cast<int>(baseWidth * scaleFactor / 100.0), return {static_cast<int>(BASE_WIDTH * scaleFactor / 100.0),
static_cast<int>(baseWidth * scaleFactor / 100.0 * aspectRatio)}; static_cast<int>(BASE_WIDTH * scaleFactor / 100.0 * ASPECT_RATIO)};
} }
/** /**
@ -244,7 +256,7 @@ void CardInfoPictureWidget::enterEvent(QEvent *event)
// If hover-to-zoom is enabled, start the hover timer // If hover-to-zoom is enabled, start the hover timer
if (hoverToZoomEnabled) { if (hoverToZoomEnabled) {
hoverTimer->start(hoverActivateThresholdInMs); hoverTimer->start(HOVER_ACTIVATE_THRESHOLD_MS);
} }
// Emit signal indicating a card is being hovered on // Emit signal indicating a card is being hovered on
@ -256,7 +268,7 @@ void CardInfoPictureWidget::enterEvent(QEvent *event)
} else { } else {
originalPos = this->pos(); // Update the baseline position originalPos = this->pos(); // Update the baseline position
animation->setStartValue(originalPos); animation->setStartValue(originalPos);
animation->setEndValue(originalPos - QPoint(0, animationOffset)); animation->setEndValue(originalPos - QPoint(0, ANIMATION_OFFSET));
} }
animation->setDirection(QAbstractAnimation::Forward); animation->setDirection(QAbstractAnimation::Forward);
animation->start(); animation->start();
@ -311,15 +323,15 @@ void CardInfoPictureWidget::mouseMoveEvent(QMouseEvent *event)
const QRect screenGeometry = QGuiApplication::screenAt(cursorPos)->geometry(); const QRect screenGeometry = QGuiApplication::screenAt(cursorPos)->geometry();
const QSize widgetSize = enlargedPixmapWidget->size(); const QSize widgetSize = enlargedPixmapWidget->size();
int newX = cursorPos.x() + enlargedPixmapOffset; int newX = cursorPos.x() + ENLARGED_PIXMAP_OFFSET;
int newY = cursorPos.y() + enlargedPixmapOffset; int newY = cursorPos.y() + ENLARGED_PIXMAP_OFFSET;
// Adjust if out of bounds // Adjust if out of bounds
if (newX + widgetSize.width() > screenGeometry.right()) { if (newX + widgetSize.width() > screenGeometry.right()) {
newX = cursorPos.x() - widgetSize.width() - enlargedPixmapOffset; newX = cursorPos.x() - widgetSize.width() - ENLARGED_PIXMAP_OFFSET;
} }
if (newY + widgetSize.height() > screenGeometry.bottom()) { if (newY + widgetSize.height() > screenGeometry.bottom()) {
newY = cursorPos.y() - widgetSize.height() - enlargedPixmapOffset; newY = cursorPos.y() - widgetSize.height() - ENLARGED_PIXMAP_OFFSET;
} }
enlargedPixmapWidget->move(newX, newY); enlargedPixmapWidget->move(newX, newY);
@ -453,21 +465,21 @@ void CardInfoPictureWidget::showEnlargedPixmap()
connect(this, &QObject::destroyed, enlargedPixmapWidget, &CardInfoPictureEnlargedWidget::deleteLater); connect(this, &QObject::destroyed, enlargedPixmapWidget, &CardInfoPictureEnlargedWidget::deleteLater);
} }
const QSize enlargedSize(static_cast<int>(size().width() * 2), static_cast<int>(size().width() * aspectRatio * 2)); const QSize enlargedSize(static_cast<int>(size().width() * 2), static_cast<int>(size().width() * ASPECT_RATIO * 2));
enlargedPixmapWidget->setCardPixmap(exactCard, enlargedSize); enlargedPixmapWidget->setCardPixmap(exactCard, enlargedSize);
const QPoint cursorPos = QCursor::pos(); const QPoint cursorPos = QCursor::pos();
const QRect screenGeometry = QGuiApplication::screenAt(cursorPos)->geometry(); const QRect screenGeometry = QGuiApplication::screenAt(cursorPos)->geometry();
const QSize widgetSize = enlargedPixmapWidget->size(); const QSize widgetSize = enlargedPixmapWidget->size();
int newX = cursorPos.x() + enlargedPixmapOffset; int newX = cursorPos.x() + ENLARGED_PIXMAP_OFFSET;
int newY = cursorPos.y() + enlargedPixmapOffset; int newY = cursorPos.y() + ENLARGED_PIXMAP_OFFSET;
if (newX + widgetSize.width() > screenGeometry.right()) { if (newX + widgetSize.width() > screenGeometry.right()) {
newX = cursorPos.x() - widgetSize.width() - enlargedPixmapOffset; newX = cursorPos.x() - widgetSize.width() - ENLARGED_PIXMAP_OFFSET;
} }
if (newY + widgetSize.height() > screenGeometry.bottom()) { if (newY + widgetSize.height() > screenGeometry.bottom()) {
newY = cursorPos.y() - widgetSize.height() - enlargedPixmapOffset; newY = cursorPos.y() - widgetSize.height() - ENLARGED_PIXMAP_OFFSET;
} }
enlargedPixmapWidget->move(newX, newY); enlargedPixmapWidget->move(newX, newY);

View file

@ -68,23 +68,17 @@ protected:
private: private:
ExactCard exactCard; ExactCard exactCard;
qreal magicTheGatheringCardAspectRatio = 1.396;
qreal yuGiOhCardAspectRatio = 1.457;
qreal aspectRatio = magicTheGatheringCardAspectRatio;
int baseWidth = 200;
int baseHeight = 200;
double scaleFactor = 100; double scaleFactor = 100;
QPixmap resizedPixmap; QPixmap resizedPixmap;
bool pixmapDirty; bool pixmapDirty;
bool hoverToZoomEnabled; bool hoverToZoomEnabled;
bool raiseOnEnter; bool raiseOnEnter;
int hoverActivateThresholdInMs = 500;
CardInfoPictureEnlargedWidget *enlargedPixmapWidget = nullptr; CardInfoPictureEnlargedWidget *enlargedPixmapWidget = nullptr;
int enlargedPixmapOffset = 10;
QTimer *hoverTimer; QTimer *hoverTimer;
QPropertyAnimation *animation; QPropertyAnimation *animation;
QPoint originalPos; // Store the original position QPoint originalPos; // Store the original position
const int animationOffset = 10; // Adjust this for how much the widget moves up
QMenu *createRightClickMenu(); QMenu *createRightClickMenu();
QMenu *createViewRelatedCardsMenu(); QMenu *createViewRelatedCardsMenu();