]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemcategorizer.cpp
fix: forgot to invoke base implementation
[dolphin.git] / src / kitemcategorizer.cpp
1 /**
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include "kitemcategorizer.h"
22
23 #include <QPainter>
24 #include <QStyleOption>
25
26 KItemCategorizer::KItemCategorizer()
27 {
28 }
29
30 KItemCategorizer::~KItemCategorizer()
31 {
32 }
33
34 void KItemCategorizer::drawCategory(const QModelIndex &index,
35 int sortRole,
36 const QStyleOption &option,
37 QPainter *painter) const
38 {
39 const QString category = categoryForItem(index, sortRole);
40
41 QColor color = option.palette.color(QPalette::Text);
42
43 painter->save();
44 painter->setRenderHint(QPainter::Antialiasing);
45
46 QStyleOptionButton opt;
47
48 opt.rect = option.rect;
49 opt.palette = option.palette;
50 opt.direction = option.direction;
51 opt.text = category;
52
53 if (option.state & QStyle::State_MouseOver)
54 {
55 const QPalette::ColorGroup group =
56 option.state & QStyle::State_Enabled ?
57 QPalette::Normal : QPalette::Disabled;
58
59 QLinearGradient gradient(option.rect.topLeft(),
60 option.rect.bottomRight());
61 gradient.setColorAt(0,
62 option.palette.color(group,
63 QPalette::Highlight).light());
64 gradient.setColorAt(1, Qt::transparent);
65
66 painter->fillRect(option.rect, gradient);
67 }
68
69 QFont painterFont = painter->font();
70 painterFont.setWeight(QFont::Bold);
71 painterFont.setPointSize(painterFont.pointSize() + 2);
72 QFontMetrics metrics(painterFont);
73 painter->setFont(painterFont);
74
75 QPainterPath path;
76 path.addRect(option.rect.left(),
77 option.rect.bottom() - 2,
78 option.rect.width(),
79 2);
80
81 QLinearGradient gradient(option.rect.topLeft(),
82 option.rect.bottomRight());
83 gradient.setColorAt(0, color);
84 gradient.setColorAt(1, Qt::transparent);
85
86 painter->setBrush(gradient);
87 painter->fillPath(path, gradient);
88
89 painter->setPen(color);
90
91 painter->drawText(option.rect, Qt::AlignVCenter | Qt::AlignLeft,
92 metrics.elidedText(category, Qt::ElideRight, option.rect.width()));
93
94 painter->restore();
95 }
96
97 int KItemCategorizer::categoryHeight(const QStyleOption &option) const
98 {
99 return option.fontMetrics.height() + 6 /* 4 separator; 2 gradient */;
100 }