diff src/converter.cpp @ 26:5acd601356ba

Make HTML work for QTextDocument
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 11 Apr 2016 13:06:48 +0200
parents e5c5ebfa4205
children ad54c98cb8d8
line wrap: on
line diff
--- a/src/converter.cpp	Mon Apr 11 11:24:17 2016 +0200
+++ b/src/converter.cpp	Mon Apr 11 13:06:48 2016 +0200
@@ -12,6 +12,8 @@
 #include <QRegularExpressionMatch>
 #include <QTextDocument>
 #include <QPrinter>
+#include <QImage>
+#include <QPainter>
 
 #include "xlsxdocument.h"
 #include "xlsxconditionalformatting.h"
@@ -61,23 +63,17 @@
     mAnswerTextFmt.setHorizontalAlignment(Format::AlignLeft);
 
     mTitleStyle = QStringLiteral("<tr><td colspan='3' style='vertical-align: top;"
-                                 "font-weight: bold;font-size:18; text-decoration:"
-                                 "underline;'>%1</td></tr>");
-    const QString questionBorders = QStringLiteral("style='border-bottom: 1pt solid black;"
-                                    "border-top: 1pt solid black;font-weight:"
-                                    "bold;font-size:11;'");
-    mQuestionStyle = QStringLiteral("<tr><td ") + questionBorders + QStringLiteral(">%1</td>") +
-                                    QStringLiteral("<td %1</td>").arg(questionBorders) +
-                                    QStringLiteral("<td %1</td>").arg(questionBorders);
-    mAnswerChoiceStyle= QStringLiteral("<tr><td style='text-align: left;font-size:11;'>Answer</td><td/><td/></tr>");
-    mChoiceTextStyle= QStringLiteral("<tr><td style='text-align: right;vertical-algin: center; font-size:11;'>%1</td>");
+                                 "font-weight: bold; text-decoration:underline; font-size: 18pt;'>"
+                                 "%1</td></tr><tr/>");
+    mQuestionStyle = QStringLiteral("<tr><td colspan='3' style='text-decoration: underline;"
+                                    "font-size: 11pt;font-weight: bold;'><hr/>%1<hr/></td></tr>");
+    mAnswerChoiceStyle= QStringLiteral("<tr><td colspan='3' style='text-align: left; font-size: 11pt;'>Answer</td></tr>");
+    mChoiceTextStyle= QStringLiteral("<tr><td align='right' style='vertical-align: middle; font-size:11pt;'>%1</td>");
     mChoiceVotesStyle = QStringLiteral("<td style='text-align: left;vertical-algin: center;"
-                                       "font-size:10;'</td>%1</td></tr>");
-    mAnswerTextStyle = QStringLiteral("<tr><td style='text-align: left;font-weight: bold;vertical-algin: center;"
-                                      "border-bottom: 1pt solid black;border-top: 1pt solid black;font-size:11;'"
-                                      ">Answer</td><td/></td></tr>");
-    mFreeTextStyle = QStringLiteral("<tr><td colspan='3' style='border-bottom: 1pt solid black;"
-                                    "border-top: 1pt solid black;font-size:11;'>%1</td></tr>");
+                                       "font-size:10pt;'</td>%1</td></tr>");
+    mAnswerTextStyle = QStringLiteral("<tr><td colspan='3' style='font-weight: bold;vertical-algin: middle;"
+                                      "font-size:11pt;'>Answer<hr/></td></tr>");
+    mFreeTextStyle = QStringLiteral("<tr><td colspan='3'; font-size:11pt;'>%1<hr/></td></tr>");
     mEmptyRow = QStringLiteral("<tr style='height: %1px'/>").arg(CHOICE_ROW_HEIGHT);
 }
 
@@ -115,10 +111,29 @@
     convertToXSLX(instream, outfile);
 }
 
-static void makeBar(QTextStream &html, double percent, QTextDocument &doc, bool forPDF)
+static void makeBar(QTextStream &html, double percent, int width, QTextDocument &doc, bool forPDF)
 {
-    html << QStringLiteral("<td style='background:linear-gradient(to right,"
-                           "#ff9933, #ff9933 %1%, #ffffff %1%)'></td>").arg(percent);
+    static int barCnt;
+    if (!forPDF) {
+        html << QStringLiteral("<td style='background:linear-gradient(to right,"
+                               BAR_COLOR ", " BAR_COLOR " %1%, #ffffff %1%)'></td>").arg(percent);
+        return;
+    }
+    QImage image(QSize(width, 25), QImage::Format_RGB32);
+    QPainter painter(&image);
+    QRect rect = image.rect();
+    qDebug() << "Image of " << width;
+    rect.setRight(rect.right() / (100 / percent));
+    painter.fillRect(rect, QColor(BAR_COLOR));
+    qDebug() << "Filled " << rect << " with color";
+    rect.setLeft(rect.right());
+    rect.setRight(width);
+    painter.fillRect(rect, Qt::white);
+    qDebug() << "Filled " << rect << " with white";
+    doc.addResource(QTextDocument::ImageResource, QUrl(QStringLiteral("internal://bar%1.png").arg(barCnt)),
+                    QVariant(image));
+    html << QStringLiteral("<td><img src=\"internal://bar%1.png\"/></td>").arg(barCnt++);
+    image.save(QStringLiteral("/tmp/foo.png"));
     return;
 }
 
@@ -151,13 +166,13 @@
     xlsx.setColumnWidth(26, sum + 1);
     xlsx.setColumnHidden(26, true);
 
+    int row = 1;
     html << "<html><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"
             "<body><table border=\"0\" style='width:\"" << HTML_WIDTH << "px\";border-collapse:collapse'>";
+    html << "<tr><th width=\"" << col1Width << "px\"</th>";
+    html << "<th width=\"" << col2Width << "px\"</th>";
+    html << "<th width=\"" << col3Width << "px\"</th>";
 
-    int row = 1;
-    html << "<tr><th width=\"" << col1Width << "\"</th>";
-    html << "<th width=\"" << col2Width << "\"</th>";
-    html << "<th width=\"" << col3Width << "\"</th>";
     const QString title = mTitle.isEmpty() ? DEFAULT_TITLE : mTitle;
     // Set the title of the Questionaire
     xlsx.write(row++, 1, title, mTitleFmt);
@@ -215,7 +230,7 @@
                 if (!ok) {
                     mErrors << "Unparsable number in string: " + choiceMatch.captured();
                 }
-                makeBar(html, percent, doc, mFmt == Format_PDF);
+                makeBar(html, percent, col2Width, doc, mFmt == Format_PDF);
                 xlsx.write(row, 2, percent);
                 const QString numVotesString = QString("%1% | %2 Number of votes").
                            arg(choiceMatch.captured(3)).arg(choiceMatch.captured(2));
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)