receiving avatars, generating missing avatars, storing state of avatars, global color palette

This commit is contained in:
Blue 2019-10-15 22:25:40 +03:00
parent c678a790e5
commit 64e33b6139
8 changed files with 375 additions and 38 deletions

View file

@ -338,9 +338,57 @@ QString Core::RosterItem::avatarHash() const
return archive->getAvatarHash();
}
bool Core::RosterItem::isAvatarAutoGenerated() const
{
return archive->getAutoAvatar();
}
QString Core::RosterItem::avatarPath() const
{
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
path += "/" + account + "/" + jid + "/avatar." + archive->getAvatarType();
return path;
}
bool Core::RosterItem::hasAvatar() const
{
return archive->getHasAvatar();
}
void Core::RosterItem::setAvatar(const QByteArray& data)
{
if (archive->setAvatar(data, false)) {
if (archive->getHasAvatar()) {
emit avatarChanged(Shared::Avatar::empty, "");
} else {
emit avatarChanged(Shared::Avatar::valid, avatarPath());
}
}
}
void Core::RosterItem::setAutoGeneratedAvatar()
{
QImage image(96, 96, QImage::Format_ARGB32_Premultiplied);
QPainter painter(&image);
quint8 colorIndex = rand() % Shared::colorPalette.size();
const QColor& bg = Shared::colorPalette[colorIndex];
painter.fillRect(image.rect(), bg);
QFont f;
f.setBold(true);
f.setPixelSize(72);
painter.setFont(f);
if (bg.lightnessF() > 0.5) {
painter.setPen(Qt::black);
} else {
painter.setPen(Qt::white);
}
painter.drawText(image.rect(), Qt::AlignCenter | Qt::AlignVCenter, jid.at(0).toUpper());
QByteArray arr;
QBuffer stream(&arr);
stream.open(QBuffer::WriteOnly);
image.save(&stream, "PNG");
stream.close();
if (archive->setAvatar(arr, true)) {
emit avatarChanged(Shared::Avatar::autocreated, avatarPath());
}
}