]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemlistwidget.cpp
Use correct selection color for inactive windows
[dolphin.git] / src / kitemviews / kfileitemlistwidget.cpp
index f6039b7a59153c23e6ff69bbb0d311253bdc09dd..897600e60312c94c3546714b9b30a23adbf4c8bd 100644 (file)
@@ -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;
+    }
+
+    const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
+    const QPalette::ColorRole role = isSelected() ? QPalette::HighlightedText : QPalette::Text;
+    return styleOption().palette.brush(group, role).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);
             }
 
@@ -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,