┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/kitemviews
diff options
context:
space:
mode:
authorPeter Penz <[email protected]>2011-08-24 22:36:05 +0200
committerPeter Penz <[email protected]>2011-08-24 22:37:16 +0200
commitcae90c168ebf8e81f5bb8569f6a4d15156923196 (patch)
treee374bcb829b5e0b3353402861f42b3884e1915d6 /src/kitemviews
parentbf20b404c820f00b6d75f0ad123b93cb45eb7487 (diff)
Fix issues with the anchor selection
Don't change the selection if the anchor is invalid. This fixes the issue that items might get selected during changing a directory.
Diffstat (limited to 'src/kitemviews')
-rw-r--r--src/kitemviews/kitemlistselectionmanager.cpp44
-rw-r--r--src/kitemviews/kitemlistselectionmanager.h8
2 files changed, 34 insertions, 18 deletions
diff --git a/src/kitemviews/kitemlistselectionmanager.cpp b/src/kitemviews/kitemlistselectionmanager.cpp
index 9aaf22f58..764227004 100644
--- a/src/kitemviews/kitemlistselectionmanager.cpp
+++ b/src/kitemviews/kitemlistselectionmanager.cpp
@@ -81,11 +81,13 @@ QSet<int> KItemListSelectionManager::selectedItems() const
{
QSet<int> selectedItems = m_selectedItems;
- if (m_isAnchoredSelectionActive && (m_anchorItem != m_currentItem)) {
+ 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);
- for (int index = from; index <= to; index++) {
+ for (int index = from; index <= to; ++index) {
selectedItems.insert(index);
}
}
@@ -95,7 +97,7 @@ QSet<int> KItemListSelectionManager::selectedItems() const
bool KItemListSelectionManager::hasSelection() const
{
- return !m_selectedItems.isEmpty() || (m_isAnchoredSelectionActive && (m_anchorItem != m_currentItem));
+ return !m_selectedItems.isEmpty() || (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem);
}
void KItemListSelectionManager::setSelected(int index, int count, SelectionMode mode)
@@ -156,17 +158,22 @@ void KItemListSelectionManager::clearSelection()
void KItemListSelectionManager::beginAnchoredSelection(int anchor)
{
- m_isAnchoredSelectionActive = true;
- setAnchorItem(anchor);
+ if (anchor >= 0 && m_model && anchor < m_model->count()) {
+ m_isAnchoredSelectionActive = true;
+ setAnchorItem(anchor);
+ Q_ASSERT(m_anchorItem >= 0);
+ }
}
void KItemListSelectionManager::endAnchoredSelection()
{
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);
- for (int index = from; index <= to; index++) {
+ for (int index = from; index <= to; ++index) {
m_selectedItems.insert(index);
}
}
@@ -176,14 +183,18 @@ void KItemListSelectionManager::endAnchoredSelection()
void KItemListSelectionManager::setAnchorItem(int anchor)
{
- const int previous = m_anchorItem;
- if (m_model && anchor < m_model->count()) {
- m_anchorItem = anchor;
- } else {
- m_anchorItem = -1;
+ if (!m_isAnchoredSelectionActive) {
+ return;
+ }
+
+ if (anchor < 0 || (m_model && anchor >= m_model->count())) {
+ // Index is out of range
+ return;
}
- if (m_anchorItem != previous) {
+ if (m_anchorItem != anchor) {
+ const int previous = m_anchorItem;
+ m_anchorItem = anchor;
emit anchorChanged(m_anchorItem, previous);
}
}
@@ -198,11 +209,6 @@ bool KItemListSelectionManager::isAnchoredSelectionActive() const
return m_isAnchoredSelectionActive;
}
-void KItemListSelectionManager::setAnchoredSelectionActive(bool active)
-{
- m_isAnchoredSelectionActive = active;
-}
-
KItemModelBase* KItemListSelectionManager::model() const
{
return m_model;
@@ -319,6 +325,10 @@ void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
}
}
m_anchorItem = anchorItem;
+ if (m_anchorItem < 0) {
+ m_isAnchoredSelectionActive = false;
+ }
+
emit anchorChanged(m_anchorItem, previousAnchor);
}
diff --git a/src/kitemviews/kitemlistselectionmanager.h b/src/kitemviews/kitemlistselectionmanager.h
index dd4c3e4fc..f8c33bb79 100644
--- a/src/kitemviews/kitemlistselectionmanager.h
+++ b/src/kitemviews/kitemlistselectionmanager.h
@@ -61,11 +61,17 @@ public:
void beginAnchoredSelection(int anchor);
void endAnchoredSelection();
+
+ /**
+ * Sets the anchor to \a anchor and emits anchorChanged() if the
+ * anchor differs from the current anchor value. If no anchor selection is active (see
+ * KItemListSelectionManager::beginAnchoredSelection()) or the index
+ * is not within the available model items the anchor will not be modified.
+ */
void setAnchorItem(int anchor);
int anchorItem() const;
bool isAnchoredSelectionActive() const;
- void setAnchoredSelectionActive(bool active);
KItemModelBase* model() const;