tomilov

videoplayer image provider

Sep 26th, 2017
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #include <QtQuick>
  4.  
  5. Q_DECLARE_LOGGING_CATEGORY(videoPlayerImageProviderCategory)
  6.  
  7. class VideoPlayerImageProvider
  8.         : public QQuickImageProvider
  9. {
  10.  
  11.     Q_DECLARE_TR_FUNCTIONS(VideoPlayerImageProvider)
  12.  
  13. public :
  14.  
  15.     VideoPlayerImageProvider(QQmlEngine * const engine);
  16.  
  17.     QImage requestImage(const QString & id, QSize * size, const QSize & requestedSize) Q_DECL_OVERRIDE;
  18.  
  19. private :
  20.  
  21.     QQmlEngine * const engine;
  22.     QHash< QString, QImage > images;
  23.  
  24. };
  25.  
  26. #include "videoplayerimageprovider.hpp"
  27.  
  28. Q_LOGGING_CATEGORY(videoPlayerImageProviderCategory, "videoPlayerImageProvider")
  29.  
  30. VideoPlayerImageProvider::VideoPlayerImageProvider(QQmlEngine * const engine)
  31.     : QQuickImageProvider{QQuickImageProvider::Image}
  32.     , engine{engine}
  33. { ; }
  34.  
  35. QImage VideoPlayerImageProvider::requestImage(const QString & id, QSize * size, const QSize & requestedSize)
  36. {
  37.     const auto selector = QQmlFileSelector::get(engine)->selector();
  38.     const auto imagePath = selector->select(QStringLiteral(":/images/videoplayer/%1").arg(id));
  39. #if 0
  40.     qDebug().noquote()
  41.             << tr("Selected filepath: %1. Installed file selectors: %2")
  42.                .arg(imagePath, selector->allSelectors().join(QStringLiteral(", ")));
  43. #endif
  44.     QImage & image = images[imagePath];
  45.     if (image.isNull()) {
  46.         QImageReader imageReader{imagePath};
  47.         if (!imageReader.read(&image)) {
  48.             qCCritical(videoPlayerImageProviderCategory).noquote()
  49.                     << tr("Unable to read image from %1: %3 (%2)")
  50.                        .arg(imagePath, imageReader.errorString()).arg(imageReader.error());
  51.         }
  52.     }
  53.     if (size) {
  54.         *size = image.size();
  55.     }
  56.     if (requestedSize.isEmpty() || (image.size() == requestedSize)) { // avoid returning invalid QImage
  57.         return image;
  58.     }
  59.     return image.scaled(requestedSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  60. }
RAW Paste Data