2 Copyright 2007 Robert Knight <robertknight@gmail.com>
3 Copyright 2007 Kevin Ottens <ervin@kde.org>
4 Copyright 2007 Alexis Menard <darktears31@gmail.com>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
23 #include "itemdelegate.h"
26 #include <QApplication>
27 #include <QFontMetrics>
29 #include <QModelIndex>
31 #include <QStyleOptionViewItem>
34 #include <KColorUtils>
37 #include <KGlobalSettings>
39 using namespace Notifier
;
41 ItemDelegate::ItemDelegate()
45 void ItemDelegate::paint(QPainter
*painter
, const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const
47 const bool hover
= option
.state
& (QStyle::State_Selected
|QStyle::State_MouseOver
|QStyle::State_HasFocus
);
48 QRect contentRect
= option
.rect
;
50 QRect decorationRect
= QStyle::alignedRect(option
.direction
,
51 option
.decorationPosition
== QStyleOptionViewItem::Left
? Qt::AlignLeft
: Qt::AlignRight
,
52 option
.decorationSize
,
54 QSize
textSize(option
.rect
.width() - decorationRect
.width() - ICON_TEXT_MARGIN
,
55 option
.rect
.height() - 2);
57 Qt::Alignment textAlignment
= option
.decorationAlignment
& Qt::AlignRight
? Qt::AlignLeft
: Qt::AlignRight
;
59 QRect textRect
= QStyle::alignedRect(option
.direction
, textAlignment
, textSize
, contentRect
.adjusted(0, 2, 0, 0));
60 QString titleText
= index
.data(Qt::DisplayRole
).value
<QString
>();
61 QString subTitleText
= index
.data(ActionRole
).value
<QString
>();
63 if (subTitleText
.isEmpty()) {
67 QFont subTitleFont
= fontForSubTitle(option
.font
);
69 QFont
titleFont(option
.font
);
71 QFontMetrics
titleMetrics(titleFont
);
72 QFontMetrics
subTitleMetrics(subTitleFont
);
73 QRectF textAreaRect
= contentRect
;
74 qreal actualTextWidth
= qMax(titleMetrics
.width(titleText
), subTitleMetrics
.width(subTitleText
));
75 textAreaRect
.adjust(decorationRect
.width() + ICON_TEXT_MARGIN
- 3,
77 (-(textRect
.width() - actualTextWidth
) + 3),
82 painter
->setPen(Qt::NoPen
);
83 QColor backgroundColor
= option
.palette
.color(QPalette::Highlight
);
84 // use a slightly translucent version of the palette's highlight color
86 backgroundColor
.setAlphaF(0.5);
87 painter
->setBrush(QBrush(backgroundColor
));
88 painter
->drawPath(roundedRectangle(textAreaRect
, 5));
93 QIcon decorationIcon
= index
.data(Qt::DecorationRole
).value
<QIcon
>();
97 painter
->setOpacity(0.7);
100 decorationIcon
.paint(painter
, decorationRect
, option
.decorationAlignment
);
109 painter
->setFont(titleFont
);
111 textAreaRect
.setHeight(textAreaRect
.height()/2);
113 painter
->drawText(textAreaRect
, Qt::AlignLeft
|Qt::AlignVCenter
, titleText
);
116 painter
->setFont(subTitleFont
);
119 painter
->setPen(QPen(Qt::gray
));
121 textAreaRect
.translate(0, textAreaRect
.height());
123 painter
->drawText(textAreaRect
, Qt::AlignLeft
|Qt::AlignVCenter
, subTitleText
);
129 QFont
ItemDelegate::fontForSubTitle(const QFont
&titleFont
) const
131 QFont subTitleFont
= titleFont
;
132 subTitleFont
.setPointSize(qMax(subTitleFont
.pointSize() - 2,
133 KGlobalSettings::smallestReadableFont().pointSize()));
137 QSize
ItemDelegate::sizeHint(const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const
140 QFontMetrics
metrics(option
.font
);
142 QFont subTitleFont
= option
.font
;
143 subTitleFont
.setPointSize(qMax(subTitleFont
.pointSize() - 2,
144 KGlobalSettings::smallestReadableFont().pointSize()));
145 QFontMetrics
subMetrics(subTitleFont
);
147 int height
= qMax(option
.decorationSize
.height(), metrics
.height() + subMetrics
.ascent() + 3);
149 QString titleText
= index
.data(Qt::DisplayRole
).value
<QString
>();
150 QString subTitleText
= index
.data(ActionRole
).value
<QString
>();
152 int width
= qMax(metrics
.width(titleText
), subMetrics
.width(subTitleText
));
153 width
+= option
.decorationSize
.width() + ICON_TEXT_MARGIN
;
155 return QSize(width
, height
);
158 // Taken from kdelibs/kio/kio/kfileitemdelegate.cpp
159 QPainterPath
ItemDelegate::roundedRectangle(const QRectF
&rect
, qreal radius
) const
161 QPainterPath
path(QPointF(rect
.left(), rect
.top() + radius
));
162 path
.quadTo(rect
.left(), rect
.top(), rect
.left() + radius
, rect
.top()); // Top left corner
163 path
.lineTo(rect
.right() - radius
, rect
.top()); // Top side
164 path
.quadTo(rect
.right(), rect
.top(), rect
.right(), rect
.top() + radius
); // Top right corner
165 path
.lineTo(rect
.right(), rect
.bottom() - radius
); // Right side
166 path
.quadTo(rect
.right(), rect
.bottom(), rect
.right() - radius
, rect
.bottom()); // Bottom right corner
167 path
.lineTo(rect
.left() + radius
, rect
.bottom()); // Bottom side
168 path
.quadTo(rect
.left(), rect
.bottom(), rect
.left(), rect
.bottom() - radius
); // Bottom left corner