]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kcategorydrawer.cpp
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
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.
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.
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.
21 #include "kcategorydrawer.h"
24 #include <QStyleOption>
26 #include <kiconloader.h>
27 #include <kcategorizedsortfilterproxymodel.h>
29 KCategoryDrawer::KCategoryDrawer()
33 KCategoryDrawer::~KCategoryDrawer()
37 void KCategoryDrawer::drawCategory(const QModelIndex
&index
,
39 const QStyleOption
&option
,
40 QPainter
*painter
) const
42 const QString category
= index
.model()->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
46 if (option
.state
& QStyle::State_Selected
)
48 color
= option
.palette
.color(QPalette::HighlightedText
);
52 color
= option
.palette
.color(QPalette::Text
);
56 painter
->setRenderHint(QPainter::Antialiasing
);
58 if (option
.state
& QStyle::State_Selected
)
60 QColor selected
= option
.palette
.color(QPalette::Highlight
);
62 QLinearGradient
gradient(option
.rect
.topLeft(),
63 option
.rect
.bottomRight());
64 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
66 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
67 : 0, Qt::transparent
);
69 painter
->fillRect(option
.rect
, gradient
);
71 else if (option
.state
& QStyle::State_MouseOver
)
73 QColor hover
= option
.palette
.color(QPalette::Highlight
).light();
76 QLinearGradient
gradient(option
.rect
.topLeft(),
77 option
.rect
.bottomRight());
78 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
80 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
81 : 0, Qt::transparent
);
83 painter
->fillRect(option
.rect
, gradient
);
86 QFont painterFont
= painter
->font();
87 painterFont
.setWeight(QFont::Bold
);
88 QFontMetrics
metrics(painterFont
);
89 painter
->setFont(painterFont
);
91 QRect
lineRect(option
.rect
.left(),
92 option
.rect
.bottom() - 1,
96 QLinearGradient
gradient(option
.rect
.topLeft(),
97 option
.rect
.bottomRight());
98 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
100 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
101 : 0, Qt::transparent
);
103 painter
->fillRect(lineRect
, gradient
);
105 painter
->setPen(color
);
107 painter
->drawText(option
.rect
, Qt::AlignVCenter
| Qt::AlignLeft
,
108 metrics
.elidedText(category
, Qt::ElideRight
, option
.rect
.width()));
113 int KCategoryDrawer::categoryHeight(const QModelIndex
&index
, const QStyleOption
&option
) const
117 return option
.fontMetrics
.height() + 4 /* 3 separator; 1 gradient */;