- // Precondition: Requires already updated m_textPos values to calculate
- // the remaining height when the alignment is vertical.
-
- const QSizeF widgetSize = size();
- const bool iconOnTop = (m_layout == IconsLayout);
- const KItemListStyleOption& option = styleOption();
- const qreal margin = option.margin;
-
- const int maxIconWidth = iconOnTop ? widgetSize.width() - 2 * margin : option.iconSize;
- const int maxIconHeight = option.iconSize;
-
- const QHash<QByteArray, QVariant> values = data();
-
- bool updatePixmap = (m_pixmap.width() != maxIconWidth || m_pixmap.height() != maxIconHeight);
- if (!updatePixmap && m_dirtyContent) {
- updatePixmap = m_dirtyContentRoles.isEmpty()
- || m_dirtyContentRoles.contains("iconPixmap")
- || m_dirtyContentRoles.contains("iconName")
- || m_dirtyContentRoles.contains("iconOverlays");
- }
-
- 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, maxIconHeight);
- } else if (m_pixmap.width() != maxIconWidth || m_pixmap.height() != maxIconHeight) {
- // A custom pixmap has been applied. Assure that the pixmap
- // is scaled to the maximum available size.
- KPixmapModifier::scale(m_pixmap, QSize(maxIconWidth, maxIconHeight));
- }
-
- 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);
- }
- }
-
- if (!m_overlay.isNull()) {
- QPainter painter(&m_pixmap);
- painter.drawPixmap(0, m_pixmap.height() - m_overlay.height(), m_overlay);
- }
-
- int scaledIconSize = 0;
- if (iconOnTop) {
- scaledIconSize = static_cast<int>(m_textPos[Name].y() - 2 * margin);
- } else {
- const int textRowsCount = (m_layout == CompactLayout) ? visibleRoles().count() : 1;
- const qreal requiredTextHeight = textRowsCount * option.fontMetrics.height();
- scaledIconSize = (requiredTextHeight < maxIconHeight) ?
- widgetSize.height() - 2 * margin : maxIconHeight;
- }
-
- const int maxScaledIconWidth = iconOnTop ? widgetSize.width() - 2 * margin : scaledIconSize;
- const int maxScaledIconHeight = scaledIconSize;
-
- m_scaledPixmapSize = m_pixmap.size();
- m_scaledPixmapSize.scale(maxScaledIconWidth, maxScaledIconHeight, Qt::KeepAspectRatio);
-
- if (iconOnTop) {
- // Center horizontally and align on bottom within the icon-area
- m_pixmapPos.setX((widgetSize.width() - m_scaledPixmapSize.width()) / 2);
- m_pixmapPos.setY(margin + scaledIconSize - m_scaledPixmapSize.height());
- } else {
- // Center horizontally and vertically within the icon-area
- m_pixmapPos.setX(m_textPos[Name].x() - 2 * margin
- - (scaledIconSize + m_scaledPixmapSize.width()) / 2);
- m_pixmapPos.setY(margin
- + (scaledIconSize - m_scaledPixmapSize.height()) / 2);
- }
-
- m_iconRect = QRectF(m_pixmapPos, QSizeF(m_scaledPixmapSize));
- 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();
- }