┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/dolphinitemcategorizer.cpp
diff options
context:
space:
mode:
authorRafael Fernández López <[email protected]>2007-07-06 17:41:04 +0000
committerRafael Fernández López <[email protected]>2007-07-06 17:41:04 +0000
commit98d01c5e23e9f9dc6b6a1071e4c6e6bd740dbab3 (patch)
treee2fde20a54d0a3cf38ee724aa8a8641faea409fe /src/dolphinitemcategorizer.cpp
parent0661c42dd9893c67a7ceaea6585df2cf7aa3a440 (diff)
Make KListView capable of drawing categories on our own way. This make things easier
when we are trying to customize it. We can also benefit from KStyle if some day it supports category drawing. KListView keyboard navigation. Tricier than I thought. Pending renaming to KCategorizedView. Seems a good name. svn path=/trunk/KDE/kdebase/apps/; revision=684478
Diffstat (limited to 'src/dolphinitemcategorizer.cpp')
-rw-r--r--src/dolphinitemcategorizer.cpp187
1 files changed, 181 insertions, 6 deletions
diff --git a/src/dolphinitemcategorizer.cpp b/src/dolphinitemcategorizer.cpp
index dfb384062..a4c84736a 100644
--- a/src/dolphinitemcategorizer.cpp
+++ b/src/dolphinitemcategorizer.cpp
@@ -32,11 +32,18 @@
#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;
@@ -167,11 +174,8 @@ 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;
}
@@ -188,3 +192,174 @@ 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()->theme()->defaultSize(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);
+ icon = QPixmap::fromImage(QImage(user.homeDir() + QDir::separator() + ".face.icon")).scaled(iconSize, iconSize);
+ 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()->theme()->defaultSize(K3Icon::Small);
+
+ return qMax(option.fontMetrics.height() + (iconSize / 4) * 2 + 2, iconSize + (iconSize / 4) * 2 + 2) /* 2 gradient */;
+}