mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-14 14:32:15 -07:00
Refactor DeckModel access
This commit is contained in:
parent
18aca5d047
commit
1c358f3462
6 changed files with 100 additions and 52 deletions
|
|
@ -436,7 +436,51 @@ QModelIndex DeckListModel::addCard(const ExactCard &card, const QString &zoneNam
|
|||
}
|
||||
sort(lastKnownColumn, lastKnownOrder);
|
||||
emitRecursiveUpdates(parentIndex);
|
||||
return nodeToIndex(cardNode);
|
||||
auto index = nodeToIndex(cardNode);
|
||||
|
||||
emit cardAddedAt(index);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
bool DeckListModel::incrementAmountAtIndex(const QModelIndex &idx)
|
||||
{
|
||||
return offsetAmountAtIndex(idx, 1);
|
||||
}
|
||||
|
||||
bool DeckListModel::decrementAmountAtIndex(const QModelIndex &idx)
|
||||
{
|
||||
return offsetAmountAtIndex(idx, -1);
|
||||
}
|
||||
|
||||
bool DeckListModel::offsetAmountAtIndex(const QModelIndex &idx, int offset)
|
||||
{
|
||||
if (!idx.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto *node = static_cast<AbstractDecklistNode *>(idx.internalPointer());
|
||||
auto *card = dynamic_cast<DecklistModelCardNode *>(node);
|
||||
|
||||
if (!card) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const QModelIndex numberIndex = idx.siblingAtColumn(DeckListModelColumns::CARD_AMOUNT);
|
||||
const int count = numberIndex.data(Qt::EditRole).toInt();
|
||||
const int newCount = count + offset;
|
||||
|
||||
if (newCount <= 0) {
|
||||
removeRow(idx.row(), idx.parent());
|
||||
} else {
|
||||
setData(numberIndex, newCount, Qt::EditRole);
|
||||
}
|
||||
|
||||
if (offset > 0) {
|
||||
emit cardAddedAt(idx);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int DeckListModel::findSortedInsertRow(InnerDecklistNode *parent, CardInfoPtr cardInfo) const
|
||||
|
|
@ -559,6 +603,7 @@ void DeckListModel::setDeckList(DeckList *_deck)
|
|||
deckList = _deck;
|
||||
}
|
||||
rebuildTree();
|
||||
emit deckReplaced();
|
||||
}
|
||||
|
||||
void DeckListModel::forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func)
|
||||
|
|
|
|||
|
|
@ -226,6 +226,18 @@ signals:
|
|||
*/
|
||||
void deckHashChanged();
|
||||
|
||||
/**
|
||||
* @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.
|
||||
* @param index The index of the card that got added.
|
||||
*/
|
||||
void cardAddedAt(const QModelIndex &index);
|
||||
|
||||
/**
|
||||
* @brief Emitted whenever the deck in the model has been replaced with a new one
|
||||
*/
|
||||
void deckReplaced();
|
||||
|
||||
public:
|
||||
explicit DeckListModel(QObject *parent = nullptr);
|
||||
~DeckListModel() override;
|
||||
|
|
@ -294,6 +306,21 @@ public:
|
|||
*/
|
||||
QModelIndex addCard(const ExactCard &card, const QString &zoneName);
|
||||
|
||||
/**
|
||||
* @brief Increments the `amount` field of the card node at the index by 1.
|
||||
* @param idx The index of a card node. No-ops if the index is invalid or not a card node
|
||||
* @return Whether the operation was successful
|
||||
*/
|
||||
bool incrementAmountAtIndex(const QModelIndex &idx);
|
||||
|
||||
/**
|
||||
* @brief Decrements the `amount` field of the card node at the index by 1.
|
||||
* Removes the node if it causes the amount to fall to 0.
|
||||
* @param idx The index of a card node. No-ops if the index is invalid or not a card node
|
||||
* @return Whether the operation was successful
|
||||
*/
|
||||
bool decrementAmountAtIndex(const QModelIndex &idx);
|
||||
|
||||
/**
|
||||
* @brief Determines the sorted insertion row for a card.
|
||||
* @param parent The parent node where the card will be inserted.
|
||||
|
|
@ -367,6 +394,9 @@ private:
|
|||
const QString &zoneName,
|
||||
const QString &providerId = "",
|
||||
const QString &cardNumber = "") const;
|
||||
|
||||
bool offsetAmountAtIndex(const QModelIndex &idx, int offset);
|
||||
|
||||
void emitRecursiveUpdates(const QModelIndex &index);
|
||||
void sortHelper(InnerDecklistNode *node, Qt::SortOrder order);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue