[DeckListModel] Consolidate methods and signals for card change (#6466)

This commit is contained in:
RickyRister
2026-01-05 09:28:59 -08:00
committed by GitHub
parent 85c9d8a9ff
commit 192dac0396
7 changed files with 86 additions and 6 deletions
@@ -12,6 +12,15 @@ DeckListModel::DeckListModel(QObject *parent)
DeckListModel::DeckListModel(QObject *parent, const QSharedPointer<DeckList> &deckList) : DeckListModel(parent)
{
setDeckList(deckList);
// forward change signals
connect(this, &DeckListModel::cardAddedAt, this, &DeckListModel::cardsChanged);
connect(this, &DeckListModel::cardRemoved, this, &DeckListModel::cardsChanged);
connect(this, &DeckListModel::deckReplaced, this, &DeckListModel::cardsChanged);
connect(this, &DeckListModel::cardNodeAddedAt, this, &DeckListModel::cardNodesChanged);
connect(this, &DeckListModel::cardNodeRemoved, this, &DeckListModel::cardNodesChanged);
connect(this, &DeckListModel::deckReplaced, this, &DeckListModel::cardNodesChanged);
}
DeckListModel::~DeckListModel()
@@ -421,6 +430,7 @@ QModelIndex DeckListModel::addCard(const ExactCard &card, const QString &zoneNam
card.getName(), printingInfo.getUuid(), printingInfo.getProperty("num")));
const auto cardSetName = printingInfo.getSet().isNull() ? "" : printingInfo.getSet()->getCorrectedShortName();
bool cardNodeAdded = false;
if (!cardNode) {
// Determine the correct index
int insertRow = findSortedInsertRow(groupNode, cardInfo);
@@ -432,6 +442,8 @@ QModelIndex DeckListModel::addCard(const ExactCard &card, const QString &zoneNam
beginInsertRows(parentIndex, insertRow, insertRow);
cardNode = new DecklistModelCardNode(decklistCard, groupNode, insertRow);
endInsertRows();
cardNodeAdded = true;
} else {
cardNode->setNumber(cardNode->getNumber() + 1);
cardNode->setCardSetShortName(cardSetName);
@@ -444,6 +456,10 @@ QModelIndex DeckListModel::addCard(const ExactCard &card, const QString &zoneNam
emitRecursiveUpdates(parentIndex);
auto index = nodeToIndex(cardNode);
if (cardNodeAdded) {
emit cardNodeAddedAt(index);
}
emit cardAddedAt(index);
return index;
@@ -468,17 +484,42 @@ bool DeckListModel::offsetCountAtIndex(const QModelIndex &idx, int offset)
if (newCount <= 0) {
removeRow(idx.row(), idx.parent());
emit cardNodeRemoved();
} else {
setData(numberIndex, newCount, Qt::EditRole);
}
if (offset > 0) {
emit cardAddedAt(idx);
} else if (offset < 0) {
emit cardRemoved();
}
return true;
}
bool DeckListModel::removeCardAtIndex(const QModelIndex &idx)
{
if (!idx.isValid()) {
return false;
}
auto *node = static_cast<AbstractDecklistNode *>(idx.internalPointer());
auto *card = dynamic_cast<DecklistModelCardNode *>(node);
if (!card) {
return false;
}
bool success = removeRow(idx.row(), idx.parent());
if (success) {
emit cardRemoved();
}
return success;
}
int DeckListModel::findSortedInsertRow(const InnerDecklistNode *parent, const CardInfoPtr &cardInfo) const
{
if (!cardInfo) {
@@ -197,6 +197,12 @@ public:
* InnerDecklistNode containers and supports grouping, sorting, adding/removing
* cards, and printing decklists.
*
* Outside code should refrain from modifying the model with methods inherited from QAbstractItemModel, such as with
* `setData` or `removeRow`.
* Instead, use the custom methods on this class to modify the model, such as `addCard`, `offsetCountAtIndex`, or
* `removeCardAtIndex`.
* This ensures the custom signals for this class are correctly emitted.
*
* Signals:
* - deckHashChanged(): emitted when the deck contents change in a way that
* affects its hash.
@@ -231,6 +237,11 @@ signals:
*/
void deckHashChanged();
/**
* @brief Emitted whenever the cards in the deck changes. This includes when the deck is replaced.
*/
void cardsChanged();
/**
* @brief Emitted whenever a card is added to the deck, regardless of whether it's an entirely new card or an
* existing card that got incremented.
@@ -238,6 +249,28 @@ signals:
*/
void cardAddedAt(const QModelIndex &index);
/**
* @brief Emitted whenever a card is removed from the deck, regardless of whether a card node was removed or an
* existing card got decremented.
*/
void cardRemoved();
/**
* @brief Emitted whenever a card node is added or removed. This includes when the deck is replaced.
*/
void cardNodesChanged();
/**
* @brief Emitted whenever a new card node is added.
* @param index The index of the card node that got added.
*/
void cardNodeAddedAt(const QModelIndex &index);
/**
* @brief Emitted whenever a card node is removed.
*/
void cardNodeRemoved();
/**
* @brief Emitted whenever the deck in the model has been replaced with a new one
*/
@@ -311,6 +344,13 @@ public:
*/
bool offsetCountAtIndex(const QModelIndex &idx, int offset);
/**
* @brief Removes the card node at the index
* @param idx The index of a card node. No-ops if the index is invalid or not a card node.
* @return Whether the node was removed.
*/
bool removeCardAtIndex(const QModelIndex &idx);
/**
* @brief Removes all cards and resets the model.
*/