From d3a578b3070e37bb45c318e9ee06cf0f1bfdf558 Mon Sep 17 00:00:00 2001 From: RickyRister Date: Mon, 21 Jul 2025 01:06:50 -0700 Subject: [PATCH] Create new class --- cockatrice/CMakeLists.txt | 1 + cockatrice/src/game/cards/exact_card.cpp | 79 ++++++++++++++++++++++++ cockatrice/src/game/cards/exact_card.h | 46 ++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 cockatrice/src/game/cards/exact_card.cpp create mode 100644 cockatrice/src/game/cards/exact_card.h diff --git a/cockatrice/CMakeLists.txt b/cockatrice/CMakeLists.txt index 2501f0fb9..f8be8aca5 100644 --- a/cockatrice/CMakeLists.txt +++ b/cockatrice/CMakeLists.txt @@ -198,6 +198,7 @@ set(cockatrice_SOURCES src/game/cards/card_database_parser/cockatrice_xml_4.cpp src/game/cards/card_info.cpp src/game/cards/card_search_model.cpp + src/game/cards/exact_card.cpp src/game/deckview/deck_view.cpp src/game/deckview/deck_view_container.cpp src/game/filters/deck_filter_string.cpp diff --git a/cockatrice/src/game/cards/exact_card.cpp b/cockatrice/src/game/cards/exact_card.cpp new file mode 100644 index 000000000..fbd60071e --- /dev/null +++ b/cockatrice/src/game/cards/exact_card.cpp @@ -0,0 +1,79 @@ +#include "exact_card.h" + +/** + * Default constructor. + * This will set the CardInfoPtr to null. + * The printing will be the default-constructed PrintingInfo. + */ +ExactCard::ExactCard() +{ +} + +/** + * @param _card The card. Can be null. + * @param _printing The printing. Can be empty. + */ +ExactCard::ExactCard(const CardInfoPtr &_card, const PrintingInfo &_printing) : card(_card), printing(_printing) +{ +} + +bool ExactCard::operator==(const ExactCard &other) const +{ + return this->card == other.card && this->printing == other.printing; +} + +/** + * Convenience method to safely get the card's name. + * @return The name in the CardInfo, or an empty string if card is null + */ +QString ExactCard::getName() const +{ + return card.isNull() ? "" : card->getName(); +} + +/** + * Gets a view of the underlying cardInfoPtr. + * @return A const reference to the CardInfo, or an empty CardInfo if card is null + */ +const CardInfo &ExactCard::getInfo() const +{ + if (card.isNull()) { + static CardInfoPtr emptyCard = CardInfo::newInstance(""); + return *emptyCard; + } + return *card; +} + +/** + * The key used to identify this exact printing in the cache + */ +QString ExactCard::getPixmapCacheKey() const +{ + QString uuid = printing.getUuid(); + QString suffix = uuid.isEmpty() ? "" : "_" + uuid; + return QLatin1String("card_") + card->getName() + suffix; +} + +/** + * Checks if the card is null or empty. + */ +bool ExactCard::isEmpty() const +{ + return card.isNull() || card->getName().isEmpty(); +} + +/** + * Returns true if isEmpty() is false + */ +ExactCard::operator bool() const +{ + return !isEmpty(); +} + +/** + * Gets the CardInfo to emit the pixmapUpdated signal + */ +void ExactCard::emitPixmapUpdated() const +{ + emit card->pixmapUpdated(printing); +} \ No newline at end of file diff --git a/cockatrice/src/game/cards/exact_card.h b/cockatrice/src/game/cards/exact_card.h new file mode 100644 index 000000000..a962ea076 --- /dev/null +++ b/cockatrice/src/game/cards/exact_card.h @@ -0,0 +1,46 @@ +#ifndef EXACT_CARD_H +#define EXACT_CARD_H + +#include "card_info.h" + +/** + * Identifies the card along with its exact printing + */ +class ExactCard +{ + CardInfoPtr card; + PrintingInfo printing; + +public: + ExactCard(); + explicit ExactCard(const CardInfoPtr &_card, const PrintingInfo &_printing = PrintingInfo()); + + /** + * Gets the CardInfoPtr. Can be null. + */ + CardInfoPtr getCardPtr() const + { + return card; + } + + /** + * Gets the PrintingInfo. Can be empty. + */ + PrintingInfo getPrinting() const + { + return printing; + } + + bool operator==(const ExactCard &other) const; + + QString getName() const; + const CardInfo &getInfo() const; + QString getPixmapCacheKey() const; + + bool isEmpty() const; + explicit operator bool() const; + + void emitPixmapUpdated() const; +}; + +#endif // EXACT_CARD_H