Rewrite image provider.

Fixes #42.
This commit is contained in:
Black Hat 2018-09-07 13:50:06 +08:00
parent ffe10e9514
commit 8fb16d0700
6 changed files with 21 additions and 52 deletions

View File

@ -26,7 +26,6 @@ SOURCES += src/main.cpp \
src/roomlistmodel.cpp \ src/roomlistmodel.cpp \
src/imageprovider.cpp \ src/imageprovider.cpp \
src/messageeventmodel.cpp \ src/messageeventmodel.cpp \
src/imageproviderconnection.cpp \
src/emojimodel.cpp \ src/emojimodel.cpp \
src/matriqueroom.cpp \ src/matriqueroom.cpp \
src/userlistmodel.cpp \ src/userlistmodel.cpp \
@ -87,7 +86,6 @@ HEADERS += \
src/roomlistmodel.h \ src/roomlistmodel.h \
src/imageprovider.h \ src/imageprovider.h \
src/messageeventmodel.h \ src/messageeventmodel.h \
src/imageproviderconnection.h \
src/emojimodel.h \ src/emojimodel.h \
src/matriqueroom.h \ src/matriqueroom.h \
src/userlistmodel.h \ src/userlistmodel.h \

View File

@ -13,13 +13,13 @@
using QMatrixClient::MediaThumbnailJob; using QMatrixClient::MediaThumbnailJob;
ImageProvider::ImageProvider(QObject* parent) ImageProvider::ImageProvider(QObject* parent)
: QQuickImageProvider( : QObject(parent),
QQuickImageProvider(
QQmlImageProviderBase::Image, QQmlImageProviderBase::Image,
QQmlImageProviderBase::ForceAsynchronousImageLoading) { QQmlImageProviderBase::ForceAsynchronousImageLoading) {
#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) #if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
qRegisterMetaType<MediaThumbnailJob*>(); qRegisterMetaType<MediaThumbnailJob*>();
#endif #endif
m_connection = new ImageProviderConnection();
} }
QImage ImageProvider::requestImage(const QString& id, QSize* pSize, QImage ImageProvider::requestImage(const QString& id, QSize* pSize,
@ -49,13 +49,10 @@ QImage ImageProvider::requestImage(const QString& id, QSize* pSize,
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
QMetaObject::invokeMethod( QMetaObject::invokeMethod(
m_connection, m_connection,
[=] { [=] { return m_connection->getThumbnail(mxcUri, requestedSize); },
return m_connection->getConnection()->getThumbnail(mxcUri,
requestedSize);
},
Qt::BlockingQueuedConnection, &job); Qt::BlockingQueuedConnection, &job);
#else #else
QMetaObject::invokeMethod(m_connection->getConnection(), "getThumbnail", QMetaObject::invokeMethod(m_connection, "getThumbnail",
Qt::BlockingQueuedConnection, Qt::BlockingQueuedConnection,
Q_RETURN_ARG(MediaThumbnailJob*, job), Q_RETURN_ARG(MediaThumbnailJob*, job),
Q_ARG(QUrl, mxcUri), Q_ARG(QSize, requestedSize)); Q_ARG(QUrl, mxcUri), Q_ARG(QSize, requestedSize));

View File

@ -6,9 +6,11 @@
#include <QtQuick/QQuickImageProvider> #include <QtQuick/QQuickImageProvider>
#include "connection.h" #include "connection.h"
#include "imageproviderconnection.h"
class ImageProvider : public QQuickImageProvider { class ImageProvider : public QObject, public QQuickImageProvider {
Q_OBJECT
Q_PROPERTY(QMatrixClient::Connection* connection READ connection WRITE
setConnection NOTIFY connectionChanged)
public: public:
explicit ImageProvider(QObject* parent = nullptr); explicit ImageProvider(QObject* parent = nullptr);
@ -17,11 +19,20 @@ class ImageProvider : public QQuickImageProvider {
void initializeEngine(QQmlEngine* engine, const char* uri); void initializeEngine(QQmlEngine* engine, const char* uri);
ImageProviderConnection* getConnection() { return m_connection; } QMatrixClient::Connection* connection() { return m_connection; }
void setConnection(QMatrixClient::Connection* newConnection) {
if (m_connection != newConnection) {
m_connection = newConnection;
emit connectionChanged();
}
}
signals:
void connectionChanged();
private: private:
QReadWriteLock m_lock; QReadWriteLock m_lock;
ImageProviderConnection* m_connection; QMatrixClient::Connection* m_connection;
}; };
#endif // IMAGEPROVIDER_H #endif // IMAGEPROVIDER_H

View File

@ -1,6 +0,0 @@
#include "imageproviderconnection.h"
ImageProviderConnection::ImageProviderConnection(QObject* parent)
: QObject(parent) {}
ImageProviderConnection::~ImageProviderConnection() {}

View File

@ -1,30 +0,0 @@
#ifndef IMAGEPROVIDERCONNECTION_H
#define IMAGEPROVIDERCONNECTION_H
#include <QObject>
#include "connection.h"
class ImageProviderConnection : public QObject {
Q_OBJECT
Q_PROPERTY(QMatrixClient::Connection* connection READ getConnection WRITE
setConnection NOTIFY connectionChanged)
public:
explicit ImageProviderConnection(QObject* parent = nullptr);
~ImageProviderConnection();
QMatrixClient::Connection* getConnection() { return m_connection; }
void setConnection(QMatrixClient::Connection* connection) {
m_connection = connection;
emit connectionChanged();
}
private:
QMatrixClient::Connection* m_connection;
signals:
void connectionChanged();
};
#endif // IMAGEPROVIDERCONNECTION_H

View File

@ -3,9 +3,9 @@
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQmlContext> #include <QQmlContext>
#include "imageitem.h"
#include "controller.h" #include "controller.h"
#include "emojimodel.h" #include "emojimodel.h"
#include "imageitem.h"
#include "imageprovider.h" #include "imageprovider.h"
#include "matriqueroom.h" #include "matriqueroom.h"
#include "messageeventmodel.h" #include "messageeventmodel.h"
@ -52,8 +52,7 @@ int main(int argc, char *argv[]) {
ImageProvider *m_provider = new ImageProvider(); ImageProvider *m_provider = new ImageProvider();
engine.rootContext()->setContextProperty("imageProvider", engine.rootContext()->setContextProperty("imageProvider", m_provider);
m_provider->getConnection());
engine.addImageProvider(QLatin1String("mxc"), m_provider); engine.addImageProvider(QLatin1String("mxc"), m_provider);