]> git.sur5r.net Git - minitube/blob - src/videosourcewidget.cpp
Upload 3.9.3-2 to unstable
[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 "fontutils.h"
23 #include "http.h"
24 #include "httputils.h"
25 #include "iconutils.h"
26 #include "variantpromise.h"
27 #include "video.h"
28 #include "videosource.h"
29
30 VideoSourceWidget::VideoSourceWidget(VideoSource *videoSource, QWidget *parent)
31     : GridWidget(parent),
32       videoSource(videoSource),
33       lastPixelRatio(0) {
34     videoSource->setParent(this);
35     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
36     connect(this, SIGNAL(activated()), SLOT(activate()));
37 }
38
39 void VideoSourceWidget::activate() {
40     emit activated(videoSource);
41 }
42
43 void VideoSourceWidget::previewVideo(const QVector<Video *> &videos) {
44     videoSource->disconnect();
45     if (videos.isEmpty()) {
46         qDebug() << "Unavailable video source" << videoSource->getName();
47         emit unavailable(this);
48         return;
49     }
50     Video *video = videos.at(0);
51     lastPixelRatio = devicePixelRatio();
52
53     video->loadThumb(size(), lastPixelRatio)
54             .then([this](auto variant) { setPixmapData(variant.toByteArray()); })
55             .onFailed([](auto msg) { qDebug() << msg; })
56             .finally([this, videos] {
57                 for (auto v : videos)
58                     v->deleteLater();
59                 emit previewLoaded();
60             });
61 }
62
63 void VideoSourceWidget::setPixmapData(const QByteArray &bytes) {
64     pixmap.loadFromData(bytes);
65     pixmap.setDevicePixelRatio(lastPixelRatio);
66     update();
67 }
68
69 EmptyPromise *VideoSourceWidget::loadPreview() {
70     auto promise = new EmptyPromise(this);
71     connect(this, &VideoSourceWidget::previewLoaded, promise, &EmptyPromise::resolve);
72     connect(this, &VideoSourceWidget::unavailable, promise, [promise] {
73         promise->reject(staticMetaObject.className() + QLatin1String(" unavailable"));
74     });
75
76     connect(videoSource, SIGNAL(gotVideos(QVector<Video *>)), SLOT(previewVideo(QVector<Video *>)),
77             Qt::UniqueConnection);
78     videoSource->loadVideos(1, 1);
79
80     return promise;
81 }
82
83 QPixmap VideoSourceWidget::playPixmap() {
84     const int s = height() / 2;
85     const int padding = s / 8;
86
87     qreal ratio = devicePixelRatio();
88     QPixmap playIcon = QPixmap(s * ratio, s * ratio);
89     playIcon.setDevicePixelRatio(ratio);
90     playIcon.fill(Qt::transparent);
91     QPainter painter(&playIcon);
92     QPolygon polygon;
93     polygon << QPoint(padding, padding)
94             << QPoint(s - padding, s / 2)
95             << QPoint(padding, s - padding);
96     painter.setRenderHints(QPainter::Antialiasing, true);
97
98     // QColor color = pressed ? Qt::black : Qt::white;
99     QColor color = Qt::white;
100     painter.setBrush(color);
101     QPen pen;
102     pen.setColor(color);
103     pen.setWidth(10);
104     pen.setJoinStyle(Qt::RoundJoin);
105     pen.setCapStyle(Qt::RoundCap);
106     painter.setPen(pen);
107     painter.drawPolygon(polygon);
108     return playIcon;
109 }
110
111 void VideoSourceWidget::paintEvent(QPaintEvent *event) {
112     GridWidget::paintEvent(event);
113     // if (devicePixelRatio() != lastPixelRatio) loadPreview();
114
115     if (pixmap.isNull()) return;
116
117     QPainter p(this);
118
119     qreal ratio = lastPixelRatio;
120     int w = width() * ratio;
121     int h = height() * ratio;
122
123     int xOffset = 0;
124     int xOrigin = 0;
125     int wDiff = pixmap.width() - w;
126     if (wDiff > 0) xOffset = wDiff / 2;
127     else xOrigin = -wDiff / 2;
128     int yOffset = 0;
129     int yOrigin = 0;
130     int hDiff = pixmap.height() - h;
131     if (hDiff > 0) yOffset = hDiff / 2;
132     else yOrigin = -hDiff / 2;
133     p.drawPixmap(xOrigin, yOrigin, pixmap, xOffset, yOffset, w, h);
134
135     w = width();
136     h = height();
137
138     if (hovered) {
139         QPixmap play = playPixmap();
140         p.save();
141         p.setOpacity(.5);
142         p.drawPixmap(
143                     (w - play.width() * ratio) / 2,
144                     (h * 2/3 - play.height() * ratio) / 2,
145                     play
146                     );
147         p.restore();
148     }
149
150     QRect nameBox = rect();
151     nameBox.adjust(0, 0, 0, -h*2/3);
152     nameBox.translate(0, h - nameBox.height());
153     p.save();
154     p.setPen(Qt::NoPen);
155     p.setBrush(QColor(0, 0, 0, 128));
156     p.drawRect(nameBox);
157     p.restore();
158
159     QString name = videoSource->getName();
160     bool tooBig = false;
161     p.save();
162     p.setFont(FontUtils::big());
163     QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
164     if (textBox.height() > nameBox.height()) {
165         p.setFont(font());
166         textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
167         if (textBox.height() > nameBox.height()) {
168             p.setClipRect(nameBox);
169             tooBig = true;
170         }
171     }
172     p.setPen(Qt::white);
173     if (tooBig)
174         p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
175     else
176         p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
177     p.restore();
178
179     if (hasFocus()) {
180         p.save();
181         QPen pen;
182         pen.setBrush(palette().highlight());
183         pen.setWidth(2);
184         p.setPen(pen);
185         p.drawRect(rect());
186         p.restore();
187     }
188 }