Refactor: Represent cardName + providerId with CardRef struct (#6039)

* card_ref.h

* update CardDatabase signatures

* make everything compile

* rename methods

* add docs

* mark stuff const

* set cardRef in CardItem

* cleanup

* fix build failure

* Fix builds on mac

---------

Co-authored-by: ZeldaZach <zahalpern+github@gmail.com>
This commit is contained in:
RickyRister 2025-07-15 19:14:02 -07:00 committed by GitHub
parent e05dad4267
commit a9b3be33e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
61 changed files with 328 additions and 305 deletions

View file

@ -12,13 +12,9 @@
#include <QPainter>
#include <algorithm>
AbstractCardItem::AbstractCardItem(QGraphicsItem *parent,
const QString &_name,
const QString &_providerId,
Player *_owner,
int _id)
: ArrowTarget(_owner, parent), id(_id), name(_name), providerId(_providerId), tapped(false), facedown(false),
tapAngle(0), bgColor(Qt::transparent), isHovered(false), realZValue(0)
AbstractCardItem::AbstractCardItem(QGraphicsItem *parent, const CardRef &cardRef, Player *_owner, int _id)
: ArrowTarget(_owner, parent), id(_id), cardRef(cardRef), tapped(false), facedown(false), tapAngle(0),
bgColor(Qt::transparent), isHovered(false), realZValue(0)
{
setCursor(Qt::OpenHandCursor);
setFlag(ItemIsSelectable);
@ -37,7 +33,7 @@ AbstractCardItem::AbstractCardItem(QGraphicsItem *parent,
AbstractCardItem::~AbstractCardItem()
{
emit deleteCardInfoPopup(name);
emit deleteCardInfoPopup(cardRef.name);
}
QRectF AbstractCardItem::boundingRect() const
@ -61,13 +57,13 @@ void AbstractCardItem::pixmapUpdated()
void AbstractCardItem::refreshCardInfo()
{
info = CardDatabaseManager::getInstance()->getCardByNameAndProviderId(name, providerId);
info = CardDatabaseManager::getInstance()->getCard(cardRef);
if (!info && !name.isEmpty()) {
if (!info && !cardRef.name.isEmpty()) {
QVariantHash properties = QVariantHash();
info = CardInfo::newInstance(name, "", true, QVariantHash(), QList<CardRelation *>(), QList<CardRelation *>(),
SetToPrintingsMap(), false, false, -1, false);
info = CardInfo::newInstance(cardRef.name, "", true, QVariantHash(), QList<CardRelation *>(),
QList<CardRelation *>(), SetToPrintingsMap(), false, false, -1, false);
}
if (info.data()) {
connect(info.data(), &CardInfo::pixmapUpdated, this, &AbstractCardItem::pixmapUpdated);
@ -114,7 +110,7 @@ void AbstractCardItem::paintPicture(QPainter *painter, const QSizeF &translatedS
QPixmap translatedPixmap;
bool paintImage = true;
if (facedown || name.isEmpty()) {
if (facedown || cardRef.name.isEmpty()) {
// never reveal card color, always paint the card back
PictureLoader::getCardBackPixmap(translatedPixmap, translatedSize.toSize());
} else {
@ -154,7 +150,7 @@ void AbstractCardItem::paintPicture(QPainter *painter, const QSizeF &translatedS
if (SettingsCache::instance().debug().getShowCardId()) {
prefix = "#" + QString::number(id) + " ";
}
nameStr = prefix + name;
nameStr = prefix + cardRef.name;
}
painter->drawText(QRectF(3 * scaleFactor, 3 * scaleFactor, translatedSize.width() - 6 * scaleFactor,
translatedSize.height() - 6 * scaleFactor),
@ -188,30 +184,17 @@ void AbstractCardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *
painter->restore();
}
void AbstractCardItem::setName(const QString &_name)
void AbstractCardItem::setCardRef(const CardRef &_cardRef)
{
if (name == _name)
return;
emit deleteCardInfoPopup(name);
if (info)
disconnect(info.data(), nullptr, this, nullptr);
name = _name;
refreshCardInfo();
}
void AbstractCardItem::setProviderId(const QString &_providerId)
{
if (providerId == _providerId) {
if (cardRef == _cardRef) {
return;
}
emit deleteCardInfoPopup(name);
emit deleteCardInfoPopup(cardRef.name);
if (info) {
disconnect(info.data(), nullptr, this, nullptr);
}
providerId = _providerId;
cardRef = _cardRef;
refreshCardInfo();
}
@ -299,7 +282,7 @@ void AbstractCardItem::setFaceDown(bool _facedown)
void AbstractCardItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if ((event->modifiers() & Qt::AltModifier) && event->button() == Qt::LeftButton) {
emit cardShiftClicked(name);
emit cardShiftClicked(cardRef.name);
} else if ((event->modifiers() & Qt::ControlModifier)) {
setSelected(!isSelected());
} else if (!isSelected()) {
@ -309,14 +292,14 @@ void AbstractCardItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
if (event->button() == Qt::LeftButton)
setCursor(Qt::ClosedHandCursor);
else if (event->button() == Qt::MiddleButton)
emit showCardInfoPopup(event->screenPos(), name, providerId);
emit showCardInfoPopup(event->screenPos(), cardRef);
event->accept();
}
void AbstractCardItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::MiddleButton)
emit deleteCardInfoPopup(name);
emit deleteCardInfoPopup(cardRef.name);
// This function ensures the parent function doesn't mess around with our selection.
event->accept();