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 "fontutils.h"
24 #include "httputils.h"
25 #include "iconutils.h"
26 #include "variantpromise.h"
28 #include "videosource.h"
30 VideoSourceWidget::VideoSourceWidget(VideoSource *videoSource, QWidget *parent)
32 videoSource(videoSource),
34 videoSource->setParent(this);
35 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
36 connect(this, SIGNAL(activated()), SLOT(activate()));
39 void VideoSourceWidget::activate() {
40 emit activated(videoSource);
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);
50 Video *video = videos.at(0);
51 lastPixelRatio = devicePixelRatio();
53 video->loadThumb(size(), lastPixelRatio)
54 .then([this](auto variant) { setPixmapData(variant.toByteArray()); })
55 .onFailed([](auto msg) { qDebug() << msg; })
56 .finally([this, videos] {
63 void VideoSourceWidget::setPixmapData(const QByteArray &bytes) {
64 pixmap.loadFromData(bytes);
65 pixmap.setDevicePixelRatio(lastPixelRatio);
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"));
76 connect(videoSource, SIGNAL(gotVideos(QVector<Video *>)), SLOT(previewVideo(QVector<Video *>)),
77 Qt::UniqueConnection);
78 videoSource->loadVideos(1, 1);
83 QPixmap VideoSourceWidget::playPixmap() {
84 const int s = height() / 2;
85 const int padding = s / 8;
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);
93 polygon << QPoint(padding, padding)
94 << QPoint(s - padding, s / 2)
95 << QPoint(padding, s - padding);
96 painter.setRenderHints(QPainter::Antialiasing, true);
98 // QColor color = pressed ? Qt::black : Qt::white;
99 QColor color = Qt::white;
100 painter.setBrush(color);
104 pen.setJoinStyle(Qt::RoundJoin);
105 pen.setCapStyle(Qt::RoundCap);
107 painter.drawPolygon(polygon);
111 void VideoSourceWidget::paintEvent(QPaintEvent *event) {
112 GridWidget::paintEvent(event);
113 // if (devicePixelRatio() != lastPixelRatio) loadPreview();
115 if (pixmap.isNull()) return;
119 qreal ratio = lastPixelRatio;
120 int w = width() * ratio;
121 int h = height() * ratio;
125 int wDiff = pixmap.width() - w;
126 if (wDiff > 0) xOffset = wDiff / 2;
127 else xOrigin = -wDiff / 2;
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);
139 QPixmap play = playPixmap();
143 (w - play.width() * ratio) / 2,
144 (h * 2/3 - play.height() * ratio) / 2,
150 QRect nameBox = rect();
151 nameBox.adjust(0, 0, 0, -h*2/3);
152 nameBox.translate(0, h - nameBox.height());
155 p.setBrush(QColor(0, 0, 0, 128));
159 QString name = videoSource->getName();
162 p.setFont(FontUtils::big());
163 QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
164 if (textBox.height() > nameBox.height()) {
166 textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
167 if (textBox.height() > nameBox.height()) {
168 p.setClipRect(nameBox);
174 p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
176 p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
182 pen.setBrush(palette().highlight());