]> git.sur5r.net Git - minitube/blob - src/playlistitemdelegate.cpp
b68134743031d38aa27a40bc9f1e7df79912999f
[minitube] / src / playlistitemdelegate.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "playlistitemdelegate.h"
22 #include "playlistmodel.h"
23 #include "fontutils.h"
24 #include "downloaditem.h"
25 #include "utils.h"
26 #include "videodefinition.h"
27 #include "video.h"
28
29 const int PlaylistItemDelegate::THUMB_HEIGHT = 90;
30 const int PlaylistItemDelegate::THUMB_WIDTH = 160;
31 const int PlaylistItemDelegate::PADDING = 10;
32
33 PlaylistItemDelegate::PlaylistItemDelegate(QObject* parent, bool downloadInfo)
34     : QStyledItemDelegate(parent),
35       downloadInfo(downloadInfo),
36       progressBar(0) {
37
38     boldFont.setBold(true);
39     smallerBoldFont = FontUtils::smallBold();
40     smallerFont = FontUtils::small();
41
42     if (downloadInfo) {
43         progressBar = new QProgressBar(qApp->activeWindow());
44         QPalette palette = progressBar->palette();
45         palette.setColor(QPalette::Window, Qt::transparent);
46         progressBar->setPalette(palette);
47         progressBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
48         progressBar->hide();
49     } else createPlayIcon();
50 }
51
52 PlaylistItemDelegate::~PlaylistItemDelegate() {
53     if (progressBar) delete progressBar;
54 }
55
56 void PlaylistItemDelegate::createPlayIcon() {
57     playIcon = QPixmap(THUMB_WIDTH, THUMB_HEIGHT);
58     playIcon.fill(Qt::transparent);
59
60     QPixmap tempPixmap(THUMB_WIDTH, THUMB_HEIGHT);
61     tempPixmap.fill(Qt::transparent);
62     QPainter painter(&tempPixmap);
63     painter.setRenderHints(QPainter::Antialiasing, true);
64
65     const int hPadding = PADDING*6;
66     const int vPadding = PADDING*2;
67
68     QPolygon polygon;
69     polygon << QPoint(hPadding, vPadding)
70             << QPoint(THUMB_WIDTH-hPadding, THUMB_HEIGHT/2)
71             << QPoint(hPadding, THUMB_HEIGHT-vPadding);
72     painter.setBrush(Qt::white);
73     QPen pen;
74     pen.setColor(Qt::white);
75     pen.setWidth(PADDING);
76     pen.setJoinStyle(Qt::RoundJoin);
77     pen.setCapStyle(Qt::RoundCap);
78     painter.setPen(pen);
79     painter.drawPolygon(polygon);
80     painter.end();
81
82     QPainter painter2(&playIcon);
83     painter2.setOpacity(.75);
84     painter2.drawPixmap(0, 0, tempPixmap);
85 }
86
87 QSize PlaylistItemDelegate::sizeHint( const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const {
88     return QSize(THUMB_WIDTH, THUMB_HEIGHT + 1);
89 }
90
91 void PlaylistItemDelegate::paint( QPainter* painter,
92                                   const QStyleOptionViewItem& option, const QModelIndex& index ) const {
93
94     int itemType = index.data(ItemTypeRole).toInt();
95     if (itemType == ItemTypeVideo) {
96         QStyleOptionViewItemV4 opt = QStyleOptionViewItemV4(option);
97         initStyleOption(&opt, index);
98         opt.text = "";
99         opt.widget->style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
100         paintBody(painter, opt, index);
101     } else
102         QStyledItemDelegate::paint( painter, option, index );
103
104 }
105
106 void PlaylistItemDelegate::paintBody( QPainter* painter,
107                                       const QStyleOptionViewItem& option,
108                                       const QModelIndex& index ) const {
109     painter->save();
110     painter->translate( option.rect.topLeft() );
111
112     QRect line(0, 0, option.rect.width(), option.rect.height());
113     if (downloadInfo) line.setWidth(line.width() / 2);
114
115     const bool isActive = index.data( ActiveTrackRole ).toBool();
116     const bool isSelected = option.state & QStyle::State_Selected;
117
118     // draw the "current track" highlight underneath the text
119     if (isActive && !isSelected)
120         paintActiveOverlay(painter, line);
121
122     // get the video metadata
123     const VideoPointer videoPointer = index.data( VideoRole ).value<VideoPointer>();
124     const Video *video = videoPointer.data();
125
126     // thumb
127     painter->drawPixmap(0, 0, video->thumbnail());
128
129     // play icon overlayed on the thumb
130     if (isActive)
131         painter->drawPixmap(playIcon.rect(), playIcon);
132
133     // time
134     drawTime(painter, video->formattedDuration(), line);
135
136     // separator
137     painter->setPen(option.palette.color(QPalette::Midlight));
138     painter->drawLine(THUMB_WIDTH, THUMB_HEIGHT, option.rect.width(), THUMB_HEIGHT);
139     if (!video->thumbnail().isNull())
140         painter->setPen(Qt::black);
141     painter->drawLine(0, THUMB_HEIGHT, THUMB_WIDTH-1, THUMB_HEIGHT);
142
143     if (line.width() > THUMB_WIDTH + 60) {
144
145         if (isActive) painter->setFont(boldFont);
146
147         // text color
148         if (isSelected)
149             painter->setPen(QPen(option.palette.brush(QPalette::HighlightedText), 0));
150         else
151             painter->setPen(QPen(option.palette.brush(QPalette::Text), 0));
152
153         // title
154         QString videoTitle = video->title();
155         QString v = videoTitle;
156         const int flags = Qt::AlignTop | Qt::TextWordWrap;
157         QRect textBox = line.adjusted(PADDING+THUMB_WIDTH, PADDING, 0, 0);
158         textBox = painter->boundingRect(textBox, flags, v);
159         while (textBox.height() > 55 && v.length() > 10) {
160             videoTitle.truncate(videoTitle.length() - 1);
161             v = videoTitle;
162             v = v.trimmed().append("...");
163             textBox = painter->boundingRect(textBox, flags, v);
164         }
165         painter->drawText(textBox, flags, v);
166
167         painter->setFont(smallerFont);
168
169         // published date
170         QString publishedString = video->published().date().toString(Qt::DefaultLocaleShortDate);
171         QSize stringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, publishedString ) );
172         QPoint textLoc(PADDING+THUMB_WIDTH, PADDING*2 + textBox.height());
173         QRect publishedTextBox(textLoc , stringSize);
174         painter->drawText(publishedTextBox, Qt::AlignLeft | Qt::AlignTop, publishedString);
175
176         if (line.width() > publishedTextBox.x() + publishedTextBox.width()*2) {
177
178             // author
179             bool authorHovered = false;
180             bool authorPressed = false;
181             const bool isHovered = index.data(HoveredItemRole).toBool();
182             if (isHovered) {
183                 authorHovered = index.data(AuthorHoveredRole).toBool();
184                 authorPressed = index.data(AuthorPressedRole).toBool();
185             }
186
187             painter->save();
188             painter->setFont(smallerBoldFont);
189             if (!isSelected) {
190                 if (authorHovered)
191                     painter->setPen(QPen(option.palette.brush(QPalette::Highlight), 0));
192                 else
193                     painter->setPen(QPen(option.palette.brush(QPalette::Mid), 0));
194             }
195             QString authorString = video->author();
196             textLoc.setX(textLoc.x() + stringSize.width() + PADDING);
197             stringSize = QSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, authorString ) );
198             QRect authorTextBox(textLoc , stringSize);
199             authorRects.insert(index.row(), authorTextBox);
200             painter->drawText(authorTextBox, Qt::AlignLeft | Qt::AlignTop, authorString);
201             painter->restore();
202
203             if (line.width() > authorTextBox.x() + 50) {
204
205                 // view count
206                 if (video->viewCount() >= 0) {
207                     QLocale locale;
208                     QString viewCountString = tr("%1 views").arg(locale.toString(video->viewCount()));
209                     textLoc.setX(textLoc.x() + stringSize.width() + PADDING);
210                     stringSize = QSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, viewCountString ) );
211                     QRect viewCountTextBox(textLoc , stringSize);
212                     painter->drawText(viewCountTextBox, Qt::AlignLeft | Qt::AlignBottom, viewCountString);
213                 }
214
215                 if (downloadInfo) {
216                     QString definitionString = VideoDefinition::getDefinitionName(video->getDefinitionCode());
217                     textLoc.setX(textLoc.x() + stringSize.width() + PADDING);
218                     stringSize = QSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, definitionString ) );
219                     QRect viewCountTextBox(textLoc , stringSize);
220                     painter->drawText(viewCountTextBox, Qt::AlignLeft | Qt::AlignBottom, definitionString);
221                 }
222
223             }
224
225         }
226
227     }
228
229     painter->restore();
230
231     if (downloadInfo) paintDownloadInfo(painter, option, index);
232
233 }
234
235 void PlaylistItemDelegate::paintActiveOverlay(QPainter *painter, const QRect &line) const {
236     static QLinearGradient linearGradient;
237     static bool initialized = false;
238
239     if (!initialized) {
240         QPalette palette;
241         QColor highlightColor = palette.color(QPalette::Highlight);
242         QColor backgroundColor = palette.color(QPalette::Base);
243         const float animation = 0.4;
244         const int gradientRange = 16;
245
246         QColor color2 = QColor::fromHsv(
247                     highlightColor.hue(),
248                     (int) (backgroundColor.saturation() * (1.0f - animation) + highlightColor.saturation() * animation),
249                     (int) (backgroundColor.value() * (1.0f - animation) + highlightColor.value() * animation)
250                     );
251         QColor color1 = QColor::fromHsv(
252                     color2.hue(),
253                     qMax(color2.saturation() - gradientRange, 0),
254                     qMin(color2.value() + gradientRange, 255)
255                     );
256
257         linearGradient = QLinearGradient(0, 0, 0, THUMB_HEIGHT);
258         linearGradient.setColorAt(0.0, color1);
259         linearGradient.setColorAt(1.0, color2);
260         initialized = true;
261     }
262
263     painter->fillRect(line, linearGradient);
264 }
265
266 void PlaylistItemDelegate::drawTime(QPainter *painter, const QString &time, const QRect &line) const {
267     static const int timePadding = 4;
268     QRect textBox = painter->boundingRect(line, Qt::AlignLeft | Qt::AlignTop, time);
269     // add padding
270     textBox.adjust(0, 0, timePadding, 0);
271     // move to bottom right corner of the thumb
272     textBox.translate(THUMB_WIDTH - textBox.width(), THUMB_HEIGHT - textBox.height());
273
274     painter->save();
275     painter->setPen(Qt::NoPen);
276     painter->setBrush(Qt::black);
277     painter->setOpacity(.5);
278     painter->drawRect(textBox);
279     painter->restore();
280
281     painter->save();
282     painter->setPen(Qt::white);
283     painter->drawText(textBox, Qt::AlignCenter, time);
284     painter->restore();
285 }
286
287 void PlaylistItemDelegate::paintDownloadInfo( QPainter* painter,
288                                               const QStyleOptionViewItem& option,
289                                               const QModelIndex& index ) const {
290
291     // get the video metadata
292     const DownloadItemPointer downloadItemPointer = index.data(DownloadItemRole).value<DownloadItemPointer>();
293     const DownloadItem *downloadItem = downloadItemPointer.data();
294
295     painter->save();
296
297     const QRect line(0, 0, option.rect.width() / 2, option.rect.height());
298
299     painter->translate(option.rect.topLeft());
300     painter->translate(line.width(), 0);
301
302     QString message;
303     DownloadItemStatus status = downloadItem->status();
304
305     if (status == Downloading) {
306         QString downloaded = DownloadItem::formattedFilesize(downloadItem->bytesReceived());
307         QString total = DownloadItem::formattedFilesize(downloadItem->bytesTotal());
308         QString speed = DownloadItem::formattedSpeed(downloadItem->currentSpeed());
309         QString eta = DownloadItem::formattedTime(downloadItem->remainingTime());
310
311         message = tr("%1 of %2 (%3) — %4").arg(
312                     downloaded,
313                     total,
314                     speed,
315                     eta
316                     );
317     } else if (status == Starting) {
318         message = tr("Preparing");
319     } else if (status == Failed) {
320         message = tr("Failed") + " — " + downloadItem->errorMessage();
321     } else if (status == Finished) {
322         message = tr("Completed");
323     } else if (status == Idle) {
324         message = tr("Stopped");
325     }
326
327     // progressBar->setPalette(option.palette);
328     if (status == Finished) {
329         progressBar->setValue(100);
330         progressBar->setEnabled(true);
331     } else if (status == Downloading) {
332         progressBar->setValue(downloadItem->currentPercent());
333         progressBar->setEnabled(true);
334     } else {
335         progressBar->setValue(0);
336         progressBar->setEnabled(false);
337     }
338
339     int progressBarWidth = line.width() - PADDING*4 - 16;
340     progressBar->setMaximumWidth(progressBarWidth);
341     progressBar->setMinimumWidth(progressBarWidth);
342     painter->save();
343     painter->translate(PADDING, PADDING);
344     progressBar->render(painter);
345     painter->restore();
346
347     bool downloadButtonHovered = false;
348     bool downloadButtonPressed = false;
349     const bool isHovered = index.data(HoveredItemRole).toBool();
350     if (isHovered) {
351         downloadButtonHovered = index.data(DownloadButtonHoveredRole).toBool();
352         downloadButtonPressed = index.data(DownloadButtonPressedRole).toBool();
353     }
354     QIcon::Mode iconMode;
355     if (downloadButtonPressed) iconMode = QIcon::Selected;
356     else if (downloadButtonHovered) iconMode = QIcon::Active;
357     else iconMode = QIcon::Normal;
358
359     if (status != Finished && status != Failed && status != Idle) {
360         if (downloadButtonHovered) message = tr("Stop downloading");
361         painter->save();
362         QIcon closeIcon = Utils::icon("window-close");
363         painter->drawPixmap(downloadButtonRect(line), closeIcon.pixmap(16, 16, iconMode));
364         painter->restore();
365     }
366
367     else if (status == Finished) {
368         if (downloadButtonHovered)
369 #ifdef APP_MAC
370             message = tr("Show in %1").arg("Finder");
371 #else
372             message = tr("Open parent folder");
373 #endif
374         painter->save();
375         QIcon searchIcon = Utils::icon("system-search");
376         painter->drawPixmap(downloadButtonRect(line), searchIcon.pixmap(16, 16, iconMode));
377         painter->restore();
378     }
379
380     else if (status == Failed || status == Idle) {
381         if (downloadButtonHovered) message = tr("Restart downloading");
382         painter->save();
383         QIcon searchIcon = Utils::icon("view-refresh");
384         painter->drawPixmap(downloadButtonRect(line), searchIcon.pixmap(16, 16, iconMode));
385         painter->restore();
386     }
387
388     QRect textBox = line.adjusted(PADDING, PADDING*2 + progressBar->sizeHint().height(), -2 * PADDING, -PADDING);
389     textBox = painter->boundingRect( textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message);
390     painter->drawText(textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message);
391
392     painter->restore();
393 }
394
395 QRect PlaylistItemDelegate::downloadButtonRect(const QRect &line) const {
396     return QRect(
397                 line.width() - PADDING*2 - 16,
398                 PADDING + progressBar->sizeHint().height() / 2 - 8,
399                 16,
400                 16);
401 }
402
403 QRect PlaylistItemDelegate::authorRect(const QModelIndex& index) const {
404     return authorRects.value(index.row());
405 }