changeset 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 0129971c7046
files src/constants.h src/converter.cpp src/l10n/main_de_DE.ts
diffstat 3 files changed, 48 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/constants.h	Mon Apr 11 11:24:17 2016 +0200
+++ b/src/constants.h	Mon Apr 11 13:06:48 2016 +0200
@@ -47,7 +47,7 @@
 #define COLUMN_CNT 3
 /**
  * @brief The width of the columns in characters. */
-#define COLUMN_WIDTHS { 40, 20, 26 }
+#define COLUMN_WIDTHS { 40, 18, 26 }
 
 /**
  * @brief Regular expression to define a question.
@@ -81,4 +81,6 @@
 
 #define HTML_WIDTH 960
 
+#define BAR_COLOR "#ff9933"
+
 #endif // CONSTANTS_H
--- 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));
--- a/src/l10n/main_de_DE.ts	Mon Apr 11 11:24:17 2016 +0200
+++ b/src/l10n/main_de_DE.ts	Mon Apr 11 13:06:48 2016 +0200
@@ -4,32 +4,32 @@
 <context>
     <name>Converter</name>
     <message>
-        <location filename="../converter.cpp" line="90"/>
+        <location filename="../converter.cpp" line="86"/>
         <source>Failed to open standard input and no input file provided.</source>
         <translation>Öffnen des Eingabekanals Fehlgeschlagen und keine Eingabedatei übergeben.</translation>
     </message>
     <message>
-        <location filename="../converter.cpp" line="96"/>
+        <location filename="../converter.cpp" line="92"/>
         <source>Failed to open %1 for reading.</source>
         <translation>Die Datei &quot;%1&quot; konnte nicht gelesen werden.</translation>
     </message>
     <message>
-        <location filename="../converter.cpp" line="105"/>
+        <location filename="../converter.cpp" line="101"/>
         <source>Failed to open standard output and no output file provided.</source>
         <translation>Öffnen des Ausgabekanals Fehlgeschlagen und keine Ausgabedatei übergeben.</translation>
     </message>
     <message>
-        <location filename="../converter.cpp" line="111"/>
+        <location filename="../converter.cpp" line="107"/>
         <source>Failed to open %1 for writing.</source>
         <translation>Die Datei &quot;%1&quot; konnte nicht geöffnet werden.</translation>
     </message>
     <message>
-        <location filename="../converter.cpp" line="310"/>
+        <location filename="../converter.cpp" line="325"/>
         <source>Failed to parse input document.</source>
         <translation>Fehler bei der verarbeitung des Eingabedokuments.</translation>
     </message>
     <message>
-        <location filename="../converter.cpp" line="314"/>
+        <location filename="../converter.cpp" line="329"/>
         <source>Saving the XLSX document failed.</source>
         <translation>Das erstellen des XLSX Dokuments ist fehlgeschlagen.</translation>
     </message>
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)