- if (updatePixmap) {
- m_pixmap = values["iconPixmap"].value<QPixmap>();
- if (m_pixmap.isNull()) {
- // Use the icon that fits to the MIME-type
- QString iconName = values["iconName"].toString();
- if (iconName.isEmpty()) {
- // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
- // use a generic icon as fallback
- iconName = QLatin1String("unknown");
- }
- m_pixmap = pixmapForIcon(iconName, iconHeight);
- m_originalPixmapSize = m_pixmap.size();
- } else if (m_pixmap.size() != QSize(iconHeight, iconHeight)) {
- // A custom pixmap has been applied. Assure that the pixmap
- // is scaled to the available size.
- const bool scale = m_pixmap.width() > iconHeight || m_pixmap.height() > iconHeight ||
- (m_pixmap.width() < iconHeight && m_pixmap.height() < iconHeight);
- if (scale) {
- KPixmapModifier::scale(m_pixmap, QSize(iconHeight, iconHeight));
- }
- m_originalPixmapSize = m_pixmap.size();
-
- // To simplify the handling of scaling the original pixmap
- // will be embedded into a square pixmap.
- QPixmap squarePixmap(iconHeight, iconHeight);
- squarePixmap.fill(Qt::transparent);
-
- QPainter painter(&squarePixmap);
- int x, y;
- if (iconOnTop) {
- x = (iconHeight - m_pixmap.width()) / 2; // Center horizontally
- y = iconHeight - m_pixmap.height(); // Align on bottom
- painter.drawPixmap(x, y, m_pixmap);
- } else {
- x = iconHeight - m_pixmap.width(); // Align right
- y = (iconHeight - m_pixmap.height()) / 2; // Center vertically
- painter.drawPixmap(x, y, m_pixmap);
- }
-
- m_pixmap = squarePixmap;
- } else {
- m_originalPixmapSize = m_pixmap.size();
- }
-
- const QStringList overlays = values["iconOverlays"].toStringList();
-
- // Strangely KFileItem::overlays() returns empty string-values, so
- // we need to check first whether an overlay must be drawn at all.
- // It is more efficient to do it here, as KIconLoader::drawOverlays()
- // assumes that an overlay will be drawn and has some additional
- // setup time.
- foreach (const QString& overlay, overlays) {
- if (!overlay.isEmpty()) {
- // There is at least one overlay, draw all overlays above m_pixmap
- // and cancel the check
- KIconLoader::global()->drawOverlays(overlays, m_pixmap, KIconLoader::Desktop);
- break;
- }
- }
-
- if (m_isCut) {
- applyCutEffect(m_pixmap);
- }
-
- if (m_isHidden) {
- applyHiddenEffect(m_pixmap);
- }
-
- Q_ASSERT(m_pixmap.height() == iconHeight);
- }
- if (!m_overlay.isNull()) {
- QPainter painter(&m_pixmap);
- painter.drawPixmap(0, m_pixmap.height() - m_overlay.height(), m_overlay);
- }
-
- m_scaledPixmapSize = QSize(scaledIconHeight, scaledIconHeight);
-
- if (iconOnTop) {
- m_pixmapPos.setX((widgetSize.width() - m_scaledPixmapSize.width()) / 2);
- } else {
- m_pixmapPos.setX(m_textPos[Name].x() - 2 * option.margin - scaledIconHeight);
- }
- m_pixmapPos.setY(option.margin);
-
- // Center the hover rectangle horizontally and align it on bottom
- qreal hoverWidth = m_originalPixmapSize.width();
- qreal hoverHeight = m_originalPixmapSize.height();
- if (scaledIconHeight != m_pixmap.height()) {
- const qreal scaleFactor = qreal(scaledIconHeight) / qreal(m_pixmap.height());
- hoverWidth *= scaleFactor;
- hoverHeight *= scaleFactor;
- }
- const qreal hoverX = m_pixmapPos.x() + (m_scaledPixmapSize.width() - hoverWidth) / 2.0;
- const qreal hoverY = m_pixmapPos.y() + m_scaledPixmapSize.height() - hoverHeight;
- m_iconRect = QRectF(hoverX, hoverY, hoverWidth, hoverHeight);
- const qreal margin = option.margin;
- m_iconRect.adjust(-margin, -margin, margin, margin);
-
- // Prepare the pixmap that is used when the item gets hovered
- if (isHovered()) {
- m_hoverPixmap = m_pixmap;
- KIconEffect* effect = KIconLoader::global()->iconEffect();
- // In the KIconLoader terminology, active = hover.
- if (effect->hasEffect(KIconLoader::Desktop, KIconLoader::ActiveState)) {
- m_hoverPixmap = effect->apply(m_pixmap, KIconLoader::Desktop, KIconLoader::ActiveState);
- } else {
- m_hoverPixmap = m_pixmap;
- }
- } else if (hoverOpacity() <= 0.0) {
- // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
- m_hoverPixmap = QPixmap();
- }