]> cloud.milkyroute.net Git - dolphin.git/blob - src/kcategorydrawer.cpp
Create the new architecture for KCategorizedView. Now DolphinModel is created, inheri...
[dolphin.git] / src / kcategorydrawer.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 "kcategorydrawer.h"
22
23 #include <QPainter>
24 #include <QStyleOption>
25
26 #include <kcategorizedsortfilterproxymodel.h>
27
28 KCategoryDrawer::KCategoryDrawer()
29 {
30 }
31
32 KCategoryDrawer::~KCategoryDrawer()
33 {
34 }
35
36 void KCategoryDrawer::drawCategory(const QModelIndex &index,
37 int sortRole,
38 const QStyleOption &option,
39 QPainter *painter) const
40 {
41 const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryRole).toString();
42
43 QColor color = option.palette.color(QPalette::Text);
44
45 painter->save();
46 painter->setRenderHint(QPainter::Antialiasing);
47
48 QStyleOptionButton opt;
49
50 opt.rect = option.rect;
51 opt.palette = option.palette;
52 opt.direction = option.direction;
53 opt.text = category;
54
55 if (option.state & QStyle::State_MouseOver)
56 {
57 const QPalette::ColorGroup group =
58 option.state & QStyle::State_Enabled ?
59 QPalette::Normal : QPalette::Disabled;
60
61 QLinearGradient gradient(option.rect.topLeft(),
62 option.rect.bottomRight());
63 gradient.setColorAt(0,
64 option.palette.color(group,
65 QPalette::Highlight).light());
66 gradient.setColorAt(1, Qt::transparent);
67
68 painter->fillRect(option.rect, gradient);
69 }
70
71 QFont painterFont = painter->font();
72 painterFont.setWeight(QFont::Bold);
73 painterFont.setPointSize(painterFont.pointSize() + 2);
74 QFontMetrics metrics(painterFont);
75 painter->setFont(painterFont);
76
77 QPainterPath path;
78 path.addRect(option.rect.left(),
79 option.rect.bottom() - 2,
80 option.rect.width(),
81 2);
82
83 QLinearGradient gradient(option.rect.topLeft(),
84 option.rect.bottomRight());
85 gradient.setColorAt(0, color);
86 gradient.setColorAt(1, Qt::transparent);
87
88 painter->setBrush(gradient);
89 painter->fillPath(path, gradient);
90
91 painter->setPen(color);
92
93 painter->drawText(option.rect, Qt::AlignVCenter | Qt::AlignLeft,
94 metrics.elidedText(category, Qt::ElideRight, option.rect.width()));
95
96 painter->restore();
97 }
98
99 int KCategoryDrawer::categoryHeight(const QStyleOption &option) const
100 {
101 return option.fontMetrics.height() + 6 /* 4 separator; 2 gradient */;
102 }