ThumbnailImageProvider.cpp 1.31 KB
Newer Older
luyikei's avatar
luyikei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include "ThumbnailImageProvider.h"

#include "Workflow/MainWorkflow.h"
#include "Main/Core.h"

ThumbnailImageProvider::ThumbnailImageProvider()
    : QQuickImageProvider( QQuickImageProvider::Pixmap )
{
    connect( Core::instance()->workflow(), &MainWorkflow::thumbnailUpdated, this, [this]
             ( const QString& uuid, quint32 pos, const QPixmap& pixmap )
    {
        auto id = uuid + "/" + QString::number( pos );

        auto it = m_pixMap.find( id );
        if ( it == m_pixMap.end() )
            m_pixMap.insert( id, pixmap );

        emit imageReady( uuid, pos );
    }, Qt::DirectConnection );
}

QPixmap
ThumbnailImageProvider::requestPixmap( const QString& id, QSize* size, const QSize& requestedSize )
{
    QString tmp = id;
    tmp.replace( "%7B", "{" );
    tmp.replace( "%7D", "}" );

    auto it = m_pixMap.find( tmp );
    if ( it == m_pixMap.end() )
    {
        *size = QSize( requestedSize.width(), requestedSize.height() );
        return QPixmap( requestedSize );
    }

    auto pixmap = it.value();
    *size = pixmap.size();
    if ( requestedSize.isValid() == true )
        return pixmap.scaled( requestedSize );
    return pixmap;
}

bool
ThumbnailImageProvider::hasImage( const QString& uuid, quint32 pos )
{
    return m_pixMap.find( uuid + "/" + QString::number( pos ) ) != m_pixMap.end();
}