| 496 | } |
| 497 | |
| 498 | InstanceList::InstListError InstanceList::loadList() |
| 499 | { |
| 500 | auto existingIds = getIdMapping(m_instances); |
| 501 | |
| 502 | std::vector<std::unique_ptr<BaseInstance>> newList; |
| 503 | |
| 504 | for (auto& id : discoverInstances()) { |
| 505 | if (existingIds.contains(id)) { |
| 506 | existingIds.remove(id); |
| 507 | qInfo() << "Should keep and soft-reload" << id; |
| 508 | } else { |
| 509 | std::unique_ptr<BaseInstance> instPtr = loadInstance(id); |
| 510 | if (instPtr) { |
| 511 | newList.push_back(std::move(instPtr)); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // TODO: looks like a general algorithm with a few specifics inserted. Do something about it. |
| 517 | if (!existingIds.isEmpty()) { |
| 518 | // get the list of removed instances and sort it by their original index, from last to first |
| 519 | auto deadList = existingIds.values(); |
| 520 | auto orderSortPredicate = [](const InstanceLocator& a, const InstanceLocator& b) -> bool { return a.second > b.second; }; |
| 521 | std::sort(deadList.begin(), deadList.end(), orderSortPredicate); |
| 522 | // remove the contiguous ranges of rows |
| 523 | int front_bookmark = -1; |
| 524 | int back_bookmark = -1; |
| 525 | int currentItem = -1; |
| 526 | auto removeNow = [this, &front_bookmark, &back_bookmark, ¤tItem]() { |
| 527 | beginRemoveRows(QModelIndex(), front_bookmark, back_bookmark); |
| 528 | m_instances.erase(m_instances.begin() + front_bookmark, m_instances.begin() + back_bookmark + 1); |
| 529 | endRemoveRows(); |
| 530 | front_bookmark = -1; |
| 531 | back_bookmark = currentItem; |
| 532 | }; |
| 533 | for (auto& removedItem : deadList) { |
| 534 | auto instPtr = removedItem.first; |
| 535 | instPtr->invalidate(); |
| 536 | currentItem = removedItem.second; |
| 537 | if (back_bookmark == -1) { |
| 538 | // no bookmark yet |
| 539 | back_bookmark = currentItem; |
| 540 | } else if (currentItem == front_bookmark - 1) { |
| 541 | // part of contiguous sequence, continue |
| 542 | } else { |
| 543 | // seam between previous and current item |
| 544 | removeNow(); |
| 545 | } |
| 546 | front_bookmark = currentItem; |
| 547 | } |
| 548 | if (back_bookmark != -1) { |
| 549 | removeNow(); |
| 550 | } |
| 551 | } |
| 552 | if (newList.size()) { |
| 553 | add(newList); |
| 554 | } |
| 555 | m_dirty = false; |
no test coverage detected