aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/bin/cli/displaytoolbox.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/cli/displaytoolbox.cpp')
-rw-r--r--src/bin/cli/displaytoolbox.cpp168
1 files changed, 168 insertions, 0 deletions
diff --git a/src/bin/cli/displaytoolbox.cpp b/src/bin/cli/displaytoolbox.cpp
new file mode 100644
index 0000000..1709d18
--- /dev/null
+++ b/src/bin/cli/displaytoolbox.cpp
@@ -0,0 +1,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;
+}