File indexing completed on 2024-05-12 05:39:25

0001 #include "displaytoolbox.h"
0002 #include <QBuffer>
0003 #include <QJsonArray>
0004 #include <QJsonObject>
0005 
0006 #ifdef PAINTER_OP
0007 #include <QFont>
0008 #include <QFontMetrics>
0009 #include <QPainter>
0010 #include <QSvgRenderer>
0011 #endif
0012 
0013 #include <QDebug>
0014 
0015 #define LINE_SPACING 5
0016 
0017 DisplayToolBox::DisplayToolBox() {}
0018 #ifdef PAINTER_OP
0019 QString DisplayToolBox::makeImage(QByteArray svgCode)
0020 {
0021     QSvgRenderer svg(svgCode);
0022 
0023     QImage image(500, 60, QImage::Format_ARGB32);
0024     image.fill(QColor(255, 255, 255, 100)); // partly transparent red-ish background
0025 
0026     // Get QPainter that paints to the image
0027     QPainter painter(&image);
0028     svg.render(&painter);
0029     QByteArray ba;
0030     QBuffer buffer(&ba);
0031     buffer.open(QIODevice::WriteOnly);
0032     image.save(&buffer, "PNG");
0033     return ba.toBase64();
0034 }
0035 #endif
0036 
0037 QString DisplayToolBox::colorToIntCode(QString str)
0038 {
0039     if(str.isEmpty() || str == QStringLiteral("black"))
0040     {
0041         return QStringLiteral("0;31");
0042     }
0043     else if(str == QStringLiteral("white"))
0044     {
0045         return QStringLiteral("97");
0046     }
0047     else if(str == QStringLiteral("blue"))
0048     {
0049         return QStringLiteral("34");
0050     }
0051     else if(str == QStringLiteral("red"))
0052     {
0053         return QStringLiteral("31");
0054     }
0055     else if(str == QStringLiteral("black"))
0056     {
0057         return QStringLiteral("30");
0058     }
0059     else if(str == QStringLiteral("green"))
0060     {
0061         return QStringLiteral("32");
0062     }
0063     else if(str == QStringLiteral("yellow"))
0064     {
0065         return QStringLiteral("33");
0066     }
0067     else if(str == QStringLiteral("cyan"))
0068     {
0069         return QStringLiteral("36");
0070     }
0071     else if(str == QStringLiteral("reset"))
0072     {
0073         return QStringLiteral("0");
0074     }
0075     return {};
0076 }
0077 
0078 QString DisplayToolBox::colorToTermCode(QString str)
0079 {
0080     return QStringLiteral("\e[").append(DisplayToolBox::colorToIntCode(str)).append("m");
0081 }
0082 
0083 QString DisplayToolBox::diceToSvg(QJsonArray array, bool withColor, bool allSameColor, bool allSameFaceCount)
0084 {
0085     Q_UNUSED(allSameColor)
0086     if(allSameFaceCount)
0087     {
0088         QStringList result;
0089         for(auto item : array)
0090         {
0091             QStringList subResult;
0092             auto obj= item.toObject();
0093             auto values= obj["values"].toArray();
0094             for(auto valRef : values)
0095             {
0096                 subResult.append(diceResultToString(valRef.toObject(), Output::Svg, withColor));
0097             }
0098             result.append(subResult.join(','));
0099         }
0100         return result.join("");
0101     }
0102     else
0103     {
0104         QStringList result;
0105         for(auto item : array)
0106         {
0107             QStringList subResult;
0108             auto obj= item.toObject();
0109             auto values= obj["values"].toArray();
0110 
0111             for(auto valRef : values)
0112             {
0113                 subResult.append(diceResultToString(valRef.toObject(), Output::Svg, withColor));
0114             }
0115             result.append(QStringLiteral("d%1:(").arg(obj["face"].toInt()));
0116             if(withColor)
0117             {
0118                 result.append(QStringLiteral("<tspan fill=\"%1\">").arg(obj["color"].toString()));
0119             }
0120             result.append(subResult.join(','));
0121             if(withColor)
0122             {
0123                 result.append(QStringLiteral("</tspan>)"));
0124             }
0125             else
0126             {
0127                 result.append(QStringLiteral(")"));
0128             }
0129         }
0130         return result.join("");
0131     }
0132 }
0133 #include <QVariantList>
0134 
0135 QString DisplayToolBox::diceResultToString(QJsonObject val, Output type, bool hasColor)
0136 {
0137     auto total= QString::number(val["total"].toDouble());
0138     auto color= val["color"].toString();
0139     auto subvalues= val["subvalues"].toArray();
0140     QStringList subStr;
0141 
0142     for(auto subval : subvalues)
0143     {
0144         subStr << QString::number(subval.toDouble());
0145     }
0146     if(!subStr.isEmpty())
0147     {
0148         total.append(QStringLiteral(" [%1]").arg(subStr.join(',')));
0149     }
0150     if(hasColor && !color.isEmpty())
0151     {
0152         if(type == Output::Terminal)
0153         {
0154             total= QStringLiteral("%1%2%3")
0155                        .arg(DisplayToolBox::colorToTermCode(color))
0156                        .arg(total)
0157                        .arg(DisplayToolBox::colorToTermCode(QStringLiteral("reset")));
0158         }
0159         else if(type == Output::Svg)
0160         {
0161             total= QStringLiteral("%1%2%3")
0162                        .arg(QStringLiteral("<tspan fill=\"%1\">").arg(color))
0163                        .arg(total)
0164                        .arg(QStringLiteral("</tspan>"));
0165         }
0166     }
0167     return total;
0168 }