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 "videosourcewidget.h"
22 #include "videosource.h"
24 #include "fontutils.h"
25 #include "iconutils.h"
27 #include "httputils.h"
29 VideoSourceWidget::VideoSourceWidget(VideoSource *videoSource, QWidget *parent)
31 videoSource(videoSource),
33 videoSource->setParent(this);
34 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
38 connect(this, SIGNAL(activated()), SLOT(activate()));
41 void VideoSourceWidget::activate() {
42 emit activated(videoSource);
45 void VideoSourceWidget::previewVideo(const QVector<Video *> &videos) {
46 videoSource->disconnect();
47 if (videos.isEmpty()) {
48 emit unavailable(this);
51 Video *video = videos.at(0);
52 lastPixelRatio = window()->devicePixelRatio();
53 bool needLargeThumb = lastPixelRatio > 1.0 || window()->width() > 1000;
54 QString url = needLargeThumb ? video->getLargeThumbnailUrl() : video->getMediumThumbnailUrl();
55 if (url.isEmpty()) url = video->getMediumThumbnailUrl();
57 QObject *reply = HttpUtils::yt().get(url);
58 connect(reply, SIGNAL(data(QByteArray)), SLOT(setPixmapData(QByteArray)));
61 void VideoSourceWidget::setPixmapData(const QByteArray &bytes) {
62 pixmap.loadFromData(bytes);
63 pixmap.setDevicePixelRatio(lastPixelRatio);
67 void VideoSourceWidget::loadPreview() {
68 connect(videoSource, SIGNAL(gotVideos(QVector<Video*>)),
69 SLOT(previewVideo(QVector<Video*>)), Qt::UniqueConnection);
70 videoSource->loadVideos(1, 1);
73 QPixmap VideoSourceWidget::playPixmap() {
74 const int s = height() / 2;
75 const int padding = s / 8;
77 qreal ratio = window()->devicePixelRatio();
78 QPixmap playIcon = QPixmap(s * ratio, s * ratio);
79 playIcon.setDevicePixelRatio(ratio);
80 playIcon.fill(Qt::transparent);
81 QPainter painter(&playIcon);
83 polygon << QPoint(padding, padding)
84 << QPoint(s - padding, s / 2)
85 << QPoint(padding, s - padding);
86 painter.setRenderHints(QPainter::Antialiasing, true);
88 // QColor color = pressed ? Qt::black : Qt::white;
89 QColor color = Qt::white;
90 painter.setBrush(color);
94 pen.setJoinStyle(Qt::RoundJoin);
95 pen.setCapStyle(Qt::RoundCap);
97 painter.drawPolygon(polygon);
101 void VideoSourceWidget::paintEvent(QPaintEvent *event) {
102 GridWidget::paintEvent(event);
103 if (pixmap.isNull()) return;
104 if (window()->devicePixelRatio() != lastPixelRatio) loadPreview();
108 qreal ratio = lastPixelRatio;
109 int w = width() * ratio;
110 int h = height() * ratio;
114 int wDiff = pixmap.width() - w;
115 if (wDiff > 0) xOffset = wDiff / 2;
116 else xOrigin = -wDiff / 2;
119 int hDiff = pixmap.height() - h;
120 if (hDiff > 0) yOffset = hDiff / 2;
121 else yOrigin = -hDiff / 2;
122 p.drawPixmap(xOrigin, yOrigin, pixmap, xOffset, yOffset, w, h);
128 QPixmap play = playPixmap();
132 (w - play.width() * ratio) / 2,
133 (h * 2/3 - play.height() * ratio) / 2,
139 QRect nameBox = rect();
140 nameBox.adjust(0, 0, 0, -h*2/3);
141 nameBox.translate(0, h - nameBox.height());
144 p.setBrush(QColor(0, 0, 0, 128));
148 QString name = videoSource->getName();
151 p.setFont(FontUtils::medium());
152 QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
153 if (textBox.height() > nameBox.height()) {
155 textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
156 if (textBox.height() > nameBox.height()) {
157 p.setClipRect(nameBox);
163 p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
165 p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
171 pen.setBrush(palette().highlight());