]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemlistwidget.cpp
Fix wrong position of selection-toggle
[dolphin.git] / src / kitemviews / kfileitemlistwidget.cpp
index 3d47285215eec6d6681046afcb1271b3aa434976..5a522f410a636a26e3c2655544535a0a79a13916 100644 (file)
@@ -53,7 +53,7 @@ KFileItemListWidget::KFileItemListWidget(QGraphicsItem* parent) :
     m_hoverPixmap(),
     m_textPos(),
     m_text(),
-    m_textBoundingRect(),
+    m_textRect(),
     m_sortedVisibleRoles(),
     m_expansionArea(),
     m_customTextColor(),
@@ -150,7 +150,7 @@ void KFileItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIte
 #endif
 }
 
-QRectF KFileItemListWidget::iconBoundingRect() const
+QRectF KFileItemListWidget::iconRect() const
 {
     const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
 
@@ -160,10 +160,10 @@ QRectF KFileItemListWidget::iconBoundingRect() const
     return bounds;
 }
 
-QRectF KFileItemListWidget::textBoundingRect() const
+QRectF KFileItemListWidget::textRect() const
 {
     const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
-    return m_textBoundingRect;
+    return m_textRect;
 }
 
 QRectF KFileItemListWidget::expansionToggleRect() const
@@ -172,6 +172,41 @@ QRectF KFileItemListWidget::expansionToggleRect() const
     return m_isDir ? m_expansionArea : QRectF();
 }
 
+QRectF KFileItemListWidget::selectionToggleRect() const
+{
+    const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
+
+    const int iconHeight = m_pixmap.height();
+
+    int toggleSize = KIconLoader::SizeSmall;
+    if (iconHeight >= KIconLoader::SizeEnormous) {
+        toggleSize = KIconLoader::SizeMedium;
+    } else if (iconHeight >= KIconLoader::SizeLarge) {
+        toggleSize = KIconLoader::SizeSmallMedium;
+    }
+
+    QPointF pos = iconRect().topLeft();
+
+    // If the selection toggle has a very small distance to the
+    // widget borders, the size of the selection toggle will get
+    // increased to prevent an accidental clicking of the item
+    // when trying to hit the toggle.
+    const int widgetHeight = size().height();
+    const int widgetWidth = size().width();
+    const int minMargin = 2;
+
+    if (toggleSize + minMargin * 2 >= widgetHeight) {
+        toggleSize = widgetHeight;
+        pos.setY(0);
+    }
+    if (toggleSize + minMargin * 2 >= widgetWidth) {
+        toggleSize = widgetWidth;
+        pos.setX(0);
+    }
+
+    return QRectF(pos, QSizeF(toggleSize, toggleSize));
+}
+
 QString KFileItemListWidget::roleText(const QByteArray& role, const QHash<QByteArray, QVariant>& values)
 {
     QString text;
@@ -192,13 +227,18 @@ QString KFileItemListWidget::roleText(const QByteArray& role, const QHash<QByteA
         if (values.value("isDir").toBool()) {
             // The item represents a directory. Show the number of sub directories
             // instead of the file size of the directory.
-            if (!roleValue.isNull()) {
+            if (roleValue.isNull()) {
+                text = i18nc("@item:intable", "Unknown");
+            } else {
                 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
                 text = i18ncp("@item:intable", "%1 item", "%1 items", size);
             }
         } else {
-            const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
-            text = KIO::convertSize(size);
+            // Show the size in kilobytes (always round up)
+            const KLocale* locale = KGlobal::locale();
+            const int roundInc = (locale->binaryUnitDialect() == KLocale::MetricBinaryDialect) ? 499 : 511;
+            const KIO::filesize_t size = roleValue.value<KIO::filesize_t>() + roundInc;
+            text = locale->formatByteSize(size, 0, KLocale::DefaultBinaryDialect, KLocale::UnitKiloByte);
         }
         break;
     }
@@ -238,7 +278,13 @@ void KFileItemListWidget::setTextColor(const QColor& color)
 
 QColor KFileItemListWidget::textColor() const
 {
-    return m_customTextColor.isValid() ? m_customTextColor : styleOption().palette.text().color();
+    if (m_customTextColor.isValid()) {
+        return m_customTextColor;
+    } else if (isSelected()) {
+        return styleOption().palette.highlightedText().color();
+    } else {
+        return styleOption().palette.text().color();
+    }
 }
 
 void KFileItemListWidget::setOverlay(const QPixmap& overlay)
@@ -304,6 +350,12 @@ void KFileItemListWidget::hoveredChanged(bool hovered)
     m_dirtyLayout = true;
 }
 
+void KFileItemListWidget::selectedChanged(bool selected)
+{
+    Q_UNUSED(selected);
+    updateAdditionalInfoTextColor();
+}
+
 void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
 {
     KItemListWidget::resizeEvent(event);
@@ -403,13 +455,14 @@ void KFileItemListWidget::updatePixmapCache()
             squarePixmap.fill(Qt::transparent);
 
             QPainter painter(&squarePixmap);
+            int x, y;
             if (iconOnTop) {
-                const int x = (iconHeight - m_pixmap.width()) / 2;  // Center horizontally
-                const int y = iconHeight - m_pixmap.height();       // Align on bottom
+                x = (iconHeight - m_pixmap.width()) / 2;  // Center horizontally
+                y = iconHeight - m_pixmap.height();       // Align on bottom
                 painter.drawPixmap(x, y, m_pixmap);
             } else {
-                const int x = iconHeight - m_pixmap.width();        // Align right
-                const int y = (iconHeight - m_pixmap.height()) / 2; // Center vertically
+                x = iconHeight - m_pixmap.width();        // Align right
+                y = (iconHeight - m_pixmap.height()) / 2; // Center vertically
                 painter.drawPixmap(x, y, m_pixmap);
             }
 
@@ -531,7 +584,7 @@ void KFileItemListWidget::updateIconsLayoutTextCache()
 
     m_text[Name].setTextWidth(maxWidth);
     m_textPos[Name] = QPointF(option.margin, widgetHeight - textLinesCount * fontHeight - option.margin);
-    m_textBoundingRect = QRectF(option.margin + (maxWidth - requiredWidthForName) / 2,
+    m_textRect = QRectF(option.margin + (maxWidth - requiredWidthForName) / 2,
                                  m_textPos[Name].y(),
                                  requiredWidthForName,
                                  m_text[Name].size().height());
@@ -570,15 +623,15 @@ void KFileItemListWidget::updateIconsLayoutTextCache()
         m_textPos[textId] = QPointF(option.margin, y);
         m_text[textId].setTextWidth(maxWidth);
 
-        const QRectF textBoundingRect(option.margin + (maxWidth - requiredWidth) / 2, y, requiredWidth, fontHeight);
-        m_textBoundingRect |= textBoundingRect;
+        const QRectF textRect(option.margin + (maxWidth - requiredWidth) / 2, y, requiredWidth, fontHeight);
+        m_textRect |= textRect;
 
         y += fontHeight;
     }
 
-    // Add a margin to the text bounding rectangle
+    // Add a margin to the text rectangle
     const qreal margin = option.margin;
-    m_textBoundingRect.adjust(-margin, -margin, margin, margin);
+    m_textRect.adjust(-margin, -margin, margin, margin);
 }
 
 void KFileItemListWidget::updateCompactLayoutTextCache()
@@ -620,7 +673,7 @@ void KFileItemListWidget::updateCompactLayoutTextCache()
         y += fontHeight;
     }
 
-    m_textBoundingRect = QRectF(x - option.margin, 0, maximumRequiredTextWidth + 2 * option.margin, widgetHeight);
+    m_textRect = QRectF(x - option.margin, 0, maximumRequiredTextWidth + 2 * option.margin, widgetHeight);
 }
 
 void KFileItemListWidget::updateDetailsLayoutTextCache()
@@ -631,7 +684,7 @@ void KFileItemListWidget::updateDetailsLayoutTextCache()
     // +------+
     // | Icon |  Name role   Additional role 1   Additional role 2
     // +------+
-    m_textBoundingRect = QRectF();
+    m_textRect = QRectF();
 
     const KItemListStyleOption& option = styleOption();
     const QHash<QByteArray, QVariant> values = data();
@@ -669,7 +722,7 @@ void KFileItemListWidget::updateDetailsLayoutTextCache()
 
         switch (textId) {
         case Name: {
-            m_textBoundingRect = QRectF(m_textPos[textId].x() - option.margin, 0,
+            m_textRect = QRectF(m_textPos[textId].x() - option.margin, 0,
                                         requiredWidth + 2 * option.margin, size().height());
 
             // The column after the name should always be aligned on the same x-position independent
@@ -690,11 +743,19 @@ void KFileItemListWidget::updateDetailsLayoutTextCache()
 
 void KFileItemListWidget::updateAdditionalInfoTextColor()
 {
+    QColor c1;
+    if (m_customTextColor.isValid()) {
+        c1 = m_customTextColor;
+    } else if (isSelected() && m_layout != DetailsLayout) {
+        c1 = styleOption().palette.highlightedText().color();
+    } else {
+        c1 = styleOption().palette.text().color();
+    }
+
     // For the color of the additional info the inactive text color
     // is not used as this might lead to unreadable text for some color schemes. Instead
-    // the text color is slightly mixed with the background color.
-    const QColor c1 = textColor();
-    const QColor c2 = styleOption().palette.background().color();
+    // the text color c1 is slightly mixed with the background color.
+    const QColor c2 = styleOption().palette.base().color();
     const int p1 = 70;
     const int p2 = 100 - p1;
     m_additionalInfoTextColor = QColor((c1.red()   * p1 + c2.red()   * p2) / 100,