- // Precondition: Requires already updated m_textPos values to calculate
- // the remaining height when the alignment is vertical.
-
- const bool iconOnTop = (m_layout == IconsLayout);
- const KItemListStyleOption& option = styleOption();
- const int iconHeight = option.iconSize;
-
- const QHash<QByteArray, QVariant> values = data();
- const QSizeF widgetSize = size();
-
- int scaledIconHeight = 0;
- if (iconOnTop) {
- scaledIconHeight = static_cast<int>(m_textPos[Name].y() - 3 * option.margin);
- } else {
- const int textRowsCount = (m_layout == CompactLayout) ? visibleRoles().count() : 1;
- const qreal requiredTextHeight = textRowsCount * option.fontMetrics.height();
- scaledIconHeight = (requiredTextHeight < iconHeight) ? widgetSize.height() - 2 * option.margin : iconHeight;
- }
-
- bool updatePixmap = (iconHeight != m_pixmap.height());
- 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, 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();
- }