From: Peter Penz Date: Wed, 7 Dec 2011 14:30:55 +0000 (+0100) Subject: Fix icon boundaries issue X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/6c29a1d98a490e585d0e9d9a4eb4824e72756390 Fix icon boundaries issue The size of the icon boundaries should be as minimal as possible. The patch for fixing the zooming-issues with the selection resulted into a minor regression where the icon boundaries might be too large. This patch remembers the original size of the pixmap to be able to scale m_iconRect. --- diff --git a/src/kitemviews/kfileitemlistwidget.cpp b/src/kitemviews/kfileitemlistwidget.cpp index 8b222e948..777cf7f74 100644 --- a/src/kitemviews/kfileitemlistwidget.cpp +++ b/src/kitemviews/kfileitemlistwidget.cpp @@ -52,7 +52,8 @@ KFileItemListWidget::KFileItemListWidget(QGraphicsItem* parent) : m_pixmapPos(), m_pixmap(), m_scaledPixmapSize(), - m_hoverPixmapRect(), + m_originalPixmapSize(), + m_iconRect(), m_hoverPixmap(), m_textPos(), m_text(), @@ -156,11 +157,7 @@ void KFileItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIte QRectF KFileItemListWidget::iconRect() const { const_cast(this)->triggerCacheRefreshing(); - - QRectF bounds(m_pixmapPos, m_scaledPixmapSize); - const qreal margin = styleOption().margin; - bounds.adjust(-margin, -margin, margin, margin); - return bounds; + return m_iconRect; } QRectF KFileItemListWidget::textRect() const @@ -477,7 +474,7 @@ void KFileItemListWidget::updatePixmapCache() iconName = QLatin1String("unknown"); } m_pixmap = pixmapForIcon(iconName, iconHeight); - m_hoverPixmapRect.setSize(m_pixmap.size()); + 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. @@ -486,7 +483,7 @@ void KFileItemListWidget::updatePixmapCache() if (scale) { KPixmapModifier::scale(m_pixmap, QSize(iconHeight, iconHeight)); } - m_hoverPixmapRect.setSize(m_pixmap.size()); + m_originalPixmapSize = m_pixmap.size(); // To simplify the handling of scaling the original pixmap // will be embedded into a square pixmap. @@ -507,7 +504,7 @@ void KFileItemListWidget::updatePixmapCache() m_pixmap = squarePixmap; } else { - m_hoverPixmapRect.setSize(m_pixmap.size()); + m_originalPixmapSize = m_pixmap.size(); } if (m_isCut) { @@ -535,10 +532,19 @@ void KFileItemListWidget::updatePixmapCache() m_pixmapPos.setY(option.margin); // Center the hover rectangle horizontally and align it on bottom - const qreal x = m_pixmapPos.x() + (m_scaledPixmapSize.width() - m_hoverPixmapRect.width()) / 2.0; - const qreal y = m_pixmapPos.y() + m_scaledPixmapSize.height() - m_hoverPixmapRect.height(); - m_hoverPixmapRect.moveTopLeft(QPointF(x, y)); - + 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; diff --git a/src/kitemviews/kfileitemlistwidget.h b/src/kitemviews/kfileitemlistwidget.h index 5baaa1b28..8f7397b75 100644 --- a/src/kitemviews/kfileitemlistwidget.h +++ b/src/kitemviews/kfileitemlistwidget.h @@ -139,8 +139,9 @@ private: QPixmap m_pixmap; QSize m_scaledPixmapSize; - QRectF m_hoverPixmapRect; - QPixmap m_hoverPixmap; + QSize m_originalPixmapSize; // Size of pixmap before it gets converted to a square pixmap + QRectF m_iconRect; // Cache for KItemListWidget::iconRect() + QPixmap m_hoverPixmap; // Cache for modified m_pixmap when hovering the item QPointF m_textPos[TextIdCount]; QStaticText m_text[TextIdCount]; diff --git a/src/kitemviews/kitemlistwidget.cpp b/src/kitemviews/kitemlistwidget.cpp index 687367adb..4934da84f 100644 --- a/src/kitemviews/kitemlistwidget.cpp +++ b/src/kitemviews/kitemlistwidget.cpp @@ -124,7 +124,7 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o const QRect iconBounds = iconRect().toRect(); const QRect textBounds = textRect().toRect(); - if (iconBounds.bottom() >= textBounds.top()) { + if (iconBounds.bottom() > textBounds.top()) { viewItemOption.rect = textBounds; } else { // See KItemListWidget::drawItemStyleOption(): The selection rectangle @@ -439,7 +439,7 @@ void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QS viewItemOption.state = styleState; viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne; - if (iconBounds.bottom() >= textBounds.top()) { + if (iconBounds.bottom() > textBounds.top()) { viewItemOption.rect = iconBounds | textBounds; widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget); } else {