1 /* Copyright © 2006-2007 Fredrik Höglund <fredrik@kde.org>
2 * (c)GPL2 (c)GPL3
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License version 2 or at your option version 3 as published
7 * by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 /*
20 * additional code: Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
21 */
22 #include "itemdelegate.h"
23 #include "crtheme.h"
24
25 #include <QApplication>
26 #include <QModelIndex>
27 #include <QPainter>
28
29
30 namespace {
31 const int decorationMargin = 8;
32 }
33
34
35 ///////////////////////////////////////////////////////////////////////////////
ItemDelegate(QObject * parent)36 ItemDelegate::ItemDelegate(QObject *parent) : QAbstractItemDelegate(parent)
37 {
38 }
39
40
~ItemDelegate()41 ItemDelegate::~ItemDelegate()
42 {
43 }
44
firstLine(const QModelIndex & index) const45 QString ItemDelegate::firstLine(const QModelIndex &index) const
46 {
47 if (index.isValid()) return index.model()->data(index, Qt::DisplayRole).toString();
48 return QString();
49 }
50
secondLine(const QModelIndex & index) const51 QString ItemDelegate::secondLine(const QModelIndex &index) const
52 {
53 if (index.isValid()) return index.model()->data(index, XCursorThemeData::DisplayDetailRole).toString();
54 return QString();
55 }
56
decoration(const QModelIndex & index) const57 QPixmap ItemDelegate::decoration(const QModelIndex &index) const
58 {
59 if (index.isValid()) return qvariant_cast<QPixmap>(index.model()->data(index, Qt::DecorationRole));
60 return QPixmap();
61 }
62
sizeHint(const QStyleOptionViewItem & option,const QModelIndex & index) const63 QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
64 {
65 if (!index.isValid()) return QSize();
66
67 QFont normalfont = option.font;
68 QFont boldfont = normalfont;
69 boldfont.setBold(true);
70 // Extract the items we want to measure
71 QString firstRow = firstLine(index);
72 QString secondRow = secondLine(index);
73 // Compute the height
74 QFontMetrics fm1(boldfont);
75 QFontMetrics fm2(normalfont);
76 int height = fm1.lineSpacing() + fm2.lineSpacing();
77 height = qMax(height, option.decorationSize.height());
78 // Compute the text width
79 int width = fm1.horizontalAdvance(firstRow);
80 width = qMax(width, fm2.horizontalAdvance(secondRow));
81 // Add decoration width + margin
82 width += option.decorationSize.width()+decorationMargin;
83 return QSize(width, height+16);
84 }
85
foregroundRole(const QStyleOptionViewItem & option,const QModelIndex & index) const86 QPalette::ColorRole ItemDelegate::foregroundRole(const QStyleOptionViewItem &option, const QModelIndex &index) const
87 {
88 Q_UNUSED(index)
89 if (option.state & QStyle::State_Selected) return QPalette::HighlightedText;
90 return QPalette::Text;
91 }
92
paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const93 void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
94 {
95 if (!index.isValid()) return;
96 painter->save();
97
98 QFont normalfont = option.font;
99 QFont boldfont = normalfont;
100 boldfont.setBold(true);
101
102 QString firstRow = firstLine(index);
103 QString secondRow = secondLine(index);
104 QPixmap pixmap = decoration(index);
105
106 QColor textcol = option.palette.color(foregroundRole(option, index));
107
108 // Draw the background
109 QStyleOptionViewItem opt = option;
110 QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
111 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
112
113 // Draw the icon
114 int x = option.rect.left()+(option.decorationSize.width()-pixmap.width()+decorationMargin)/2;
115 int y = option.rect.top()+(option.rect.height()-pixmap.height())/2;
116 QRect pixmapRect = QStyle::visualRect(option.direction, option.rect, QRect(x, y, pixmap.width(), pixmap.height()));
117 painter->drawPixmap(pixmapRect.x(), pixmapRect.y(), pixmap);
118
119 // Draw the text
120 QFontMetrics fm1(boldfont);
121 QFontMetrics fm2(normalfont);
122
123 int textAreaHeight = fm1.lineSpacing()+fm2.lineSpacing();
124
125 x = option.rect.left()+option.decorationSize.width()+decorationMargin;
126 int y1 = option.rect.top()+(option.rect.height()-textAreaHeight)/2;
127 int y2 = y1+fm1.lineSpacing();
128 QRect firstRowRect = QStyle::visualRect(option.direction, option.rect, QRect(x, y1, fm1.horizontalAdvance(firstRow), fm1.lineSpacing()));
129 QRect secondRowRect = QStyle::visualRect(option.direction, option.rect, QRect(x, y2, fm2.horizontalAdvance(secondRow), fm2.lineSpacing()));
130 painter->setPen(textcol);
131
132 // First line
133 painter->setFont(boldfont);
134 painter->drawText(firstRowRect, Qt::AlignCenter, firstRow);
135
136 // Second line
137 painter->setFont(normalfont);
138 painter->drawText(secondRowRect, Qt::AlignCenter, secondRow);
139
140 painter->restore();
141 }
142