BUG: 290736
REVIEW: 108356
FIXED-IN: 4.10
{
// Store the current selection (needed in the selectionChanged() signal)
const QSet<int> previousSelection = selectedItems();
{
// Store the current selection (needed in the selectionChanged() signal)
const QSet<int> previousSelection = selectedItems();
+ const int previousCurrent = m_currentItem;
// Update the current item
// Update the current item
- if (m_currentItem >= 0) {
- const int previousCurrent = m_currentItem;
- // 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(m_currentItem, itemRanges);
- if (m_currentItem != previousCurrent) {
- emit currentChanged(m_currentItem, previousCurrent);
- }
-
+ m_currentItem = indexAfterRangesRemoving(m_currentItem, itemRanges, DiscardRemovedIndex);
+ if (m_currentItem != previousCurrent) {
+ emit currentChanged(m_currentItem, previousCurrent);
- // The current item has been removed.
- m_currentItem = qMin(previousCurrent, m_model->count() - 1);
+ // 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) {
emit currentChanged(m_currentItem, -1);
}
}
// Update the anchor item
if (m_anchorItem >= 0) {
- m_anchorItem = indexAfterRangesRemoving(m_anchorItem, itemRanges);
+ m_anchorItem = indexAfterRangesRemoving(m_anchorItem, itemRanges, DiscardRemovedIndex);
if (m_anchorItem < 0) {
m_isAnchoredSelectionActive = false;
}
if (m_anchorItem < 0) {
m_isAnchoredSelectionActive = false;
}
m_selectedItems.reserve(previous.count());
QSetIterator<int> it(previous);
while (it.hasNext()) {
m_selectedItems.reserve(previous.count());
QSetIterator<int> it(previous);
while (it.hasNext()) {
- const int index = indexAfterRangesRemoving(it.next(), itemRanges);
+ const int index = indexAfterRangesRemoving(it.next(), itemRanges, DiscardRemovedIndex);
if (index >= 0) {
m_selectedItems.insert(index);
}
if (index >= 0) {
m_selectedItems.insert(index);
}
-int KItemListSelectionManager::indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges) const
+int KItemListSelectionManager::indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges,
+ const RangesRemovingBehaviour behaviour) const
{
int dec = 0;
foreach (const KItemRange& itemRange, itemRanges) {
{
int dec = 0;
foreach (const KItemRange& itemRange, itemRanges) {
- if (index < itemRange.index + itemRange.count) {
+ dec += itemRange.count;
+
+ const int firstIndexAfterRange = itemRange.index + itemRange.count;
+ if (index < firstIndexAfterRange) {
// The index is part of the removed range
// The index is part of the removed range
+ if (behaviour == DiscardRemovedIndex) {
+ return -1;
+ } else {
+ // Use the first item after the range as new index
+ index = firstIndexAfterRange;
+ break;
+ }
-
- dec += itemRange.count;
+ return qBound(-1, index - dec, m_model->count() - 1);
}
#include "kitemlistselectionmanager.moc"
}
#include "kitemlistselectionmanager.moc"
+ enum RangesRemovingBehaviour {
+ DiscardRemovedIndex,
+ AdjustRemovedIndex
+ };
+
public:
enum SelectionMode {
Select,
public:
enum SelectionMode {
Select,
* Helper method for itemsRemoved. Returns the changed index after removing
* the given range. If the index is part of the range, -1 will be returned.
*/
* Helper method for itemsRemoved. Returns the changed index after removing
* the given range. If the index is part of the range, -1 will be returned.
*/
- int indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges) const;
+ int indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges, const RangesRemovingBehaviour behaviour) const;
private:
int m_currentItem;
private:
int m_currentItem;
QTest::newRow("Remove before") << 50 << 0 << 10 << 40;
QTest::newRow("Remove after") << 50 << 51 << 10 << 50;
QTest::newRow("Remove exactly current item") << 50 << 50 << 1 << 50;
QTest::newRow("Remove before") << 50 << 0 << 10 << 40;
QTest::newRow("Remove after") << 50 << 51 << 10 << 50;
QTest::newRow("Remove exactly current item") << 50 << 50 << 1 << 50;
- QTest::newRow("Remove around current item") << 50 << 45 << 10 << 50;
+ QTest::newRow("Remove around current item") << 50 << 45 << 10 << 45;
QTest::newRow("Remove all except one item") << 50 << 1 << 99 << 0;
}
QTest::newRow("Remove all except one item") << 50 << 1 << 99 << 0;
}