From 24deeda0fe95fd352b823b48a3bc2e660f6a261d Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 14 Oct 2016 10:06:44 +0900 Subject: MediaThumbnailJob: Use QSize instead of two separate int's for size Connection::getThumbnail() is now overloaded to provide compatibility with the previous interface. --- connection.cpp | 10 +++++++--- connection.h | 3 ++- jobs/mediathumbnailjob.cpp | 12 +++++------- jobs/mediathumbnailjob.h | 2 +- user.cpp | 35 ++++++++++++++++++----------------- 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/connection.cpp b/connection.cpp index bc6d1f26..ec251500 100644 --- a/connection.cpp +++ b/connection.cpp @@ -27,7 +27,6 @@ #include "jobs/postreceiptjob.h" #include "jobs/joinroomjob.h" #include "jobs/leaveroomjob.h" -#include "jobs/roommembersjob.h" #include "jobs/roommessagesjob.h" #include "jobs/syncjob.h" #include "jobs/mediathumbnailjob.h" @@ -234,13 +233,18 @@ RoomMessagesJob* Connection::getMessages(Room* room, QString from) return job; } -MediaThumbnailJob* Connection::getThumbnail(QUrl url, int requestedWidth, int requestedHeight) +MediaThumbnailJob* Connection::getThumbnail(QUrl url, QSize requestedSize) { - MediaThumbnailJob* job = new MediaThumbnailJob(d->data, url, requestedWidth, requestedHeight); + MediaThumbnailJob* job = new MediaThumbnailJob(d->data, url, requestedSize); job->start(); return job; } +MediaThumbnailJob* Connection::getThumbnail(QUrl url, int requestedWidth, int requestedHeight) +{ + return getThumbnail(url, QSize(requestedWidth, requestedHeight)); +} + QUrl Connection::homeserver() const { return d->data->baseUrl(); diff --git a/connection.h b/connection.h index fed0b1c4..c5dad246 100644 --- a/connection.h +++ b/connection.h @@ -58,7 +58,8 @@ namespace QMatrixClient Q_INVOKABLE virtual void joinRoom( QString roomAlias ); Q_INVOKABLE virtual void leaveRoom( Room* room ); Q_INVOKABLE virtual RoomMessagesJob* getMessages( Room* room, QString from ); - virtual MediaThumbnailJob* getThumbnail( QUrl url, int requestedWidth, int requestedHeight ); + virtual MediaThumbnailJob* getThumbnail( QUrl url, QSize requestedSize ); + MediaThumbnailJob* getThumbnail( QUrl url, int requestedWidth, int requestedHeight ); Q_INVOKABLE QUrl homeserver() const; Q_INVOKABLE User* user(QString userId); diff --git a/jobs/mediathumbnailjob.cpp b/jobs/mediathumbnailjob.cpp index 8a5dc4ea..975a533d 100644 --- a/jobs/mediathumbnailjob.cpp +++ b/jobs/mediathumbnailjob.cpp @@ -27,19 +27,17 @@ class MediaThumbnailJob::Private public: QUrl url; QPixmap thumbnail; - int requestedHeight; - int requestedWidth; + QSize requestedSize; ThumbnailType thumbnailType; }; -MediaThumbnailJob::MediaThumbnailJob(ConnectionData* data, QUrl url, int requestedWidth, int requestedHeight, +MediaThumbnailJob::MediaThumbnailJob(ConnectionData* data, QUrl url, QSize requestedSize, ThumbnailType thumbnailType) : BaseJob(data, JobHttpType::GetJob, "MediaThumbnailJob") , d(new Private) { d->url = url; - d->requestedHeight = requestedHeight; - d->requestedWidth = requestedWidth; + d->requestedSize = requestedSize; d->thumbnailType = thumbnailType; } @@ -61,8 +59,8 @@ QString MediaThumbnailJob::apiPath() const QUrlQuery MediaThumbnailJob::query() const { QUrlQuery query; - query.addQueryItem("width", QString::number(d->requestedWidth)); - query.addQueryItem("height", QString::number(d->requestedHeight)); + query.addQueryItem("width", QString::number(d->requestedSize.width())); + query.addQueryItem("height", QString::number(d->requestedSize.height())); if( d->thumbnailType == ThumbnailType::Scale ) query.addQueryItem("method", "scale"); else diff --git a/jobs/mediathumbnailjob.h b/jobs/mediathumbnailjob.h index 3babf845..8a6cdce2 100644 --- a/jobs/mediathumbnailjob.h +++ b/jobs/mediathumbnailjob.h @@ -30,7 +30,7 @@ namespace QMatrixClient class MediaThumbnailJob: public BaseJob { public: - MediaThumbnailJob(ConnectionData* data, QUrl url, int requestedWidth, int requestedHeight, + MediaThumbnailJob(ConnectionData* data, QUrl url, QSize requestedSize, ThumbnailType thumbnailType=ThumbnailType::Scale); virtual ~MediaThumbnailJob(); diff --git a/user.cpp b/user.cpp index 60140e5b..7d4f0d1a 100644 --- a/user.cpp +++ b/user.cpp @@ -24,7 +24,6 @@ #include "jobs/mediathumbnailjob.h" #include -#include #include using namespace QMatrixClient; @@ -39,11 +38,10 @@ class User::Private Connection* connection; QPixmap avatar; - int requestedWidth; - int requestedHeight; + QSize requestedSize; bool avatarValid; bool avatarOngoingRequest; - QHash,QPixmap> scaledMap; + QVector scaledAvatars; void requestAvatar(); }; @@ -51,11 +49,11 @@ class User::Private User::User(QString userId, Connection* connection) : QObject(connection), d(new Private) { - d->q = this; d->connection = connection; d->userId = userId; d->avatarValid = false; d->avatarOngoingRequest = false; + d->q = this; } User::~User() @@ -82,15 +80,16 @@ QString User::displayname() const QPixmap User::avatar(int width, int height) { + QSize size(width, height); + if( !d->avatarValid - || width > d->requestedWidth - || height > d->requestedHeight ) + || width > d->requestedSize.width() + || height > d->requestedSize.height() ) { if( !d->avatarOngoingRequest && d->avatarUrl.isValid() ) { qDebug() << "Getting avatar for" << id(); - d->requestedWidth = width; - d->requestedHeight = height; + d->requestedSize = size; d->avatarOngoingRequest = true; QTimer::singleShot(0, this, SLOT(requestAvatar())); } @@ -98,12 +97,15 @@ QPixmap User::avatar(int width, int height) if( d->avatar.isNull() ) return d->avatar; - QPair size(width, height); - if( !d->scaledMap.contains(size) ) + for (const QPixmap& p: d->scaledAvatars) { - d->scaledMap.insert(size, d->avatar.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation)); + if (p.size() == size) + return p; } - return d->scaledMap.value(size); + QPixmap newlyScaled = + d->avatar.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation); + d->scaledAvatars.push_back(newlyScaled); + return newlyScaled; } void User::processEvent(Event* event) @@ -132,14 +134,13 @@ void User::requestAvatar() void User::Private::requestAvatar() { - MediaThumbnailJob* job = - connection->getThumbnail(avatarUrl, requestedWidth, requestedHeight); + MediaThumbnailJob* job = connection->getThumbnail(avatarUrl, requestedSize); connect( job, &MediaThumbnailJob::success, [=]() { avatarOngoingRequest = false; avatarValid = true; - avatar = job->thumbnail().scaled(requestedWidth, requestedHeight, + avatar = job->thumbnail().scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); - scaledMap.clear(); + scaledAvatars.clear(); emit q->avatarChanged(q); }); } -- cgit v1.2.3