┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFrank Reininghaus <[email protected]>2011-10-03 16:42:02 +0200
committerFrank Reininghaus <[email protected]>2011-10-03 16:42:02 +0200
commitaeb16937ced1bf426c2ca21dcc270f9e4a96645c (patch)
treeb3f4072528ced2bdfbc0b1ee38ab2518dbdd6fdc /src
parent96d39ad6a9796e9058b993e37af39f0d26ce5caf (diff)
Add function KItemListSelectionManager::isSelected(int index)
This function is used in KItemListView::updateWidgetProperties() to find out if an item is selected in a more efficient way. The new function is tested in KItemListSelectionManagerTest. I've factored out some code from KItemListSelectionManagerTest::testChangeSelection() to a new member to simplify the test.
Diffstat (limited to 'src')
-rw-r--r--src/kitemviews/kitemlistselectionmanager.cpp20
-rw-r--r--src/kitemviews/kitemlistselectionmanager.h1
-rw-r--r--src/kitemviews/kitemlistview.cpp9
-rw-r--r--src/tests/kitemlistselectionmanagertest.cpp75
4 files changed, 54 insertions, 51 deletions
diff --git a/src/kitemviews/kitemlistselectionmanager.cpp b/src/kitemviews/kitemlistselectionmanager.cpp
index 131ee46e6..fb279796f 100644
--- a/src/kitemviews/kitemlistselectionmanager.cpp
+++ b/src/kitemviews/kitemlistselectionmanager.cpp
@@ -95,6 +95,26 @@ QSet<int> KItemListSelectionManager::selectedItems() const
return selectedItems;
}
+bool KItemListSelectionManager::isSelected(int index) const
+{
+ if (m_selectedItems.contains(index)) {
+ return true;
+ }
+
+ if (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem) {
+ Q_ASSERT(m_anchorItem >= 0);
+ Q_ASSERT(m_currentItem >= 0);
+ const int from = qMin(m_anchorItem, m_currentItem);
+ const int to = qMax(m_anchorItem, m_currentItem);
+
+ if (from <= index && index <= to) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
bool KItemListSelectionManager::hasSelection() const
{
return !m_selectedItems.isEmpty() || (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem);
diff --git a/src/kitemviews/kitemlistselectionmanager.h b/src/kitemviews/kitemlistselectionmanager.h
index a8ef5ca2d..82ae13abb 100644
--- a/src/kitemviews/kitemlistselectionmanager.h
+++ b/src/kitemviews/kitemlistselectionmanager.h
@@ -54,6 +54,7 @@ public:
void setSelectedItems(const QSet<int>& items);
QSet<int> selectedItems() const;
+ bool isSelected(int index) const;
bool hasSelection() const;
void setSelected(int index, int count = 1, SelectionMode mode = Select);
diff --git a/src/kitemviews/kitemlistview.cpp b/src/kitemviews/kitemlistview.cpp
index d96cf14ce..ad4221394 100644
--- a/src/kitemviews/kitemlistview.cpp
+++ b/src/kitemviews/kitemlistview.cpp
@@ -1393,16 +1393,9 @@ void KItemListView::updateWidgetProperties(KItemListWidget* widget, int index)
const KItemListSelectionManager* selectionManager = m_controller->selectionManager();
widget->setCurrent(index == selectionManager->currentItem());
-
- if (selectionManager->hasSelection()) {
- const QSet<int> selectedItems = selectionManager->selectedItems();
- widget->setSelected(selectedItems.contains(index));
- } else {
- widget->setSelected(false);
- }
+ widget->setSelected(selectionManager->isSelected(index));
widget->setHovered(false);
-
widget->setIndex(index);
widget->setData(m_model->data(index));
}
diff --git a/src/tests/kitemlistselectionmanagertest.cpp b/src/tests/kitemlistselectionmanagertest.cpp
index 2a3601bb0..2fa6f677b 100644
--- a/src/tests/kitemlistselectionmanagertest.cpp
+++ b/src/tests/kitemlistselectionmanagertest.cpp
@@ -69,6 +69,8 @@ private slots:
void testChangeSelection();
private:
+ void verifySelectionChange(QSignalSpy& spy, const QSet<int>& currentSelection, const QSet<int>& previousSelection) const;
+
KItemListSelectionManager* m_selectionManager;
};
@@ -425,18 +427,8 @@ void KItemListSelectionManagerTest::testChangeSelection()
// Perform the initial selectiion
m_selectionManager->setSelectedItems(initialSelection);
- QCOMPARE(m_selectionManager->selectedItems(), initialSelection);
- if (initialSelection.isEmpty()) {
- QVERIFY(!m_selectionManager->hasSelection());
- QCOMPARE(spySelectionChanged.count(), 0);
- }
- else {
- QVERIFY(m_selectionManager->hasSelection());
- QCOMPARE(spySelectionChanged.count(), 1);
- QList<QVariant> arguments = spySelectionChanged.takeFirst();
- QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), initialSelection);
- QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), QSet<int>());
- }
+
+ verifySelectionChange(spySelectionChanged, initialSelection, QSet<int>());
// Perform an anchored selection.
// Note that current and anchor index are equal first because this is the case in typical uses of the
@@ -446,17 +438,8 @@ void KItemListSelectionManagerTest::testChangeSelection()
m_selectionManager->setCurrentItem(current);
QCOMPARE(m_selectionManager->m_anchorItem, anchor);
QCOMPARE(m_selectionManager->currentItem(), current);
- QCOMPARE(m_selectionManager->selectedItems(), expectedSelection);
- QCOMPARE(m_selectionManager->hasSelection(), !expectedSelection.isEmpty());
- if (expectedSelection == initialSelection) {
- QCOMPARE(spySelectionChanged.count(), 0);
- }
- else {
- QCOMPARE(spySelectionChanged.count(), 1);
- QList<QVariant> arguments = spySelectionChanged.takeFirst();
- QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), expectedSelection);
- QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), initialSelection);
- }
+
+ verifySelectionChange(spySelectionChanged, expectedSelection, initialSelection);
// Change the model by inserting or removing items.
switch (changeType) {
@@ -483,31 +466,37 @@ void KItemListSelectionManagerTest::testChangeSelection()
break;
}
- QCOMPARE(m_selectionManager->selectedItems(), finalSelection);
- QCOMPARE(m_selectionManager->hasSelection(), !finalSelection.isEmpty());
- if (finalSelection == expectedSelection) {
- QCOMPARE(spySelectionChanged.count(), 0);
- }
- else {
- QCOMPARE(spySelectionChanged.count(), 1);
- QList<QVariant> arguments = spySelectionChanged.takeFirst();
- QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), finalSelection);
- QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), expectedSelection);
- }
+ verifySelectionChange(spySelectionChanged, finalSelection, expectedSelection);
// Finally, clear the selection
m_selectionManager->clearSelection();
- QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
- QVERIFY(!m_selectionManager->hasSelection());
- if (finalSelection.isEmpty()) {
- // Selection has been empty already
- QCOMPARE(spySelectionChanged.count(), 0);
+
+ verifySelectionChange(spySelectionChanged, QSet<int>(), finalSelection);
+}
+
+void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy& spy,
+ const QSet<int>& currentSelection,
+ const QSet<int>& previousSelection) const
+{
+ QCOMPARE(m_selectionManager->selectedItems(), currentSelection);
+ QCOMPARE(m_selectionManager->hasSelection(), !currentSelection.isEmpty());
+ for (int index = 0; index < m_selectionManager->model()->count(); ++index) {
+ if (currentSelection.contains(index)) {
+ QVERIFY(m_selectionManager->isSelected(index));
+ }
+ else {
+ QVERIFY(!m_selectionManager->isSelected(index));
+ }
+ }
+
+ if (currentSelection == previousSelection) {
+ QCOMPARE(spy.count(), 0);
}
else {
- QCOMPARE(spySelectionChanged.count(), 1);
- QList<QVariant> arguments = spySelectionChanged.takeFirst();
- QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), QSet<int>());
- QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), finalSelection);
+ QCOMPARE(spy.count(), 1);
+ QList<QVariant> arguments = spy.takeFirst();
+ QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), currentSelection);
+ QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), previousSelection);
}
}