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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
#include "displaytoolbox.h"
#include <QJsonObject>
#include <QJsonArray>
#include <QBuffer>
#ifdef PAINTER_OP
#include <QFontMetrics>
#include <QPainter>
#include <QFont>
//#include <QFontDatabase>
#endif
#include <QDebug>
#define LINE_SPACING 5
DisplayToolBox::DisplayToolBox()
{
}
#ifdef PAINTER_OP
QString DisplayToolBox::makeImage(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString comment, bool allSameFaceCount,bool allSameColor)
{
//qDebug() <<"scarlarText:" <<scalarText << "resultStr:"<< resultStr<<"array" << array<< "with color:" << withColor << "cmd" << cmd << "comment" << comment << "allsamecolor"<< allSameColor;
QString textTotal("%1 Details:[%3 %2]");
int lineCount=2;
if(!comment.isEmpty())
{
lineCount = 3;
}
if(resultStr.isEmpty())
{
auto list = diceToText(array,false,allSameFaceCount,allSameColor);
textTotal = textTotal.arg(scalarText).arg(list).arg(cmd);
}
else
{
--lineCount;
textTotal = resultStr;
}
if(comment.size()>textTotal.size())
{
textTotal.prepend(QStringLiteral("%1\n").arg(comment));
}
QFont font("Helvetica", 15); //QFontDatabase::systemFont(QFontDatabase::GeneralFont);
QFontMetrics fm(font);
QRect rect = fm.boundingRect(textTotal);
QImage img(rect.width(),(rect.height()+LINE_SPACING)*lineCount,QImage::Format_ARGB32);
img.fill(QColor(255,255,255,100));
QPainter painter(&img);
painter.setFont(font);
int y = rect.height();
if(!comment.isEmpty())
{
QPen pen = painter.pen();
pen.setColor(Qt::black);
painter.setPen(pen);
painter.drawText(QPoint(0,y),comment);
y += rect.height()+LINE_SPACING;
}
if(!resultStr.isEmpty())
{
QPen pen = painter.pen();
pen.setColor(Qt::black);
painter.setPen(pen);
painter.drawText(QPoint(0,y),resultStr);
}
else
{
painter.save();
QPen pen = painter.pen();
pen.setColor(Qt::red);
painter.setPen(pen);
painter.drawText(QPoint(5,y),scalarText);
y += rect.height()+LINE_SPACING;
painter.restore();
int x = 5;
QString text=QStringLiteral("Details:[%1 (").arg(cmd);
painter.drawText(QPoint(x,y),text);
x += fm.boundingRect(text).width();
if(allSameFaceCount)
{
int i = 0;
for(auto item : array)
{
QStringList result;
bool last = (array.size()-1==i);
auto obj = item.toObject();
auto values= obj["values"].toArray();
for(auto valRef : values)
{
result.append(diceResultToString(valRef.toObject()));
}
painter.save();
QPen pen = painter.pen();
QColor color;
auto colorStr = obj["color"].toString();
if(colorStr.isEmpty())
color = QColor(Qt::black);
else
color.setNamedColor(colorStr);
pen.setColor(color);
painter.setPen(pen);
text = QStringLiteral("%1").arg(result.join(','));
if(!last)
{
text.append(",");
}
painter.drawText(QPoint(x,y),text);
x += fm.boundingRect(text).width();
painter.restore();
++i;
}
}
else
{
int i = 0;
for(auto item : array)
{
QStringList result;
auto obj = item.toObject();
bool last = (array.size()-1==i);
auto values= obj["values"].toArray();
for(auto valRef : values)
{
result.append(diceResultToString(valRef.toObject()));
}
text =QStringLiteral("d%1:(").arg(obj["face"].toInt());
painter.drawText(QPoint(x,y),text);
x += fm.boundingRect(text).width();
painter.save();
QPen pen = painter.pen();
QColor color;
auto colorStr = obj["color"].toString();
if(colorStr.isEmpty())
color = QColor(Qt::black);
else
color.setNamedColor(colorStr);
pen.setColor(color);
painter.setPen(pen);
text = QStringLiteral("%1").arg(result.join(','));
painter.drawText(QPoint(x,y),text);
x += fm.boundingRect(text).width();
painter.restore();
text = QStringLiteral(")");
if(!last)
{
text.append(",");
}
painter.drawText(QPoint(x,y),text);
x += fm.boundingRect(text).width();
painter.restore();
++i;
}
}
text = QStringLiteral(")");
x += fm.boundingRect(text).width();
painter.drawText(QPoint(x,y),text);
}
painter.end();
img.save("/home/renaud/image.png","PNG");
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "PNG");
//return {};
return ba.toBase64();
}
#endif
QString DisplayToolBox::colorToTermCode(QString str)
{
if(str.isEmpty()|| str==QStringLiteral("black"))
{
return QStringLiteral("\e[0;31m");
}
else if(str==QStringLiteral("white"))
{
return QStringLiteral("\e[97m");
}
else if(str==QStringLiteral("blue"))
{
return QStringLiteral("\e[34m");
}
else if(str==QStringLiteral("red"))
{
return QStringLiteral("\e[31m");
}
else if(str==QStringLiteral("black"))
{
return QStringLiteral("\e[30m");
}
else if(str==QStringLiteral("green"))
{
return QStringLiteral("\e[32m");
}
else if(str==QStringLiteral("yellow"))
{
return QStringLiteral("\e[33m");
}
else if(str==QStringLiteral("reset"))
{
return QStringLiteral("\e[0m");
}
return {};
}
QString DisplayToolBox::diceToSvg(QJsonArray array,bool withColor,bool allSameColor,bool allSameFaceCount )
{
Q_UNUSED(allSameColor)
if(allSameFaceCount)
{
QStringList result;
for(auto item : array)
{
QStringList subResult;
auto obj = item.toObject();
auto values= obj["values"].toArray();
for(auto valRef : values)
{
subResult.append(diceResultToString(valRef.toObject()));
}
if(withColor)
{
result.append(QStringLiteral("<tspan fill=\"%1\">").arg(obj["color"].toString()));
result.append(subResult.join(','));
result.append(QStringLiteral("</tspan>"));
}
else
{
result.append(subResult.join(','));
}
}
return result.join("");
}
else
{
QStringList result;
for(auto item : array)
{
QStringList subResult;
auto obj = item.toObject();
auto values= obj["values"].toArray();
for(auto valRef : values)
{
subResult.append(diceResultToString(valRef.toObject()));
}
result.append(QStringLiteral("d%1:(").arg(obj["face"].toInt()));
if(withColor)
{
result.append(QStringLiteral("<tspan fill=\"%1\">").arg(obj["color"].toString()));
}
result.append(subResult.join(','));
if(withColor)
{
result.append(QStringLiteral("</tspan>)"));
}
else
{
result.append(QStringLiteral(")"));
}
}
return result.join("");
}
}
QJsonArray DisplayToolBox::diceToJson(QList<ExportedDiceResult>& diceList,bool& allSameFaceCount,bool& allSameColor)
{
allSameFaceCount = true;
allSameColor = true;
QJsonArray array;
for(auto dice : diceList)
{
if(dice.size()>1)
{
allSameFaceCount = false;
}
for(int face: dice.keys())
{
ListDiceResult diceResults = dice.value(face);
std::vector<std::vector<HighLightDice>> sameColorDice;
std::vector<QString> alreadyDoneColor;
for(auto & dice : diceResults)
{
auto it=std::find_if(alreadyDoneColor.begin(), alreadyDoneColor.end(),[dice](QString color){
return color == dice.getColor();
});
if( it == alreadyDoneColor.end())
{
sameColorDice.push_back(std::vector<HighLightDice>());
alreadyDoneColor.push_back(dice.getColor());
it = alreadyDoneColor.end();
--it;
}
auto i = std::distance(alreadyDoneColor.begin(), it);
sameColorDice[static_cast<std::size_t>(i)].push_back(dice);
}
int i = 0;
if(alreadyDoneColor.size()>1)
{
allSameColor = false;
}
for(auto it = alreadyDoneColor.begin() ; it != alreadyDoneColor.end(); ++it)
{
auto list = sameColorDice[static_cast<std::size_t>(i)];
QJsonObject object;
object["color"]=*it;
object["face"]=face;
QJsonArray values;
for(auto const& dice : list)
{
QJsonObject diceObj;
auto listValues = dice.getResult();
if(!listValues.isEmpty())
{
diceObj["total"]=static_cast<qint64>(listValues.takeFirst());
QJsonArray subValues;
for(auto result : listValues)
{
subValues.push_back(static_cast<qint64>(result));
}
diceObj["subvalues"]=subValues;
}
values.push_back(diceObj);
}
object["values"]=values;
++i;
array.push_back(object);
}
}
}
return array;
}
QString DisplayToolBox::diceResultToString(QJsonObject val)
{
auto total = QString::number(val["total"].toDouble());
auto subvalues = val["subvalues"].toArray();
QStringList subStr;
for(auto subval : subvalues)
{
subStr << QString::number(subval.toDouble());
}
if(!subStr.isEmpty())
{
total.append(QStringLiteral(" [%1]").arg(subStr.join(',')));
}
return total;
}
QString DisplayToolBox::diceToText(QJsonArray array, bool withColor,bool allSameFaceCount, bool allSameColor)
{
Q_UNUSED(allSameColor)
QStringList result;
for(auto item : array)
{
QString subResult("");
auto obj = item.toObject();
auto values= obj["values"].toArray();
QStringList diceResult;
for(auto valRef : values)
{
diceResult += diceResultToString(valRef.toObject());
}
if (!diceResult.isEmpty())
{
if(!allSameFaceCount)
{
subResult += QStringLiteral("d%1:(").arg(obj["face"].toInt());
}
if(withColor)
{
subResult += DisplayToolBox::colorToTermCode(obj["color"].toString());
}
subResult += diceResult.join(" ");
if(withColor)
{
subResult += DisplayToolBox::colorToTermCode(QStringLiteral("reset"));
}
if(!allSameFaceCount)
{
subResult += QStringLiteral(")");
}
}
if (!subResult.isEmpty())
{
result += subResult;
}
}
return result.join(" - ");
}
|