From 0735f164a8f8cf7d7c2c618af4bb1e28516ae397 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 8 Sep 2023 02:02:21 -0500 Subject: [PATCH] Revert back to model reset for updating the computer list It's possible that multiple changes occur in parallel, so we can't rely on receiving them one at a time even if the callbacks are synchronized. Handling this substantially complicates the logic such that it's not really worth doing individual insertions and deletions anymore. This reverts 94d821a4a9affdd5d3a94d188109928a4f27e078 and replaces it with a solution that is basically the old code except that it properly handles changes to the structure of the list. --- app/gui/computermodel.cpp | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/app/gui/computermodel.cpp b/app/gui/computermodel.cpp index 7be89b4c..0d386547 100644 --- a/app/gui/computermodel.cpp +++ b/app/gui/computermodel.cpp @@ -188,34 +188,17 @@ void ComputerModel::handleComputerStateChanged(NvComputer* computer) { QVector newComputerList = m_ComputerManager->getComputers(); - // Check if this PC was added or moved - int newListIndex = newComputerList.indexOf(computer); - int oldListIndex = m_Computers.indexOf(computer); - if (newListIndex != oldListIndex) { - // We should never learn of a removal in this callback - Q_ASSERT(newListIndex != -1); - - // If the PC was present in the old list, remove it to reinsert it. - if (oldListIndex != -1) { - beginRemoveRows(QModelIndex(), oldListIndex, oldListIndex); - m_Computers.remove(oldListIndex); - endRemoveRows(); - } - - // Insert the PC in the new location - beginInsertRows(QModelIndex(), newListIndex, newListIndex); - m_Computers.insert(newListIndex, computer); - endInsertRows(); + // Reset the model if the structural layout of the list has changed + if (m_Computers != newComputerList) { + beginResetModel(); + m_Computers = newComputerList; + endResetModel(); } else { - Q_ASSERT(oldListIndex != -1); - // Let the view know that this specific computer changed - emit dataChanged(createIndex(newListIndex, 0), createIndex(newListIndex, 0)); + int index = m_Computers.indexOf(computer); + emit dataChanged(createIndex(index, 0), createIndex(index, 0)); } - - // The old and new vectors should be equivalent - Q_ASSERT(newComputerList == m_Computers); } #include "computermodel.moc"