ViewSolutions
Loading...
Searching...
No Matches
avatargenerator.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2025-2025 QuasarApp.
3//# Distributed under the GPLv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7
8
9#include "avatargenerator.h"
10
11#include <QFontMetrics>
12#include <QPainter>
13
14namespace ViewSolutions {
15
17
19 const QString &nickName,
20 const QFont &font,
21 const QColor &color) const {
22
23 const int size = std::max(std::max(result.width(), result.height()), 256);
24
25
26 QBrush solidBrush = QBrush(color);
27 if (!color.isValid()) {
28 auto mainColor = QColor(qHash(nickName + "red") % 250,
29 qHash(nickName + "green") % 250,
30 qHash(nickName + "blue") % 250);
31
32 // radnom dark
33 auto secondColor = QColor(qHash(nickName + "red dark") % 50,
34 qHash(nickName + "green dark") % 50,
35 qHash(nickName + "blue dark") % 50);
36
37 QLinearGradient g(0.0, 0.0, 0.0, size);
38 g.setColorAt(0, mainColor);
39 g.setColorAt(1, secondColor);
40
41 solidBrush = QBrush(g);
42 }
43
44 QImage image(size, size, QImage::Format_ARGB32);
45 image.fill(Qt::transparent);
46
47 QPainter painter(&image);
48 painter.setRenderHint(QPainter::Antialiasing);
49
50
51 painter.setBrush(solidBrush);
52 painter.setPen(Qt::NoPen);
53 painter.drawEllipse(0, 0, size, size);
54
55 QString firstLetter = nickName.left(1).toUpper();
56 QFont currentFont = font;
57 currentFont.setPixelSize( size / 2);
58 painter.setPen(QColor(Qt::white));
59 QFontMetrics metrics(currentFont);
60 painter.setFont(currentFont);
61
62 int textWidth = metrics.horizontalAdvance(firstLetter);
63 int textHeight = metrics.height();
64 int x = (size - textWidth) / 2;
65 int y = (size + textHeight / 2) / 1.9;
66 painter.drawText(x, y, firstLetter);
67
68 result = image;
69
70 return true;
71}
72
73}
bool generateDefaultAvatar(QImage &result, const QString &nickName, const QFont &font, const QColor &color={}) const
generateDefaultAvatar generates default avatar. This function generate Ellipse with 2 first letters o...
the ViewSolutions namespace