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 qDebug() << "Unavailable video source" << videoSource->getName();
49 emit unavailable(this);
52 Video *video = videos.at(0);
53 lastPixelRatio = window()->devicePixelRatio();
54 bool needLargeThumb = lastPixelRatio > 1.0 || window()->width() > 1000;
55 QString url = needLargeThumb ? video->getLargeThumbnailUrl() : video->getMediumThumbnailUrl();
56 if (url.isEmpty()) url = video->getThumbnailUrl();
58 QObject *reply = HttpUtils::yt().get(url);
59 connect(reply, SIGNAL(data(QByteArray)), SLOT(setPixmapData(QByteArray)));
62 void VideoSourceWidget::setPixmapData(const QByteArray &bytes) {
63 pixmap.loadFromData(bytes);
64 pixmap.setDevicePixelRatio(lastPixelRatio);
68 void VideoSourceWidget::loadPreview() {
69 connect(videoSource, SIGNAL(gotVideos(QVector<Video*>)),
70 SLOT(previewVideo(QVector<Video*>)), Qt::UniqueConnection);
71 videoSource->loadVideos(1, 1);
74 QPixmap VideoSourceWidget::playPixmap() {
75 const int s = height() / 2;
76 const int padding = s / 8;
78 qreal ratio = window()->devicePixelRatio();
79 QPixmap playIcon = QPixmap(s * ratio, s * ratio);
80 playIcon.setDevicePixelRatio(ratio);
81 playIcon.fill(Qt::transparent);
82 QPainter painter(&playIcon);
84 polygon << QPoint(padding, padding)
85 << QPoint(s - padding, s / 2)
86 << QPoint(padding, s - padding);
87 painter.setRenderHints(QPainter::Antialiasing, true);
89 // QColor color = pressed ? Qt::black : Qt::white;
90 QColor color = Qt::white;
91 painter.setBrush(color);
95 pen.setJoinStyle(Qt::RoundJoin);
96 pen.setCapStyle(Qt::RoundCap);
98 painter.drawPolygon(polygon);
102 void VideoSourceWidget::paintEvent(QPaintEvent *event) {
103 GridWidget::paintEvent(event);
104 if (pixmap.isNull()) return;
105 if (window()->devicePixelRatio() != lastPixelRatio) loadPreview();
109 qreal ratio = lastPixelRatio;
110 int w = width() * ratio;
111 int h = height() * ratio;
115 int wDiff = pixmap.width() - w;
116 if (wDiff > 0) xOffset = wDiff / 2;
117 else xOrigin = -wDiff / 2;
120 int hDiff = pixmap.height() - h;
121 if (hDiff > 0) yOffset = hDiff / 2;
122 else yOrigin = -hDiff / 2;
123 p.drawPixmap(xOrigin, yOrigin, pixmap, xOffset, yOffset, w, h);
129 QPixmap play = playPixmap();
133 (w - play.width() * ratio) / 2,
134 (h * 2/3 - play.height() * ratio) / 2,
140 QRect nameBox = rect();
141 nameBox.adjust(0, 0, 0, -h*2/3);
142 nameBox.translate(0, h - nameBox.height());
145 p.setBrush(QColor(0, 0, 0, 128));
149 QString name = videoSource->getName();
152 p.setFont(FontUtils::medium());
153 QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
154 if (textBox.height() > nameBox.height()) {
156 textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
157 if (textBox.height() > nameBox.height()) {
158 p.setClipRect(nameBox);
164 p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
166 p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
172 pen.setBrush(palette().highlight());