File: kidenticongenerator.cpp

package info (click to toggle)
plasma-workspace 4%3A5.8.6-2.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 54,996 kB
  • sloc: cpp: 70,006; sh: 868; xml: 867; python: 46; makefile: 16
file content (301 lines) | stat: -rw-r--r-- 8,322 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 *   Copyright 2010 Ivan Cukic <ivan.cukic(at)kde.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library/Lesser General Public License
 *   version 2, or (at your option) any later version, as published by the
 *   Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library/Lesser General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "kidenticongenerator.h"

#include <QHash>
#include <QPainter>
#include <QDebug>
#include <QCryptographicHash>

#include <kiconeffect.h>

#include <Plasma/Svg>
#include <Plasma/Theme>

#define VALUE_LIMIT_UP 192
#define VALUE_LIMIT_DOWN 64

class KIdenticonGenerator::Private {
public:
    QPixmap generatePattern(int size, quint32 hash, QIcon::Mode mode);

    QString elementName(const QString & element, QIcon::Mode mode);
    QColor colorForHash(quint32 hash) const;
    quint32 hash(const QString & data);

    static KIdenticonGenerator * instance;

    Plasma::Theme *theme;
    Plasma::Svg shapes;
    Plasma::Svg svg;
};

QPixmap KIdenticonGenerator::Private::generatePattern(int size, quint32 hash, QIcon::Mode mode)
{
    // We are dividing the pixmap into 9 blocks - 3 x 3
    int blockSize = size / 3;

    // pulling parts of the hash
    quint32 tmp = hash;

    quint8 block[4];
    block[0] = tmp & 31; tmp >>= 5;
    block[1] = tmp & 31; tmp >>= 5;
    block[2] = tmp & 31; tmp >>= 5;

    // Painting alpha channel
    QImage pixmapAlpha({size, size}, QImage::Format_ARGB32);
    pixmapAlpha.fill(Qt::black);

    QPainter painterAlpha(& pixmapAlpha);

    QRectF rect(0, 0, blockSize + 0.5, blockSize + 0.5);

    for (int i = 0; i < 4; i++) {
        // Painting the corner item
        rect.moveTopLeft(QPoint(0, 0));
        shapes.paint(& painterAlpha, rect, "shape" + QString::number(block[0] + 1));

        // Painting side item
        rect.moveTopLeft(QPoint(blockSize, 0));
        shapes.paint(& painterAlpha, rect, "shape" + QString::number(block[1] + 1));

        // Rotating the canvas to paint other edges
        painterAlpha.translate(size, 0);
        painterAlpha.rotate(90);
    }

    // Painting center item
    rect.moveTopLeft(QPoint(blockSize, blockSize));
    shapes.paint(& painterAlpha, rect, "shape" + QString::number(block[2] + 1));

    painterAlpha.end();

    // Painting final pixmap
    QImage pixmapResult(size, size, QImage::Format_ARGB32);
    pixmapResult.fill(Qt::transparent);

    // QRadialGradient gradient(50, 50, 100);
    // gradient.setColorAt(0, color.lighter());
    // gradient.setColorAt(1, color.darker());

    QPainter resultPainter(& pixmapResult);
    // resultPainter.fillRect(0, 0, size, size, gradient);
    svg.paint(& resultPainter, QRect(0, 0, size, size), elementName(QStringLiteral("content"), mode));

    resultPainter.end();

    pixmapResult.setAlphaChannel(pixmapAlpha);

    // QImage itmp = pixmapResult.toImage();
    // KIconEffect::colorize(itmp, colorForHash(hash), 1.0);
    // pixmapResult = pixmapResult.fromImage(itmp);

    return QPixmap::fromImage(pixmapResult);
}

QColor KIdenticonGenerator::Private::colorForHash(quint32 hash) const
{
    // Color is chosen according to hash
    QColor color;

    // Getting the value from color svg, but we must restrain it to
    // values in range from VALUE_LIMIT_DOWN to VALUE_LIMIT_UP

    int value = theme->color(Plasma::Theme::TextColor).value();
    if (value < VALUE_LIMIT_DOWN) {
        value = VALUE_LIMIT_DOWN;
    } else if (value > VALUE_LIMIT_UP) {
        value = VALUE_LIMIT_UP;
    }

    color.setHsv(
        hash % 359 + 1, // hue depending on hash
        250,            // high saturation level
        value
    );

    return color;

}

QString KIdenticonGenerator::Private::elementName(const QString & element, QIcon::Mode mode)
{
    QString prefix;

    switch (mode) {
        case QIcon::Normal:
            prefix = QStringLiteral("normal-");
            break;

        case QIcon::Disabled:
            prefix = QStringLiteral("disabled-");
            break;

        case QIcon::Selected:
            prefix = QStringLiteral("selected-");
            break;

        case QIcon::Active:
            prefix = QStringLiteral("active-");
            break;

        default:
            break;

    }

    if (svg.hasElement(prefix + element)) {
        return prefix + element;
    } else {
        return element;
    }
}

quint32 KIdenticonGenerator::Private::hash(const QString & data)
{
    // qHash function doesn't give random enough results
    // and gives similar hashes for similar strings.

    QByteArray bytes = QCryptographicHash::hash(data.toUtf8(), QCryptographicHash::Md5);

    // Generating hash
    quint32 hash = 0;

    char * hashBytes = (char *) & hash;
    for (int i = 0; i < bytes.size(); i++) {
        // Using XOR for mixing the bytes because
        // it is fast and cryptographically safe
        // (more than enough for our use-case)
        hashBytes[i % 4] ^= bytes.at(i);
    }

    return hash;
}

KIdenticonGenerator * KIdenticonGenerator::Private::instance = NULL;

KIdenticonGenerator * KIdenticonGenerator::self()
{
    if (!Private::instance) {
        Private::instance = new KIdenticonGenerator();
    }

    return Private::instance;
}

KIdenticonGenerator::KIdenticonGenerator()
    : d(new Private())
{
    d->theme = new Plasma::Theme(0);
    // loading SVGs
    d->shapes.setImagePath(QStringLiteral("widgets/identiconshapes"));
    d->shapes.setContainsMultipleImages(true);

    d->svg.setImagePath(QStringLiteral("widgets/identiconsvg"));
    d->svg.setContainsMultipleImages(true);
}

KIdenticonGenerator::~KIdenticonGenerator()
{
    delete d->theme;
    delete d;
}

#define generateIconModes( PARAM ) \
    for (int omode = QIcon::Normal; omode <= QIcon::Selected; omode++) {   \
        QIcon::Mode mode = (QIcon::Mode)omode;                             \
        result.addPixmap(generatePixmap(size, PARAM, mode), mode);         \
    }

QIcon KIdenticonGenerator::generate(int size, quint32 hash)
{
    QIcon result;
    generateIconModes(hash);
    return result;
}

QIcon KIdenticonGenerator::generate(int size, const QString & data)
{
    QIcon result;
    generateIconModes(data);
    return result;
}

QIcon KIdenticonGenerator::generate(int size, const QIcon & icon)
{
    QIcon result;
    generateIconModes(icon);
    return result;
}

QPixmap KIdenticonGenerator::generatePixmap(int size, QString id, QIcon::Mode mode)
{
    return generatePixmap(size, d->hash(id), mode);
}

QPixmap KIdenticonGenerator::generatePixmap(int size, quint32 hash, QIcon::Mode mode)
{
    QPixmap pixmap(size, size);
    pixmap.fill(Qt::transparent);

    // Painting background and the pattern
    {
    QPainter painter(& pixmap);
    d->svg.paint(& painter, QRect(0, 0, size, size), d->elementName(QStringLiteral("background"), mode));
    painter.drawPixmap(0, 0, d->generatePattern(size, hash, mode));
    painter.end();
    }

    // coloring the painted image
    QImage itmp = pixmap.toImage();
    KIconEffect::colorize(itmp, d->colorForHash(hash), 1.0);
    if (mode == QIcon::Disabled) {
        KIconEffect::toGray(itmp, 0.7);
    }
    pixmap = pixmap.fromImage(itmp);

    // Drawing the overlay
    {
    QPainter painter(& pixmap);
    d->svg.paint(& painter, QRect(0, 0, size, size), d->elementName(QStringLiteral("overlay"), mode));
    }

    return pixmap;
}

QPixmap KIdenticonGenerator::generatePixmap(int size, const QIcon & icon, QIcon::Mode mode)
{
    QPixmap pixmap(size, size);
    pixmap.fill(Qt::transparent);

    QRect paintRect(0, 0, size, size);

    // Painting background and the pattern
    QPainter painter(& pixmap);
    d->svg.paint(& painter, QRect(0, 0, size, size), d->elementName(QStringLiteral("background"), mode));

    icon.paint(& painter, paintRect, Qt::AlignCenter, mode);

    painter.end();

    return pixmap;
}