]> git.sur5r.net Git - minitube/blobdiff - src/videosourcewidget.cpp
Switch watchfile to Github API
[minitube] / src / videosourcewidget.cpp
index ce1a92fc498b143b56a1de64638bfc74df9c1e26..2e4adf7613ad8f28615eddb3c0241f2d49a29c61 100644 (file)
@@ -19,25 +19,21 @@ along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
 $END_LICENSE */
 
 #include "videosourcewidget.h"
-#include "videosource.h"
-#include "video.h"
 #include "fontutils.h"
+#include "http.h"
+#include "httputils.h"
 #include "iconutils.h"
-#include "networkaccess.h"
-
-namespace The {
-NetworkAccess* http();
-}
+#include "variantpromise.h"
+#include "video.h"
+#include "videosource.h"
 
 VideoSourceWidget::VideoSourceWidget(VideoSource *videoSource, QWidget *parent)
     : GridWidget(parent),
       videoSource(videoSource),
       lastPixelRatio(0) {
-
+    videoSource->setParent(this);
     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
-
     loadPreview();
-
     connect(this, SIGNAL(activated()), SLOT(activate()));
 }
 
@@ -45,17 +41,23 @@ void VideoSourceWidget::activate() {
     emit activated(videoSource);
 }
 
-void VideoSourceWidget::previewVideo(const QList<Video *> &videos) {
+void VideoSourceWidget::previewVideo(const QVector<Video *> &videos) {
     videoSource->disconnect();
-    if (videos.isEmpty()) return;
-    Video *video = videos.first();
-    lastPixelRatio = window()->devicePixelRatio();
-    bool needLargeThumb = lastPixelRatio > 1.0 || window()->width() > 2000;
-    QString url =  needLargeThumb ? video->largeThumbnailUrl() : video->mediumThumbnailUrl();
-    if (url.isEmpty()) url = video->mediumThumbnailUrl();
-    video->deleteLater();
-    QObject *reply = The::http()->get(url);
-    connect(reply, SIGNAL(data(QByteArray)), SLOT(setPixmapData(QByteArray)));
+    if (videos.isEmpty()) {
+        qDebug() << "Unavailable video source" << videoSource->getName();
+        emit unavailable(this);
+        return;
+    }
+    Video *video = videos.at(0);
+    lastPixelRatio = devicePixelRatio();
+
+    video->loadThumb(size(), lastPixelRatio)
+            .then([this](auto variant) { setPixmapData(variant.toByteArray()); })
+            .onFailed([](auto msg) { qDebug() << msg; })
+            .finally([videos] {
+                for (auto v : videos)
+                    v->deleteLater();
+            });
 }
 
 void VideoSourceWidget::setPixmapData(const QByteArray &bytes) {
@@ -65,8 +67,8 @@ void VideoSourceWidget::setPixmapData(const QByteArray &bytes) {
 }
 
 void VideoSourceWidget::loadPreview() {
-    connect(videoSource, SIGNAL(gotVideos(QList<Video*>)),
-            SLOT(previewVideo(QList<Video*>)), Qt::UniqueConnection);
+    connect(videoSource, SIGNAL(gotVideos(QVector<Video*>)),
+            SLOT(previewVideo(QVector<Video*>)), Qt::UniqueConnection);
     videoSource->loadVideos(1, 1);
 }
 
@@ -74,7 +76,7 @@ QPixmap VideoSourceWidget::playPixmap() {
     const int s = height() / 2;
     const int padding = s / 8;
 
-    qreal ratio = window()->devicePixelRatio();
+    qreal ratio = devicePixelRatio();
     QPixmap playIcon = QPixmap(s * ratio, s * ratio);
     playIcon.setDevicePixelRatio(ratio);
     playIcon.fill(Qt::transparent);
@@ -100,8 +102,9 @@ QPixmap VideoSourceWidget::playPixmap() {
 
 void VideoSourceWidget::paintEvent(QPaintEvent *event) {
     GridWidget::paintEvent(event);
+    // if (devicePixelRatio() != lastPixelRatio) loadPreview();
+
     if (pixmap.isNull()) return;
-    if (window()->devicePixelRatio() != lastPixelRatio) loadPreview();
 
     QPainter p(this);
 
@@ -117,7 +120,7 @@ void VideoSourceWidget::paintEvent(QPaintEvent *event) {
     int yOffset = 0;
     int yOrigin = 0;
     int hDiff = pixmap.height() - h;
-    if (hDiff > 0) yOffset = hDiff / 4;
+    if (hDiff > 0) yOffset = hDiff / 2;
     else yOrigin = -hDiff / 2;
     p.drawPixmap(xOrigin, yOrigin, pixmap, xOffset, yOffset, w, h);
 
@@ -148,7 +151,7 @@ void VideoSourceWidget::paintEvent(QPaintEvent *event) {
     QString name = videoSource->getName();
     bool tooBig = false;
     p.save();
-    p.setFont(FontUtils::medium());
+    p.setFont(FontUtils::big());
     QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
     if (textBox.height() > nameBox.height()) {
         p.setFont(font());