From: Peter Penz Date: Tue, 13 Jul 2010 17:06:11 +0000 (+0000) Subject: Forward port: Fix issue that dragging several directories shows a document as drag... X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/5c1d647a716efa21ff72538c711a8c85717bf615 Forward port: Fix issue that dragging several directories shows a document as drag item. Instead try to show the icons of the dragged items. CCBUG: 244305 svn path=/trunk/KDE/kdebase/apps/; revision=1149557 --- diff --git a/src/draganddrophelper.cpp b/src/draganddrophelper.cpp index a85e3cb6a..136b770a7 100644 --- a/src/draganddrophelper.cpp +++ b/src/draganddrophelper.cpp @@ -33,6 +33,7 @@ #include #include #include +#include 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(itemView->model()); - KDirModel* dirModel = static_cast(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(itemView->model()); + KDirModel* dirModel = static_cast(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" diff --git a/src/draganddrophelper.h b/src/draganddrophelper.h index f107350a7..3cb506c43 100644 --- a/src/draganddrophelper.h +++ b/src/draganddrophelper.h @@ -23,13 +23,14 @@ #include "libdolphin_export.h" #include +#include 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;