aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/displaytoolbox.cpp
blob: 9dc8fb6047780b858dba7a022c87e845c10b3bb8 (plain) (blame)
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
#include "displaytoolbox.h"
#include <QBuffer>
#include <QJsonArray>
#include <QJsonObject>

#ifdef PAINTER_OP
#include <QFont>
#include <QFontMetrics>
#include <QPainter>
#include <QSvgRenderer>
#endif

#include <QDebug>

#define LINE_SPACING 5

DisplayToolBox::DisplayToolBox() {}
#ifdef PAINTER_OP
QString DisplayToolBox::makeImage(QByteArray svgCode)
{
    QSvgRenderer svg(svgCode);

    QImage image(500, 60, QImage::Format_ARGB32);
    image.fill(QColor(255, 255, 255, 100)); // partly transparent red-ish background

    // Get QPainter that paints to the image
    QPainter painter(&image);
    svg.render(&painter);
    QByteArray ba;
    QBuffer buffer(&ba);
    buffer.open(QIODevice::WriteOnly);
    image.save(&buffer, "PNG");
    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(), Output::Svg, withColor));
            }
            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(), Output::Svg, withColor));
            }
            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("");
    }
}
#include <QVariantList>
QJsonArray DisplayToolBox::diceToJson(QList<ExportedDiceResult>& diceList, bool& allSameFaceCount, bool& allSameColor)
{
    allSameFaceCount= true;

    QJsonArray array;
    QStringList colorList;
    for(auto dice : diceList)
    {
        if(dice.size() > 1)
        {
            allSameFaceCount= false;
        }
        for(quint64 face : dice.keys())
        {
            ListDiceResult diceResults= dice.value(face);
            QJsonObject object;
            QVariantList listVariant;
            object["face"]= static_cast<int>(face);
            listVariant.reserve(diceResults.size());
            for(auto const& dice : diceResults)
            {
                QJsonObject diceObj;
                auto listValues= dice.getResult();
                if(!listValues.isEmpty())
                {
                    diceObj["total"]= static_cast<qint64>(listValues.takeFirst());
                    diceObj["face"]= static_cast<int>(face);
                    auto color= dice.getColor();
                    diceObj["color"]= color;
                    if(!colorList.contains(color))
                        colorList.append(color);
                    QJsonArray subValues;
                    for(auto result : listValues)
                    {
                        subValues.push_back(static_cast<qint64>(result));
                    }
                    diceObj["subvalues"]= subValues;
                }
                listVariant.append(QVariant::fromValue(diceObj));
            }
            object["values"]= QJsonArray::fromVariantList(listVariant);
            array.push_back(object);
        }
    }
    if(colorList.size() > 1)
        allSameColor= false;
    return array;
}
QString DisplayToolBox::diceResultToString(QJsonObject val, Output type, bool hasColor)
{
    auto total= QString::number(val["total"].toDouble());
    auto color= val["color"].toString();
    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(',')));
    }
    if(hasColor && !color.isEmpty())
    {
        if(type == Output::Terminal)
        {
            total= QStringLiteral("%1%2%3")
                       .arg(DisplayToolBox::colorToTermCode(color))
                       .arg(total)
                       .arg(DisplayToolBox::colorToTermCode(QStringLiteral("reset")));
        }
        else if(type == Output::Svg)
        {
            total= QStringLiteral("%1%2%3")
                       .arg(QStringLiteral("<tspan fill=\"%1\">").arg(color))
                       .arg(total)
                       .arg(QStringLiteral("</tspan>"));
        }
    }
    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(), Output::Terminal, withColor);
        }
        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(" - ");
}