From 27b25141e02c32b1e53a25e82b579e6a3d8089c9 Mon Sep 17 00:00:00 2001 From: Malte Brandy Date: Mon, 24 Oct 2016 21:03:44 +0200 Subject: Crop avatar to ensure it has exactly the wanted size; Fixes memory leak #54 --- user.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/user.cpp b/user.cpp index 7d4f0d1a..f6256a19 100644 --- a/user.cpp +++ b/user.cpp @@ -25,6 +25,7 @@ #include #include +#include using namespace QMatrixClient; @@ -102,10 +103,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) -- cgit v1.2.3 From d350174d10eafb2dfc85bac93bf59ff9f218c61b Mon Sep 17 00:00:00 2001 From: Malte Brandy Date: Tue, 25 Oct 2016 23:55:29 +0200 Subject: Introduce cropedAvatar --- user.cpp | 5 +++++ user.h | 1 + 2 files changed, 6 insertions(+) diff --git a/user.cpp b/user.cpp index f6256a19..7d5024e5 100644 --- a/user.cpp +++ b/user.cpp @@ -80,6 +80,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); diff --git a/user.h b/user.h index 33a89e0b..c408eb67 100644 --- a/user.h +++ b/user.h @@ -49,6 +49,7 @@ namespace QMatrixClient Q_INVOKABLE QString displayname() const; QPixmap avatar(int requestedWidth, int requestedHeight); + QPixmap croppedAvatar(int requestedWidth, int requestedHeight); void processEvent(Event* event); -- cgit v1.2.3