diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-09-21 20:44:45 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-09-21 20:44:45 +0900 |
commit | 9cc3f82026c64862bae6a535d2b883b8cf97fba0 (patch) | |
tree | 8d2cb44133edc2237278025e2260f88595fa9971 /user.cpp | |
parent | 16fd53ff0be54531661e9e0cd27c4f893295b62e (diff) | |
parent | f936182135d166e5dea734775e24cabd4b763c64 (diff) | |
download | libquotient-9cc3f82026c64862bae6a535d2b883b8cf97fba0.tar.gz libquotient-9cc3f82026c64862bae6a535d2b883b8cf97fba0.zip |
Merge branch 'master' into kitsune-invite-kick
Diffstat (limited to 'user.cpp')
-rw-r--r-- | user.cpp | 49 |
1 files changed, 23 insertions, 26 deletions
@@ -28,7 +28,6 @@ #include <QtCore/QDebug> #include <QtGui/QIcon> #include <QtCore/QRegularExpression> -#include <algorithm> using namespace QMatrixClient; @@ -52,7 +51,9 @@ class User::Private QSize requestedSize; bool avatarValid; bool avatarOngoingRequest; - QVector<QPixmap> scaledAvatars; + /// Map of requested size to the actual pixmap used for it + /// (it's a shame that QSize has no predefined qHash()). + QHash<QPair<int,int>, QPixmap> scaledAvatars; QString bridged; void requestAvatar(); @@ -92,24 +93,21 @@ 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) |