]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kcategorizedview.cpp
Fixes the problem with the keyboard navigation when a selection with the right-click...
[dolphin.git] / src / kcategorizedview.cpp
index bb014b31e7819c219468c0c54882ae4af97356d0..72936113c5b3dbd999c3d3a5e05195e095862a3a 100644 (file)
@@ -24,7 +24,6 @@
 #include <math.h> // trunc on C99 compliant systems
 #include <kdefakes.h> // trunc for not C99 compliant systems
 
-#include <QApplication>
 #include <QPainter>
 #include <QScrollBar>
 #include <QPaintEvent>
 #include "kcategorydrawer.h"
 #include "kcategorizedsortfilterproxymodel.h"
 
+// By defining DOLPHIN_DRAGANDDROP the custom drag and drop implementation of
+// KCategorizedView is bypassed to have a consistent drag and drop look for all
+// views. Hopefully transparent pixmaps for drag objects will be supported in
+// Qt 4.4, so that this workaround can be skipped.
+#define DOLPHIN_DRAGANDDROP
+
 KCategorizedView::Private::Private(KCategorizedView *listView)
     : listView(listView)
     , categoryDrawer(0)
@@ -355,7 +360,7 @@ void KCategorizedView::Private::drawNewCategory(const QModelIndex &index,
     }
 
     QStyleOption optionCopy = option;
-    const QString category = proxyModel->data(index, KCategorizedSortFilterProxyModel::CategoryRole).toString();
+    const QString category = proxyModel->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
 
     optionCopy.state &= ~QStyle::State_Selected;
 
@@ -390,6 +395,8 @@ void KCategorizedView::Private::updateScrollbars()
     int lastItemBottom = cachedRectIndex(lastIndex).top() +
                          listView->spacing() + (listView->gridSize().isEmpty() ? 0 : listView->gridSize().height()) - listView->viewport()->height();
 
+    listView->horizontalScrollBar()->setRange(0, 0);
+
     listView->verticalScrollBar()->setSingleStep(listView->viewport()->height() / 10);
     listView->verticalScrollBar()->setPageStep(listView->viewport()->height());
     listView->verticalScrollBar()->setRange(0, lastItemBottom);
@@ -414,6 +421,36 @@ void KCategorizedView::Private::drawDraggedItems(QPainter *painter)
     }
 }
 
+void KCategorizedView::Private::layoutChanged(bool forceItemReload)
+{
+    if ((listView->viewMode() == KCategorizedView::IconMode) && proxyModel &&
+        categoryDrawer && proxyModel->isCategorizedModel() &&
+        ((forceItemReload ||
+          (modelSortRole != proxyModel->sortRole()) ||
+          (modelSortColumn != proxyModel->sortColumn()) ||
+          (modelSortOrder != proxyModel->sortOrder()) ||
+          (modelLastRowCount != proxyModel->rowCount()) ||
+          (modelCategorized != proxyModel->isCategorizedModel()))))
+    {
+        // Force the view to update all elements
+        listView->rowsInsertedArtifficial(QModelIndex(), 0, proxyModel->rowCount() - 1);
+
+        if (!forceItemReload)
+        {
+            modelSortRole = proxyModel->sortRole();
+            modelSortColumn = proxyModel->sortColumn();
+            modelSortOrder = proxyModel->sortOrder();
+            modelLastRowCount = proxyModel->rowCount();
+            modelCategorized = proxyModel->isCategorizedModel();
+        }
+    }
+    else if ((listView->viewMode() == KCategorizedView::IconMode) && proxyModel &&
+             categoryDrawer && proxyModel->isCategorizedModel())
+    {
+        updateScrollbars();
+    }
+}
+
 void KCategorizedView::Private::drawDraggedItems()
 {
     QRect rectToUpdate;
@@ -456,7 +493,7 @@ void KCategorizedView::setGridSize(const QSize &size)
 {
     QListView::setGridSize(size);
 
-    slotLayoutChanged();
+    d->layoutChanged(true);
 }
 
 void KCategorizedView::setModel(QAbstractItemModel *model)
@@ -476,10 +513,6 @@ void KCategorizedView::setModel(QAbstractItemModel *model)
 
     if (d->proxyModel)
     {
-        QObject::disconnect(d->proxyModel,
-                            SIGNAL(layoutAboutToBeChanged()),
-                            this, SLOT(slotLayoutAboutToBeChanged()));
-
         QObject::disconnect(d->proxyModel,
                             SIGNAL(layoutChanged()),
                             this, SLOT(slotLayoutChanged()));
@@ -487,6 +520,10 @@ void KCategorizedView::setModel(QAbstractItemModel *model)
         QObject::disconnect(d->proxyModel,
                             SIGNAL(dataChanged(QModelIndex,QModelIndex)),
                             this, SLOT(slotLayoutChanged()));
+
+        QObject::disconnect(d->proxyModel,
+                            SIGNAL(rowsRemoved(QModelIndex,int,int)),
+                            this, SLOT(rowsRemoved(QModelIndex,int,int)));
     }
 
     QListView::setModel(model);
@@ -498,10 +535,8 @@ void KCategorizedView::setModel(QAbstractItemModel *model)
         d->modelSortRole = d->proxyModel->sortRole();
         d->modelSortColumn = d->proxyModel->sortColumn();
         d->modelSortOrder = d->proxyModel->sortOrder();
-
-        QObject::connect(d->proxyModel,
-                         SIGNAL(layoutAboutToBeChanged()),
-                         this, SLOT(slotLayoutAboutToBeChanged()));
+        d->modelLastRowCount = d->proxyModel->rowCount();
+        d->modelCategorized = d->proxyModel->isCategorizedModel();
 
         QObject::connect(d->proxyModel,
                          SIGNAL(layoutChanged()),
@@ -510,6 +545,19 @@ void KCategorizedView::setModel(QAbstractItemModel *model)
         QObject::connect(d->proxyModel,
                          SIGNAL(dataChanged(QModelIndex,QModelIndex)),
                          this, SLOT(slotLayoutChanged()));
+
+        QObject::connect(d->proxyModel,
+                         SIGNAL(rowsRemoved(QModelIndex,int,int)),
+                         this, SLOT(rowsRemoved(QModelIndex,int,int)));
+
+        if (d->proxyModel->rowCount())
+        {
+            d->layoutChanged(true);
+        }
+    }
+    else
+    {
+        d->modelCategorized = false;
     }
 }
 
@@ -551,10 +599,6 @@ void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer)
 
     if (!categoryDrawer && d->proxyModel)
     {
-        QObject::disconnect(d->proxyModel,
-                            SIGNAL(layoutAboutToBeChanged()),
-                            this, SLOT(slotLayoutAboutToBeChanged()));
-
         QObject::disconnect(d->proxyModel,
                             SIGNAL(layoutChanged()),
                             this, SLOT(slotLayoutChanged()));
@@ -562,13 +606,13 @@ void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer)
         QObject::disconnect(d->proxyModel,
                             SIGNAL(dataChanged(QModelIndex,QModelIndex)),
                             this, SLOT(slotLayoutChanged()));
+
+        QObject::disconnect(d->proxyModel,
+                            SIGNAL(rowsRemoved(QModelIndex,int,int)),
+                            this, SLOT(rowsRemoved(QModelIndex,int,int)));
     }
     else if (categoryDrawer && d->proxyModel)
     {
-        QObject::connect(d->proxyModel,
-                         SIGNAL(layoutAboutToBeChanged()),
-                         this, SLOT(slotLayoutAboutToBeChanged()));
-
         QObject::connect(d->proxyModel,
                          SIGNAL(layoutChanged()),
                          this, SLOT(slotLayoutChanged()));
@@ -576,6 +620,10 @@ void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer)
         QObject::connect(d->proxyModel,
                          SIGNAL(dataChanged(QModelIndex,QModelIndex)),
                          this, SLOT(slotLayoutChanged()));
+
+        QObject::connect(d->proxyModel,
+                         SIGNAL(rowsRemoved(QModelIndex,int,int)),
+                         this, SLOT(rowsRemoved(QModelIndex,int,int)));
     }
 
     d->categoryDrawer = categoryDrawer;
@@ -584,7 +632,10 @@ void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer)
     {
         if (d->proxyModel)
         {
-            rowsInserted(QModelIndex(), 0, d->proxyModel->rowCount() - 1);
+            if (d->proxyModel->rowCount())
+            {
+                d->layoutChanged(true);
+            }
         }
     }
     else
@@ -702,6 +753,7 @@ void KCategorizedView::paintEvent(QPaintEvent *event)
 
     // Redraw categories
     QStyleOptionViewItem otherOption;
+    bool intersectedInThePast = false;
     foreach (const QString &category, d->categories)
     {
         otherOption = option;
@@ -710,11 +762,18 @@ void KCategorizedView::paintEvent(QPaintEvent *event)
 
         if (otherOption.rect.intersects(area))
         {
+            intersectedInThePast = true;
+
             QModelIndex indexToDraw = d->proxyModel->index(d->categoriesIndexes[category][0].row(), d->proxyModel->sortColumn());
 
             d->drawNewCategory(indexToDraw,
                                d->proxyModel->sortRole(), otherOption, &painter);
         }
+        else if (intersectedInThePast)
+        {
+            break; // the visible area has been finished, we don't need to keep asking, the rest won't intersect
+                   // this is doable because we know that categories are correctly ordered on the list
+        }
     }
 
     if (d->mouseButtonPressed && !d->isDragging)
@@ -811,12 +870,29 @@ void KCategorizedView::setSelection(const QRect &rect,
         return;
     }
 
+    int viewportWidth = viewport()->width() - spacing();
+    int itemWidth;
+
+    if (gridSize().isEmpty())
+    {
+        itemWidth = d->biggestItemSize.width();
+    }
+    else
+    {
+        itemWidth = gridSize().width();
+    }
+
+    int itemWidthPlusSeparation = spacing() + itemWidth;
+    int elementsPerRow = viewportWidth / itemWidthPlusSeparation;
+
     QItemSelection selection;
 
     if (!d->mouseButtonPressed)
     {
         selection = QItemSelection(dirtyIndexes[0], dirtyIndexes[0]);
         d->currentViewIndex = dirtyIndexes[0];
+        selectionModel()->setCurrentIndex(d->currentViewIndex, flags);
+        d->forcedSelectionPosition = d->elementsInfo[d->currentViewIndex.row()].relativeOffsetToCategory % elementsPerRow;
     }
     else
     {
@@ -838,6 +914,12 @@ void KCategorizedView::setSelection(const QRect &rect,
 
         if (last.isValid())
             selection << QItemSelectionRange(first, last);
+
+        if (first == last)
+        {
+            selectionModel()->setCurrentIndex(first, QItemSelectionModel::SelectCurrent);
+            d->forcedSelectionPosition = d->elementsInfo[first.row()].relativeOffsetToCategory % elementsPerRow;
+        }
     }
 
     if (d->lastSelection.count())
@@ -1001,7 +1083,9 @@ void KCategorizedView::startDrag(Qt::DropActions supportedActions)
     //        ARGB window so it is no transparent. Use QAbstractItemView when
     //        this is fixed on Qt.
     // QAbstractItemView::startDrag(supportedActions);
+#if !defined(DOLPHIN_DRAGANDDROP)
     QListView::startDrag(supportedActions);
+#endif
 
     d->isDragging = false;
     d->mouseButtonPressed = false;
@@ -1027,7 +1111,11 @@ void KCategorizedView::dragMoveEvent(QDragMoveEvent *event)
     if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
         !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
     {
+#if defined(DOLPHIN_DRAGANDDROP)
+        QAbstractItemView::dragMoveEvent(event);
+#else
         QListView::dragMoveEvent(event);
+#endif
         return;
     }
 
@@ -1038,7 +1126,20 @@ void KCategorizedView::dragLeaveEvent(QDragLeaveEvent *event)
 {
     d->dragLeftViewport = true;
 
+#if defined(DOLPHIN_DRAGANDDROP)
+    QAbstractItemView::dragLeaveEvent(event);
+#else
     QListView::dragLeaveEvent(event);
+#endif
+}
+
+void KCategorizedView::dropEvent(QDropEvent *event)
+{
+#if defined(DOLPHIN_DRAGANDDROP)
+    QAbstractItemView::dropEvent(event);
+#else
+    QListView::dropEvent(event);
+#endif
 }
 
 QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction,
@@ -1225,8 +1326,8 @@ void KCategorizedView::rowsInserted(const QModelIndex &parent,
 }
 
 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent,
-                                        int start,
-                                        int end)
+                                               int start,
+                                               int end)
 {
     Q_UNUSED(parent);
 
@@ -1249,26 +1350,25 @@ void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent,
         return;
     }
 
-    // Add all elements mapped to the source model
-    for (int k = 0; k < d->proxyModel->rowCount(); k++)
-    {
-        d->biggestItemSize = QSize(qMax(sizeHintForIndex(d->proxyModel->index(k, 0)).width(),
-                                        d->biggestItemSize.width()),
-                                   qMax(sizeHintForIndex(d->proxyModel->index(k, 0)).height(),
-                                        d->biggestItemSize.height()));
-
-        d->modelIndexList << d->proxyModel->index(k, d->proxyModel->sortColumn());
-    }
-
-    // Explore categories
-    QString prevCategory = d->proxyModel->data(d->modelIndexList[0], KCategorizedSortFilterProxyModel::CategoryRole).toString();
+    // Add all elements mapped to the source model and explore categories
+    QString prevCategory = d->proxyModel->data(d->proxyModel->index(0, d->proxyModel->sortColumn()), KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
     QString lastCategory = prevCategory;
     QModelIndexList modelIndexList;
     struct Private::ElementInfo elementInfo;
     int offset = -1;
-    foreach (const QModelIndex &index, d->modelIndexList)
+    for (int k = 0; k < d->proxyModel->rowCount(); ++k)
     {
-        lastCategory = d->proxyModel->data(index, KCategorizedSortFilterProxyModel::CategoryRole).toString();
+        QModelIndex index = d->proxyModel->index(k, d->proxyModel->sortColumn());
+        QModelIndex indexSize = d->proxyModel->index(k, 0);
+
+        d->biggestItemSize = QSize(qMax(sizeHintForIndex(indexSize).width(),
+                                        d->biggestItemSize.width()),
+                                   qMax(sizeHintForIndex(indexSize).height(),
+                                        d->biggestItemSize.height()));
+
+        d->modelIndexList << index;
+
+        lastCategory = d->proxyModel->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
 
         elementInfo.category = lastCategory;
 
@@ -1299,14 +1399,14 @@ void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent,
 }
 
 void KCategorizedView::rowsRemoved(const QModelIndex &parent,
-                            int start,
-                            int end)
+                                   int start,
+                                   int end)
 {
     if ((viewMode() == KCategorizedView::IconMode) && d->proxyModel &&
         d->categoryDrawer && d->proxyModel->isCategorizedModel())
     {
         // Force the view to update all elements
-        rowsInsertedArtifficial(parent, start, end);
+        rowsInsertedArtifficial(QModelIndex(), 0, d->proxyModel->rowCount() - 1);
     }
 }
 
@@ -1324,32 +1424,9 @@ void KCategorizedView::updateGeometries()
     QAbstractItemView::updateGeometries();
 }
 
-void KCategorizedView::slotLayoutAboutToBeChanged()
-{
-    if ((viewMode() == KCategorizedView::IconMode) && d->proxyModel &&
-        d->categoryDrawer && d->proxyModel->isCategorizedModel())
-    {
-        d->modelSortRole = d->proxyModel->sortRole();
-        d->modelSortColumn = d->proxyModel->sortColumn();
-        d->modelSortOrder = d->proxyModel->sortOrder();
-    }
-}
-
 void KCategorizedView::slotLayoutChanged()
 {
-    if ((viewMode() == KCategorizedView::IconMode) && d->proxyModel &&
-        d->categoryDrawer && d->proxyModel->isCategorizedModel() &&
-        ((d->modelSortRole != d->proxyModel->sortRole()) ||
-         (d->modelSortColumn != d->proxyModel->sortColumn()) ||
-         (d->modelSortOrder != d->proxyModel->sortOrder())))
-    {
-        // Force the view to update all elements
-        rowsInsertedArtifficial(QModelIndex(), 0, d->proxyModel->rowCount() - 1);
-
-        d->modelSortRole = d->proxyModel->sortRole();
-        d->modelSortColumn = d->proxyModel->sortColumn();
-        d->modelSortOrder = d->proxyModel->sortOrder();
-    }
+    d->layoutChanged();
 }
 
 #include "kcategorizedview.moc"