]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kcategorizedview.cpp
Fixed issue that the "Show 'Delete' command in context menu" setting is not remembere...
[dolphin.git] / src / kcategorizedview.cpp
index 7fe80605552a23b7a4c9336326c7ec42bf55c516..20fbdc7577e6ad3ae603eb7488a71a62e5365557 100644 (file)
@@ -414,6 +414,30 @@ void KCategorizedView::Private::drawDraggedItems(QPainter *painter)
     }
 }
 
+void KCategorizedView::Private::layoutChanged(bool forceItemReload)
+{
+    if ((listView->viewMode() == KCategorizedView::IconMode) && proxyModel &&
+        categoryDrawer && proxyModel->isCategorizedModel() &&
+        (((modelSortRole != proxyModel->sortRole()) ||
+          (modelSortColumn != proxyModel->sortColumn()) ||
+          (modelSortOrder != proxyModel->sortOrder()) ||
+          (modelCategorized != proxyModel->isCategorizedModel())) || forceItemReload))
+    {
+        // Force the view to update all elements
+        listView->rowsInsertedArtifficial(QModelIndex(), 0, proxyModel->rowCount() - 1);
+
+        modelSortRole = proxyModel->sortRole();
+        modelSortColumn = proxyModel->sortColumn();
+        modelCategorized = proxyModel->isCategorizedModel();
+        modelSortOrder = proxyModel->sortOrder();
+    }
+    else if ((listView->viewMode() == KCategorizedView::IconMode) && proxyModel &&
+             categoryDrawer && proxyModel->isCategorizedModel())
+    {
+        updateScrollbars();
+    }
+}
+
 void KCategorizedView::Private::drawDraggedItems()
 {
     QRect rectToUpdate;
@@ -456,7 +480,7 @@ void KCategorizedView::setGridSize(const QSize &size)
 {
     QListView::setGridSize(size);
 
-    slotLayoutChanged();
+    d->layoutChanged(true);
 }
 
 void KCategorizedView::setModel(QAbstractItemModel *model)
@@ -483,6 +507,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);
@@ -491,6 +519,11 @@ void KCategorizedView::setModel(QAbstractItemModel *model)
 
     if (d->proxyModel)
     {
+        d->modelSortRole = d->proxyModel->sortRole();
+        d->modelSortColumn = d->proxyModel->sortColumn();
+        d->modelCategorized = true;
+        d->modelSortOrder = d->proxyModel->sortOrder();
+
         QObject::connect(d->proxyModel,
                          SIGNAL(layoutChanged()),
                          this, SLOT(slotLayoutChanged()));
@@ -498,6 +531,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())
+        {
+            rowsInsertedArtifficial(QModelIndex(), 0, d->proxyModel->rowCount() - 1);
+        }
+    }
+    else
+    {
+        d->modelCategorized = false;
     }
 }
 
@@ -546,6 +592,10 @@ 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)
     {
@@ -556,6 +606,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;
@@ -564,7 +618,7 @@ void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer)
     {
         if (d->proxyModel)
         {
-            rowsInserted(QModelIndex(), 0, d->proxyModel->rowCount() - 1);
+            rowsInsertedArtifficial(QModelIndex(), 0, d->proxyModel->rowCount() - 1);
         }
     }
     else
@@ -980,8 +1034,8 @@ void KCategorizedView::startDrag(Qt::DropActions supportedActions)
     //        pixmap of selected icons to the dragging cursor, but it sets a non
     //        ARGB window so it is no transparent. Use QAbstractItemView when
     //        this is fixed on Qt.
-    //QListView::startDrag(supportedActions);
-    QAbstractItemView::startDrag(supportedActions);
+    // QAbstractItemView::startDrag(supportedActions);
+    QListView::startDrag(supportedActions);
 
     d->isDragging = false;
     d->mouseButtonPressed = false;
@@ -1022,7 +1076,7 @@ void KCategorizedView::dragLeaveEvent(QDragLeaveEvent *event)
 }
 
 QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction,
-                                  Qt::KeyboardModifiers modifiers)
+                                         Qt::KeyboardModifiers modifiers)
 {
     if ((viewMode() != KCategorizedView::IconMode) ||
          !d->proxyModel ||
@@ -1133,6 +1187,16 @@ QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction,
         }
 
         case QAbstractItemView::MoveLeft:
+            if (layoutDirection() == Qt::RightToLeft)
+            {
+                d->forcedSelectionPosition = d->elementsInfo[current.row() + 1].relativeOffsetToCategory % elementsPerRow;
+
+                if (d->forcedSelectionPosition < 0)
+                    d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow;
+
+                return d->proxyModel->index(current.row() + 1, 0);
+            }
+
             d->forcedSelectionPosition = d->elementsInfo[current.row() - 1].relativeOffsetToCategory % elementsPerRow;
 
             if (d->forcedSelectionPosition < 0)
@@ -1141,6 +1205,16 @@ QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction,
             return d->proxyModel->index(current.row() - 1, 0);
 
         case QAbstractItemView::MoveRight:
+            if (layoutDirection() == Qt::RightToLeft)
+            {
+                d->forcedSelectionPosition = d->elementsInfo[current.row() - 1].relativeOffsetToCategory % elementsPerRow;
+
+                if (d->forcedSelectionPosition < 0)
+                    d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow;
+
+                return d->proxyModel->index(current.row() - 1, 0);
+            }
+
             d->forcedSelectionPosition = d->elementsInfo[current.row() + 1].relativeOffsetToCategory % elementsPerRow;
 
             if (d->forcedSelectionPosition < 0)
@@ -1156,8 +1230,8 @@ QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction,
 }
 
 void KCategorizedView::rowsInserted(const QModelIndex &parent,
-                             int start,
-                             int end)
+                                    int start,
+                                    int end)
 {
     QListView::rowsInserted(parent, start, end);
 
@@ -1185,8 +1259,8 @@ void KCategorizedView::rowsInserted(const QModelIndex &parent,
 }
 
 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent,
-                                        int start,
-                                        int end)
+                                               int start,
+                                               int end)
 {
     Q_UNUSED(parent);
 
@@ -1209,25 +1283,24 @@ 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::CategoryRole).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)
     {
+        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::CategoryRole).toString();
 
         elementInfo.category = lastCategory;
@@ -1259,14 +1332,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);
     }
 }
 
@@ -1286,12 +1359,7 @@ void KCategorizedView::updateGeometries()
 
 void KCategorizedView::slotLayoutChanged()
 {
-    if ((viewMode() == KCategorizedView::IconMode) && d->proxyModel &&
-        d->categoryDrawer && d->proxyModel->isCategorizedModel())
-    {
-        // Force the view to update all elements
-        rowsInsertedArtifficial(QModelIndex(), 0, d->proxyModel->rowCount() - 1);
-    }
+    d->layoutChanged();
 }
 
 #include "kcategorizedview.moc"