1 #include "PrettyItemDelegate.h"
2 #include "../ListModel.h"
4 #include <QFontMetricsF>
7 const qreal PrettyItemDelegate::THUMB_HEIGHT = 90.0;
8 const qreal PrettyItemDelegate::THUMB_WIDTH = 120.0;
9 const qreal PrettyItemDelegate::PADDING = 10.0;
11 PrettyItemDelegate::PrettyItemDelegate( QObject* parent ) : QStyledItemDelegate( parent ) {
15 void PrettyItemDelegate::createPlayIcon() {
16 playIcon = QPixmap(THUMB_WIDTH, THUMB_HEIGHT);
17 playIcon.fill(Qt::transparent);
18 QPainter painter(&playIcon);
20 polygon << QPoint(PADDING*4, PADDING*2)
21 << QPoint(THUMB_WIDTH-PADDING*4, THUMB_HEIGHT/2)
22 << QPoint(PADDING*4, THUMB_HEIGHT-PADDING*2);
23 painter.setRenderHints(QPainter::Antialiasing, true);
24 painter.setBrush(Qt::white);
26 pen.setColor(Qt::white);
27 pen.setWidth(PADDING);
28 pen.setJoinStyle(Qt::RoundJoin);
29 pen.setCapStyle(Qt::RoundCap);
31 painter.drawPolygon(polygon);
34 PrettyItemDelegate::~PrettyItemDelegate() { }
36 QSize PrettyItemDelegate::sizeHint( const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const {
37 return QSize( 256, THUMB_HEIGHT+1.0);
40 void PrettyItemDelegate::paint( QPainter* painter,
41 const QStyleOptionViewItem& option, const QModelIndex& index ) const {
43 int itemType = index.data(ItemTypeRole).toInt();
44 if (itemType == ItemTypeVideo) {
45 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
46 paintBody( painter, option, index );
48 QStyledItemDelegate::paint( painter, option, index );
52 void PrettyItemDelegate::paintBody( QPainter* painter,
53 const QStyleOptionViewItem& option,
54 const QModelIndex& index ) const {
57 painter->translate( option.rect.topLeft() );
59 const QRectF line(0, 0, option.rect.width(), option.rect.height());
64 boldFont.setBold(true);
66 smallerFont.setPointSize(smallerFont.pointSize()*.85);
67 QFont smallerBoldFont;
68 smallerBoldFont.setBold(true);
69 smallerBoldFont.setPointSize(smallerBoldFont.pointSize()*.85);
71 const bool isActive = index.data( ActiveTrackRole ).toBool();
72 const bool isSelected = option.state & QStyle::State_Selected;
74 // draw the "current track" highlight underneath the text
75 if (isActive && !isSelected) {
76 paintActiveOverlay(painter, line.x(), line.y(), line.width(), line.height());
79 // get the video metadata
80 const VideoPointer videoPointer = index.data( VideoRole ).value<VideoPointer>();
81 const Video *video = videoPointer.data();
84 if (!video->thumbnail().isNull()) {
85 painter->drawImage(QRect(0, 0, THUMB_WIDTH, THUMB_HEIGHT), video->thumbnail());
87 // play icon overlayed on the thumb
89 paintPlayIcon(painter);
94 int duration = video->duration();
95 if ( duration > 3600 )
96 timeString = QTime().addSecs(duration).toString("h:mm:ss");
98 timeString = QTime().addSecs(duration).toString("m:ss");
99 drawTime(painter, timeString, line);
101 if (isActive) painter->setFont(boldFont);
102 const QFontMetricsF fm(painter->font());
103 const QFontMetricsF boldMetrics(boldFont);
107 painter->setPen(QPen(option.palette.brush(QPalette::HighlightedText), 0));
109 painter->setPen(QPen(option.palette.brush(QPalette::Text), 0));
112 QString videoTitle = video->title();
113 QRectF textBox = line.adjusted(PADDING+THUMB_WIDTH, PADDING, -2 * PADDING, -PADDING);
114 textBox = painter->boundingRect( textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, videoTitle);
115 painter->drawText(textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, videoTitle);
117 painter->setFont(smallerFont);
120 QString publishedString = video->published().date().toString(Qt::DefaultLocaleShortDate);
121 QSizeF publishedStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, publishedString ) );
122 QPointF textLoc(PADDING+THUMB_WIDTH, PADDING*2 + textBox.height());
123 QRectF publishedTextBox( textLoc , publishedStringSize);
124 painter->drawText(publishedTextBox, Qt::AlignLeft | Qt::AlignTop, publishedString);
128 painter->setFont(smallerBoldFont);
129 if (!isSelected && !isActive)
130 painter->setPen(QPen(option.palette.brush(QPalette::Mid), 0));
131 QString authorString = video->author();
132 QSizeF authorStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, authorString ) );
133 textLoc.setX(textLoc.x() + publishedStringSize.width() + PADDING);
134 QRectF authorTextBox( textLoc , authorStringSize);
135 painter->drawText(authorTextBox, Qt::AlignLeft | Qt::AlignTop, authorString);
139 if (video->viewCount() >= 0) {
142 QString viewCountString = tr("%1 views").arg(locale.toString(video->viewCount()));
143 QSizeF viewCountStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, viewCountString ) );
144 textLoc.setX(textLoc.x() + authorStringSize.width() + PADDING);
145 QRectF viewCountTextBox( textLoc , viewCountStringSize);
146 painter->drawText(viewCountTextBox, Qt::AlignLeft | Qt::AlignBottom, viewCountString);
151 QLinearGradient myGradient;
154 QPointF baseline(authorTextBox.x(), authorTextBox.y() + authorTextBox.height());
156 myPath.addText(baseline, boldFont, authorString);
157 painter->setBrush(palette.color(QPalette::WindowText));
158 painter->setPen(palette.color(QPalette::Dark));
159 painter->setRenderHints (QPainter::Antialiasing, true);
160 painter->drawPath(myPath);
164 painter->setPen(palette.color(QPalette::Midlight));
165 painter->drawLine(0, THUMB_HEIGHT, line.width(), THUMB_HEIGHT);
171 QPointF PrettyItemDelegate::centerImage( const QPixmap& pixmap, const QRectF& rect ) const {
172 qreal pixmapRatio = ( qreal )pixmap.width() / ( qreal )pixmap.height();
177 if ( pixmapRatio >= 1 )
178 moveByY = ( rect.height() - ( rect.width() / pixmapRatio ) ) / 2.0;
180 moveByX = ( rect.width() - ( rect.height() * pixmapRatio ) ) / 2.0;
182 return QPointF( moveByX, moveByY );
185 void PrettyItemDelegate::paintActiveOverlay( QPainter *painter, qreal x, qreal y, qreal w, qreal h ) const {
188 QColor highlightColor = palette.color(QPalette::Highlight);
189 QColor backgroundColor = palette.color(QPalette::Base);
190 const float animation = 0.25;
191 const int gradientRange = 16;
193 QColor color2 = QColor::fromHsv(
194 highlightColor.hue(),
195 (int) (backgroundColor.saturation() * (1.0f - animation) + highlightColor.saturation() * animation),
196 (int) (backgroundColor.value() * (1.0f - animation) + highlightColor.value() * animation)
198 QColor color1 = QColor::fromHsv(
200 qMax(color2.saturation() - gradientRange, 0),
201 qMin(color2.value() + gradientRange, 255)
203 QRect rect((int) x, (int) y, (int) w, (int) h);
205 painter->setPen(Qt::NoPen);
206 QLinearGradient linearGradient(0, 0, 0, rect.height());
207 linearGradient.setColorAt(0.0, color1);
208 linearGradient.setColorAt(1.0, color2);
209 painter->setBrush(linearGradient);
210 painter->drawRect(rect);
214 void PrettyItemDelegate::paintPlayIcon(QPainter *painter) const {
216 painter->setOpacity(.5);
217 painter->drawPixmap(playIcon.rect(), playIcon);
221 void PrettyItemDelegate::drawTime(QPainter *painter, QString time, QRectF line) const {
222 static const int timePadding = 4;
223 QRectF textBox = painter->boundingRect(line, Qt::AlignLeft | Qt::AlignTop, time);
225 textBox.adjust(0, 0, timePadding, 0);
226 // move to bottom right corner of the thumb
227 textBox.translate(THUMB_WIDTH - textBox.width(), THUMB_HEIGHT - textBox.height());
230 painter->setPen(Qt::NoPen);
231 painter->setBrush(Qt::black);
232 painter->setOpacity(.5);
233 painter->drawRect(textBox);
237 painter->setPen(Qt::white);
238 painter->drawText(textBox, Qt::AlignCenter, time);