diff options
author | KitsuneRal <Kitsune-Ral@users.sf.net> | 2016-11-25 23:34:48 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-25 23:34:48 +0900 |
commit | 6776be9849bb32012dc057fe0f6738437b0cd79d (patch) | |
tree | f23be8d886572dff1c6f2708ac8a52ccf800ece4 | |
parent | 8113e61fec79b60be5c534af0d1b19e92d970e67 (diff) | |
parent | d350174d10eafb2dfc85bac93bf59ff9f218c61b (diff) | |
download | libquotient-6776be9849bb32012dc057fe0f6738437b0cd79d.tar.gz libquotient-6776be9849bb32012dc057fe0f6738437b0cd79d.zip |
Merge pull request #46 from maralorn/crop-avatar
Crop avatar to ensure it has exactly the wanted size; Fixes memory leak #45
-rw-r--r-- | user.cpp | 18 | ||||
-rw-r--r-- | user.h | 1 |
2 files changed, 15 insertions, 4 deletions
@@ -25,6 +25,7 @@ #include <QtCore/QTimer> #include <QtCore/QDebug> +#include <algorithm> using namespace QMatrixClient; @@ -80,6 +81,11 @@ QString User::displayname() 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 @@ -102,10 +108,14 @@ QPixmap User::avatar(int width, int height) if (p.size() == size) return p; } - QPixmap newlyScaled = - d->avatar.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation); - d->scaledAvatars.push_back(newlyScaled); - return newlyScaled; + 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; } void User::processEvent(Event* event) @@ -48,6 +48,7 @@ namespace QMatrixClient Q_INVOKABLE QString displayname() const; QPixmap avatar(int requestedWidth, int requestedHeight); + QPixmap croppedAvatar(int requestedWidth, int requestedHeight); void processEvent(Event* event); |