]> cloud.milkyroute.net Git - dolphin.git/blob - src/kcategorydrawer.cpp
Icon naming spec compliance (code changes - KDE/):
[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 <kiconloader.h>
27 #include <kcategorizedsortfilterproxymodel.h>
28
29 KCategoryDrawer::KCategoryDrawer()
30 {
31 }
32
33 KCategoryDrawer::~KCategoryDrawer()
34 {
35 }
36
37 void KCategoryDrawer::drawCategory(const QModelIndex &index,
38 int sortRole,
39 const QStyleOption &option,
40 QPainter *painter) const
41 {
42 const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
43
44 QColor color;
45
46 if (option.state & QStyle::State_Selected)
47 {
48 color = option.palette.color(QPalette::HighlightedText);
49 }
50 else
51 {
52 color = option.palette.color(QPalette::Text);
53 }
54
55 painter->save();
56 painter->setRenderHint(QPainter::Antialiasing);
57
58 QStyleOptionButton opt;
59
60 opt.rect = option.rect;
61 opt.palette = option.palette;
62 opt.direction = option.direction;
63 opt.text = category;
64
65 int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
66
67 if (option.direction == Qt::LeftToRight)
68 {
69 opt.rect.setLeft(opt.rect.left() + (iconSize / 4));
70 }
71 else
72 {
73 opt.rect.setRight(opt.rect.width() - (iconSize / 4));
74 }
75
76 if (option.state & QStyle::State_Selected)
77 {
78 QColor selected = option.palette.color(QPalette::Highlight);
79
80 QLinearGradient gradient(option.rect.topLeft(),
81 option.rect.bottomRight());
82 gradient.setColorAt(option.direction == Qt::LeftToRight ? 0
83 : 1, selected);
84 gradient.setColorAt(option.direction == Qt::LeftToRight ? 1
85 : 0, Qt::transparent);
86
87 painter->fillRect(option.rect, gradient);
88 }
89 else if (option.state & QStyle::State_MouseOver)
90 {
91 QColor hover = option.palette.color(QPalette::Highlight).light();
92 hover.setAlpha(88);
93
94 QLinearGradient gradient(option.rect.topLeft(),
95 option.rect.bottomRight());
96 gradient.setColorAt(option.direction == Qt::LeftToRight ? 0
97 : 1, hover);
98 gradient.setColorAt(option.direction == Qt::LeftToRight ? 1
99 : 0, Qt::transparent);
100
101 painter->fillRect(option.rect, gradient);
102 }
103
104 QFont painterFont = painter->font();
105 painterFont.setWeight(QFont::Bold);
106 painterFont.setPointSize(painterFont.pointSize() + 2);
107 QFontMetrics metrics(painterFont);
108 painter->setFont(painterFont);
109
110 QPainterPath path;
111 path.addRect(option.rect.left(),
112 option.rect.bottom() - 2,
113 option.rect.width(),
114 2);
115
116 QLinearGradient gradient(option.rect.topLeft(),
117 option.rect.bottomRight());
118 gradient.setColorAt(option.direction == Qt::LeftToRight ? 0
119 : 1, color);
120 gradient.setColorAt(option.direction == Qt::LeftToRight ? 1
121 : 0, Qt::transparent);
122
123 painter->setBrush(gradient);
124 painter->fillPath(path, gradient);
125
126 painter->setPen(color);
127
128 painter->drawText(opt.rect, Qt::AlignVCenter | Qt::AlignLeft,
129 metrics.elidedText(category, Qt::ElideRight, option.rect.width()));
130
131 painter->restore();
132 }
133
134 int KCategoryDrawer::categoryHeight(const QStyleOption &option) const
135 {
136 return option.fontMetrics.height() + 6 /* 4 separator; 2 gradient */;
137 }