diff options
| author | Frank Reininghaus <[email protected]> | 2011-08-10 15:21:26 +0200 |
|---|---|---|
| committer | Frank Reininghaus <[email protected]> | 2011-08-10 15:21:26 +0200 |
| commit | a03f7dcd22ae9e2a60f5998e23762dbf2d14b41e (patch) | |
| tree | 6fe4bfd70c42cabb4d2d4cf27641d2bebc879efb /src | |
| parent | 6d6fe76dcf46ae71e31d730981118cdcddb16f8b (diff) | |
Implement beginAnchoredSelection() and endAnchoredSelection().
Unit test included.
Diffstat (limited to 'src')
| -rw-r--r-- | src/kitemviews/kitemlistselectionmanager.cpp | 14 | ||||
| -rw-r--r-- | src/tests/kitemlistselectionmanagertest.cpp | 43 |
2 files changed, 56 insertions, 1 deletions
diff --git a/src/kitemviews/kitemlistselectionmanager.cpp b/src/kitemviews/kitemlistselectionmanager.cpp index 26ce1b2f7..18fa2f660 100644 --- a/src/kitemviews/kitemlistselectionmanager.cpp +++ b/src/kitemviews/kitemlistselectionmanager.cpp @@ -1,5 +1,6 @@ /*************************************************************************** * Copyright (C) 2011 by Peter Penz <[email protected]> * + * Copyright (C) 2011 by Frank Reininghaus <[email protected]> * * * * Based on the Itemviews NG project from Trolltech Labs: * * http://qt.gitorious.org/qt-labs/itemviews-ng * @@ -149,11 +150,22 @@ void KItemListSelectionManager::clearSelection() void KItemListSelectionManager::beginAnchoredSelection(int anchor) { - Q_UNUSED(anchor); + m_isAnchoredSelectionActive = true; + setAnchorItem(anchor); } void KItemListSelectionManager::endAnchoredSelection() { + if (m_isAnchoredSelectionActive) { + const int from = qMin(m_anchorItem, m_currentItem); + const int to = qMax(m_anchorItem, m_currentItem); + + for (int index = from; index <= to; index++) { + m_selectedItems.insert(index); + } + + m_isAnchoredSelectionActive = false; + } } void KItemListSelectionManager::setAnchorItem(int anchor) diff --git a/src/tests/kitemlistselectionmanagertest.cpp b/src/tests/kitemlistselectionmanagertest.cpp index 9063b7884..4c45fd1b8 100644 --- a/src/tests/kitemlistselectionmanagertest.cpp +++ b/src/tests/kitemlistselectionmanagertest.cpp @@ -1,5 +1,6 @@ /*************************************************************************** * Copyright (C) 2011 by Peter Penz <[email protected]> * + * Copyright (C) 2011 by Frank Reininghaus <[email protected]> * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -63,6 +64,7 @@ private slots: void testSetSelected(); void testItemsInserted(); void testItemsRemoved(); + void testAnchoredSelection(); private: KItemListSelectionManager* m_selectionManager; @@ -260,6 +262,47 @@ void KItemListSelectionManagerTest::testItemsRemoved() QVERIFY(selectedItems.contains(7)); } +void KItemListSelectionManagerTest::testAnchoredSelection() +{ + m_selectionManager->beginAnchoredSelection(5); + QVERIFY(m_selectionManager->isAnchoredSelectionActive()); + QCOMPARE(m_selectionManager->anchorItem(), 5); + + m_selectionManager->setCurrentItem(6); + QCOMPARE(m_selectionManager->currentItem(), 6); + QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6); + + m_selectionManager->setCurrentItem(4); + QCOMPARE(m_selectionManager->currentItem(), 4); + QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5); + + m_selectionManager->setCurrentItem(7); + QCOMPARE(m_selectionManager->currentItem(), 7); + QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7); + + // Ending the anchored selection should not change the selected items. + m_selectionManager->endAnchoredSelection(); + QVERIFY(!m_selectionManager->isAnchoredSelectionActive()); + QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7); + + // Start a new anchored selection that overlaps the previous one + m_selectionManager->beginAnchoredSelection(9); + QVERIFY(m_selectionManager->isAnchoredSelectionActive()); + QCOMPARE(m_selectionManager->anchorItem(), 9); + + m_selectionManager->setCurrentItem(6); + QCOMPARE(m_selectionManager->currentItem(), 6); + QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8 << 9); + + m_selectionManager->setCurrentItem(10); + QCOMPARE(m_selectionManager->currentItem(), 10); + QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10); + + m_selectionManager->endAnchoredSelection(); + QVERIFY(!m_selectionManager->isAnchoredSelectionActive()); + QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10); +} + QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI) #include "kitemlistselectionmanagertest.moc" |
