Refactor DeckModel access

This commit is contained in:
RickyRister 2025-12-23 05:28:51 -08:00
parent 18aca5d047
commit 1c358f3462
6 changed files with 100 additions and 52 deletions

View file

@ -607,7 +607,7 @@ void DeckEditorDeckDockWidget::actIncrementSelection()
auto selectedRows = getSelectedCardNodes();
for (const auto &index : selectedRows) {
offsetCountAtIndex(index, 1);
offsetCountAtIndex(index, true);
}
}
@ -669,7 +669,7 @@ bool DeckEditorDeckDockWidget::swapCard(const QModelIndex &currentIndex)
return false;
const QString zoneName = gparent.siblingAtColumn(DeckListModelColumns::CARD_NAME).data(Qt::EditRole).toString();
offsetCountAtIndex(currentIndex, -1);
offsetCountAtIndex(currentIndex, false);
const QString otherZoneName = zoneName == DECK_ZONE_MAIN ? DECK_ZONE_SIDE : DECK_ZONE_MAIN;
if (ExactCard card = CardDatabaseManager::query()->getCard({cardName, cardProviderID})) {
@ -699,7 +699,7 @@ void DeckEditorDeckDockWidget::actDecrementCard(const ExactCard &card, QString z
deckView->clearSelection();
deckView->setCurrentIndex(proxy->mapToSource(idx));
offsetCountAtIndex(idx, -1);
offsetCountAtIndex(idx, false);
}
void DeckEditorDeckDockWidget::actDecrementSelection()
@ -713,7 +713,7 @@ void DeckEditorDeckDockWidget::actDecrementSelection()
}
for (const auto &index : selectedRows) {
offsetCountAtIndex(index, -1);
offsetCountAtIndex(index, false);
}
deckView->setSelectionMode(QAbstractItemView::ExtendedSelection);
@ -750,7 +750,12 @@ void DeckEditorDeckDockWidget::actRemoveCard()
}
}
void DeckEditorDeckDockWidget::offsetCountAtIndex(const QModelIndex &idx, int offset)
/**
* @brief Increments or decrements the amount of the card node at the index by 1.
* @param idx The proxy index
* @param isIncrement If true, increments the count. If false, decrements the count
*/
void DeckEditorDeckDockWidget::offsetCountAtIndex(const QModelIndex &idx, bool isIncrement)
{
if (!idx.isValid() || deckModel->hasChildren(idx)) {
return;
@ -758,26 +763,22 @@ void DeckEditorDeckDockWidget::offsetCountAtIndex(const QModelIndex &idx, int of
QModelIndex sourceIndex = proxy->mapToSource(idx);
const QModelIndex numberIndex = sourceIndex.siblingAtColumn(DeckListModelColumns::CARD_AMOUNT);
const QModelIndex nameIndex = sourceIndex.siblingAtColumn(DeckListModelColumns::CARD_NAME);
QString cardName = sourceIndex.siblingAtColumn(DeckListModelColumns::CARD_NAME).data(Qt::EditRole).toString();
QString providerId =
sourceIndex.siblingAtColumn(DeckListModelColumns::CARD_PROVIDER_ID).data(Qt::DisplayRole).toString();
const QString cardName = nameIndex.data(Qt::EditRole).toString();
const int count = numberIndex.data(Qt::EditRole).toInt();
const int new_count = count + offset;
const auto reason =
QString(tr("%1 %2 × \"%3\" (%4)"))
.arg(offset > 0 ? tr("Added") : tr("Removed"))
.arg(qAbs(offset))
.arg(cardName)
.arg(sourceIndex.siblingAtColumn(DeckListModelColumns::CARD_PROVIDER_ID).data(Qt::DisplayRole).toString());
const auto reason = QString(tr("%1 %2 × \"%3\" (%4)"))
.arg(isIncrement ? tr("Added") : tr("Removed"))
.arg(1)
.arg(cardName)
.arg(providerId);
emit requestDeckHistorySave(reason);
if (new_count <= 0) {
deckModel->removeRow(sourceIndex.row(), sourceIndex.parent());
if (isIncrement) {
deckModel->incrementAmountAtIndex(sourceIndex);
} else {
deckModel->setData(numberIndex, new_count, Qt::EditRole);
deckModel->decrementAmountAtIndex(sourceIndex);
}
emit deckModified();

View file

@ -106,7 +106,7 @@ private:
QAction *aRemoveCard, *aIncrement, *aDecrement, *aSwapCard;
[[nodiscard]] QModelIndexList getSelectedCardNodes() const;
void offsetCountAtIndex(const QModelIndex &idx, int offset);
void offsetCountAtIndex(const QModelIndex &idx, bool isIncrement);
private slots:
void decklistCustomMenu(QPoint point);
@ -122,7 +122,7 @@ private slots:
void updateShowBannerCardComboBox(bool visible);
void updateShowTagsWidget(bool visible);
void syncBannerCardComboBoxSelectionWithDeck();
void recursiveExpand(const QModelIndex &sourceIndex);
void recursiveExpand(const QModelIndex &parent);
};
#endif // DECK_EDITOR_DECK_DOCK_WIDGET_H

View file

@ -228,33 +228,6 @@ void CardAmountWidget::removePrintingSideboard()
decrementCardHelper(DECK_ZONE_SIDE);
}
/**
* @brief Offsets the card count at the specified index by the given amount.
*
* @param idx The model index of the card.
* @param offset The amount to add or subtract from the card count.
*/
void CardAmountWidget::offsetCountAtIndex(const QModelIndex &idx, int offset)
{
if (!idx.isValid() || offset == 0) {
return;
}
const QModelIndex numberIndex = idx.siblingAtColumn(DeckListModelColumns::CARD_AMOUNT);
const int count = numberIndex.data(Qt::EditRole).toInt();
const int new_count = count + offset;
deckView->setCurrentIndex(numberIndex);
if (new_count <= 0) {
deckModel->removeRow(idx.row(), idx.parent());
} else {
deckModel->setData(numberIndex, new_count, Qt::EditRole);
}
deckEditor->setModified(true);
}
/**
* @brief Helper function to decrement the card count for a given zone.
*
@ -274,7 +247,7 @@ void CardAmountWidget::decrementCardHelper(const QString &zone)
QModelIndex idx = deckModel->findCard(rootCard.getName(), zone, rootCard.getPrinting().getUuid(),
rootCard.getPrinting().getProperty("num"));
offsetCountAtIndex(idx, -1);
deckModel->decrementAmountAtIndex(idx);
deckEditor->setModified(true);
}

View file

@ -57,7 +57,6 @@ private:
bool hovered;
void offsetCountAtIndex(const QModelIndex &idx, int offset);
void decrementCardHelper(const QString &zoneName);
private slots:

View file

@ -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)

View file

@ -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);