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