]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Improve selection speed. I think we can still improve this a little bit, but I think...
authorRafael Fernández López <ereslibre@kde.org>
Sat, 23 Jun 2007 15:39:21 +0000 (15:39 +0000)
committerRafael Fernández López <ereslibre@kde.org>
Sat, 23 Jun 2007 15:39:21 +0000 (15:39 +0000)
svn path=/trunk/KDE/kdebase/apps/; revision=679308

src/klistview.cpp
src/klistview_p.h

index 0c65b9141f4409f471dcfa89f9f8ba9687d6b36c..4fe0f571f4b18fe196a7d5d6d8bc3796c6fc2639 100644 (file)
@@ -529,6 +529,7 @@ void KListView::reset()
     d->elementDictionary.clear();
     d->categoriesIndexes.clear();
     d->categoriesPosition.clear();
+    d->isIndexSelected.clear();       // selection cache
     d->categories.clear();
     d->intersectedIndexes.clear();
     d->sourceModelIndexList.clear();
@@ -670,39 +671,83 @@ void KListView::setSelection(const QRect &rect,
         return;
     }
 
-    // FIXME: I have to rethink and rewrite this method (ereslibre)
+    if (flags & QItemSelectionModel::Clear)
+    {
+        selectionModel()->clear();
+        d->isIndexSelected.clear();
+        d->isTemporarySelected.clear();
+    }
 
+    QItemSelection selection;
     QModelIndexList dirtyIndexes = d->intersectionSet(rect);
     foreach (const QModelIndex &index, dirtyIndexes)
     {
         if (!d->mouseButtonPressed && rect.intersects(visualRect(index)))
         {
-            selectionModel()->select(index, flags);
+            if (d->isIndexSelected.contains(index))
+            {
+                if (!d->isIndexSelected[index])
+                    selection.select(index, index);
+
+                d->isIndexSelected[index] = true;
+            }
+            else
+            {
+                d->isIndexSelected.insert(index, true);
+                selection.select(index, index);
+            }
         }
-        else
+        else if (d->mouseButtonPressed)           // selection cache
         {
-            selectionModel()->select(index, QItemSelectionModel::Select);
+            if (!d->isIndexSelected.contains(index) ||
+                (d->isIndexSelected.contains(index) && !d->isIndexSelected[index]))
+            {
+                if (d->isTemporarySelected.contains(index))
+                {
+                    d->isTemporarySelected[index] = true;
+                }
+                else
+                {
+                    d->isTemporarySelected.insert(index, true);
+                }
+            }
+
+            if (d->isIndexSelected.contains(index))
+            {
+                if (!d->isIndexSelected[index])
+                    selection.select(index, index);
 
-            if (d->mouseButtonPressed)
-                d->tempSelected.append(index);
+                d->isIndexSelected[index] = true;
+            }
+            else
+            {
+                d->isIndexSelected.insert(index, true);
+                selection.select(index, index);
+            }
         }
     }
 
-    if (d->mouseButtonPressed)
+    QItemSelection deselect;
+
+    foreach (const QModelIndex &index, d->isIndexSelected.keys())
     {
-        foreach (const QModelIndex &index, selectionModel()->selectedIndexes())
+        if (!rect.intersects(visualRect(index)))
         {
-            if (!rect.intersects(visualRect(index)))
+            if (d->isTemporarySelected.contains(index) &&
+                d->isTemporarySelected[index])
             {
-                selectionModel()->select(index, QItemSelectionModel::Deselect);
-
-                if (d->mouseButtonPressed)
-                {
-                    d->tempSelected.removeAll(index);
-                }
+                deselect.select(index, index);
+                d->isTemporarySelected[index] = false;
+                d->isIndexSelected[index] = false;
             }
         }
     }
+
+    if (selection.count())
+        selectionModel()->select(selection, QItemSelectionModel::Select);
+
+    if (deselect.count())
+        selectionModel()->select(deselect, QItemSelectionModel::Deselect);
 }
 
 void KListView::mouseMoveEvent(QMouseEvent *event)
@@ -726,8 +771,6 @@ void KListView::mousePressEvent(QMouseEvent *event)
 {
     QListView::mousePressEvent(event);
 
-    d->tempSelected.clear();
-
     if ((viewMode() == KListView::ListMode) || !d->proxyModel ||
         !d->itemCategorizer)
     {
@@ -764,7 +807,7 @@ void KListView::mouseReleaseEvent(QMouseEvent *event)
 
     event->accept();
 
-    // FIXME: I have to rethink and rewrite this method (ereslibre)
+    d->isTemporarySelected.clear();     // selection cache
 
     QPoint initialPressPosition = viewport()->mapFromGlobal(QCursor::pos());
     initialPressPosition.setY(initialPressPosition.y() + verticalOffset());
@@ -772,28 +815,23 @@ void KListView::mouseReleaseEvent(QMouseEvent *event)
 
     if (initialPressPosition == d->initialPressPosition)
     {
+        QItemSelection selection;
         foreach(const QString &category, d->categories)
         {
             if (d->categoryVisualRect(category).contains(event->pos()))
             {
                 QModelIndex index;
-                QItemSelectionModel::SelectionFlag flag;
                 foreach (const QModelIndex &mappedIndex,
                          d->categoriesIndexes[category])
                 {
                     index = d->proxyModel->mapFromSource(mappedIndex);
 
-                    if (selectionModel()->selectedIndexes().contains(index))
-                    {
-                        flag = QItemSelectionModel::Deselect;
-                    }
-                    else
-                    {
-                        flag = QItemSelectionModel::Select;
-                    }
-
-                    selectionModel()->select(index, flag);
+                    selection.select(index, index);
                 }
+
+                selectionModel()->select(selection, QItemSelectionModel::Toggle);
+
+                break;
             }
         }
     }
@@ -849,6 +887,7 @@ void KListView::rowsInsertedArtifficial(const QModelIndex &parent,
     d->elementDictionary.clear();
     d->categoriesIndexes.clear();
     d->categoriesPosition.clear();
+    d->isIndexSelected.clear();       // selection cache
     d->categories.clear();
     d->intersectedIndexes.clear();
     d->sourceModelIndexList.clear();
index 58ac3486e280eb003197b7efe0743f7b017153c5..4a0a53f9b2a6c64461f744e21432ab06bbfab017 100644 (file)
@@ -127,7 +127,8 @@ public:
     QHash<QString, QRect> categoriesPosition;
     QStringList categories;
     QModelIndexList intersectedIndexes;
-    QModelIndexList tempSelected;
+    QHash<QModelIndex, bool> isIndexSelected;            // selection cache
+    QHash<QModelIndex, bool> isTemporarySelected;        // selection cache
 
     // Attributes for speed reasons
     KSortFilterProxyModel *proxyModel;