aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/displaytoolbox.cpp
blob: 1709d181cf9647ac89e33efa21df32bb06d592d1 (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
#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::colorToIntCode(QString str)
{
    if(str.isEmpty() || str == QStringLiteral("black"))
    {
        return QStringLiteral("0;31");
    }
    else if(str == QStringLiteral("white"))
    {
        return QStringLiteral("97");
    }
    else if(str == QStringLiteral("blue"))
    {
        return QStringLiteral("34");
    }
    else if(str == QStringLiteral("red"))
    {
        return QStringLiteral("31");
    }
    else if(str == QStringLiteral("black"))
    {
        return QStringLiteral("30");
    }
    else if(str == QStringLiteral("green"))
    {
        return QStringLiteral("32");
    }
    else if(str == QStringLiteral("yellow"))
    {
        return QStringLiteral("33");
    }
    else if(str == QStringLiteral("cyan"))
    {
        return QStringLiteral("36");
    }
    else if(str == QStringLiteral("reset"))
    {
        return QStringLiteral("0");
    }
    return {};
}

QString DisplayToolBox::colorToTermCode(QString str)
{
    return QStringLiteral("\e[").append(DisplayToolBox::colorToIntCode(str)).append("m");
}

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>

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;
}