2 * Copyright © 2006-2007 Fredrik Höglund <fredrik@kde.org>
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.
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.
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.
20 #include "itemdelegate.h"
21 #include "cursortheme.h"
23 #include <QApplication>
24 #include <QModelIndex>
29 const int decorationMargin
= 8;
33 ItemDelegate::ItemDelegate(QObject
*parent
)
34 : QAbstractItemDelegate(parent
)
39 ItemDelegate::~ItemDelegate()
44 QString
ItemDelegate::firstLine(const QModelIndex
&index
) const
47 return index
.model()->data(index
, Qt::DisplayRole
).toString();
53 QString
ItemDelegate::secondLine(const QModelIndex
&index
) const
56 return index
.model()->data(index
, CursorTheme::DisplayDetailRole
).toString();
62 QPixmap
ItemDelegate::decoration(const QModelIndex
&index
) const
65 return qvariant_cast
<QPixmap
>(index
.model()->data(index
, Qt::DecorationRole
));
71 QSize
ItemDelegate::sizeHint(const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const
76 QFont normalfont
= option
.font
;
77 QFont boldfont
= normalfont
;
78 boldfont
.setBold(true);
80 // Extract the items we want to measure
81 QString firstRow
= firstLine(index
);
82 QString secondRow
= secondLine(index
);
85 QFontMetrics
fm1(boldfont
);
86 QFontMetrics
fm2(normalfont
);
87 int height
= fm1
.lineSpacing() + fm2
.lineSpacing();
88 height
= qMax(height
, option
.decorationSize
.height());
90 // Compute the text width
91 int width
= fm1
.width(firstRow
);
92 width
= qMax(width
, fm2
.width(secondRow
));
94 // Add decoration width + margin
95 width
+= option
.decorationSize
.width() + decorationMargin
;
97 return QSize(width
, height
+ 16);
101 QPalette::ColorRole
ItemDelegate::foregroundRole(const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const
105 if (option
.state
& QStyle::State_Selected
)
106 return QPalette::HighlightedText
;
108 return QPalette::Text
;
112 void ItemDelegate::paint(QPainter
*painter
, const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const
114 if (!index
.isValid())
119 QFont normalfont
= option
.font
;
120 QFont boldfont
= normalfont
;
121 boldfont
.setBold(true);
123 QString firstRow
= firstLine(index
);
124 QString secondRow
= secondLine(index
);
125 QPixmap pixmap
= decoration(index
);
127 QColor textcol
= option
.palette
.color(foregroundRole(option
, index
));
129 // Draw the background
130 QStyleOptionViewItemV4 opt
= option
;
131 QStyle
*style
= opt
.widget
? opt
.widget
->style() : QApplication::style();
132 style
->drawPrimitive(QStyle::PE_PanelItemViewItem
, &opt
, painter
, opt
.widget
);
135 int x
= option
.rect
.left() + (option
.decorationSize
.width() - pixmap
.width() + decorationMargin
) / 2;
136 int y
= option
.rect
.top() + (option
.rect
.height() - pixmap
.height()) / 2;
138 QRect pixmapRect
= QStyle::visualRect(option
.direction
, option
.rect
,
139 QRect(x
, y
, pixmap
.width(), pixmap
.height()));
141 painter
->drawPixmap(pixmapRect
.x(), pixmapRect
.y(), pixmap
);
144 QFontMetrics
fm1(boldfont
);
145 QFontMetrics
fm2(normalfont
);
147 int textAreaHeight
= fm1
.lineSpacing() + fm2
.lineSpacing();
149 x
= option
.rect
.left() + option
.decorationSize
.width() + decorationMargin
;
150 int y1
= option
.rect
.top() + (option
.rect
.height() - textAreaHeight
) / 2;
151 int y2
= y1
+ fm1
.lineSpacing();
153 QRect firstRowRect
= QStyle::visualRect(option
.direction
, option
.rect
,
154 QRect(x
, y1
, fm1
.width(firstRow
), fm1
.lineSpacing()));
156 QRect secondRowRect
= QStyle::visualRect(option
.direction
, option
.rect
,
157 QRect(x
, y2
, fm2
.width(secondRow
), fm2
.lineSpacing()));
159 painter
->setPen(textcol
);
162 painter
->setFont(boldfont
);
163 painter
->drawText(firstRowRect
, Qt::AlignCenter
, firstRow
);
166 painter
->setFont(normalfont
);
167 painter
->drawText(secondRowRect
, Qt::AlignCenter
, secondRow
);