Process insertions and moves in the ComputerModel without resetting

This commit is contained in:
Cameron Gutman
2023-09-03 16:19:24 -05:00
parent 6a854f7a64
commit 94d821a4a9
+27 -11
View File
@@ -186,20 +186,36 @@ void ComputerModel::handlePairingCompleted(NvComputer*, QString error)
void ComputerModel::handleComputerStateChanged(NvComputer* computer) void ComputerModel::handleComputerStateChanged(NvComputer* computer)
{ {
// If this is an existing computer, we can report the data changed QVector<NvComputer*> newComputerList = m_ComputerManager->getComputers();
int index = m_Computers.indexOf(computer);
if (index >= 0) { // Check if this PC was added or moved
// Let the view know that this specific computer changed int newListIndex = newComputerList.indexOf(computer);
emit dataChanged(createIndex(index, 0), createIndex(index, 0)); 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.emplace(newListIndex, computer);
endInsertRows();
} }
else { else {
// This is a new PC which may be inserted at an arbitrary point Q_ASSERT(oldListIndex != -1);
// in our computer list (since it comes from CM's QMap). Reload
// the whole model state to ensure it stays consistent. // Let the view know that this specific computer changed
beginResetModel(); emit dataChanged(createIndex(newListIndex, 0), createIndex(newListIndex, 0));
m_Computers = m_ComputerManager->getComputers();
endResetModel();
} }
// The old and new vectors should be equivalent
Q_ASSERT(newComputerList == m_Computers);
} }
#include "computermodel.moc" #include "computermodel.moc"