diff --git a/src/delegates/ImageManipulationDelegate.h b/src/delegates/ImageManipulationDelegate.h index 141329aef6e6f2f036afa1932d4e30b91b0158ca..776df95df69bf2ab955ecb0c918d2f428514ae0e 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 30e916e74ed26351080bfcee5bdc8c3b77be6951..0862f20c063b17732e61cfe2973d3f6e2fe8aa09 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) {