]> git.sur5r.net Git - minitube/blob - src/playlist/PrettyItemDelegate.cpp
Imported Upstream version 1.2
[minitube] / src / playlist / PrettyItemDelegate.cpp
1 #include "PrettyItemDelegate.h"
2 #include "../ListModel.h"
3 #include "../fontutils.h"
4 #include "../downloaditem.h"
5 #include "../iconloader/qticonloader.h"
6
7 #include <QFontMetricsF>
8 #include <QPainter>
9
10 const qreal PrettyItemDelegate::THUMB_HEIGHT = 90.0;
11 const qreal PrettyItemDelegate::THUMB_WIDTH = 120.0;
12 const qreal PrettyItemDelegate::PADDING = 10.0;
13
14 PrettyItemDelegate::PrettyItemDelegate(QObject* parent, bool downloadInfo)
15     : QStyledItemDelegate(parent),
16     downloadInfo(downloadInfo) {
17     boldFont.setBold(true);
18     smallerBoldFont = FontUtils::smallBold();
19     smallerFont = FontUtils::small();
20
21     if (downloadInfo) {
22         progressBar = new QProgressBar(qApp->activeWindow());
23         QPalette palette = progressBar->palette();
24         palette.setColor(QPalette::Window, Qt::transparent);
25         progressBar->setPalette(palette);
26         progressBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
27         progressBar->hide();
28     } else createPlayIcon();
29 }
30
31 void PrettyItemDelegate::createPlayIcon() {
32     playIcon = QPixmap(THUMB_WIDTH, THUMB_HEIGHT);
33     playIcon.fill(Qt::transparent);
34     QPainter painter(&playIcon);
35     QPolygon polygon;
36     polygon << QPoint(PADDING*4, PADDING*2)
37             << QPoint(THUMB_WIDTH-PADDING*4, THUMB_HEIGHT/2)
38             << QPoint(PADDING*4, THUMB_HEIGHT-PADDING*2);
39     painter.setRenderHints(QPainter::Antialiasing, true);
40     painter.setBrush(Qt::white);
41     QPen pen;
42     pen.setColor(Qt::white);
43     pen.setWidth(PADDING);
44     pen.setJoinStyle(Qt::RoundJoin);
45     pen.setCapStyle(Qt::RoundCap);
46     painter.setPen(pen);
47     painter.drawPolygon(polygon);
48 }
49
50 PrettyItemDelegate::~PrettyItemDelegate() { }
51
52 QSize PrettyItemDelegate::sizeHint( const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const {
53     return QSize( 256, THUMB_HEIGHT+1.0);
54 }
55
56 void PrettyItemDelegate::paint( QPainter* painter,
57                                 const QStyleOptionViewItem& option, const QModelIndex& index ) const {
58
59     int itemType = index.data(ItemTypeRole).toInt();
60     if (itemType == ItemTypeVideo) {
61         QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
62         paintBody( painter, option, index );
63     } else
64         QStyledItemDelegate::paint( painter, option, index );
65
66 }
67
68 void PrettyItemDelegate::paintBody( QPainter* painter,
69                                     const QStyleOptionViewItem& option,
70                                     const QModelIndex& index ) const {
71
72     painter->save();
73     painter->translate( option.rect.topLeft() );
74
75
76     QRectF line(0, 0, option.rect.width(), option.rect.height());
77     if (downloadInfo) line.setWidth(line.width() / 2);
78     painter->setClipRect(line);
79
80     const bool isActive = index.data( ActiveTrackRole ).toBool();
81     const bool isSelected = option.state & QStyle::State_Selected;
82
83     // draw the "current track" highlight underneath the text
84     if (isActive && !isSelected) {
85         paintActiveOverlay(painter, line.x(), line.y(), line.width(), line.height());
86     }
87
88     // get the video metadata
89     const VideoPointer videoPointer = index.data( VideoRole ).value<VideoPointer>();
90     const Video *video = videoPointer.data();
91
92     // thumb
93     if (!video->thumbnail().isNull()) {
94         painter->drawImage(QRect(0, 0, THUMB_WIDTH, THUMB_HEIGHT), video->thumbnail());
95
96         // play icon overlayed on the thumb
97         if (isActive)
98             paintPlayIcon(painter);
99
100         // time
101         QString timeString;
102         int duration = video->duration();
103         if ( duration > 3600 )
104             timeString = QTime().addSecs(duration).toString("h:mm:ss");
105         else
106             timeString = QTime().addSecs(duration).toString("m:ss");
107         drawTime(painter, timeString, line);
108
109     }
110
111     if (isActive) painter->setFont(boldFont);
112
113     // text color
114     if (isSelected)
115         painter->setPen(QPen(option.palette.brush(QPalette::HighlightedText), 0));
116     else
117         painter->setPen(QPen(option.palette.brush(QPalette::Text), 0));
118
119     // title
120     QString videoTitle = video->title();
121     QRectF textBox = line.adjusted(PADDING+THUMB_WIDTH, PADDING, -2 * PADDING, -PADDING);
122     textBox = painter->boundingRect( textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, videoTitle);
123     painter->drawText(textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, videoTitle);
124
125     painter->setFont(smallerFont);
126
127     // published date
128     QString publishedString = video->published().date().toString(Qt::DefaultLocaleShortDate);
129     QSizeF publishedStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, publishedString ) );
130     QPointF textLoc(PADDING+THUMB_WIDTH, PADDING*2 + textBox.height());
131     QRectF publishedTextBox( textLoc , publishedStringSize);
132     painter->drawText(publishedTextBox, Qt::AlignLeft | Qt::AlignTop, publishedString);
133
134     // author
135     painter->save();
136     painter->setFont(smallerBoldFont);
137     if (!isSelected && !isActive)
138         painter->setPen(QPen(option.palette.brush(QPalette::Mid), 0));
139     QString authorString = video->author();
140     QSizeF authorStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, authorString ) );
141     textLoc.setX(textLoc.x() + publishedStringSize.width() + PADDING);
142     QRectF authorTextBox( textLoc , authorStringSize);
143     painter->drawText(authorTextBox, Qt::AlignLeft | Qt::AlignTop, authorString);
144     painter->restore();
145
146     // view count
147     if (video->viewCount() >= 0) {
148         painter->save();
149         QLocale locale;
150         QString viewCountString = tr("%1 views").arg(locale.toString(video->viewCount()));
151         QSizeF viewCountStringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, viewCountString ) );
152         textLoc.setX(textLoc.x() + authorStringSize.width() + PADDING);
153         QRectF viewCountTextBox( textLoc , viewCountStringSize);
154         painter->drawText(viewCountTextBox, Qt::AlignLeft | Qt::AlignBottom, viewCountString);
155         painter->restore();
156     }
157
158     /*
159     QLinearGradient myGradient;
160     QPen myPen;
161     QFont myFont;
162     QPointF baseline(authorTextBox.x(), authorTextBox.y() + authorTextBox.height());
163     QPainterPath myPath;
164     myPath.addText(baseline, boldFont, authorString);
165     painter->setBrush(palette.color(QPalette::WindowText));
166     painter->setPen(palette.color(QPalette::Dark));
167     painter->setRenderHints (QPainter::Antialiasing, true);
168     painter->drawPath(myPath);
169     */
170
171     // separator
172     painter->setClipping(false);
173     painter->setPen(option.palette.color(QPalette::Midlight));
174     painter->drawLine(THUMB_WIDTH, THUMB_HEIGHT, option.rect.width(), THUMB_HEIGHT);
175     if (!video->thumbnail().isNull())
176         painter->setPen(Qt::black);
177     painter->drawLine(0, THUMB_HEIGHT, THUMB_WIDTH-1, THUMB_HEIGHT);
178
179     painter->restore();
180
181     if (downloadInfo) paintDownloadInfo(painter, option, index);
182
183 }
184
185 void PrettyItemDelegate::paintActiveOverlay( QPainter *painter, qreal x, qreal y, qreal w, qreal h ) const {
186
187     QPalette palette;
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;
192
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)
197             );
198     QColor color1 = QColor::fromHsv(
199             color2.hue(),
200             qMax(color2.saturation() - gradientRange, 0),
201             qMin(color2.value() + gradientRange, 255)
202             );
203     QRect rect((int) x, (int) y, (int) w, (int) h);
204     painter->save();
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);
211     painter->restore();
212 }
213
214 void PrettyItemDelegate::paintPlayIcon(QPainter *painter) const {
215     painter->save();
216     painter->setOpacity(.5);
217     painter->drawPixmap(playIcon.rect(), playIcon);
218     painter->restore();
219 }
220
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);
224     // add padding
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());
228
229     painter->save();
230     painter->setPen(Qt::NoPen);
231     painter->setBrush(Qt::black);
232     painter->setOpacity(.5);
233     painter->drawRect(textBox);
234     painter->restore();
235
236     painter->save();
237     painter->setPen(Qt::white);
238     painter->drawText(textBox, Qt::AlignCenter, time);
239     painter->restore();
240 }
241
242 void PrettyItemDelegate::paintDownloadInfo( QPainter* painter,
243                                             const QStyleOptionViewItem& option,
244                                             const QModelIndex& index ) const {
245
246     // get the video metadata
247     const DownloadItemPointer downloadItemPointer = index.data(DownloadItemRole).value<DownloadItemPointer>();
248     const DownloadItem *downloadItem = downloadItemPointer.data();
249
250     painter->save();
251
252     const QRect line(0, 0, option.rect.width() / 2, option.rect.height());
253
254     painter->translate(option.rect.topLeft());
255     painter->translate(line.width(), 0);
256
257     QString message;
258     DownloadItemStatus status = downloadItem->status();
259
260     if (status == Downloading) {
261         QString downloaded = DownloadItem::formattedFilesize(downloadItem->bytesReceived());
262         QString total = DownloadItem::formattedFilesize(downloadItem->bytesTotal());
263         QString speed = DownloadItem::formattedSpeed(downloadItem->currentSpeed());
264         QString eta = DownloadItem::formattedTime(downloadItem->remainingTime());
265
266         message = tr("%1 of %2 (%3) — %4").arg(
267                 downloaded,
268                 total,
269                 speed,
270                 eta
271                 );
272     } else if (status == Starting) {
273         message = tr("Preparing");
274     } else if (status == Failed) {
275         message = tr("Failed") + " — " + downloadItem->errorMessage();
276     } else if (status == Finished) {
277         message = tr("Completed");
278     } else if (status == Idle) {
279         message = tr("Stopped");
280     }
281
282     // progressBar->setPalette(option.palette);
283     if (status == Finished) {
284         progressBar->setValue(100);
285         progressBar->setEnabled(true);
286     } else if (status == Downloading) {
287         progressBar->setValue(downloadItem->currentPercent());
288         progressBar->setEnabled(true);
289     } else {
290         progressBar->setValue(0);
291         progressBar->setEnabled(false);
292     }
293
294     int progressBarWidth = line.width() - PADDING*4 - 16;
295     progressBar->setMaximumWidth(progressBarWidth);
296     progressBar->setMinimumWidth(progressBarWidth);
297     painter->save();
298     painter->translate(PADDING, PADDING);
299     progressBar->render(painter);
300     painter->restore();
301
302     bool downloadButtonHovered = false;
303     bool downloadButtonPressed = false;
304     const bool isHovered = index.data(HoveredItemRole).toBool();
305     if (isHovered) {
306         downloadButtonHovered = index.data(DownloadButtonHoveredRole).toBool();
307         downloadButtonPressed = index.data(DownloadButtonPressedRole).toBool();
308     }
309     QIcon::Mode iconMode;
310     if (downloadButtonPressed) iconMode = QIcon::Selected;
311     else if (downloadButtonHovered) iconMode = QIcon::Active;
312     else iconMode = QIcon::Normal;
313
314     if (status != Finished && status != Failed && status != Idle) {
315         if (downloadButtonHovered) message = tr("Stop downloading");
316         painter->save();
317         QIcon closeIcon = QtIconLoader::icon("window-close");
318         painter->drawPixmap(downloadButtonRect(line), closeIcon.pixmap(16, 16, iconMode));
319         painter->restore();
320     }
321
322     else if (status == Finished) {
323         if (downloadButtonHovered)
324 #ifdef APP_MAC
325         message = tr("Show in %1").arg("Finder");
326 #else
327         message = tr("Open parent folder");
328 #endif
329         painter->save();
330         QIcon searchIcon = QtIconLoader::icon("system-search");
331         painter->drawPixmap(downloadButtonRect(line), searchIcon.pixmap(16, 16, iconMode));
332         painter->restore();
333     }
334
335     else if (status == Failed || status == Idle) {
336         if (downloadButtonHovered) message = tr("Restart downloading");
337         painter->save();
338         QIcon searchIcon = QtIconLoader::icon("view-refresh");
339         painter->drawPixmap(downloadButtonRect(line), searchIcon.pixmap(16, 16, iconMode));
340         painter->restore();
341     }
342
343     QRectF textBox = line.adjusted(PADDING, PADDING*2 + progressBar->sizeHint().height(), -2 * PADDING, -PADDING);
344     textBox = painter->boundingRect( textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message);
345     painter->drawText(textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message);
346
347     painter->restore();
348
349 }
350
351 QRect PrettyItemDelegate::downloadButtonRect(QRect line) const {
352     return QRect(
353             line.width() - PADDING*2 - 16,
354             PADDING + progressBar->sizeHint().height() / 2 - 8,
355             16,
356             16);
357 }