#include "displaytoolbox.h" #include #include #include #ifdef PAINTER_OP #include #include #include //#include #endif #include #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:" < 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(); painter.save(); 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("").arg(obj["color"].toString())); result.append(subResult.join(',')); result.append(QStringLiteral("")); } 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("").arg(obj["color"].toString())); } result.append(subResult.join(',')); if(withColor) { result.append(QStringLiteral(")")); } else { result.append(QStringLiteral(")")); } } return result.join(""); } } QJsonArray DisplayToolBox::diceToJson(QList& 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 alreadyDoneColor; std::vector> sameColorDice; for(auto& diceResult : diceResults) { if(!diceResult.getColor().isEmpty()) { allSameColor= false; } auto it= std::find_if(alreadyDoneColor.begin(), alreadyDoneColor.end(), [diceResult](QString color) { return color == diceResult.getColor(); }); if(it == alreadyDoneColor.end()) { sameColorDice.push_back(std::vector()); alreadyDoneColor.push_back(diceResult.getColor()); it= alreadyDoneColor.end(); --it; } auto i= std::distance(alreadyDoneColor.begin(), it); sameColorDice[static_cast(i)].push_back(diceResult); } int i= 0; for(auto it= alreadyDoneColor.begin(); it != alreadyDoneColor.end() && i < sameColorDice.size(); ++it) { auto list= sameColorDice[static_cast(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(listValues.takeFirst()); QJsonArray subValues; for(auto result : listValues) { subValues.push_back(static_cast(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(" - "); }