From 81c04033fa32ed0fa45c44db22ce11ff0636669b Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 20 Sep 2017 20:47:28 +0900 Subject: User: no more croppedAvatar(); added avatarUrl() accessor avatarUrl() is not yet invokable from QML; I'm considering to make all the simple things in User Q_PROPERTies instead. --- user.cpp | 49 +++++++++++++++++++++++-------------------------- user.h | 3 ++- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/user.cpp b/user.cpp index f9f48539..12eb2e0b 100644 --- a/user.cpp +++ b/user.cpp @@ -28,7 +28,6 @@ #include #include #include -#include using namespace QMatrixClient; @@ -52,7 +51,9 @@ class User::Private QSize requestedSize; bool avatarValid; bool avatarOngoingRequest; - QVector scaledAvatars; + /// Map of requested size to the actual pixmap used for it + /// (it's a shame that QSize has no predefined qHash()). + QHash, QPixmap> scaledAvatars; QString bridged; void requestAvatar(); @@ -91,25 +92,22 @@ QString User::bridged() const { } QPixmap User::avatar(int width, int height) -{ - return croppedAvatar(width, height); // FIXME: Return an uncropped avatar; -} - -QPixmap User::croppedAvatar(int width, int height) { QSize size(width, height); - if( !d->avatarValid + // FIXME: Alternating between longer-width and longer-height requests + // is a sure way to trick the below code into constantly getting another + // image from the server because the existing one is alleged unsatisfactory. + // This is plain abuse by the client, though; so not critical for now. + if( (!d->avatarValid && d->avatarUrl.isValid() && !d->avatarOngoingRequest) || width > d->requestedSize.width() || height > d->requestedSize.height() ) { - if( !d->avatarOngoingRequest && d->avatarUrl.isValid() ) - { - qCDebug(MAIN) << "Getting avatar for" << id(); - d->requestedSize = size; - d->avatarOngoingRequest = true; - QTimer::singleShot(0, this, SLOT(requestAvatar())); - } + qCDebug(MAIN) << "Getting avatar for" << id() + << "from" << d->avatarUrl.toString(); + d->requestedSize = size; + d->avatarOngoingRequest = true; + QTimer::singleShot(0, this, SLOT(requestAvatar())); } if( d->avatar.isNull() ) @@ -120,19 +118,18 @@ QPixmap User::croppedAvatar(int width, int height) d->avatar = d->defaultIcon.pixmap(size); } - for (const QPixmap& p: d->scaledAvatars) + auto& pixmap = d->scaledAvatars[{width, height}]; // Create the entry if needed + if (pixmap.isNull()) { - if (p.size() == size) - return p; + pixmap = d->avatar.scaled(width, height, + Qt::KeepAspectRatio, Qt::SmoothTransformation); } - QPixmap newlyScaled = d->avatar.scaled(size, - Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); - QPixmap scaledAndCroped = newlyScaled.copy( - std::max((newlyScaled.width() - width)/2, 0), - std::max((newlyScaled.height() - height)/2, 0), - width, height); - d->scaledAvatars.push_back(scaledAndCroped); - return scaledAndCroped; + return pixmap; +} + +const QUrl& User::avatarUrl() const +{ + return d->avatarUrl; } void User::processEvent(Event* event) diff --git a/user.h b/user.h index ff81305b..a2d58908 100644 --- a/user.h +++ b/user.h @@ -53,7 +53,8 @@ namespace QMatrixClient Q_INVOKABLE QString bridged() const; QPixmap avatar(int requestedWidth, int requestedHeight); - QPixmap croppedAvatar(int requestedWidth, int requestedHeight); + + const QUrl& avatarUrl() const; void processEvent(Event* event); -- cgit v1.2.3