/**************************************************************************
 **                                                                      **
 ** Copyright (C) 2011-2021 Lukas Spies                                  **
 ** Contact: http://photoqt.org                                          **
 **                                                                      **
 ** This file is part of PhotoQt.                                        **
 **                                                                      **
 ** PhotoQt 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 2 of the License, or    **
 ** (at your option) any later version.                                  **
 **                                                                      **
 ** PhotoQt 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 PhotoQt. If not, see <http://www.gnu.org/licenses/>.      **
 **                                                                      **
 **************************************************************************/

#include "imageproviderthumb.h"
#include <QDebug>
//继承自QQuickImageProvider,与QQuickImageResponse结合起来使用，实现异步加载图片到qml
QQuickImageResponse *PQAsyncImageProviderThumb::requestImageResponse(const QString &url, const QSize &requestedSize)
{
    ScreenImageProvider *response = new ScreenImageProvider(url, requestedSize);
    //开线程池
    QThreadPool::globalInstance()->setMaxThreadCount(1);
    pool.start(response);
    return response;
}

ScreenImageProvider::ScreenImageProvider(const QString &url, const QSize &requestedSize)
    : m_url(url), m_requestedSize(requestedSize)
{
    setAutoDelete(false);
}

ScreenImageProvider::~ScreenImageProvider() {}
QQuickTextureFactory *ScreenImageProvider::textureFactory() const
{
    return QQuickTextureFactory::textureFactoryForImage(m_image);
}

void ScreenImageProvider::run()
{
    if (THEMEDARK == Variable::g_themeStyle || THEMEBLACK == Variable::g_themeStyle) {
        m_image.load(Variable::DAMAGED_IMAGE_BLACK);
    } else {
        m_image.load(Variable::DAMAGED_IMAGE_WHITE);
    }
    if (m_url.isEmpty()) {
        Q_EMIT finished();
        return;
    }
    if (Variable::g_isClosing == true) {
        Q_EMIT finished();
        return;
    }
    //如果图片已经缓存过，则直接从map里取
    if (Variable::imageHasLoaded(m_url)) {
        m_image = Variable::getLoadedImage(m_url);
        Q_EMIT finished();
        return;
    }
    QString realFormat = File::realFormat(m_url);
    //如果时多页tiff
    if (realFormat == "tiff" || realFormat == "tif") {
        if (Variable::imageHasLoaded(m_url + "0")) {
            m_image = Variable::getLoadedImage(m_url);
            Q_EMIT finished();
            return;
        }
    }
    MatAndFileinfo maf = File::loadImage(m_url, realFormat);
    bool openSuccess = maf.openSuccess; //判断是否打开失败-损坏
    if (!openSuccess) {
        Q_EMIT finished();
        return;
    }
    QPixmap nowImage = Processing::converFormat(maf.mat);


    if (nowImage.isNull()) {
        Q_EMIT finished();
        return;
    }
    if (!(realFormat == "tiff" || realFormat == "tif")) {
        Variable::setLoadedImageMap(m_url, nowImage.toImage());
    }
    m_image = nowImage.toImage();
    Q_EMIT finished();
}
