mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-12 19:11:31 -07:00
[DeckDockWidget] Correctly handle auto-expanding tree (#6446)
* move method * remove expandAll calls * update recursiveExpand * Refactor DeckModel access * [DeckDockWidget] Correctly handle auto-expand
This commit is contained in:
@@ -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;
|
||||
@@ -250,7 +262,6 @@ public:
|
||||
[[nodiscard]] int rowCount(const QModelIndex &parent) const override;
|
||||
[[nodiscard]] int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const override;
|
||||
[[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
|
||||
void emitBackgroundUpdates(const QModelIndex &parent);
|
||||
[[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
[[nodiscard]] QModelIndex index(int row, int column, const QModelIndex &parent) const override;
|
||||
[[nodiscard]] QModelIndex parent(const QModelIndex &index) const override;
|
||||
@@ -258,6 +269,12 @@ public:
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
bool removeRows(int row, int count, const QModelIndex &parent) override;
|
||||
|
||||
/**
|
||||
* Recursively emits the dataChanged signal for all child nodes.
|
||||
* @param parent The parent node
|
||||
*/
|
||||
void emitBackgroundUpdates(const QModelIndex &parent);
|
||||
|
||||
/**
|
||||
* @brief Finds a card by name, zone, and optional identifiers.
|
||||
* @param cardName The card's name.
|
||||
@@ -289,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.
|
||||
@@ -362,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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user