| 459 | } |
| 460 | |
| 461 | InstanceList::InstListError InstanceList::loadList() |
| 462 | { |
| 463 | auto existingIds = getIdMapping(m_instances); |
| 464 | |
| 465 | QList<InstancePtr> newList; |
| 466 | |
| 467 | for (auto& id : discoverInstances()) { |
| 468 | if (existingIds.contains(id)) { |
| 469 | auto instPair = existingIds[id]; |
| 470 | existingIds.remove(id); |
| 471 | qDebug() << "Should keep and soft-reload" << id; |
| 472 | } else { |
| 473 | InstancePtr instPtr = loadInstance(id); |
| 474 | if (instPtr) { |
| 475 | newList.append(instPtr); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // TODO: looks like a general algorithm with a few specifics inserted. Do something about it. |
| 481 | if (!existingIds.isEmpty()) { |
| 482 | // get the list of removed instances and sort it by their original index, from last to first |
| 483 | auto deadList = existingIds.values(); |
| 484 | auto orderSortPredicate = [](const InstanceLocator& a, const InstanceLocator& b) -> bool { return a.second > b.second; }; |
| 485 | std::sort(deadList.begin(), deadList.end(), orderSortPredicate); |
| 486 | // remove the contiguous ranges of rows |
| 487 | int front_bookmark = -1; |
| 488 | int back_bookmark = -1; |
| 489 | int currentItem = -1; |
| 490 | auto removeNow = [&]() { |
| 491 | beginRemoveRows(QModelIndex(), front_bookmark, back_bookmark); |
| 492 | m_instances.erase(m_instances.begin() + front_bookmark, m_instances.begin() + back_bookmark + 1); |
| 493 | endRemoveRows(); |
| 494 | front_bookmark = -1; |
| 495 | back_bookmark = currentItem; |
| 496 | }; |
| 497 | for (auto& removedItem : deadList) { |
| 498 | auto instPtr = removedItem.first; |
| 499 | instPtr->invalidate(); |
| 500 | currentItem = removedItem.second; |
| 501 | if (back_bookmark == -1) { |
| 502 | // no bookmark yet |
| 503 | back_bookmark = currentItem; |
| 504 | } else if (currentItem == front_bookmark - 1) { |
| 505 | // part of contiguous sequence, continue |
| 506 | } else { |
| 507 | // seam between previous and current item |
| 508 | removeNow(); |
| 509 | } |
| 510 | front_bookmark = currentItem; |
| 511 | } |
| 512 | if (back_bookmark != -1) { |
| 513 | removeNow(); |
| 514 | } |
| 515 | } |
| 516 | if (newList.size()) { |
| 517 | add(newList); |
| 518 | } |
no test coverage detected