From f02a32b439356f12798e36ff65af53549f371155 Mon Sep 17 00:00:00 2001
From: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Date: Tue, 19 Apr 2016 15:01:07 -0400
Subject: [PATCH] pixmapmanipulator: add cropping possibility

contact images provided by OSX Contact app are all squared.
There was no handling of other images resolution, then drawing was
all wrong for images with width != height

Change-Id: Ib888d6a0d9c609f8090058d7d062a438f48fe35f
Tuleap: #530
---
 src/delegates/ImageManipulationDelegate.h  |  5 +++
 src/delegates/ImageManipulationDelegate.mm | 47 +++++++++++++++++++---
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/src/delegates/ImageManipulationDelegate.h b/src/delegates/ImageManipulationDelegate.h
index 141329ae..776df95d 100644
--- a/src/delegates/ImageManipulationDelegate.h
+++ b/src/delegates/ImageManipulationDelegate.h
@@ -55,6 +55,11 @@ namespace Interfaces {
         //Helper
         QPixmap drawDefaultUserPixmap(const QSize& size, bool displayPresence, bool isPresent);
         CGImageRef resizeCGImage(CGImageRef image, const QSize& size);
+
+        /**
+         * Return a version of size destSize centered of the bigger photo
+         */
+        QPixmap crop(QPixmap& photo, const QSize& destSize);
     };
 
 } // namespace Interfaces
diff --git a/src/delegates/ImageManipulationDelegate.mm b/src/delegates/ImageManipulationDelegate.mm
index 30e916e7..0862f20c 100644
--- a/src/delegates/ImageManipulationDelegate.mm
+++ b/src/delegates/ImageManipulationDelegate.mm
@@ -48,8 +48,15 @@ namespace Interfaces {
 
         QPixmap pxm;
         if (c && c->photo().isValid()) {
-            QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaledToWidth(size.height(),
-                                                                                  Qt::TransformationMode::SmoothTransformation));
+            QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaled(size, Qt::KeepAspectRatioByExpanding,
+                                                                           Qt::SmoothTransformation));
+
+            QPixmap finalImg;
+            if (contactPhoto.size() != size) {
+                finalImg = crop(contactPhoto, size);
+            } else
+                finalImg = contactPhoto;
+
             pxm = QPixmap(size);
             pxm.fill(Qt::transparent);
             QPainter painter(&pxm);
@@ -61,16 +68,16 @@ namespace Interfaces {
             painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
 
             //Add corner radius to the Pixmap
-            QRect pxRect = contactPhoto.rect();
+            QRect pxRect = finalImg.rect();
             QBitmap mask(pxRect.size());
             QPainter customPainter(&mask);
             customPainter.setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
             customPainter.fillRect       (pxRect                , Qt::white );
             customPainter.setBackground  (Qt::black                         );
             customPainter.setBrush       (Qt::black                         );
-            customPainter.drawRoundedRect(pxRect,radius,radius);
-            contactPhoto.setMask         (mask                              );
-            painter.drawPixmap           (0,0,contactPhoto                  );
+            customPainter.drawRoundedRect(pxRect,radius,radius              );
+            finalImg.setMask             (mask                              );
+            painter.drawPixmap           (0,0,finalImg                      );
             painter.setBrush             (Qt::NoBrush                       );
             painter.setPen               (Qt::black                         );
             painter.setCompositionMode   (QPainter::CompositionMode_SourceIn);
@@ -83,6 +90,34 @@ namespace Interfaces {
         return pxm;
     }
 
+    QPixmap
+    ImageManipulationDelegate::crop(QPixmap& photo, const QSize& destSize)
+    {
+        auto initSize = photo.size();
+        float leftDelta = 0;
+        float topDelta = 0;
+
+        if (destSize.height() == initSize.height()) {
+            leftDelta = (destSize.width() - initSize.width()) / 2;
+        } else {
+            topDelta = (destSize.height() - initSize.height()) / 2;
+        }
+
+        float xScale = (float)destSize.width()  / initSize.width();
+        float yScale = (float)destSize.height() / initSize.height();
+
+        QRectF destRect(leftDelta, topDelta,
+                            initSize.width() - leftDelta, initSize.height() - topDelta);
+
+        destRect.setLeft(leftDelta * xScale);
+        destRect.setTop(topDelta * yScale);
+
+        destRect.setWidth((initSize.width() - leftDelta) * xScale);
+        destRect.setHeight((initSize.height() - topDelta) * yScale);
+
+        return photo.copy(destRect.toRect());
+    }
+
     QVariant
     ImageManipulationDelegate::callPhoto(Call* c, const QSize& size, bool displayPresence)
     {
-- 
GitLab