1 #include "PrettyItemDelegate.h"
2 #include "../ListModel.h"
3 #include "../fontutils.h"
4 #include "../downloaditem.h"
5 #include "../iconloader/qticonloader.h"
7 #include <QFontMetricsF>
11 const qreal PrettyItemDelegate::THUMB_HEIGHT = 90.0;
12 const qreal PrettyItemDelegate::THUMB_WIDTH = 120.0;
13 const qreal PrettyItemDelegate::PADDING = 10.0;
16 QHash<int, QRect> authorRects;
18 PrettyItemDelegate::PrettyItemDelegate(QObject* parent, bool downloadInfo)
19 : QStyledItemDelegate(parent),
20 downloadInfo(downloadInfo) {
21 boldFont.setBold(true);
22 smallerBoldFont = FontUtils::smallBold();
23 smallerFont = FontUtils::small();
26 progressBar = new QProgressBar(qApp->activeWindow());
27 QPalette palette = progressBar->palette();
28 palette.setColor(QPalette::Window, Qt::transparent);
29 progressBar->setPalette(palette);
30 progressBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
32 } else createPlayIcon();
35 void PrettyItemDelegate::createPlayIcon() {
36 playIcon = QPixmap(THUMB_WIDTH, THUMB_HEIGHT);
37 playIcon.fill(Qt::transparent);
38 QPainter painter(&playIcon);
40 polygon << QPoint(PADDING*4, PADDING*2)
41 << QPoint(THUMB_WIDTH-PADDING*4, THUMB_HEIGHT/2)
42 << QPoint(PADDING*4, THUMB_HEIGHT-PADDING*2);
43 painter.setRenderHints(QPainter::Antialiasing, true);
44 painter.setBrush(Qt::white);
46 pen.setColor(Qt::white);
47 pen.setWidth(PADDING);
48 pen.setJoinStyle(Qt::RoundJoin);
49 pen.setCapStyle(Qt::RoundCap);
51 painter.drawPolygon(polygon);
54 PrettyItemDelegate::~PrettyItemDelegate() { }
56 QSize PrettyItemDelegate::sizeHint( const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const {
57 return QSize( 256, THUMB_HEIGHT+1.0);
60 void PrettyItemDelegate::paint( QPainter* painter,
61 const QStyleOptionViewItem& option, const QModelIndex& index ) const {
63 int itemType = index.data(ItemTypeRole).toInt();
64 if (itemType == ItemTypeVideo) {
65 QStyleOptionViewItemV4 opt = QStyleOptionViewItemV4(option);
66 initStyleOption(&opt, index);
68 opt.widget->style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
69 paintBody(painter, opt, index);
71 QStyledItemDelegate::paint( painter, option, index );
75 void PrettyItemDelegate::paintBody( QPainter* painter,
76 const QStyleOptionViewItem& option,
77 const QModelIndex& index ) const {
80 painter->translate( option.rect.topLeft() );
83 QRectF line(0, 0, option.rect.width(), option.rect.height());
84 if (downloadInfo) line.setWidth(line.width() / 2);
85 painter->setClipRect(line);
87 const bool isActive = index.data( ActiveTrackRole ).toBool();
88 const bool isSelected = option.state & QStyle::State_Selected;
90 // draw the "current track" highlight underneath the text
91 if (isActive && !isSelected) {
92 paintActiveOverlay(painter, line.x(), line.y(), line.width(), line.height());
95 // get the video metadata
96 const VideoPointer videoPointer = index.data( VideoRole ).value<VideoPointer>();
97 const Video *video = videoPointer.data();
100 if (!video->thumbnail().isNull()) {
101 painter->drawImage(QRect(0, 0, THUMB_WIDTH, THUMB_HEIGHT), video->thumbnail());
103 // play icon overlayed on the thumb
105 paintPlayIcon(painter);
109 int duration = video->duration();
110 if ( duration > 3600 )
111 timeString = QTime().addSecs(duration).toString("h:mm:ss");
113 timeString = QTime().addSecs(duration).toString("m:ss");
114 drawTime(painter, timeString, line);
118 if (isActive) painter->setFont(boldFont);
122 painter->setPen(QPen(option.palette.brush(QPalette::HighlightedText), 0));
124 painter->setPen(QPen(option.palette.brush(QPalette::Text), 0));
127 QString videoTitle = video->title();
128 QRectF textBox = line.adjusted(PADDING+THUMB_WIDTH, PADDING, -2 * PADDING, -PADDING);
129 textBox = painter->boundingRect( textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, videoTitle);
130 painter->drawText(textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, videoTitle);
132 painter->setFont(smallerFont);
135 QString publishedString = video->published().date().toString(Qt::DefaultLocaleShortDate);
136 QSizeF publishedStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, publishedString ) );
137 QPointF textLoc(PADDING+THUMB_WIDTH, PADDING*2 + textBox.height());
138 QRectF publishedTextBox( textLoc , publishedStringSize);
139 painter->drawText(publishedTextBox, Qt::AlignLeft | Qt::AlignTop, publishedString);
142 bool authorHovered = false;
143 bool authorPressed = false;
144 const bool isHovered = index.data(HoveredItemRole).toBool();
146 authorHovered = index.data(AuthorHoveredRole).toBool();
147 authorPressed = index.data(AuthorPressedRole).toBool();
151 painter->setFont(smallerBoldFont);
154 painter->setPen(QPen(option.palette.brush(QPalette::Highlight), 0));
156 painter->setPen(QPen(option.palette.brush(QPalette::Mid), 0));
158 QString authorString = video->author();
159 QSizeF authorStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, authorString ) );
160 textLoc.setX(textLoc.x() + publishedStringSize.width() + PADDING);
161 QRectF authorTextBox( textLoc , authorStringSize);
162 authorRects.insert(index.row(), authorTextBox.toRect());
163 painter->drawText(authorTextBox, Qt::AlignLeft | Qt::AlignTop, authorString);
167 if (video->viewCount() >= 0) {
170 QString viewCountString = tr("%1 views").arg(locale.toString(video->viewCount()));
171 QSizeF viewCountStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, viewCountString ) );
172 textLoc.setX(textLoc.x() + authorStringSize.width() + PADDING);
173 QRectF viewCountTextBox( textLoc , viewCountStringSize);
174 painter->drawText(viewCountTextBox, Qt::AlignLeft | Qt::AlignBottom, viewCountString);
179 QLinearGradient myGradient;
182 QPointF baseline(authorTextBox.x(), authorTextBox.y() + authorTextBox.height());
184 myPath.addText(baseline, boldFont, authorString);
185 painter->setBrush(palette.color(QPalette::WindowText));
186 painter->setPen(palette.color(QPalette::Dark));
187 painter->setRenderHints (QPainter::Antialiasing, true);
188 painter->drawPath(myPath);
192 painter->setClipping(false);
193 painter->setPen(option.palette.color(QPalette::Midlight));
194 painter->drawLine(THUMB_WIDTH, THUMB_HEIGHT, option.rect.width(), THUMB_HEIGHT);
195 if (!video->thumbnail().isNull())
196 painter->setPen(Qt::black);
197 painter->drawLine(0, THUMB_HEIGHT, THUMB_WIDTH-1, THUMB_HEIGHT);
201 if (downloadInfo) paintDownloadInfo(painter, option, index);
205 void PrettyItemDelegate::paintActiveOverlay( QPainter *painter, qreal x, qreal y, qreal w, qreal h ) const {
208 QColor highlightColor = palette.color(QPalette::Highlight);
209 QColor backgroundColor = palette.color(QPalette::Base);
210 const float animation = 0.25;
211 const int gradientRange = 16;
213 QColor color2 = QColor::fromHsv(
214 highlightColor.hue(),
215 (int) (backgroundColor.saturation() * (1.0f - animation) + highlightColor.saturation() * animation),
216 (int) (backgroundColor.value() * (1.0f - animation) + highlightColor.value() * animation)
218 QColor color1 = QColor::fromHsv(
220 qMax(color2.saturation() - gradientRange, 0),
221 qMin(color2.value() + gradientRange, 255)
223 QRect rect((int) x, (int) y, (int) w, (int) h);
225 painter->setPen(Qt::NoPen);
226 QLinearGradient linearGradient(0, 0, 0, rect.height());
227 linearGradient.setColorAt(0.0, color1);
228 linearGradient.setColorAt(1.0, color2);
229 painter->setBrush(linearGradient);
230 painter->drawRect(rect);
234 void PrettyItemDelegate::paintPlayIcon(QPainter *painter) const {
236 painter->setOpacity(.5);
237 painter->drawPixmap(playIcon.rect(), playIcon);
241 void PrettyItemDelegate::drawTime(QPainter *painter, QString time, QRectF line) const {
242 static const int timePadding = 4;
243 QRectF textBox = painter->boundingRect(line, Qt::AlignLeft | Qt::AlignTop, time);
245 textBox.adjust(0, 0, timePadding, 0);
246 // move to bottom right corner of the thumb
247 textBox.translate(THUMB_WIDTH - textBox.width(), THUMB_HEIGHT - textBox.height());
250 painter->setPen(Qt::NoPen);
251 painter->setBrush(Qt::black);
252 painter->setOpacity(.5);
253 painter->drawRect(textBox);
257 painter->setPen(Qt::white);
258 painter->drawText(textBox, Qt::AlignCenter, time);
262 void PrettyItemDelegate::paintDownloadInfo( QPainter* painter,
263 const QStyleOptionViewItem& option,
264 const QModelIndex& index ) const {
266 // get the video metadata
267 const DownloadItemPointer downloadItemPointer = index.data(DownloadItemRole).value<DownloadItemPointer>();
268 const DownloadItem *downloadItem = downloadItemPointer.data();
272 const QRect line(0, 0, option.rect.width() / 2, option.rect.height());
274 painter->translate(option.rect.topLeft());
275 painter->translate(line.width(), 0);
278 DownloadItemStatus status = downloadItem->status();
280 if (status == Downloading) {
281 QString downloaded = DownloadItem::formattedFilesize(downloadItem->bytesReceived());
282 QString total = DownloadItem::formattedFilesize(downloadItem->bytesTotal());
283 QString speed = DownloadItem::formattedSpeed(downloadItem->currentSpeed());
284 QString eta = DownloadItem::formattedTime(downloadItem->remainingTime());
286 message = tr("%1 of %2 (%3) — %4").arg(
292 } else if (status == Starting) {
293 message = tr("Preparing");
294 } else if (status == Failed) {
295 message = tr("Failed") + " — " + downloadItem->errorMessage();
296 } else if (status == Finished) {
297 message = tr("Completed");
298 } else if (status == Idle) {
299 message = tr("Stopped");
302 // progressBar->setPalette(option.palette);
303 if (status == Finished) {
304 progressBar->setValue(100);
305 progressBar->setEnabled(true);
306 } else if (status == Downloading) {
307 progressBar->setValue(downloadItem->currentPercent());
308 progressBar->setEnabled(true);
310 progressBar->setValue(0);
311 progressBar->setEnabled(false);
314 int progressBarWidth = line.width() - PADDING*4 - 16;
315 progressBar->setMaximumWidth(progressBarWidth);
316 progressBar->setMinimumWidth(progressBarWidth);
318 painter->translate(PADDING, PADDING);
319 progressBar->render(painter);
322 bool downloadButtonHovered = false;
323 bool downloadButtonPressed = false;
324 const bool isHovered = index.data(HoveredItemRole).toBool();
326 downloadButtonHovered = index.data(DownloadButtonHoveredRole).toBool();
327 downloadButtonPressed = index.data(DownloadButtonPressedRole).toBool();
329 QIcon::Mode iconMode;
330 if (downloadButtonPressed) iconMode = QIcon::Selected;
331 else if (downloadButtonHovered) iconMode = QIcon::Active;
332 else iconMode = QIcon::Normal;
334 if (status != Finished && status != Failed && status != Idle) {
335 if (downloadButtonHovered) message = tr("Stop downloading");
337 QIcon closeIcon = QtIconLoader::icon("window-close");
338 painter->drawPixmap(downloadButtonRect(line), closeIcon.pixmap(16, 16, iconMode));
342 else if (status == Finished) {
343 if (downloadButtonHovered)
345 message = tr("Show in %1").arg("Finder");
347 message = tr("Open parent folder");
350 QIcon searchIcon = QtIconLoader::icon("system-search");
351 painter->drawPixmap(downloadButtonRect(line), searchIcon.pixmap(16, 16, iconMode));
355 else if (status == Failed || status == Idle) {
356 if (downloadButtonHovered) message = tr("Restart downloading");
358 QIcon searchIcon = QtIconLoader::icon("view-refresh");
359 painter->drawPixmap(downloadButtonRect(line), searchIcon.pixmap(16, 16, iconMode));
363 QRectF textBox = line.adjusted(PADDING, PADDING*2 + progressBar->sizeHint().height(), -2 * PADDING, -PADDING);
364 textBox = painter->boundingRect( textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message);
365 painter->drawText(textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message);
371 QRect PrettyItemDelegate::downloadButtonRect(QRect line) const {
373 line.width() - PADDING*2 - 16,
374 PADDING + progressBar->sizeHint().height() / 2 - 8,
379 QRect PrettyItemDelegate::authorRect(const QModelIndex& index) const {
380 return authorRects.value(index.row());