summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swift/QtUI')
-rw-r--r--Swift/QtUI/QtScaledAvatarCache.cpp26
1 files changed, 23 insertions, 3 deletions
diff --git a/Swift/QtUI/QtScaledAvatarCache.cpp b/Swift/QtUI/QtScaledAvatarCache.cpp
index 6d683bd..a0baa62 100644
--- a/Swift/QtUI/QtScaledAvatarCache.cpp
+++ b/Swift/QtUI/QtScaledAvatarCache.cpp
@@ -23,6 +23,26 @@ namespace Swift {
QtScaledAvatarCache::QtScaledAvatarCache(int size) : size(size) {
}
+static QPixmap cropToBiggestCenteredSquare(const QPixmap& input) {
+ QPixmap squareCropped;
+ if (input.width() != input.height()) {
+ QRect centeredSquare;
+ if (input.width() > input.height()) {
+ int x = (input.width() - input.height()) / 2;
+ centeredSquare = QRect(x, 0, input.height(), input.height());
+ }
+ else {
+ int y = (input.height() - input.width()) / 2;
+ centeredSquare = QRect(0, y, input.width(), input.width());
+ }
+ squareCropped = input.copy(centeredSquare);
+ }
+ else {
+ squareCropped = input;
+ }
+ return squareCropped;
+}
+
QString QtScaledAvatarCache::getScaledAvatarPath(const QString& path) {
QFileInfo avatarFile(path);
if (avatarFile.exists()) {
@@ -36,15 +56,15 @@ QString QtScaledAvatarCache::getScaledAvatarPath(const QString& path) {
if (!QFileInfo(targetFile).exists()) {
QPixmap avatarPixmap;
if (avatarPixmap.load(path)) {
- QPixmap maskedAvatar(avatarPixmap.size());
+ QPixmap squaredAvatarPixmap = cropToBiggestCenteredSquare(avatarPixmap);
+ QPixmap maskedAvatar(squaredAvatarPixmap.size());
maskedAvatar.fill(QColor(0, 0, 0, 0));
QPainter maskPainter(&maskedAvatar);
maskPainter.setBrush(Qt::black);
maskPainter.drawRoundedRect(maskedAvatar.rect(), 25.0, 25.0, Qt::RelativeSize);
maskPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
- maskPainter.drawPixmap(0, 0, avatarPixmap);
+ maskPainter.drawPixmap(0, 0, squaredAvatarPixmap);
maskPainter.end();
-
if (!maskedAvatar.scaled(size, size, Qt::KeepAspectRatio, Qt::SmoothTransformation).save(targetFile, "PNG")) {
return path;
}