summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2016-04-25 18:39:44 (GMT)
committerKevin Smith <kevin.smith@isode.com>2016-07-22 17:28:47 (GMT)
commit3679c59d32a65f93ad404698bdaa9a16bdd0c28a (patch)
tree56114f958793990ea99a74def7374cd825cfd10d
parenta4a6ae738a2df297b889e3c622910431e8736152 (diff)
downloadswift-3679c59d32a65f93ad404698bdaa9a16bdd0c28a.zip
swift-3679c59d32a65f93ad404698bdaa9a16bdd0c28a.tar.bz2
Crop avatars to squares so they look better inside the chat view
Test-Information: Tested by clearing the avatar cache and joining MUC some larger MUC rooms. All but our current default avatar look much better. Change-Id: I515adbded1114632f41d6ee4eba6602b77dad975
-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;
}