]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinitemcategorizer.cpp
Fixed 'Select All' and 'Invert Selection' for the column view (only the items of...
[dolphin.git] / src / dolphinitemcategorizer.cpp
index c83f9383f2bf3d2d35cec291149e0ead3fdfdbde..2a25a8f1275d5fda20eec6aeb70a4da2048955bf 100644 (file)
 #include <kdatetime.h>
 #include <kdirmodel.h>
 #include <kfileitem.h>
+#include <kiconloader.h>
 #include <klocale.h>
 #include <kurl.h>
+#include <kuser.h>
+#include <kmimetype.h>
+#include <kstandarddirs.h>
+#include <kpixmapeffect.h>
 
 #include <QList>
 #include <QSortFilterProxyModel>
+#include <QPainter>
+#include <QDir>
 
 DolphinItemCategorizer::DolphinItemCategorizer() :
     KItemCategorizer()
@@ -48,7 +55,7 @@ DolphinItemCategorizer::~DolphinItemCategorizer()
 }
 
 QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
-                                                int sortRole)
+                                                int sortRole) const
 {
     QString retString;
 
@@ -84,7 +91,7 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
                     retString = data.toString().toUpper().at(1);
                 else if (item->isHidden() && data.toString().at(0) == '.' &&
                          !data.toString().at(1).isLetter())
-                    retString = i18n("Others");
+                    retString = i18nc("@title:group Name", "Others");
                 else if (item->isHidden() && data.toString().at(0) != '.')
                     retString = data.toString().toUpper().at(0);
                 else if (item->isHidden())
@@ -99,13 +106,13 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
                         if (currA->isLetter())
                             validCategory = true;
                         else if (currA->isDigit())
-                            return i18n("Others");
+                            return i18nc("@title:group", "Others");
                         else
                             ++currA;
                     }
 
                     if (!validCategory)
-                        retString = i18n("Others");
+                        retString = i18nc("@title:group Name", "Others");
                     else
                         retString = *currA;
                 }
@@ -115,22 +122,21 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
 
         case DolphinView::SortByDate:
         {
-            KDateTime modifiedTime;
-            modifiedTime.setTime_t(item->time(KIO::UDS_MODIFICATION_TIME));
+            KDateTime modifiedTime = item->time(KFileItem::ModificationTime);
             modifiedTime = modifiedTime.toLocalZone();
 
             if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 0)
-                retString = i18n("Today");
+                retString = i18nc("@title:group Date", "Today");
             else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 1)
-                retString = i18n("Yesterday");
+                retString = i18nc("@title:group Date", "Yesterday");
             else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 7)
-                retString = i18n("Less than a week");
+                retString = i18nc("@title:group Date", "Less than a week");
             else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 31)
-                retString = i18n("Less than a month");
+                retString = i18nc("@title:group Date", "Less than a month");
             else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 365)
-                retString = i18n("Less than a year");
+                retString = i18nc("@title:group Date", "Less than a year");
             else
-                retString = i18n("More than a year");
+                retString = i18nc("@title:group Date", "More than a year");
             break;
         }
 
@@ -149,13 +155,13 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
         case DolphinView::SortBySize: {
             const int fileSize = item ? item->size() : -1;
             if (item && item->isDir()) {
-                retString = i18n("Folders");
+                retString = i18nc("@title:group Size", "Folders");
             } else if (fileSize < 5242880) {
-                retString = i18nc("Size", "Small");
+                retString = i18nc("@title:group Size", "Small");
             } else if (fileSize < 10485760) {
-                retString = i18nc("Size", "Medium");
+                retString = i18nc("@title:group Size", "Medium");
             } else {
-                retString = i18nc("Size", "Big");
+                retString = i18nc("@title:group Size", "Big");
             }
             break;
         }
@@ -167,16 +173,17 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
 #ifdef HAVE_NEPOMUK
         case DolphinView::SortByRating: {
             const quint32 rating = DolphinSortFilterProxyModel::ratingForIndex(index);
-            if (rating) {
-                retString = i18np("1 star", "%1 stars", rating);
-            } else {
-                retString = i18n("Not yet rated");
-            }
+
+            retString = QString::number(rating);
             break;
         }
 
         case DolphinView::SortByTags: {
             retString = DolphinSortFilterProxyModel::tagsForIndex(index);
+
+            if (retString.isEmpty())
+                retString = i18nc("@title:group Tags", "Not yet tagged");
+
             break;
         }
 #endif
@@ -184,3 +191,181 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
 
     return retString;
 }
+
+void DolphinItemCategorizer::drawCategory(const QModelIndex &index,
+                                          int sortRole,
+                                          const QStyleOption &option,
+                                          QPainter *painter) const
+{
+    QRect starRect = option.rect;
+
+    int iconSize =  KIconLoader::global()->currentSize(K3Icon::Small);                       
+    const QString category = categoryForItem(index, sortRole);
+
+    QColor color = option.palette.color(QPalette::Text);
+
+    painter->save();
+    painter->setRenderHint(QPainter::Antialiasing);
+
+    QStyleOptionButton opt;
+
+    opt.rect = option.rect;
+    opt.palette = option.palette;
+    opt.direction = option.direction;
+    opt.text = category;
+
+    if (option.state & QStyle::State_MouseOver)
+    {
+        const QPalette::ColorGroup group =
+                                          option.state & QStyle::State_Enabled ?
+                                          QPalette::Normal : QPalette::Disabled;
+
+        QLinearGradient gradient(option.rect.topLeft(),
+                                 option.rect.bottomRight());
+        gradient.setColorAt(0,
+                            option.palette.color(group,
+                                                 QPalette::Highlight).light());
+        gradient.setColorAt(1, Qt::transparent);
+
+        painter->fillRect(option.rect, gradient);
+    }
+
+    QFont painterFont = painter->font();
+    painterFont.setWeight(QFont::Bold);
+    QFontMetrics metrics(painterFont);
+    painter->setFont(painterFont);
+
+    QPainterPath path;
+    path.addRect(option.rect.left(),
+                 option.rect.bottom() - 2,
+                 option.rect.width(),
+                 2);
+
+    QLinearGradient gradient(option.rect.topLeft(),
+                             option.rect.bottomRight());
+    gradient.setColorAt(0, color);
+    gradient.setColorAt(1, Qt::transparent);
+
+    painter->setBrush(gradient);
+    painter->fillPath(path, gradient);
+
+    opt.rect.setLeft(opt.rect.left() + (iconSize / 4));
+    starRect.setLeft(starRect.left() + (iconSize / 4));
+    starRect.setRight(starRect.right() + (iconSize / 4));
+
+    bool paintIcon = true;
+    bool paintText = true;
+
+    QPixmap icon;
+    switch (sortRole) {
+        case DolphinView::SortByName:
+            paintIcon = false;
+            break;
+
+        case DolphinView::SortByDate:
+            paintIcon = false;
+            break;
+
+        case DolphinView::SortByPermissions:
+            paintIcon = false; // FIXME: let's think about how to represent permissions
+            break;
+
+        case DolphinView::SortByOwner: {
+            opt.rect.setTop(option.rect.top() + (iconSize / 4));
+            KUser user(category);
+            if (QFile::exists(user.homeDir() + QDir::separator() + ".face.icon"))
+            {
+                icon = QPixmap::fromImage(QImage(user.homeDir() + QDir::separator() + ".face.icon")).scaled(iconSize, iconSize);
+            }
+            else
+            {
+                icon = KIconLoader::global()->loadIcon("user", K3Icon::Small);
+            }
+            break;
+        }
+
+        case DolphinView::SortByGroup:
+            paintIcon = false;
+            break;
+
+        case DolphinView::SortBySize:
+            paintIcon = false;
+            break;
+
+        case DolphinView::SortByType: {
+            opt.rect.setTop(option.rect.top() + (option.rect.height() / 2) - (iconSize / 2));
+            const KDirModel *model = static_cast<const KDirModel*>(index.model());
+            KFileItem *item = model->itemForIndex(index);
+            icon = KIconLoader::global()->loadIcon(KMimeType::iconNameForUrl(item->url()),
+                                                   K3Icon::Small);
+            break;
+        }
+
+#ifdef HAVE_NEPOMUK
+        case DolphinView::SortByRating: {
+            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);
+                starRect.setLeft(starRect.left() + iconSize + (iconSize / 4) /* separator between stars */);
+            }
+
+            if (rating && rating % 2) {
+                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));
+                starRect.setSize(QSize(iconSize / 2, iconSize / 2));
+                starRect.setLeft(starRect.left() + (iconSize / 2) + (iconSize / 4));
+                starRect.setSize(QSize(iconSize, iconSize));
+            }
+
+            for (int i = rating; i < 9; i += 2) {
+                painter->drawPixmap(starRect, disabledPixmap);
+                starRect.setLeft(starRect.left() + iconSize + (iconSize / 4));
+            }
+
+            break;
+        }
+
+        case DolphinView::SortByTags:
+            paintIcon = false;
+            break;
+#endif
+    }
+
+    if (paintIcon) {
+        painter->drawPixmap(QRect(opt.rect.left(), opt.rect.top(), iconSize, iconSize), icon);
+        opt.rect.setLeft(opt.rect.left() + iconSize + (iconSize / 4));
+    }
+
+    if (paintText) {
+        opt.rect.setTop(option.rect.top() + (iconSize / 4));
+        opt.rect.setBottom(opt.rect.bottom() - 2);
+        painter->setPen(color);
+
+        painter->drawText(opt.rect, Qt::AlignVCenter | Qt::AlignLeft,
+        metrics.elidedText(category, Qt::ElideRight, opt.rect.width()));
+    }
+
+    painter->restore();
+}
+
+int DolphinItemCategorizer::categoryHeight(const QStyleOption &option) const
+{
+    int iconSize = KIconLoader::global()->currentSize(K3Icon::Small);
+
+    return qMax(option.fontMetrics.height() + (iconSize / 4) * 2 + 2, iconSize + (iconSize / 4) * 2 + 2) /* 2 gradient */;
+}