Commit d7952d5b authored by Anthony Léonard's avatar Anthony Léonard Committed by Guillaume Roguez

draw picture for new lrc model of conversation

The conversationPhoto() from PixmapManipulator is implemented in order
to provide the picture associated to a conversation. In case no
picture is present in database, we draw the fallback avatar.

With this implementation, picture for conference will be the one of
the first participant in the conversation. This limitation is left
for future improvement.

Change-Id: I6be58465d48bfe7a32e4c19b4aca50a077030324
Reviewed-by: Guillaume Roguez's avatarGuillaume Roguez <>
parent c1b3e452
......@@ -36,10 +36,16 @@ namespace Interfaces {
class ImageManipulationDelegate : public PixmapManipulatorI {
static constexpr int IMG_SIZE = 80;
QVariant contactPhoto(Person* c, const QSize& size, bool displayPresence = true) override;
virtual QByteArray toByteArray(const QVariant& pxm) override;
virtual QVariant personPhoto(const QByteArray& data, const QString& type = "PNG") override;
QVariant conversationPhoto(const lrc::api::conversation::Info& conversation,
const lrc::api::account::Info& accountInfo,
const QSize& size = QSize(IMG_SIZE, IMG_SIZE),
bool displayPresence = true) override;
QVariant callPhoto(Call* c, const QSize& size, bool displayPresence = true) override;
QVariant callPhoto(const ContactMethod* n, const QSize& size, bool displayPresence = true) override;
......@@ -65,6 +71,7 @@ namespace Interfaces {
QHash<QString, QPixmap> m_hDefaultUserPixmap;
QHash<QString, QPair<QMetaObject::Connection, QPixmap>> m_hContactsPixmap;
QHash<QString, QPixmap> convPixmCache;
static const QColor avatarColors_[];
......@@ -33,11 +33,16 @@
#import <QtMacExtras/qmacfunctions.h>
#import <QtGui/QPalette>
#import <person.h>
#import <profilemodel.h>
#import <profile.h>
#import <contactmethod.h>
#import <api/conversation.h>
#import <api/account.h>
#import <api/contactmodel.h>
#import <api/contact.h>
#import <api/profile.h>
namespace Interfaces {
......@@ -197,6 +202,86 @@ namespace Interfaces {
return QPixmap::fromImage(image);
char letterForDefaultUserPixmap(const lrc::api::contact::Info& contact)
if (!contact.profileInfo.alias.empty())
return std::toupper(;
else if(contact.profileInfo.type == lrc::api::profile::Type::RING && !contact.registeredName.empty())
return std::toupper(;
return std::toupper(;
QVariant ImageManipulationDelegate::conversationPhoto(const lrc::api::conversation::Info& conversation,
const lrc::api::account::Info& accountInfo,
const QSize& size,
bool displayPresence)
try {
auto contact = accountInfo.contactModel->getContact(conversation.participants[0]);
auto& avatar = contact.profileInfo.avatar;
if (!avatar.empty()) {
QPixmap pxm;
const int radius = size.height() / 2;
// Check cache
auto index = QStringLiteral("%1%2%3").arg(size.width())
if (convPixmCache.contains(index)) {
return convPixmCache.value(index);
auto contactPhoto = qvariant_cast<QPixmap>(personPhoto(QByteArray::fromStdString(avatar)));
contactPhoto = contactPhoto.scaled(size, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
QPixmap finalImg;
// We crop the avatar if picture is not squared as scaled() keep ratio of original picture
if (contactPhoto.size() != size) {
finalImg = crop(contactPhoto, size);
} else
finalImg = contactPhoto;
// Creating clean QPixmap
pxm = QPixmap(size);
//Add corner radius to the Pixmap
QPainter painter(&pxm);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
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 );
finalImg.setMask (mask );
painter.drawPixmap (0,0,finalImg );
painter.setBrush (Qt::NoBrush );
painter.setPen (Qt::black );
painter.setCompositionMode (QPainter::CompositionMode_SourceIn);
// Save in cache
convPixmCache.insert(index, pxm);
return pxm;
} else {
char color =;
char letter = letterForDefaultUserPixmap(contact);
return drawDefaultUserPixmap(size, color, letter);
} catch (const std::out_of_range& e) {
return drawDefaultUserPixmap(size, '?', '?');
QByteArray ImageManipulationDelegate::toByteArray(const QVariant& pxm)
//Preparation of our QPixmap
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment