]> git.sur5r.net Git - minitube/blob - src/videosourcewidget.cpp
a2eacf7eb540c81c7743ebd89f2a0b5fc8d7340c
[minitube] / src / videosourcewidget.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 "videosourcewidget.h"
22 #include "videosource.h"
23 #include "video.h"
24 #include "fontutils.h"
25 #include "iconutils.h"
26 #include "http.h"
27 #include "httputils.h"
28
29 VideoSourceWidget::VideoSourceWidget(VideoSource *videoSource, QWidget *parent)
30     : GridWidget(parent),
31       videoSource(videoSource),
32       lastPixelRatio(0) {
33     videoSource->setParent(this);
34     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
35
36     loadPreview();
37
38     connect(this, SIGNAL(activated()), SLOT(activate()));
39 }
40
41 void VideoSourceWidget::activate() {
42     emit activated(videoSource);
43 }
44
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);
50         return;
51     }
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();
57     video->deleteLater();
58     QObject *reply = HttpUtils::yt().get(url);
59     connect(reply, SIGNAL(data(QByteArray)), SLOT(setPixmapData(QByteArray)));
60 }
61
62 void VideoSourceWidget::setPixmapData(const QByteArray &bytes) {
63     pixmap.loadFromData(bytes);
64     pixmap.setDevicePixelRatio(lastPixelRatio);
65     update();
66 }
67
68 void VideoSourceWidget::loadPreview() {
69     connect(videoSource, SIGNAL(gotVideos(QVector<Video*>)),
70             SLOT(previewVideo(QVector<Video*>)), Qt::UniqueConnection);
71     videoSource->loadVideos(1, 1);
72 }
73
74 QPixmap VideoSourceWidget::playPixmap() {
75     const int s = height() / 2;
76     const int padding = s / 8;
77
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);
83     QPolygon polygon;
84     polygon << QPoint(padding, padding)
85             << QPoint(s - padding, s / 2)
86             << QPoint(padding, s - padding);
87     painter.setRenderHints(QPainter::Antialiasing, true);
88
89     // QColor color = pressed ? Qt::black : Qt::white;
90     QColor color = Qt::white;
91     painter.setBrush(color);
92     QPen pen;
93     pen.setColor(color);
94     pen.setWidth(10);
95     pen.setJoinStyle(Qt::RoundJoin);
96     pen.setCapStyle(Qt::RoundCap);
97     painter.setPen(pen);
98     painter.drawPolygon(polygon);
99     return playIcon;
100 }
101
102 void VideoSourceWidget::paintEvent(QPaintEvent *event) {
103     GridWidget::paintEvent(event);
104     if (pixmap.isNull()) return;
105     if (window()->devicePixelRatio() != lastPixelRatio) loadPreview();
106
107     QPainter p(this);
108
109     qreal ratio = lastPixelRatio;
110     int w = width() * ratio;
111     int h = height() * ratio;
112
113     int xOffset = 0;
114     int xOrigin = 0;
115     int wDiff = pixmap.width() - w;
116     if (wDiff > 0) xOffset = wDiff / 2;
117     else xOrigin = -wDiff / 2;
118     int yOffset = 0;
119     int yOrigin = 0;
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);
124
125     w = width();
126     h = height();
127
128     if (hovered) {
129         QPixmap play = playPixmap();
130         p.save();
131         p.setOpacity(.5);
132         p.drawPixmap(
133                     (w - play.width() * ratio) / 2,
134                     (h * 2/3 - play.height() * ratio) / 2,
135                     play
136                     );
137         p.restore();
138     }
139
140     QRect nameBox = rect();
141     nameBox.adjust(0, 0, 0, -h*2/3);
142     nameBox.translate(0, h - nameBox.height());
143     p.save();
144     p.setPen(Qt::NoPen);
145     p.setBrush(QColor(0, 0, 0, 128));
146     p.drawRect(nameBox);
147     p.restore();
148
149     QString name = videoSource->getName();
150     bool tooBig = false;
151     p.save();
152     p.setFont(FontUtils::medium());
153     QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
154     if (textBox.height() > nameBox.height()) {
155         p.setFont(font());
156         textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
157         if (textBox.height() > nameBox.height()) {
158             p.setClipRect(nameBox);
159             tooBig = true;
160         }
161     }
162     p.setPen(Qt::white);
163     if (tooBig)
164         p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
165     else
166         p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
167     p.restore();
168
169     if (hasFocus()) {
170         p.save();
171         QPen pen;
172         pen.setBrush(palette().highlight());
173         pen.setWidth(2);
174         p.setPen(pen);
175         p.drawRect(rect());
176         p.restore();
177     }
178 }