]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphincategorydrawer.cpp
Better looking categories. Thanks to Aurélien Gâteau for the original patch.
[dolphin.git] / src / dolphincategorydrawer.cpp
index 5b2278629ba7cd3951b8ba70ecee2ee37d3ba898..8148a7b8ed02f0ab8f1d1bd50cf267998786ab76 100644 (file)
@@ -1,6 +1,6 @@
 /**
   * This file is part of the KDE project
-  * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
+  * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Library General Public
   */
 
 #include "dolphincategorydrawer.h"
-
+#include "ratingpainter.h"
 #include <QPainter>
 #include <QFile>
 #include <QDir>
 
 #include <kiconloader.h>
 #include <kcategorizedsortfilterproxymodel.h>
-#include <kpixmapeffect.h>
+#include <qimageblitz.h>
 #include <kuser.h>
 
 #include <config-nepomuk.h>
@@ -52,8 +52,8 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
 {
     QRect starRect = option.rect;
 
-    int iconSize =  KIconLoader::global()->currentSize(K3Icon::Small);
-    QVariant categoryVariant = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryRole);
+    int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
+    QVariant categoryVariant = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
 
     if (!categoryVariant.isValid())
     {
@@ -62,7 +62,16 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
 
     const QString category = categoryVariant.toString();
 
-    QColor color = option.palette.color(QPalette::Text);
+    QColor color;
+
+    if (option.state & QStyle::State_Selected)
+    {
+        color = option.palette.color(QPalette::HighlightedText);
+    }
+    else
+    {
+        color = option.palette.color(QPalette::Text);
+    }
 
     painter->save();
     painter->setRenderHint(QPainter::Antialiasing);
@@ -74,9 +83,22 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
     opt.direction = option.direction;
     opt.text = category;
 
-    if (option.state & QStyle::State_MouseOver)
+    if (option.state & QStyle::State_Selected)
+    {
+        QColor selected = option.palette.color(QPalette::Highlight);
+
+        QLinearGradient gradient(option.rect.topLeft(),
+                                 option.rect.bottomRight());
+        gradient.setColorAt(option.direction == Qt::LeftToRight ? 0
+                                                                : 1, selected);
+        gradient.setColorAt(option.direction == Qt::LeftToRight ? 1
+                                                                : 0, Qt::transparent);
+
+        painter->fillRect(option.rect, gradient);
+    }
+    else if (option.state & QStyle::State_MouseOver)
     {
-        QColor hover = option.palette.color(QPalette::Highlight).light();
+        QColor hover = option.palette.color(QPalette::Highlight);
         hover.setAlpha(88);
 
         QLinearGradient gradient(option.rect.topLeft(),
@@ -96,9 +118,9 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
 
     QPainterPath path;
     path.addRect(option.rect.left(),
-                 option.rect.bottom() - 2,
+                 option.rect.bottom() - 1,
                  option.rect.width(),
-                 2);
+                 1);
 
     QLinearGradient gradient(option.rect.topLeft(),
                              option.rect.bottomRight());
@@ -112,15 +134,15 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
 
     if (option.direction == Qt::LeftToRight)
     {
-        opt.rect.setLeft(opt.rect.left() + (iconSize / 4));
-        starRect.setLeft(starRect.left() + (iconSize / 4));
-        starRect.setRight(starRect.right() + (iconSize / 4));
+        opt.rect.setLeft(opt.rect.left());
+        starRect.setLeft(starRect.left());
+        starRect.setRight(starRect.right());
     }
     else
     {
-        opt.rect.setRight(opt.rect.width() - (iconSize / 4));
-        starRect.setLeft(starRect.width() - iconSize);
-        starRect.setRight(starRect.width() - (iconSize / 4));
+        opt.rect.setRight(opt.rect.width());
+        starRect.setLeft(starRect.width());
+        starRect.setRight(starRect.width());
     }
 
     bool paintIcon = true;
@@ -145,16 +167,21 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
             break;
 
         case KDirModel::Owner: {
-            opt.rect.setTop(option.rect.top() + (iconSize / 4));
+            opt.rect.setTop(option.rect.bottom() - (iconSize / 4));
             KUser user(category);
-            if (QFile::exists(user.homeDir() + QDir::separator() + ".face.icon"))
+            QString faceIconPath = user.faceIconPath();
+
+            if (!faceIconPath.isEmpty())
             {
-                icon = QPixmap::fromImage(QImage(user.homeDir() + QDir::separator() + ".face.icon")).scaled(iconSize, iconSize);
+                icon = QPixmap::fromImage(QImage(faceIconPath).scaledToHeight(option.fontMetrics.height(), Qt::SmoothTransformation));
             }
             else
             {
-                icon = KIconLoader::global()->loadIcon("user", K3Icon::Small);
+                icon = KIconLoader::global()->loadIcon("user-identity", KIconLoader::NoGroup, option.fontMetrics.height());
             }
+
+            opt.rect.setTop(opt.rect.top() - icon.height());
+
             break;
         }
 
@@ -163,7 +190,7 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
             break;
 
         case KDirModel::Type: {
-            opt.rect.setTop(option.rect.top() + (option.rect.height() / 2) - (iconSize / 2));
+            opt.rect.setTop(option.rect.bottom() - (iconSize / 4));
             const KCategorizedSortFilterProxyModel *proxyModel = static_cast<const KCategorizedSortFilterProxyModel*>(index.model());
             const DolphinModel *model = static_cast<const DolphinModel*>(proxyModel->sourceModel());
             KFileItem item = model->itemForIndex(proxyModel->mapToSource(index));
@@ -172,7 +199,10 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
             // so the group icon drawn is that one particularly. This way assures the drawn
             // icon is the one of the mimetype of the group itself. (ereslibre)
             icon = KIconLoader::global()->loadMimeTypeIcon(item.mimeTypePtr()->iconName(),
-                                                           K3Icon::Small);
+                                                           KIconLoader::NoGroup, option.fontMetrics.height());
+
+            opt.rect.setTop(opt.rect.top() - icon.height());
+
             break;
         }
 
@@ -181,78 +211,11 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
             paintText = false;
             paintIcon = false;
 
-            starRect.setTop(option.rect.top() + (option.rect.height() / 2) - (iconSize / 2));
-            starRect.setSize(QSize(iconSize, iconSize));
-
-            QPixmap pixmap = KIconLoader::global()->loadIcon("rating", K3Icon::Small);
-            QPixmap smallPixmap = KIconLoader::global()->loadIcon("rating", K3Icon::NoGroup, iconSize / 2);
-            QPixmap disabledPixmap = KIconLoader::global()->loadIcon("rating", K3Icon::Small);
-
-            KPixmapEffect::toGray(disabledPixmap, false);
-
-            int rating = category.toInt();
-
-            for (int i = 0; i < rating - (rating % 2); i += 2) {
-                painter->drawPixmap(starRect, pixmap);
-
-                if (option.direction == Qt::LeftToRight)
-                {
-                    starRect.setLeft(starRect.left() + iconSize + (iconSize / 4) /* separator between stars */);
-                    starRect.setRight(starRect.right() + iconSize + (iconSize / 4) /* separator between stars */);
-                }
-                else
-                {
-                    starRect.setLeft(starRect.left() - iconSize - (iconSize / 4) /* separator between stars */);
-                    starRect.setRight(starRect.right() - iconSize - (iconSize / 4) /* separator between stars */);
-                }
-            }
-
-            if (rating && rating % 2) {
-                if (option.direction == Qt::RightToLeft)
-                {
-                    starRect.setLeft(starRect.left() + (iconSize / 2) /* separator between stars */);
-                }
-
-                starRect.setTop(option.rect.top() + (option.rect.height() / 2) - (iconSize / 4));
-                starRect.setSize(QSize(iconSize / 2, iconSize / 2));
-                painter->drawPixmap(starRect, smallPixmap);
-                starRect.setTop(opt.rect.top() + (option.rect.height() / 2) - (iconSize / 2));
-
-                if (option.direction == Qt::LeftToRight)
-                {
-                    starRect.setLeft(starRect.left() + (iconSize / 2) + (iconSize / 4));
-                    starRect.setRight(starRect.right() + (iconSize / 2) + (iconSize / 4));
-                }
-                else
-                {
-                    starRect.setLeft(starRect.left() - (iconSize / 2) - (iconSize / 4));
-                    starRect.setRight(starRect.right() - (iconSize / 2) - (iconSize / 4));
-                }
-
-                if (option.direction == Qt::RightToLeft)
-                {
-                    starRect.setLeft(starRect.left() - (iconSize / 2));
-                    starRect.setRight(starRect.right() - (iconSize / 2));
-                }
-
-                starRect.setSize(QSize(iconSize, iconSize));
-            }
-
-            for (int i = rating; i < 9; i += 2) {
-                painter->drawPixmap(starRect, disabledPixmap);
-
-                if (option.direction == Qt::LeftToRight)
-                {
-                    starRect.setLeft(starRect.left() + iconSize + (iconSize / 4));
-                    starRect.setRight(starRect.right() + iconSize + (iconSize / 4));
-                }
-                else
-                {
-                    starRect.setLeft(starRect.left() - iconSize - (iconSize / 4));
-                    starRect.setRight(starRect.right() - iconSize - (iconSize / 4));
-                }
-            }
-
+            painter->setLayoutDirection( option.direction );
+            QRect ratingRect( option.rect );
+            ratingRect.setTop(option.rect.top() + (option.rect.height() / 2) - (iconSize / 2));
+            ratingRect.setHeight( iconSize );
+            Nepomuk::RatingPainter::drawRating( painter, ratingRect, Qt::AlignLeft, category.toInt() );
             break;
         }
 
@@ -264,11 +227,11 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
 
     if (paintIcon) {
         painter->drawPixmap(QRect(option.direction == Qt::LeftToRight ? opt.rect.left()
-                                                                      : opt.rect.right() - iconSize + (iconSize / 4), opt.rect.top(), iconSize, iconSize), icon);
+                                                                      : opt.rect.right() - icon.width() + (iconSize / 4), opt.rect.top(), icon.width(), icon.height()), icon);
 
         if (option.direction == Qt::LeftToRight)
         {
-            opt.rect.setLeft(opt.rect.left() + iconSize + (iconSize / 4));
+            opt.rect.setLeft(opt.rect.left() + icon.width() + (iconSize / 4));
         }
         else
         {
@@ -278,15 +241,15 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
 
     if (paintText) {
         opt.rect.setTop(option.rect.top() + (iconSize / 4));
-        opt.rect.setBottom(opt.rect.bottom() - 2);
+        opt.rect.setBottom(opt.rect.bottom() - 1);
         painter->setPen(color);
 
         QRect textRect = opt.rect;
 
         if (option.direction == Qt::RightToLeft)
         {
-            textRect.setWidth(textRect.width() - (paintIcon ? iconSize + (iconSize / 4)
-                                                            : 0));
+            textRect.setWidth(textRect.width() - (paintIcon ? icon.width() + (iconSize / 4)
+                                                            : -(iconSize / 4)));
         }
 
         painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft,
@@ -296,9 +259,23 @@ void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
     painter->restore();
 }
 
-int DolphinCategoryDrawer::categoryHeight(const QStyleOption &option) const
+int DolphinCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
 {
-    int iconSize = KIconLoader::global()->currentSize(K3Icon::Small);
+    int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
+    int heightWithoutIcon = option.fontMetrics.height() + (iconSize / 4) * 2 + 1; /* 1 pixel-width gradient */
+    bool paintIcon;
+
+    switch (index.column()) {
+        case KDirModel::Owner:
+        case KDirModel::Type:
+            paintIcon = true;
+            break;
+        default:
+            paintIcon = false;
+    }
+
+    if (paintIcon)
+        return qMax(heightWithoutIcon, iconSize + (iconSize / 4) * 2 + 1) /* 1 pixel-width gradient */;
 
-    return qMax(option.fontMetrics.height() + (iconSize / 4) * 2 + 2, iconSize + (iconSize / 4) * 2 + 2) /* 2 gradient */;
+    return heightWithoutIcon;
 }