Spectral/matrix/imageprovider.cpp

77 lines
2.1 KiB
C++
Raw Normal View History

2018-03-02 08:56:36 +00:00
#include "imageprovider.h"
#include <QtCore/QWaitCondition>
#include <QtCore/QDebug>
#include <QMetaObject>
2018-03-15 09:10:27 +00:00
#include "jobs/mediathumbnailjob.h"
#include "connection.h"
2018-03-02 08:56:36 +00:00
using QMatrixClient::MediaThumbnailJob;
ImageProviderConnection::ImageProviderConnection(QObject* parent) : QObject(parent) {
}
ImageProviderConnection::~ImageProviderConnection() {
}
ImageProvider::ImageProvider(QObject* parent)
2018-03-02 08:56:36 +00:00
: QQuickImageProvider(QQmlImageProviderBase::Image,
QQmlImageProviderBase::ForceAsynchronousImageLoading)
{
#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
qRegisterMetaType<MediaThumbnailJob*>();
#endif
m_connection = new ImageProviderConnection();
2018-03-02 08:56:36 +00:00
}
QImage ImageProvider::requestImage(const QString& id,
QSize* pSize, const QSize& requestedSize)
{
if (!id.startsWith("mxc://"))
{
qWarning() << "ImageProvider: won't fetch an invalid id:" << id
<< "doesn't follow server/mediaId pattern";
return {};
}
QUrl mxcUri { id };
qDebug() << "ImageProvider::requestImage:" << mxcUri.toString();
MediaThumbnailJob* job = nullptr;
QReadLocker locker(&m_lock);
2018-03-02 08:56:36 +00:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
QMetaObject::invokeMethod(m_connection,
[=] { return m_connection->getConnection()->getThumbnail(mxcUri, requestedSize); },
2018-03-02 08:56:36 +00:00
Qt::BlockingQueuedConnection, &job);
#else
QMetaObject::invokeMethod(m_connection, "getThumbnail",
Qt::BlockingQueuedConnection, Q_RETURN_ARG(MediaThumbnailJob*, job),
Q_ARG(QUrl, mxcUri), Q_ARG(QSize, requestedSize));
#endif
if (!job)
{
qDebug() << "ImageProvider: failed to send a request";
return {};
}
QImage result;
{
QWaitCondition condition; // The most compact way to block on a signal
job->connect(job, &MediaThumbnailJob::finished, job, [&] {
result = job->thumbnail();
condition.wakeAll();
});
condition.wait(&m_lock);
}
2018-03-06 11:11:39 +00:00
if(pSize != nullptr)
2018-03-02 08:56:36 +00:00
*pSize = result.size();
return result;
}