From: Peter Penz Date: Sat, 19 Nov 2011 06:34:34 +0000 (+0100) Subject: Improve dragging pixmap X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/cbab7fdc624dea5871ad9db14a77b7a2c8d47d41 Improve dragging pixmap When dragging multiple files, those files should be shown as part of the dragging pixmap like in Dolphin 1.7. BUG: 285031 FIXED-IN: 4.8.0 --- diff --git a/src/kitemviews/kfileitemlistview.cpp b/src/kitemviews/kfileitemlistview.cpp index 528140deb..fa1a23fac 100644 --- a/src/kitemviews/kfileitemlistview.cpp +++ b/src/kitemviews/kfileitemlistview.cpp @@ -23,12 +23,14 @@ #include "kfileitemmodelrolesupdater.h" #include "kfileitemlistwidget.h" #include "kfileitemmodel.h" +#include "kpixmapmodifier_p.h" #include #include #include #include +#include #include #include @@ -220,22 +222,83 @@ QHash KFileItemListView::visibleRolesSizes(const KItemRangeL QPixmap KFileItemListView::createDragPixmap(const QSet& indexes) const { - QPixmap pixmap; - - if (model()) { - QSetIterator it(indexes); - while (it.hasNext()) { - const int index = it.next(); - // TODO: Only one item is considered currently - pixmap = model()->data(index).value("iconPixmap").value(); - if (pixmap.isNull()) { - KIcon icon(model()->data(index).value("iconName").toString()); - pixmap = icon.pixmap(itemSize().toSize()); - } + if (!model()) { + return QPixmap(); + } + + const int itemCount = indexes.count(); + Q_ASSERT(itemCount > 0); + if (itemCount == 1) { + // Only one item is selected. Use the original icon without resizing. + const int index = indexes.values().first(); + QPixmap dragPixmap = model()->data(index).value("iconPixmap").value(); + if (dragPixmap.isNull()) { + KIcon icon(model()->data(index).value("iconName").toString()); + dragPixmap = icon.pixmap(itemSize().toSize()); + } + return dragPixmap; + } + + // 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; + int size; + if (itemCount > 16) { + xCount = 5; + size = KIconLoader::SizeSmall; + } else if (itemCount > 9) { + xCount = 4; + size = KIconLoader::SizeSmallMedium; + } else { + xCount = 3; + size = KIconLoader::SizeMedium; + } + + 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; + QSetIterator it(indexes); + while (it.hasNext()) { + const int index = it.next(); + + QPixmap pixmap = model()->data(index).value("iconPixmap").value(); + if (pixmap.isNull()) { + KIcon icon(model()->data(index).value("iconName").toString()); + pixmap = icon.pixmap(size, size); + } else { + KPixmapModifier::scale(pixmap, QSize(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 pixmap; + return dragPixmap; } void KFileItemListView::initializeItemListWidget(KItemListWidget* item)