Beispiel #1
0
/**
 * Converts a KItemSet to a QSet<int>.
 */
static QSet<int> KItemSet2QSet(const KItemSet& itemSet)
{
    QSet<int> result;
    for (int i : itemSet) {
        result.insert(i);
    }

    // Check that the conversion was successful.
    Q_ASSERT(itemSet.count() == result.count());

    for (int i : itemSet) {
        Q_ASSERT(result.contains(i));
    }

    foreach (int i, result) {
        Q_ASSERT(itemSet.contains(i));
    }
QPixmap KFileItemListView::createDragPixmap(const KItemSet& indexes) const
{
    if (!model()) {
        return QPixmap();
    }

    const int itemCount = indexes.count();
    Q_ASSERT(itemCount > 0);
    if (itemCount == 1) {
        return KItemListView::createDragPixmap(indexes);
    }

    // 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;
    }

    const qreal dpr = scene()->views()[0]->devicePixelRatio();
    // Draw the selected items into the grid cells.
    QPixmap dragPixmap(QSize(xCount * size + xCount, yCount * size + yCount) * dpr);
    dragPixmap.setDevicePixelRatio(dpr);
    dragPixmap.fill(Qt::transparent);

    QPainter painter(&dragPixmap);
    int x = 0;
    int y = 0;

    for (int index : indexes) {
        QPixmap pixmap = model()->data(index).value("iconPixmap").value<QPixmap>();
        if (pixmap.isNull()) {
            QIcon icon = QIcon::fromTheme(model()->data(index).value("iconName").toString());
            pixmap = icon.pixmap(size, size);
        } else {
            KPixmapModifier::scale(pixmap, QSize(size, size) * dpr);
        }

        painter.drawPixmap(x, y, pixmap);

        x += size + 1;
        if (x >= dragPixmap.width()) {
            x = 0;
            y += size + 1;
        }

        if (y >= dragPixmap.height()) {
            break;
        }
    }

    return dragPixmap;
}