refactor CardList (#5197)

This commit is contained in:
RickyRister 2024-11-26 21:15:35 -08:00 committed by GitHub
parent a8471f62bc
commit 0ca8bdb3a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 41 deletions

View file

@ -9,42 +9,31 @@ CardList::CardList(bool _contentsKnown) : QList<CardItem *>(), contentsKnown(_co
{ {
} }
CardItem *CardList::findCard(const int id, const bool remove, int *position) /**
* @brief Finds the CardItem with the given id in the list.
* If contentsKnown is false, then this just returns the first element of the list.
*
* @param cardId The id of the card to find.
*
* @returns A pointer to the CardItem, or a nullptr if not found.
*/
CardItem *CardList::findCard(const int cardId) const
{ {
if (!contentsKnown) { if (!contentsKnown && !empty()) {
if (empty()) return at(0);
return 0; } else {
CardItem *temp = at(0); for (auto *cardItem : *this) {
if (remove) if (cardItem->getId() == cardId) {
removeAt(0); return cardItem;
if (position)
*position = id;
return temp;
} else
for (int i = 0; i < size(); i++) {
CardItem *temp = at(i);
if (temp->getId() == id) {
if (remove)
removeAt(i);
if (position)
*position = i;
return temp;
} }
} }
return 0; }
return nullptr;
} }
class CardList::compareFunctor void CardList::sort(int flags)
{ {
private: auto comparator = [flags](CardItem *a, CardItem *b) {
int flags;
public:
explicit compareFunctor(int _flags) : flags(_flags)
{
}
inline bool operator()(CardItem *a, CardItem *b) const
{
if (flags & SortByType) { if (flags & SortByType) {
QString t1 = a->getInfo() ? a->getInfo()->getMainCardType() : QString(); QString t1 = a->getInfo() ? a->getInfo()->getMainCardType() : QString();
QString t2 = b->getInfo() ? b->getInfo()->getMainCardType() : QString(); QString t2 = b->getInfo() ? b->getInfo()->getMainCardType() : QString();
@ -53,11 +42,7 @@ public:
return t1 < t2; return t1 < t2;
} else } else
return a->getName() < b->getName(); return a->getName() < b->getName();
} };
};
void CardList::sort(int flags) std::sort(begin(), end(), comparator);
{
compareFunctor cf(flags);
std::sort(begin(), end(), cf);
} }

View file

@ -7,9 +7,6 @@ class CardItem;
class CardList : public QList<CardItem *> class CardList : public QList<CardItem *>
{ {
private:
class compareFunctor;
protected: protected:
bool contentsKnown; bool contentsKnown;
@ -20,7 +17,7 @@ public:
SortByType = 2 SortByType = 2
}; };
CardList(bool _contentsKnown); CardList(bool _contentsKnown);
CardItem *findCard(const int id, const bool remove, int *position = NULL); CardItem *findCard(const int cardId) const;
bool getContentsKnown() const bool getContentsKnown() const
{ {
return contentsKnown; return contentsKnown;

View file

@ -142,7 +142,7 @@ void CardZone::addCard(CardItem *card, bool reorganize, int x, int y)
CardItem *CardZone::getCard(int cardId, const QString &cardName) CardItem *CardZone::getCard(int cardId, const QString &cardName)
{ {
CardItem *c = cards.findCard(cardId, false); CardItem *c = cards.findCard(cardId);
if (!c) { if (!c) {
qDebug() << "CardZone::getCard: card id=" << cardId << "not found"; qDebug() << "CardZone::getCard: card id=" << cardId << "not found";
return 0; return 0;