2
* This file is part of the KDE project
3
* Copyright (C) 2007, 2006 Rafael Fernández López <ereslibre@gmail.com>
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 version 2 as published by the Free Software Foundation.
9
* This library 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
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public License
15
* along with this library; see the file COPYING.LIB. If not, write to
16
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#include "appletitemdelegate.h"
22
class AppletItemDelegate::Private
25
QString title(const QModelIndex &index) const;
26
QString description(const QModelIndex &index) const;
27
QPixmap icon(const QModelIndex &index, int width, int height) const;
28
int calculateCenter(const QRect &rect, int pixmapHeight) const;
38
QString AppletItemDelegate::Private::title(const QModelIndex &index) const
40
return index.model()->data(index, Qt::DisplayRole).toString();
43
QString AppletItemDelegate::Private::description(const QModelIndex &index) const
45
return index.model()->data(index, SecondaryDisplayRole).toString();
48
QPixmap AppletItemDelegate::Private::icon(const QModelIndex &index, int width, int height) const
50
QVariant icon = index.model()->data(index, Qt::DecorationRole);
52
return icon.value<QIcon>().pixmap(width, height);
55
int AppletItemDelegate::Private::calculateCenter(const QRect &rect, int pixmapHeight) const
57
return (rect.height() / 2) - (pixmapHeight / 2);
60
AppletItemDelegate::AppletItemDelegate(QObject *parent)
61
: QItemDelegate(parent)
66
AppletItemDelegate::~AppletItemDelegate()
71
void AppletItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
73
QFontMetrics fontMetrics = painter->fontMetrics();
75
QColor unselectedTextColor = option.palette.text().color();
76
QColor selectedTextColor = option.palette.highlightedText().color();
77
QPen currentPen = painter->pen();
78
QPen unselectedPen = QPen(currentPen);
79
QPen selectedPen = QPen(currentPen);
81
unselectedPen.setColor(unselectedTextColor);
82
selectedPen.setColor(selectedTextColor);
84
if (option.state & QStyle::State_Selected)
86
painter->fillRect(option.rect, option.palette.highlight());
87
painter->setPen(selectedPen);
91
painter->setPen(unselectedPen);
94
QPixmap iconPixmap = d->icon(index, d->iconWidth, d->iconHeight);
96
QFont title(painter->font());
97
QFont previousFont(painter->font());
99
title.setPointSize(title.pointSize() + 2);
100
title.setWeight(QFont::Bold);
104
painter->setRenderHint(QPainter::Antialiasing, true);
106
painter->setFont(title);
108
QString display = painter->fontMetrics().elidedText(d->title(index), Qt::ElideRight, option.rect.width() - d->leftMargin - d->rightMargin - iconPixmap.width() - d->separatorPixels);
110
QString secondaryDisplay = fontMetrics.elidedText(d->description(index), Qt::ElideRight, option.rect.width() - d->leftMargin - d->rightMargin - iconPixmap.width() - d->separatorPixels);
112
painter->drawText(d->leftMargin + d->separatorPixels + iconPixmap.width(), d->separatorPixels + option.rect.top(), painter->fontMetrics().width(display), painter->fontMetrics().height(), Qt::AlignLeft, display);
114
painter->setFont(previousFont);
116
painter->drawText(d->leftMargin + d->separatorPixels + iconPixmap.width(), option.rect.height() - d->separatorPixels - fontMetrics.height() + option.rect.top(), painter->fontMetrics().width(secondaryDisplay), painter->fontMetrics().height(), Qt::AlignLeft, secondaryDisplay);
118
painter->drawPixmap(d->leftMargin, d->calculateCenter(option.rect, iconPixmap.height()) + option.rect.top(), iconPixmap);
124
QSize AppletItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
126
int itemWidth = qMax(option.rect.width(), d->minimumItemWidth);
127
int itemHeight = (d->separatorPixels * 2) + d->iconHeight;
129
return QSize(itemWidth, itemHeight);
132
void AppletItemDelegate::setIconSize(int width, int height)
134
d->iconWidth = width;
135
d->iconHeight = height;
138
void AppletItemDelegate::setMinimumItemWidth(int minimumItemWidth)
140
d->minimumItemWidth = minimumItemWidth;
143
void AppletItemDelegate::setLeftMargin(int leftMargin)
145
d->leftMargin = leftMargin;
148
void AppletItemDelegate::setRightMargin(int rightMargin)
150
d->rightMargin = rightMargin;
153
void AppletItemDelegate::setSeparatorPixels(int separatorPixels)
155
d->separatorPixels = separatorPixels;
158
#include "appletitemdelegate.moc"