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