diff options
| author | 2019-03-23 01:15:02 +0100 | |
|---|---|---|
| committer | 2019-03-23 01:15:02 +0100 | |
| commit | d0f326f7289add5d84c662c6c813f1c3c88fca4f (patch) | |
| tree | b4eef57399baee34b8e349abb3f0f1ea390360b8 /cli | |
| parent | 489661ca4599a84033a57a5645e150e4129a70a0 (diff) | |
| download | OneRoll-d0f326f7289add5d84c662c6c813f1c3c88fca4f.tar.gz OneRoll-d0f326f7289add5d84c662c6c813f1c3c88fca4f.zip | |
Add color management for every dice.
Diffstat (limited to 'cli')
| -rw-r--r-- | cli/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | cli/displaytoolbox.cpp | 269 | ||||
| -rw-r--r-- | cli/displaytoolbox.h | 12 | ||||
| -rw-r--r-- | cli/main.cpp | 52 |
4 files changed, 97 insertions, 239 deletions
diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 4374b9f..3c14576 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -37,6 +37,7 @@ ADD_DEFINITIONS( set(dice_RESOURCES diceparser.qrc) FIND_PACKAGE(Qt5LinguistTools) +find_package(Qt5Svg) IF(UPDATE_TRANSLATIONS) @@ -111,7 +112,7 @@ SET( dice_sources add_executable( dice ${dice_sources} ${dice_QM} ) -target_link_libraries(dice ${Qt5Core_LIBRARIES} ${Qt5Gui_LIBRARIES}) +target_link_libraries(dice ${Qt5Core_LIBRARIES} ${Qt5Gui_LIBRARIES} ${Qt5Svg_LIBRARIES}) INSTALL_TARGETS(/bin dice) #qt5_use_modules() diff --git a/cli/displaytoolbox.cpp b/cli/displaytoolbox.cpp index 3474cf9..5f0b64a 100644 --- a/cli/displaytoolbox.cpp +++ b/cli/displaytoolbox.cpp @@ -7,7 +7,7 @@ #include <QFont> #include <QFontMetrics> #include <QPainter> -//#include <QFontDatabase> +#include <QSvgRenderer> #endif #include <QDebug> @@ -16,165 +16,20 @@ DisplayToolBox::DisplayToolBox() {} #ifdef PAINTER_OP -QString DisplayToolBox::makeImage(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, - QString comment, bool allSameFaceCount, bool allSameColor) +QString DisplayToolBox::makeImage(QByteArray svgCode) { - // qDebug() <<"scarlarText:" <<scalarText << "resultStr:"<< resultStr<<"array" << array<< "with color:" << withColor - // << "cmd" << cmd << "comment" << comment << "allsamecolor"<< allSameColor; - QString textTotal("%1 Details:[%3 %2]"); + QSvgRenderer svg(svgCode); - int lineCount= 2; - if(!comment.isEmpty()) - { - lineCount= 3; - } - - if(resultStr.isEmpty()) - { - auto list= diceToText(array, false, allSameFaceCount, allSameColor); - textTotal= textTotal.arg(scalarText).arg(list).arg(cmd); - } - else - { - --lineCount; - textTotal= resultStr; - } - if(comment.size() > 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); + QImage image(500, 60, QImage::Format_ARGB32); + image.fill(QColor(255, 255, 255, 100)); // partly transparent red-ish background - 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"); + // Get QPainter that paints to the image + QPainter painter(&image); + svg.render(&painter); QByteArray ba; QBuffer buffer(&ba); buffer.open(QIODevice::WriteOnly); - img.save(&buffer, "PNG"); - - // return {}; + image.save(&buffer, "PNG"); return ba.toBase64(); } #endif @@ -229,18 +84,9 @@ QString DisplayToolBox::diceToSvg(QJsonArray array, bool withColor, bool allSame auto values= obj["values"].toArray(); for(auto valRef : values) { - subResult.append(diceResultToString(valRef.toObject())); - } - if(withColor) - { - result.append(QStringLiteral("<tspan fill=\"%1\">").arg(obj["color"].toString())); - result.append(subResult.join(',')); - result.append(QStringLiteral("</tspan>")); - } - else - { - result.append(subResult.join(',')); + subResult.append(diceResultToString(valRef.toObject(), Output::Svg, withColor)); } + result.append(subResult.join(',')); } return result.join(""); } @@ -255,7 +101,7 @@ QString DisplayToolBox::diceToSvg(QJsonArray array, bool withColor, bool allSame for(auto valRef : values) { - subResult.append(diceResultToString(valRef.toObject())); + subResult.append(diceResultToString(valRef.toObject(), Output::Svg, withColor)); } result.append(QStringLiteral("d%1:(").arg(obj["face"].toInt())); if(withColor) @@ -278,8 +124,9 @@ QString DisplayToolBox::diceToSvg(QJsonArray array, bool withColor, bool allSame QJsonArray DisplayToolBox::diceToJson(QList<ExportedDiceResult>& diceList, bool& allSameFaceCount, bool& allSameColor) { allSameFaceCount= true; - allSameColor= true; + QJsonArray array; + QStringList colorList; for(auto dice : diceList) { if(dice.size() > 1) @@ -289,64 +136,45 @@ QJsonArray DisplayToolBox::diceToJson(QList<ExportedDiceResult>& diceList, bool& for(int face : dice.keys()) { ListDiceResult diceResults= dice.value(face); - std::vector<QString> alreadyDoneColor; - std::vector<std::vector<HighLightDice>> 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<HighLightDice>()); - alreadyDoneColor.push_back(diceResult.getColor()); - it= alreadyDoneColor.end(); - --it; - } - auto i= std::distance(alreadyDoneColor.begin(), it); - sameColorDice[static_cast<std::size_t>(i)].push_back(diceResult); - } - int i= 0; - for(auto it= alreadyDoneColor.begin(); it != alreadyDoneColor.end() && i < sameColorDice.size(); ++it) + QJsonObject object; + QJsonArray values; + object["face"]= face; + for(auto const& dice : diceResults) { - auto list= sameColorDice[static_cast<std::size_t>(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()) { - QJsonObject diceObj; - auto listValues= dice.getResult(); - if(!listValues.isEmpty()) + diceObj["total"]= static_cast<qint64>(listValues.takeFirst()); + diceObj["face"]= face; + auto color= dice.getColor(); + diceObj["color"]= color; + if(!colorList.contains(color)) + colorList.append(color); + QJsonArray subValues; + for(auto result : listValues) { - diceObj["total"]= static_cast<qint64>(listValues.takeFirst()); - QJsonArray subValues; - for(auto result : listValues) - { - subValues.push_back(static_cast<qint64>(result)); - } - diceObj["subvalues"]= subValues; + subValues.push_back(static_cast<qint64>(result)); } - values.push_back(diceObj); + diceObj["subvalues"]= subValues; } - object["values"]= values; - ++i; - array.push_back(object); + values.push_back(diceObj); } + object["values"]= values; + array.push_back(object); } } + if(colorList.size() > 1) + allSameColor= false; return array; } -QString DisplayToolBox::diceResultToString(QJsonObject val) +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()); @@ -355,6 +183,23 @@ QString DisplayToolBox::diceResultToString(QJsonObject val) { 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) @@ -370,7 +215,7 @@ QString DisplayToolBox::diceToText(QJsonArray array, bool withColor, bool allSam QStringList diceResult; for(auto valRef : values) { - diceResult+= diceResultToString(valRef.toObject()); + diceResult+= diceResultToString(valRef.toObject(), Output::Terminal, withColor); } if(!diceResult.isEmpty()) { diff --git a/cli/displaytoolbox.h b/cli/displaytoolbox.h index 34859e9..30b7ba3 100644 --- a/cli/displaytoolbox.h +++ b/cli/displaytoolbox.h @@ -9,16 +9,22 @@ class DisplayToolBox { public: + enum class Output + { + Svg, + Terminal, + Json, + Image + }; DisplayToolBox(); #ifdef PAINTER_OP - static QString makeImage(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, - QString comment, bool allSameFaceCount, bool allSameColor); + static QString makeImage(QByteArray svgCode); #endif static QString colorToTermCode(QString str); static QString diceToText(QJsonArray array, bool withColor, bool allSameFaceCount, bool allSameColor); static QJsonArray diceToJson(QList<ExportedDiceResult>& diceList, bool& allSameFaceCount, bool& allSameColor); static QString diceToSvg(QJsonArray array, bool withColor, bool allSameColor, bool allSameFaceCount); - static QString diceResultToString(QJsonObject val); + static QString diceResultToString(QJsonObject val, Output type, bool hasColor); }; #endif // GENERATEIMAGE_H diff --git a/cli/main.cpp b/cli/main.cpp index e261d63..1a2f56a 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -112,14 +112,7 @@ QString diceToMarkdown(QJsonArray array, bool withColor, bool allSameColor, bool return result.join(' '); } } -#ifdef PAINTER_OP -void displayImage(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString comment, - bool allSameFaceCount, bool allSameColor) -{ - out << DisplayToolBox::makeImage( - scalarText, resultStr, array, withColor, cmd, comment, allSameFaceCount, allSameColor); -} -#endif + void displayJSon(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor) { @@ -170,7 +163,7 @@ void displayMarkdown(QString scalarText, QString resultStr, QJsonArray array, bo str.append(QStringLiteral("```")); out << str; } -void displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, +QString displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor) { QString str( @@ -200,19 +193,21 @@ void displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool wi if(resultStr.isEmpty()) { if(withColor) - str.append(QStringLiteral( - "<text font-size=\"16\" x=\"0\" y=\"%4\"><tspan fill=\"red\">%1</tspan> details:[%3 (%2)]</text>") + str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%4\"><tspan fill=\"red\">%1</tspan>\n" + "<tspan x=\"0\" y=\"%5\">details:</tspan>[%3 (%2)]</text>") .arg(scalarText) .arg(diceList) .arg(cmd) - .arg(y)); + .arg(y) + .arg(y * 2)); else - str.append( - QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%4\"><tspan>%1</tspan> details:[%3 (%2)]</text>") - .arg(scalarText) - .arg(diceList) - .arg(cmd) - .arg(y)); + str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%4\"><tspan>%1</tspan>\n" + "<tspan x=\"0\" y=\"%5\">details:</tspan>[%3 (%2)]</text>") + .arg(scalarText) + .arg(diceList) + .arg(cmd) + .arg(y) + .arg(y * 2)); } else if(!resultStr.isEmpty()) { @@ -221,9 +216,19 @@ void displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool wi } } str.append(QStringLiteral("</svg>\n")); - out << str << "\n"; + return str; } +#ifdef PAINTER_OP +void displayImage(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, + QString warning, QString comment, bool allSameFaceCount, bool allSameColor) +{ + auto svg= displaySVG( + scalarText, resultStr, array, withColor, cmd, error, warning, comment, allSameFaceCount, allSameColor); + out << DisplayToolBox::makeImage(svg.toUtf8()); +} +#endif + void displayCommandResult(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor) { @@ -399,8 +404,9 @@ int startDiceParsing(QStringList& cmds, QString& treeFile, bool withColor, EXPOR allSameFaceCount, allSameColor); break; case SVG: - displaySVG(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment, - allSameFaceCount, allSameColor); + out << displaySVG(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment, + allSameFaceCount, allSameColor) + << "\n"; break; case BOT: case MARKDOWN: @@ -413,8 +419,8 @@ int startDiceParsing(QStringList& cmds, QString& treeFile, bool withColor, EXPOR break; #ifdef PAINTER_OP case IMAGE: - displayImage( - scalarText, resultStr, array, withColor, cmdRework, comment, allSameFaceCount, allSameColor); + displayImage(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment, + allSameFaceCount, allSameColor); break; #endif } |