]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Fix icon boundaries issue
authorPeter Penz <peter.penz19@gmail.com>
Wed, 7 Dec 2011 14:30:55 +0000 (15:30 +0100)
committerPeter Penz <peter.penz19@gmail.com>
Wed, 7 Dec 2011 14:34:34 +0000 (15:34 +0100)
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.

src/kitemviews/kfileitemlistwidget.cpp
src/kitemviews/kfileitemlistwidget.h
src/kitemviews/kitemlistwidget.cpp

index 8b222e9487da679899b9f001b12a827b668687d7..777cf7f74932d76c4de11b511f7058941af9f491 100644 (file)
@@ -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<KFileItemListWidget*>(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;
index 5baaa1b2861a68829771f537c4b16b2202576e5f..8f7397b754b248b950488b0635ef53fa660268c5 100644 (file)
@@ -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];
index 687367adb9fffa04917a78aa2eb83ce1d00a1031..4934da84f8eadb8b745d2e117f32d5187658b632 100644 (file)
@@ -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 {