Skip to content
Snippets Groups Projects
Commit f02a32b4 authored by Alexandre Lision's avatar Alexandre Lision Committed by gerrit2
Browse files

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
parent e40ebf37
No related branches found
No related tags found
No related merge requests found
...@@ -55,6 +55,11 @@ namespace Interfaces { ...@@ -55,6 +55,11 @@ namespace Interfaces {
//Helper //Helper
QPixmap drawDefaultUserPixmap(const QSize& size, bool displayPresence, bool isPresent); QPixmap drawDefaultUserPixmap(const QSize& size, bool displayPresence, bool isPresent);
CGImageRef resizeCGImage(CGImageRef image, const QSize& size); 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 } // namespace Interfaces
...@@ -48,8 +48,15 @@ namespace Interfaces { ...@@ -48,8 +48,15 @@ namespace Interfaces {
QPixmap pxm; QPixmap pxm;
if (c && c->photo().isValid()) { if (c && c->photo().isValid()) {
QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaledToWidth(size.height(), QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaled(size, Qt::KeepAspectRatioByExpanding,
Qt::TransformationMode::SmoothTransformation)); Qt::SmoothTransformation));
QPixmap finalImg;
if (contactPhoto.size() != size) {
finalImg = crop(contactPhoto, size);
} else
finalImg = contactPhoto;
pxm = QPixmap(size); pxm = QPixmap(size);
pxm.fill(Qt::transparent); pxm.fill(Qt::transparent);
QPainter painter(&pxm); QPainter painter(&pxm);
...@@ -61,7 +68,7 @@ namespace Interfaces { ...@@ -61,7 +68,7 @@ namespace Interfaces {
painter.setCompositionMode(QPainter::CompositionMode_SourceOver); painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
//Add corner radius to the Pixmap //Add corner radius to the Pixmap
QRect pxRect = contactPhoto.rect(); QRect pxRect = finalImg.rect();
QBitmap mask(pxRect.size()); QBitmap mask(pxRect.size());
QPainter customPainter(&mask); QPainter customPainter(&mask);
customPainter.setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform); customPainter.setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
...@@ -69,8 +76,8 @@ namespace Interfaces { ...@@ -69,8 +76,8 @@ namespace Interfaces {
customPainter.setBackground (Qt::black ); customPainter.setBackground (Qt::black );
customPainter.setBrush (Qt::black ); customPainter.setBrush (Qt::black );
customPainter.drawRoundedRect(pxRect,radius,radius ); customPainter.drawRoundedRect(pxRect,radius,radius );
contactPhoto.setMask (mask ); finalImg.setMask (mask );
painter.drawPixmap (0,0,contactPhoto ); painter.drawPixmap (0,0,finalImg );
painter.setBrush (Qt::NoBrush ); painter.setBrush (Qt::NoBrush );
painter.setPen (Qt::black ); painter.setPen (Qt::black );
painter.setCompositionMode (QPainter::CompositionMode_SourceIn); painter.setCompositionMode (QPainter::CompositionMode_SourceIn);
...@@ -83,6 +90,34 @@ namespace Interfaces { ...@@ -83,6 +90,34 @@ namespace Interfaces {
return pxm; 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 QVariant
ImageManipulationDelegate::callPhoto(Call* c, const QSize& size, bool displayPresence) ImageManipulationDelegate::callPhoto(Call* c, const QSize& size, bool displayPresence)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment