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();

View file

@ -3,6 +3,7 @@
#include "../cards/card_info.h"
#include "arrow_target.h"
#include "card_ref.h"
class Player;
@ -15,8 +16,7 @@ class AbstractCardItem : public ArrowTarget
protected:
CardInfoPtr info;
int id;
QString name;
QString providerId;
CardRef cardRef;
bool tapped;
bool facedown;
int tapAngle;
@ -34,7 +34,7 @@ public slots:
signals:
void hovered(AbstractCardItem *card);
void showCardInfoPopup(const QPoint &pos, const QString &cardName, const QString &providerId);
void showCardInfoPopup(const QPoint &pos, const CardRef &cardRef);
void deleteCardInfoPopup(QString cardName);
void sigPixmapUpdated();
void cardShiftClicked(QString cardName);
@ -49,8 +49,7 @@ public:
return Type;
}
explicit AbstractCardItem(QGraphicsItem *parent = nullptr,
const QString &_name = QString(),
const QString &_providerId = QString(),
const CardRef &cardRef = {},
Player *_owner = nullptr,
int _id = -1);
~AbstractCardItem() override;
@ -73,14 +72,17 @@ public:
}
QString getName() const
{
return name;
return cardRef.name;
}
void setName(const QString &_name = QString());
QString getProviderId() const
{
return providerId;
return cardRef.providerId;
}
void setCardRef(const CardRef &_cardRef);
CardRef getCardRef() const
{
return cardRef;
}
void setProviderId(const QString &_providerId = QString());
qreal getRealZValue() const
{
return realZValue;
@ -105,7 +107,7 @@ public:
void processHoverEvent();
void deleteCardInfoPopup()
{
emit deleteCardInfoPopup(name);
emit deleteCardInfoPopup(cardRef.name);
}
protected:

View file

@ -18,14 +18,9 @@
#include <QMenu>
#include <QPainter>
CardItem::CardItem(Player *_owner,
QGraphicsItem *parent,
const QString &_name,
const QString &_providerId,
int _cardid,
CardZone *_zone)
: AbstractCardItem(parent, _name, _providerId, _owner, _cardid), zone(_zone), attacking(false),
destroyOnZoneChange(false), doesntUntap(false), dragItem(nullptr), attachedTo(nullptr)
CardItem::CardItem(Player *_owner, QGraphicsItem *parent, const CardRef &cardRef, int _cardid, CardZone *_zone)
: AbstractCardItem(parent, cardRef, _owner, _cardid), zone(_zone), attacking(false), destroyOnZoneChange(false),
doesntUntap(false), dragItem(nullptr), attachedTo(nullptr)
{
owner->addCard(this);
@ -246,8 +241,7 @@ void CardItem::processCardInfo(const ServerInfo_Card &_info)
}
setId(_info.id());
setProviderId(QString::fromStdString(_info.provider_id()));
setName(QString::fromStdString(_info.name()));
setCardRef({QString::fromStdString(_info.name()), QString::fromStdString(_info.provider_id())});
setAttacking(_info.attacking());
setFaceDown(_info.face_down());
setPT(QString::fromStdString(_info.pt()));

View file

@ -51,8 +51,7 @@ public:
}
explicit CardItem(Player *_owner,
QGraphicsItem *parent = nullptr,
const QString &_name = QString(),
const QString &_providerId = QString(),
const CardRef &cardRef = {},
int _cardid = -1,
CardZone *_zone = nullptr);
~CardItem() override;