[DeckDockWidget] Fix VDE crash due to not mapping proxy index (#6479)

This commit is contained in:
RickyRister 2026-01-02 05:32:22 -08:00 committed by GitHub
parent 84e6907fa9
commit bbd8671e6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 24 deletions

View file

@ -578,18 +578,19 @@ void DeckEditorDeckDockWidget::expandAll()
} }
/** /**
* Gets the index of all the currently selected card nodes in the decklist table. * Gets the source index of all the currently selected card nodes in the decklist table.
* The list is in reverse order of the visual selection, so that rows can be deleted while iterating over them. * The list is in reverse order of the visual selection, so that rows can be deleted while iterating over them.
* *
* @return A model index list containing all selected card nodes * @return A list containing the source indices of all selected card nodes
*/ */
QModelIndexList DeckEditorDeckDockWidget::getSelectedCardNodes() const QModelIndexList DeckEditorDeckDockWidget::getSelectedCardNodeSourceIndices() const
{ {
auto selectedRows = deckView->selectionModel()->selectedRows(); auto selectedRows = deckView->selectionModel()->selectedRows();
const auto notLeafNode = [this](const QModelIndex &index) { const auto mapToSource = [this](const QModelIndex &index) { return proxy->mapToSource(index); };
return getModel()->hasChildren(proxy->mapToSource(index)); std::transform(selectedRows.begin(), selectedRows.end(), selectedRows.begin(), mapToSource);
};
const auto notLeafNode = [this](const QModelIndex &sourceIndex) { return getModel()->hasChildren(sourceIndex); };
selectedRows.erase(std::remove_if(selectedRows.begin(), selectedRows.end(), notLeafNode), selectedRows.end()); selectedRows.erase(std::remove_if(selectedRows.begin(), selectedRows.end(), notLeafNode), selectedRows.end());
std::reverse(selectedRows.begin(), selectedRows.end()); std::reverse(selectedRows.begin(), selectedRows.end());
@ -608,10 +609,10 @@ void DeckEditorDeckDockWidget::actAddCard(const ExactCard &card, const QString &
void DeckEditorDeckDockWidget::actIncrementSelection() void DeckEditorDeckDockWidget::actIncrementSelection()
{ {
auto selectedRows = getSelectedCardNodes(); auto selectedRows = getSelectedCardNodeSourceIndices();
for (const auto &index : selectedRows) { for (const auto &sourceIndex : selectedRows) {
offsetCountAtIndex(index, true); offsetCountAtIndex(sourceIndex, true);
} }
} }
@ -630,7 +631,7 @@ void DeckEditorDeckDockWidget::actSwapCard(const ExactCard &card, const QString
void DeckEditorDeckDockWidget::actSwapSelection() void DeckEditorDeckDockWidget::actSwapSelection()
{ {
auto selectedRows = getSelectedCardNodes(); auto selectedRows = getSelectedCardNodeSourceIndices();
// hack to maintain the old reselection behavior when currently selected row of a single-selection gets deleted // hack to maintain the old reselection behavior when currently selected row of a single-selection gets deleted
// TODO: remove the hack and also handle reselection when all rows of a multi-selection gets deleted // TODO: remove the hack and also handle reselection when all rows of a multi-selection gets deleted
@ -638,8 +639,8 @@ void DeckEditorDeckDockWidget::actSwapSelection()
deckView->setSelectionMode(QAbstractItemView::SingleSelection); deckView->setSelectionMode(QAbstractItemView::SingleSelection);
} }
for (const auto &currentIndex : selectedRows) { for (const auto &sourceIndex : selectedRows) {
deckStateManager->swapCardAtIndex(currentIndex); deckStateManager->swapCardAtIndex(sourceIndex);
} }
deckView->setSelectionMode(QAbstractItemView::ExtendedSelection); deckView->setSelectionMode(QAbstractItemView::ExtendedSelection);
@ -659,7 +660,7 @@ void DeckEditorDeckDockWidget::actDecrementCard(const ExactCard &card, QString z
void DeckEditorDeckDockWidget::actDecrementSelection() void DeckEditorDeckDockWidget::actDecrementSelection()
{ {
auto selectedRows = getSelectedCardNodes(); auto selectedRows = getSelectedCardNodeSourceIndices();
// hack to maintain the old reselection behavior when currently selected row of a single-selection gets deleted // hack to maintain the old reselection behavior when currently selected row of a single-selection gets deleted
// TODO: remove the hack and also handle reselection when all rows of a multi-selection gets deleted // TODO: remove the hack and also handle reselection when all rows of a multi-selection gets deleted
@ -667,8 +668,8 @@ void DeckEditorDeckDockWidget::actDecrementSelection()
deckView->setSelectionMode(QAbstractItemView::SingleSelection); deckView->setSelectionMode(QAbstractItemView::SingleSelection);
} }
for (const auto &index : selectedRows) { for (const auto &sourceIndex : selectedRows) {
offsetCountAtIndex(index, false); offsetCountAtIndex(sourceIndex, false);
} }
deckView->setSelectionMode(QAbstractItemView::ExtendedSelection); deckView->setSelectionMode(QAbstractItemView::ExtendedSelection);
@ -676,7 +677,7 @@ void DeckEditorDeckDockWidget::actDecrementSelection()
void DeckEditorDeckDockWidget::actRemoveCard() void DeckEditorDeckDockWidget::actRemoveCard()
{ {
auto selectedRows = getSelectedCardNodes(); auto selectedRows = getSelectedCardNodeSourceIndices();
// hack to maintain the old reselection behavior when currently selected row of a single-selection gets deleted // hack to maintain the old reselection behavior when currently selected row of a single-selection gets deleted
// TODO: remove the hack and also handle reselection when all rows of a multi-selection gets deleted // TODO: remove the hack and also handle reselection when all rows of a multi-selection gets deleted
@ -684,8 +685,8 @@ void DeckEditorDeckDockWidget::actRemoveCard()
deckView->setSelectionMode(QAbstractItemView::SingleSelection); deckView->setSelectionMode(QAbstractItemView::SingleSelection);
} }
for (const auto &row : selectedRows) { for (const auto &sourceIndex : selectedRows) {
deckStateManager->removeCardAtIndex(row); deckStateManager->removeCardAtIndex(sourceIndex);
} }
deckView->setSelectionMode(QAbstractItemView::ExtendedSelection); deckView->setSelectionMode(QAbstractItemView::ExtendedSelection);
@ -693,7 +694,7 @@ void DeckEditorDeckDockWidget::actRemoveCard()
/** /**
* @brief Increments or decrements the amount of the card node at the index by 1. * @brief Increments or decrements the amount of the card node at the index by 1.
* @param idx The proxy index * @param idx The source index
* @param isIncrement If true, increments the count. If false, decrements the count * @param isIncrement If true, increments the count. If false, decrements the count
*/ */
void DeckEditorDeckDockWidget::offsetCountAtIndex(const QModelIndex &idx, bool isIncrement) void DeckEditorDeckDockWidget::offsetCountAtIndex(const QModelIndex &idx, bool isIncrement)
@ -702,12 +703,10 @@ void DeckEditorDeckDockWidget::offsetCountAtIndex(const QModelIndex &idx, bool i
return; return;
} }
QModelIndex sourceIndex = proxy->mapToSource(idx);
if (isIncrement) { if (isIncrement) {
deckStateManager->incrementCountAtIndex(sourceIndex); deckStateManager->incrementCountAtIndex(idx);
} else { } else {
deckStateManager->decrementCountAtIndex(sourceIndex); deckStateManager->decrementCountAtIndex(idx);
} }
} }

View file

@ -90,7 +90,7 @@ private:
QAction *aRemoveCard, *aIncrement, *aDecrement, *aSwapCard; QAction *aRemoveCard, *aIncrement, *aDecrement, *aSwapCard;
DeckListModel *getModel() const; DeckListModel *getModel() const;
[[nodiscard]] QModelIndexList getSelectedCardNodes() const; [[nodiscard]] QModelIndexList getSelectedCardNodeSourceIndices() const;
void offsetCountAtIndex(const QModelIndex &idx, bool isIncrement); void offsetCountAtIndex(const QModelIndex &idx, bool isIncrement);
private slots: private slots: