]> git.sur5r.net Git - minitube/blobdiff - src/videosourcewidget.cpp
Switch watchfile to Github API
[minitube] / src / videosourcewidget.cpp
index 90b893b0f6145632462f1a16b5af99a7280fb522..2e4adf7613ad8f28615eddb3c0241f2d49a29c61 100644 (file)
@@ -1,47 +1,84 @@
+/* $BEGIN_LICENSE
+
+This file is part of Minitube.
+Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
+
+Minitube is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Minitube is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+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 "variantpromise.h"
+#include "video.h"
+#include "videosource.h"
 
 VideoSourceWidget::VideoSourceWidget(VideoSource *videoSource, QWidget *parent)
-    : QWidget(parent),
+    : GridWidget(parent),
       videoSource(videoSource),
-      hovered(false),
-      pressed(false) {
-
+      lastPixelRatio(0) {
+    videoSource->setParent(this);
     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
-    setCursor(Qt::PointingHandCursor);
-    setFocusPolicy(Qt::StrongFocus);
-
-    connect(videoSource, SIGNAL(gotVideos(QList<Video*>)),
-            SLOT(previewVideo(QList<Video*>)), Qt::UniqueConnection);
-    videoSource->loadVideos(1, 1);
+    loadPreview();
+    connect(this, SIGNAL(activated()), SLOT(activate()));
 }
 
 void VideoSourceWidget::activate() {
     emit activated(videoSource);
 }
 
-void VideoSourceWidget::previewVideo(QList<Video*> videos) {
+void VideoSourceWidget::previewVideo(const QVector<Video *> &videos) {
     videoSource->disconnect();
-    if (videos.isEmpty()) return;
-    video = videos.first();
-    connect(video, SIGNAL(gotMediumThumbnail(QByteArray)),
-            SLOT(setPixmapData(QByteArray)), Qt::UniqueConnection);
-    video->loadMediumThumbnail();
+    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(QByteArray bytes) {
-    video->deleteLater();
-    video = 0;
+void VideoSourceWidget::setPixmapData(const QByteArray &bytes) {
     pixmap.loadFromData(bytes);
+    pixmap.setDevicePixelRatio(lastPixelRatio);
     update();
 }
 
+void VideoSourceWidget::loadPreview() {
+    connect(videoSource, SIGNAL(gotVideos(QVector<Video*>)),
+            SLOT(previewVideo(QVector<Video*>)), Qt::UniqueConnection);
+    videoSource->loadVideos(1, 1);
+}
+
 QPixmap VideoSourceWidget::playPixmap() {
     const int s = height() / 2;
     const int padding = s / 8;
-    QPixmap playIcon = QPixmap(s, s);
+
+    qreal ratio = devicePixelRatio();
+    QPixmap playIcon = QPixmap(s * ratio, s * ratio);
+    playIcon.setDevicePixelRatio(ratio);
     playIcon.fill(Qt::transparent);
     QPainter painter(&playIcon);
     QPolygon polygon;
@@ -63,13 +100,17 @@ QPixmap VideoSourceWidget::playPixmap() {
     return playIcon;
 }
 
-void VideoSourceWidget::paintEvent(QPaintEvent *) {
+void VideoSourceWidget::paintEvent(QPaintEvent *event) {
+    GridWidget::paintEvent(event);
+    // if (devicePixelRatio() != lastPixelRatio) loadPreview();
+
     if (pixmap.isNull()) return;
 
     QPainter p(this);
 
-    const int w = width();
-    const int h = height();
+    qreal ratio = lastPixelRatio;
+    int w = width() * ratio;
+    int h = height() * ratio;
 
     int xOffset = 0;
     int xOrigin = 0;
@@ -79,17 +120,20 @@ void VideoSourceWidget::paintEvent(QPaintEvent *) {
     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);
 
+    w = width();
+    h = height();
+
     if (hovered) {
         QPixmap play = playPixmap();
         p.save();
         p.setOpacity(.5);
         p.drawPixmap(
-                    (w - play.width()) / 2,
-                    (h * 2/3 - play.height()) / 2,
+                    (w - play.width() * ratio) / 2,
+                    (h * 2/3 - play.height() * ratio) / 2,
                     play
                     );
         p.restore();
@@ -107,7 +151,7 @@ void VideoSourceWidget::paintEvent(QPaintEvent *) {
     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());
@@ -134,39 +178,3 @@ void VideoSourceWidget::paintEvent(QPaintEvent *) {
         p.restore();
     }
 }
-
-void VideoSourceWidget::mouseMoveEvent (QMouseEvent *event) {
-    QWidget::mouseMoveEvent(event);
-    hovered = rect().contains(event->pos());
-}
-
-void VideoSourceWidget::mousePressEvent(QMouseEvent *event) {
-    QWidget::mousePressEvent(event);
-    if (event->button() != Qt::LeftButton) return;
-    pressed = true;
-    update();
-}
-
-void VideoSourceWidget::mouseReleaseEvent(QMouseEvent *event) {
-    QWidget::mouseReleaseEvent(event);
-    if (event->button() != Qt::LeftButton) return;
-    pressed = false;
-    if (hovered) emit activated(videoSource);
-}
-
-void VideoSourceWidget::leaveEvent(QEvent *event) {
-    QWidget::leaveEvent(event);
-    hovered = false;
-    update();
-}
-
-void VideoSourceWidget::enterEvent(QEvent *event) {
-    QWidget::enterEvent(event);
-    hovered = true;
-    update();
-}
-
-void VideoSourceWidget::keyReleaseEvent(QKeyEvent *event) {
-    if (event->key() == Qt::Key_Return)
-        emit activated(videoSource);
-}