3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "playlistitemdelegate.h"
22 #include "playlistmodel.h"
23 #include "fontutils.h"
24 #include "downloaditem.h"
25 #include "iconutils.h"
26 #include "videodefinition.h"
28 #include "datautils.h"
30 const int PlaylistItemDelegate::THUMB_HEIGHT = 90;
31 const int PlaylistItemDelegate::THUMB_WIDTH = 160;
32 const int PlaylistItemDelegate::PADDING = 10;
34 PlaylistItemDelegate::PlaylistItemDelegate(QObject* parent, bool downloadInfo)
35 : QStyledItemDelegate(parent),
36 downloadInfo(downloadInfo),
39 boldFont.setBold(true);
40 smallerBoldFont = FontUtils::smallBold();
41 smallerFont = FontUtils::small();
44 progressBar = new QProgressBar(qApp->activeWindow());
45 QPalette palette = progressBar->palette();
46 palette.setColor(QPalette::Window, Qt::transparent);
47 progressBar->setPalette(palette);
48 progressBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
50 } else createPlayIcon();
53 PlaylistItemDelegate::~PlaylistItemDelegate() {
54 if (progressBar) delete progressBar;
57 void PlaylistItemDelegate::createPlayIcon() {
58 qreal maxRatio = IconUtils::maxSupportedPixelRatio();
59 playIcon = QPixmap(THUMB_WIDTH * maxRatio, THUMB_HEIGHT * maxRatio);
60 playIcon.setDevicePixelRatio(maxRatio);
61 playIcon.fill(Qt::transparent);
63 QPixmap tempPixmap(THUMB_WIDTH * maxRatio, THUMB_HEIGHT * maxRatio);
64 tempPixmap.setDevicePixelRatio(maxRatio);
65 tempPixmap.fill(Qt::transparent);
66 QPainter painter(&tempPixmap);
67 painter.setRenderHints(QPainter::Antialiasing, true);
69 const int hPadding = PADDING*6;
70 const int vPadding = PADDING*2;
73 polygon << QPoint(hPadding, vPadding)
74 << QPoint(THUMB_WIDTH-hPadding, THUMB_HEIGHT/2)
75 << QPoint(hPadding, THUMB_HEIGHT-vPadding);
76 painter.setBrush(Qt::white);
78 pen.setColor(Qt::white);
79 pen.setWidth(PADDING);
80 pen.setJoinStyle(Qt::RoundJoin);
81 pen.setCapStyle(Qt::RoundCap);
83 painter.drawPolygon(polygon);
86 QPainter painter2(&playIcon);
87 painter2.setOpacity(.75);
88 painter2.drawPixmap(0, 0, tempPixmap);
91 QSize PlaylistItemDelegate::sizeHint( const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const {
92 return QSize(THUMB_WIDTH, THUMB_HEIGHT + 1);
95 void PlaylistItemDelegate::paint( QPainter* painter,
96 const QStyleOptionViewItem& option, const QModelIndex& index ) const {
98 int itemType = index.data(ItemTypeRole).toInt();
99 if (itemType == ItemTypeVideo) {
100 QStyleOptionViewItemV4 opt = QStyleOptionViewItemV4(option);
101 initStyleOption(&opt, index);
103 opt.widget->style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
104 paintBody(painter, opt, index);
106 QStyledItemDelegate::paint( painter, option, index );
110 void PlaylistItemDelegate::paintBody( QPainter* painter,
111 const QStyleOptionViewItem& option,
112 const QModelIndex& index ) const {
114 painter->translate( option.rect.topLeft() );
116 QRect line(0, 0, option.rect.width(), option.rect.height());
117 if (downloadInfo) line.setWidth(line.width() / 2);
119 const bool isActive = index.data( ActiveTrackRole ).toBool();
120 const bool isSelected = option.state & QStyle::State_Selected;
122 // get the video metadata
123 const Video *video = index.data(VideoRole).value<VideoPointer>().data();
125 // draw the "current track" highlight underneath the text
126 if (isActive && !isSelected)
127 paintActiveOverlay(painter, option, line);
130 painter->setPen(option.palette.color(QPalette::Midlight));
131 painter->drawLine(THUMB_WIDTH, THUMB_HEIGHT, option.rect.width(), THUMB_HEIGHT);
132 if (!video->thumbnail().isNull())
133 painter->setPen(Qt::black);
134 painter->drawLine(0, THUMB_HEIGHT, THUMB_WIDTH-1, THUMB_HEIGHT);
137 painter->drawPixmap(0, 0, video->thumbnail());
139 // play icon overlayed on the thumb
141 painter->drawPixmap(0, 0, playIcon);
144 if (video->duration() > 0)
145 drawTime(painter, video->formattedDuration(), line);
147 if (line.width() > THUMB_WIDTH + 60) {
149 // if (isActive) painter->setFont(boldFont);
153 painter->setPen(QPen(option.palette.highlightedText(), 0));
155 painter->setPen(QPen(option.palette.text(), 0));
158 QString videoTitle = video->title();
159 QString v = videoTitle;
160 const int flags = Qt::AlignTop | Qt::TextWordWrap;
161 QRect textBox = line.adjusted(PADDING+THUMB_WIDTH, PADDING, 0, 0);
162 textBox = painter->boundingRect(textBox, flags, v);
163 while (textBox.height() > 55 && v.length() > 10) {
164 videoTitle.truncate(videoTitle.length() - 1);
166 v = v.trimmed().append("...");
167 textBox = painter->boundingRect(textBox, flags, v);
169 painter->drawText(textBox, flags, v);
171 painter->setFont(smallerFont);
174 QString publishedString = DataUtils::formatDateTime(video->published());
175 QSize stringSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, publishedString ) );
176 QPoint textLoc(PADDING+THUMB_WIDTH, PADDING*2 + textBox.height());
177 QRect publishedTextBox(textLoc , stringSize);
178 painter->drawText(publishedTextBox, Qt::AlignLeft | Qt::AlignTop, publishedString);
180 if (line.width() > publishedTextBox.x() + publishedTextBox.width()*2) {
183 bool authorHovered = false;
184 bool authorPressed = false;
185 const bool isHovered = index.data(HoveredItemRole).toBool();
187 authorHovered = index.data(AuthorHoveredRole).toBool();
188 authorPressed = index.data(AuthorPressedRole).toBool();
192 painter->setFont(smallerBoldFont);
195 painter->setPen(QPen(option.palette.brush(QPalette::Highlight), 0));
197 painter->setOpacity(.5);
199 const QString &authorString = video->channelTitle();
200 textLoc.setX(textLoc.x() + stringSize.width() + PADDING);
201 stringSize = QSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, authorString ) );
202 QRect authorTextBox(textLoc , stringSize);
203 authorRects.insert(index.row(), authorTextBox);
204 painter->drawText(authorTextBox, Qt::AlignLeft | Qt::AlignTop, authorString);
207 if (line.width() > authorTextBox.x() + 50) {
210 if (video->viewCount() >= 0) {
212 QString viewCountString = tr("%1 views").arg(locale.toString(video->viewCount()));
213 textLoc.setX(textLoc.x() + stringSize.width() + PADDING);
214 stringSize = QSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, viewCountString ) );
215 QRect viewCountTextBox(textLoc , stringSize);
216 painter->drawText(viewCountTextBox, Qt::AlignLeft | Qt::AlignBottom, viewCountString);
220 const QString definitionString = VideoDefinition::getDefinitionFor(video->getDefinitionCode()).getName();
221 textLoc.setX(textLoc.x() + stringSize.width() + PADDING);
222 stringSize = QSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, definitionString ) );
223 QRect viewCountTextBox(textLoc , stringSize);
224 painter->drawText(viewCountTextBox, Qt::AlignLeft | Qt::AlignBottom, definitionString);
233 const bool isHovered = index.data(HoveredItemRole).toBool();
234 if (!isActive && isHovered) {
235 painter->setFont(smallerFont);
236 painter->setPen(Qt::white);
237 QString videoTitle = video->title();
238 QString v = videoTitle;
239 const int flags = Qt::AlignTop | Qt::TextWordWrap;
240 QRect textBox(PADDING, PADDING, THUMB_WIDTH - PADDING*2, THUMB_HEIGHT - PADDING*2);
241 textBox = painter->boundingRect(textBox, flags, v);
242 while (textBox.height() > THUMB_HEIGHT && v.length() > 10) {
243 videoTitle.truncate(videoTitle.length() - 1);
245 v = v.trimmed().append("...");
246 textBox = painter->boundingRect(textBox, flags, v);
248 painter->fillRect(QRect(0, 0, THUMB_WIDTH, textBox.height() + PADDING*2), QColor(0, 0, 0, 128));
249 painter->drawText(textBox, flags, v);
256 if (downloadInfo) paintDownloadInfo(painter, option, index);
260 void PlaylistItemDelegate::paintActiveOverlay(QPainter *painter, const QStyleOptionViewItem& option, const QRect &line) const {
262 painter->setOpacity(.1);
263 painter->fillRect(line, option.palette.highlight());
267 void PlaylistItemDelegate::drawTime(QPainter *painter, const QString &time, const QRect &line) const {
268 static const int timePadding = 4;
269 QRect textBox = painter->boundingRect(line, Qt::AlignLeft | Qt::AlignTop, time);
271 textBox.adjust(0, 0, timePadding, 0);
272 // move to bottom right corner of the thumb
273 textBox.translate(THUMB_WIDTH - textBox.width(), THUMB_HEIGHT - textBox.height());
276 painter->setPen(Qt::NoPen);
277 painter->setBrush(Qt::black);
278 painter->setOpacity(.5);
279 painter->drawRect(textBox);
283 painter->setPen(Qt::white);
284 painter->drawText(textBox, Qt::AlignCenter, time);
288 void PlaylistItemDelegate::paintDownloadInfo( QPainter* painter,
289 const QStyleOptionViewItem& option,
290 const QModelIndex& index ) const {
292 // get the video metadata
293 const DownloadItemPointer downloadItemPointer = index.data(DownloadItemRole).value<DownloadItemPointer>();
294 const DownloadItem *downloadItem = downloadItemPointer.data();
298 const QRect line(0, 0, option.rect.width() / 2, option.rect.height());
300 painter->translate(option.rect.topLeft());
301 painter->translate(line.width(), 0);
304 DownloadItemStatus status = downloadItem->status();
306 if (status == Downloading) {
307 QString downloaded = DownloadItem::formattedFilesize(downloadItem->bytesReceived());
308 QString total = DownloadItem::formattedFilesize(downloadItem->bytesTotal());
309 QString speed = DownloadItem::formattedSpeed(downloadItem->currentSpeed());
310 QString eta = DownloadItem::formattedTime(downloadItem->remainingTime());
312 message = tr("%1 of %2 (%3) — %4").arg(
318 } else if (status == Starting) {
319 message = tr("Preparing");
320 } else if (status == Failed) {
321 message = tr("Failed") + " — " + downloadItem->errorMessage();
322 } else if (status == Finished) {
323 message = tr("Completed");
324 } else if (status == Idle) {
325 message = tr("Stopped");
328 // progressBar->setPalette(option.palette);
329 if (status == Finished) {
330 progressBar->setValue(100);
331 progressBar->setEnabled(true);
332 } else if (status == Downloading) {
333 progressBar->setValue(downloadItem->currentPercent());
334 progressBar->setEnabled(true);
336 progressBar->setValue(0);
337 progressBar->setEnabled(false);
340 int progressBarWidth = line.width() - PADDING*4 - 16;
341 progressBar->setMaximumWidth(progressBarWidth);
342 progressBar->setMinimumWidth(progressBarWidth);
344 painter->translate(PADDING, PADDING);
345 progressBar->render(painter);
348 bool downloadButtonHovered = false;
349 bool downloadButtonPressed = false;
350 const bool isHovered = index.data(HoveredItemRole).toBool();
352 downloadButtonHovered = index.data(DownloadButtonHoveredRole).toBool();
353 downloadButtonPressed = index.data(DownloadButtonPressedRole).toBool();
355 QIcon::Mode iconMode;
356 if (downloadButtonPressed) iconMode = QIcon::Selected;
357 else if (downloadButtonHovered) iconMode = QIcon::Active;
358 else iconMode = QIcon::Normal;
360 if (status != Finished && status != Failed && status != Idle) {
361 if (downloadButtonHovered) message = tr("Stop downloading");
363 QIcon closeIcon = IconUtils::icon("window-close");
364 painter->drawPixmap(downloadButtonRect(line), closeIcon.pixmap(16, 16, iconMode));
368 else if (status == Finished) {
369 if (downloadButtonHovered)
371 message = tr("Show in %1").arg("Finder");
373 message = tr("Open parent folder");
376 QIcon searchIcon = IconUtils::icon("system-search");
377 painter->drawPixmap(downloadButtonRect(line), searchIcon.pixmap(16, 16, iconMode));
381 else if (status == Failed || status == Idle) {
382 if (downloadButtonHovered) message = tr("Restart downloading");
384 QIcon searchIcon = IconUtils::icon("view-refresh");
385 painter->drawPixmap(downloadButtonRect(line), searchIcon.pixmap(16, 16, iconMode));
389 QRect textBox = line.adjusted(PADDING, PADDING*2 + progressBar->sizeHint().height(), -2 * PADDING, -PADDING);
390 textBox = painter->boundingRect( textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message);
391 painter->drawText(textBox, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, message);
396 QRect PlaylistItemDelegate::downloadButtonRect(const QRect &line) const {
398 line.width() - PADDING*2 - 16,
399 PADDING + progressBar->sizeHint().height() / 2 - 8,
404 QRect PlaylistItemDelegate::authorRect(const QModelIndex& index) const {
405 return authorRects.value(index.row());