| 411 | } |
| 412 | |
| 413 | InstanceList::InstListError InstanceList::loadList() |
| 414 | { |
| 415 | auto existingIds = getIdMapping(m_instances); |
| 416 | |
| 417 | QList<InstancePtr> newList; |
| 418 | |
| 419 | for (auto& id : discoverInstances()) { |
| 420 | if (existingIds.contains(id)) { |
| 421 | auto instPair = existingIds[id]; |
| 422 | existingIds.remove(id); |
| 423 | qDebug() << "Should keep and soft-reload" << id; |
| 424 | } else { |
| 425 | InstancePtr instPtr = loadInstance(id); |
| 426 | if (instPtr) { |
| 427 | newList.append(instPtr); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // TODO: looks like a general algorithm with a few specifics inserted. Do something about it. |
| 433 | if (!existingIds.isEmpty()) { |
| 434 | // get the list of removed instances and sort it by their original index, from last to first |
| 435 | auto deadList = existingIds.values(); |
| 436 | auto orderSortPredicate = [](const InstanceLocator& a, const InstanceLocator& b) -> bool { return a.second > b.second; }; |
| 437 | std::sort(deadList.begin(), deadList.end(), orderSortPredicate); |
| 438 | // remove the contiguous ranges of rows |
| 439 | int front_bookmark = -1; |
| 440 | int back_bookmark = -1; |
| 441 | int currentItem = -1; |
| 442 | auto removeNow = [&]() { |
| 443 | beginRemoveRows(QModelIndex(), front_bookmark, back_bookmark); |
| 444 | m_instances.erase(m_instances.begin() + front_bookmark, m_instances.begin() + back_bookmark + 1); |
| 445 | endRemoveRows(); |
| 446 | front_bookmark = -1; |
| 447 | back_bookmark = currentItem; |
| 448 | }; |
| 449 | for (auto& removedItem : deadList) { |
| 450 | auto instPtr = removedItem.first; |
| 451 | instPtr->invalidate(); |
| 452 | currentItem = removedItem.second; |
| 453 | if (back_bookmark == -1) { |
| 454 | // no bookmark yet |
| 455 | back_bookmark = currentItem; |
| 456 | } else if (currentItem == front_bookmark - 1) { |
| 457 | // part of contiguous sequence, continue |
| 458 | } else { |
| 459 | // seam between previous and current item |
| 460 | removeNow(); |
| 461 | } |
| 462 | front_bookmark = currentItem; |
| 463 | } |
| 464 | if (back_bookmark != -1) { |
| 465 | removeNow(); |
| 466 | } |
| 467 | } |
| 468 | if (newList.size()) { |
| 469 | add(newList); |
| 470 | } |
no test coverage detected