mirror of
https://github.com/nextcloud/desktop.git
synced 2024-12-18 11:51:51 +03:00
Convert ImageProvider in an async image provider using an internal image response
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
parent
88a2904651
commit
896e61ae04
2 changed files with 47 additions and 24 deletions
|
@ -1613,35 +1613,53 @@ int UserModel::findUserIdForAccount(AccountState *account) const
|
|||
}
|
||||
/*-------------------------------------------------------------------------------------*/
|
||||
|
||||
ImageProvider::ImageProvider()
|
||||
: QQuickImageProvider(QQuickImageProvider::Image)
|
||||
class ImageResponse : public QQuickImageResponse
|
||||
{
|
||||
}
|
||||
public:
|
||||
ImageResponse(const QString &id, const QSize &requestedSize, QThreadPool *pool)
|
||||
{
|
||||
Q_UNUSED(pool)
|
||||
|
||||
QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
|
||||
{
|
||||
Q_UNUSED(size)
|
||||
Q_UNUSED(requestedSize)
|
||||
const auto makeIcon = [](const QString &path) {
|
||||
QImage image(128, 128, QImage::Format_ARGB32);
|
||||
image.fill(Qt::GlobalColor::transparent);
|
||||
QPainter painter(&image);
|
||||
QSvgRenderer renderer(path);
|
||||
renderer.render(&painter);
|
||||
return image;
|
||||
};
|
||||
|
||||
const auto makeIcon = [](const QString &path) {
|
||||
QImage image(128, 128, QImage::Format_ARGB32);
|
||||
image.fill(Qt::GlobalColor::transparent);
|
||||
QPainter painter(&image);
|
||||
QSvgRenderer renderer(path);
|
||||
renderer.render(&painter);
|
||||
return image;
|
||||
};
|
||||
if (id == QLatin1String("fallbackWhite")) {
|
||||
handleDone(makeIcon(QStringLiteral(":/client/theme/white/user.svg")));
|
||||
return;
|
||||
} else if (id == QLatin1String("fallbackBlack")) {
|
||||
handleDone(makeIcon(QStringLiteral(":/client/theme/black/user.svg")));
|
||||
return;
|
||||
}
|
||||
|
||||
if (id == QLatin1String("fallbackWhite")) {
|
||||
return makeIcon(QStringLiteral(":/client/theme/white/user.svg"));
|
||||
|
||||
handleDone(UserModel::instance()->avatarById(id.toInt()));
|
||||
}
|
||||
|
||||
if (id == QLatin1String("fallbackBlack")) {
|
||||
return makeIcon(QStringLiteral(":/client/theme/black/user.svg"));
|
||||
void handleDone(const QImage &image)
|
||||
{
|
||||
_image = image;
|
||||
emit finished();
|
||||
}
|
||||
|
||||
const int uid = id.toInt();
|
||||
return UserModel::instance()->avatarById(uid);
|
||||
QQuickTextureFactory *textureFactory() const override
|
||||
{
|
||||
return QQuickTextureFactory::textureFactoryForImage(_image);
|
||||
}
|
||||
|
||||
private:
|
||||
QImage _image;
|
||||
};
|
||||
|
||||
QQuickImageResponse *ImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize)
|
||||
{
|
||||
const auto response = new class ImageResponse(id, requestedSize, &pool);
|
||||
return response;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------------------*/
|
||||
|
@ -1720,3 +1738,6 @@ QHash<int, QByteArray> UserAppsModel::roleNames() const
|
|||
return roles;
|
||||
}
|
||||
}
|
||||
|
||||
#include "usermodel.moc"
|
||||
|
||||
|
|
|
@ -276,11 +276,13 @@ private:
|
|||
void buildUserList();
|
||||
};
|
||||
|
||||
class ImageProvider : public QQuickImageProvider
|
||||
class ImageProvider : public QQuickAsyncImageProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ImageProvider();
|
||||
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override;
|
||||
ImageProvider() = default;
|
||||
QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override;
|
||||
};
|
||||
|
||||
class UserAppsModel : public QAbstractListModel
|
||||
|
|
Loading…
Reference in a new issue