]> cloud.milkyroute.net Git - dolphin.git/blob - src/kcategorydrawer.cpp
Fix the problem of the tree view. When we are expanding too many nodes (or very large...
[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@kde.org>
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::CategoryDisplayRole).toString();
42
43 QColor color;
44
45 if (option.state & QStyle::State_Selected)
46 {
47 color = option.palette.color(QPalette::HighlightedText);
48 }
49 else
50 {
51 color = option.palette.color(QPalette::Text);
52 }
53
54 painter->save();
55 painter->setRenderHint(QPainter::Antialiasing);
56
57 QStyleOptionButton opt;
58
59 opt.rect = option.rect;
60 opt.palette = option.palette;
61 opt.direction = option.direction;
62 opt.text = category;
63
64 if (option.state & QStyle::State_Selected)
65 {
66 QColor selected = option.palette.color(QPalette::Highlight);
67
68 QLinearGradient gradient(option.rect.topLeft(),
69 option.rect.bottomRight());
70 gradient.setColorAt(option.direction == Qt::LeftToRight ? 0
71 : 1, selected);
72 gradient.setColorAt(option.direction == Qt::LeftToRight ? 1
73 : 0, Qt::transparent);
74
75 painter->fillRect(option.rect, gradient);
76 }
77 else if (option.state & QStyle::State_MouseOver)
78 {
79 QColor hover = option.palette.color(QPalette::Highlight).light();
80 hover.setAlpha(88);
81
82 QLinearGradient gradient(option.rect.topLeft(),
83 option.rect.bottomRight());
84 gradient.setColorAt(option.direction == Qt::LeftToRight ? 0
85 : 1, hover);
86 gradient.setColorAt(option.direction == Qt::LeftToRight ? 1
87 : 0, Qt::transparent);
88
89 painter->fillRect(option.rect, gradient);
90 }
91
92 QFont painterFont = painter->font();
93 painterFont.setWeight(QFont::Bold);
94 painterFont.setPointSize(painterFont.pointSize() + 2);
95 QFontMetrics metrics(painterFont);
96 painter->setFont(painterFont);
97
98 QPainterPath path;
99 path.addRect(option.rect.left(),
100 option.rect.bottom() - 2,
101 option.rect.width(),
102 2);
103
104 QLinearGradient gradient(option.rect.topLeft(),
105 option.rect.bottomRight());
106 gradient.setColorAt(option.direction == Qt::LeftToRight ? 0
107 : 1, color);
108 gradient.setColorAt(option.direction == Qt::LeftToRight ? 1
109 : 0, Qt::transparent);
110
111 painter->setBrush(gradient);
112 painter->fillPath(path, gradient);
113
114 painter->setPen(color);
115
116 painter->drawText(option.rect, Qt::AlignVCenter | Qt::AlignLeft,
117 metrics.elidedText(category, Qt::ElideRight, option.rect.width()));
118
119 painter->restore();
120 }
121
122 int KCategoryDrawer::categoryHeight(const QStyleOption &option) const
123 {
124 return option.fontMetrics.height() + 6 /* 4 separator; 2 gradient */;
125 }