- if (m_currentItem >= 0) {
- int currentItem = m_currentItem;
- foreach (const KItemRange& itemRange, itemRanges) {
- if (currentItem < itemRange.index) {
- break;
- }
- if (currentItem >= itemRange.index + itemRange.count) {
- currentItem -= itemRange.count;
- } else if (currentItem >= m_model->count()) {
- currentItem = m_model->count() - 1;
+ m_currentItem = indexAfterRangesRemoving(m_currentItem, itemRanges, DiscardRemovedIndex);
+ if (m_currentItem != previousCurrent) {
+ emit currentChanged(m_currentItem, previousCurrent);
+ if (m_currentItem < 0) {
+ // Calling setCurrentItem() would trigger the selectionChanged signal, but we want to
+ // emit it only once in this function -> change the current item manually and emit currentChanged
+ m_currentItem = indexAfterRangesRemoving(previousCurrent, itemRanges, AdjustRemovedIndex);
+ emit currentChanged(m_currentItem, -1);
+ }
+ }
+
+ // Update the anchor item
+ if (m_anchorItem >= 0) {
+ m_anchorItem = indexAfterRangesRemoving(m_anchorItem, itemRanges, DiscardRemovedIndex);
+ if (m_anchorItem < 0) {
+ m_isAnchoredSelectionActive = false;
+ }
+ }
+
+ // Update the selections and the anchor item
+ if (!m_selectedItems.isEmpty()) {
+ const KItemSet previous = m_selectedItems;
+ m_selectedItems.clear();
+
+ for (int oldIndex : previous) {
+ const int index = indexAfterRangesRemoving(oldIndex, itemRanges, DiscardRemovedIndex);
+ if (index >= 0) {
+ m_selectedItems.insert(index);