]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Forward port: Fix issue that dragging several directories shows a document as drag...
authorPeter Penz <peter.penz19@gmail.com>
Tue, 13 Jul 2010 17:06:11 +0000 (17:06 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Tue, 13 Jul 2010 17:06:11 +0000 (17:06 +0000)
CCBUG: 244305

svn path=/trunk/KDE/kdebase/apps/; revision=1149557

src/draganddrophelper.cpp
src/draganddrophelper.h

index a85e3cb6acfa9340c47147971b2f16bbb0cae91b..136b770a7154a6ebbb6f27144d119cbac2772bcc 100644 (file)
@@ -33,6 +33,7 @@
 #include <QAbstractProxyModel>
 #include <QtDBus>
 #include <QDrag>
+#include <QPainter>
 
 class DragAndDropHelperSingleton
 {
@@ -63,7 +64,7 @@ void DragAndDropHelper::startDrag(QAbstractItemView* itemView,
     }
     isDragging = true;
 
-    QModelIndexList indexes = itemView->selectionModel()->selectedIndexes();
+    const QModelIndexList indexes = itemView->selectionModel()->selectedIndexes();
     if (!indexes.isEmpty()) {
         QMimeData *data = itemView->model()->mimeData(indexes);
         if (data == 0) {
@@ -75,18 +76,7 @@ void DragAndDropHelper::startDrag(QAbstractItemView* itemView,
         }
 
         QDrag* drag = new QDrag(itemView);
-        QPixmap pixmap;
-        if (indexes.count() == 1) {
-            QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(itemView->model());
-            KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
-            const QModelIndex index = proxyModel->mapToSource(indexes.first());
-
-            const KFileItem item = dirModel->itemForIndex(index);
-            pixmap = item.pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium);
-        } else {
-            pixmap = KIcon("document-multiple").pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium);
-        }
-        drag->setPixmap(pixmap);
+        drag->setPixmap(createDragPixmap(itemView));
         drag->setMimeData(data);
 
         m_dragSource = itemView;
@@ -96,7 +86,7 @@ void DragAndDropHelper::startDrag(QAbstractItemView* itemView,
     isDragging = false;
 }
 
-bool DragAndDropHelper::isDragSource(QAbstractItemView* itemView)
+bool DragAndDropHelper::isDragSource(QAbstractItemView* itemView) const
 {
     return (m_dragSource != 0) && (m_dragSource == itemView);
 }
@@ -136,4 +126,64 @@ DragAndDropHelper::DragAndDropHelper()
 {
 }
 
+QPixmap DragAndDropHelper::createDragPixmap(QAbstractItemView* itemView) const
+{
+    const QModelIndexList selectedIndexes = itemView->selectionModel()->selectedIndexes();    
+    Q_ASSERT(!selectedIndexes.isEmpty());
+    
+    QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(itemView->model());
+    KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
+    
+    const int itemCount = selectedIndexes.count();
+    
+    // If more than one item is dragged, align the items inside a
+    // rectangular grid. The maximum grid size is limited to 5 x 5 items.
+    int xCount = 3;
+    int size = KIconLoader::SizeMedium;
+    if (itemCount > 16) {
+        xCount = 5;
+        size = KIconLoader::SizeSmall;
+    } else if (itemCount > 9) {
+        xCount = 4;
+        size = KIconLoader::SizeSmallMedium;
+    }
+    
+    if (itemCount < xCount) {
+        xCount = itemCount;
+    }
+    
+    int yCount = itemCount / xCount;
+    if (itemCount % xCount != 0) {
+        ++yCount;
+    }
+    if (yCount > xCount) {
+        yCount = xCount;
+    }
+
+    // Draw the selected items into the grid cells    
+    QPixmap dragPixmap(xCount * size + xCount - 1, yCount * size + yCount - 1);
+    dragPixmap.fill(Qt::transparent);
+    
+    QPainter painter(&dragPixmap);
+    int x = 0;
+    int y = 0;
+    foreach (const QModelIndex& selectedIndex, selectedIndexes) {
+        const QModelIndex index = proxyModel->mapToSource(selectedIndex);
+        const KFileItem item = dirModel->itemForIndex(index);
+        const QPixmap pixmap = item.pixmap(size, size);
+        painter.drawPixmap(x, y, pixmap);
+        
+        x += size + 1;
+        if (x >= dragPixmap.width()) {
+            x = 0;
+            y += size + 1;
+        }
+        if (y >= dragPixmap.height()) {
+            break;
+        }
+    }
+    
+    return dragPixmap;
+}
+
 #include "draganddrophelper.moc"
index f107350a711d7069da16fd3a84aaf4e2a26e17f1..3cb506c43a0dca90fc12b19ebb976432782f9a85 100644 (file)
 
 #include "libdolphin_export.h"
 #include <QObject>
+#include <QPixmap>
 
 class DolphinViewController;
 class KFileItem;
 class KUrl;
 class QDropEvent;
-class QMimeData;
 class QAbstractItemView;
+class QMimeData;
 class QWidget;
 
 /**
@@ -63,7 +64,7 @@ public:
      * Returns true if and only if the view \a itemView was the last view to 
      * be passed to startDrag(...), and that drag is still in progress.
      */
-    bool isDragSource(QAbstractItemView* itemView);
+    bool isDragSource(QAbstractItemView* itemView) const;
 
     /**
      * Handles the dropping of URLs to the given
@@ -84,6 +85,13 @@ signals:
 
 private:
     DragAndDropHelper();
+    
+    /**
+     * Creates a pixmap the contains the all icons of the items
+     * that are dragged.
+     */
+    QPixmap createDragPixmap(QAbstractItemView* itemView) const;
+    
     // The last view passed in startDrag(...), or 0 if
     // no startDrag(...) initiated drag is in progress.
     QAbstractItemView *m_dragSource;