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);
37 connect(this, SIGNAL(activated()), SLOT(activate()));
40 void VideoSourceWidget::activate() {
41 emit activated(videoSource);
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);
51 Video *video = videos.at(0);
52 lastPixelRatio = devicePixelRatio();
54 video->loadThumb(size(), lastPixelRatio)
55 .then([this](auto variant) { setPixmapData(variant.toByteArray()); })
56 .onFailed([](auto msg) { qDebug() << msg; })
63 void VideoSourceWidget::setPixmapData(const QByteArray &bytes) {
64 pixmap.loadFromData(bytes);
65 pixmap.setDevicePixelRatio(lastPixelRatio);
69 void VideoSourceWidget::loadPreview() {
70 connect(videoSource, SIGNAL(gotVideos(QVector<Video*>)),
71 SLOT(previewVideo(QVector<Video*>)), Qt::UniqueConnection);
72 videoSource->loadVideos(1, 1);
75 QPixmap VideoSourceWidget::playPixmap() {
76 const int s = height() / 2;
77 const int padding = s / 8;
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);
85 polygon << QPoint(padding, padding)
86 << QPoint(s - padding, s / 2)
87 << QPoint(padding, s - padding);
88 painter.setRenderHints(QPainter::Antialiasing, true);
90 // QColor color = pressed ? Qt::black : Qt::white;
91 QColor color = Qt::white;
92 painter.setBrush(color);
96 pen.setJoinStyle(Qt::RoundJoin);
97 pen.setCapStyle(Qt::RoundCap);
99 painter.drawPolygon(polygon);
103 void VideoSourceWidget::paintEvent(QPaintEvent *event) {
104 GridWidget::paintEvent(event);
105 // if (devicePixelRatio() != lastPixelRatio) loadPreview();
107 if (pixmap.isNull()) return;
111 qreal ratio = lastPixelRatio;
112 int w = width() * ratio;
113 int h = height() * ratio;
117 int wDiff = pixmap.width() - w;
118 if (wDiff > 0) xOffset = wDiff / 2;
119 else xOrigin = -wDiff / 2;
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);
131 QPixmap play = playPixmap();
135 (w - play.width() * ratio) / 2,
136 (h * 2/3 - play.height() * ratio) / 2,
142 QRect nameBox = rect();
143 nameBox.adjust(0, 0, 0, -h*2/3);
144 nameBox.translate(0, h - nameBox.height());
147 p.setBrush(QColor(0, 0, 0, 128));
151 QString name = videoSource->getName();
154 p.setFont(FontUtils::big());
155 QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
156 if (textBox.height() > nameBox.height()) {
158 textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
159 if (textBox.height() > nameBox.height()) {
160 p.setClipRect(nameBox);
166 p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
168 p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
174 pen.setBrush(palette().highlight());