changeset 25:e5c5ebfa4205

Refactor to centralise html tags
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 11 Apr 2016 11:24:17 +0200
parents 1470cb8a00a8
children 5acd601356ba
files src/converter.cpp src/converter.h src/l10n/main_de_DE.ts
diffstat 3 files changed, 44 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/converter.cpp	Mon Apr 11 11:00:59 2016 +0200
+++ b/src/converter.cpp	Mon Apr 11 11:24:17 2016 +0200
@@ -34,7 +34,6 @@
     mTitleFmt.setFontBold(true);
     mTitleFmt.setVerticalAlignment(Format::AlignTop);
 
-    mTitleStyle = QStringLiteral("style='vertical-align: top;font-weight: bold;font-size:18; text-decoration: underline;'");
 
     mQuestionFmt.setFontSize(11);
     mQuestionFmt.setFontName("Calibri");
@@ -42,30 +41,44 @@
     mQuestionFmt.setTopBorderStyle(Format::BorderThin);
     mQuestionFmt.setBottomBorderStyle(Format::BorderThin);
     mQuestionFmt.setTextWarp(true);
-    mQuestionStyle = QStringLiteral("style='border-bottom: 1pt solid black;border-top: 1pt solid black;font-weight: bold;font-size:11;'");
 
     mAnswerChoiceFmt.setFontSize(11);
     mAnswerChoiceFmt.setFontName("Calibri");
     mAnswerChoiceFmt.setHorizontalAlignment(Format::AlignLeft);
     mAnswerChoiceFmt.setTextWarp(true);
-    mAnswerChoiceStyle= QStringLiteral("style='text-align: left;font-size:11;'");
 
     mChoiceTextFmt = mAnswerChoiceFmt;
     mChoiceTextFmt.setVerticalAlignment(Format::AlignVCenter);
-    mChoiceTextStyle= QStringLiteral("style='text-align: right;vertical-algin: center; font-size:11;'");
 
     mChoiceVotesFmt = mChoiceTextFmt;
     mChoiceVotesFmt.setFontSize(10);
-    mChoiceVotesStyle = QStringLiteral("style='text-align: left;vertical-algin: center; font-size:10;'");
 
     mFreeTextFmt = mQuestionFmt;
     mFreeTextFmt.setFontBold(false);
-    mFreeTextStyle = QStringLiteral("style='border-bottom: 1pt solid black;border-top: 1pt solid black;font-size:11;'");
 
     mAnswerTextFmt = mQuestionFmt;
     mAnswerTextFmt.setVerticalAlignment(Format::AlignVCenter);
     mAnswerTextFmt.setHorizontalAlignment(Format::AlignLeft);
-    mAnswerTextStyle = QStringLiteral("style='text-align: left;font-weight: bold;vertical-algin: center; border-bottom: 1pt solid black;border-top: 1pt solid black;font-size:11;'");
+
+    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>");
+    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>");
+    mEmptyRow = QStringLiteral("<tr style='height: %1px'/>").arg(CHOICE_ROW_HEIGHT);
 }
 
 void Converter::run()
@@ -102,6 +115,13 @@
     convertToXSLX(instream, outfile);
 }
 
+static void makeBar(QTextStream &html, double percent, QTextDocument &doc, bool forPDF)
+{
+    html << QStringLiteral("<td style='background:linear-gradient(to right,"
+                           "#ff9933, #ff9933 %1%, #ffffff %1%)'></td>").arg(percent);
+    return;
+}
+
 void Converter::convertToXSLX(QTextStream& instream, QFile &output)
 {
     Document xlsx;
@@ -138,11 +158,10 @@
     html << "<tr><th width=\"" << col1Width << "\"</th>";
     html << "<th width=\"" << col2Width << "\"</th>";
     html << "<th width=\"" << col3Width << "\"</th>";
-    html << "<tr><td colspan='3' " << mTitleStyle << "/>";
     const QString title = mTitle.isEmpty() ? DEFAULT_TITLE : mTitle;
     // Set the title of the Questionaire
     xlsx.write(row++, 1, title, mTitleFmt);
-    html << title.toHtmlEscaped() << "</td></tr>";
+    html << mTitleStyle.arg(title.toHtmlEscaped());
     xlsx.mergeCells("A1:C1");
     xlsx.setRowHeight(1, TITLE_ROW_HEIGHT);
 
@@ -169,15 +188,13 @@
         xlsx.write(row, 2, QString(" "), mQuestionFmt);
         xlsx.write(row, 3, QString(" "), mQuestionFmt);
         xlsx.write(row++, 1, question, mQuestionFmt);
-        html << "<tr><td " << mQuestionStyle << ">" << question.toHtmlEscaped() << "</td>"
-            "<td " << mQuestionStyle << "/>"
-            "<td " << mQuestionStyle << "/></tr>";
+        html << mQuestionStyle.arg(question.toHtmlEscaped());
 
         if (answerLine == QStringLiteral(CHOICE_IDENTIFIER)) {
             QRegularExpressionMatch choiceMatch = choiceEx.match(input, cursor);
             xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
             xlsx.write(row++, 1, "Answer", mAnswerChoiceFmt);
-            html << "<tr><td " << mAnswerChoiceStyle << ">" << "Answer" << "</td><td/><td/></tr>";
+            html << mAnswerChoiceStyle;
             int firstChoiceRow = row;
             int lastChoiceRow = row;
             while (choiceMatch.hasMatch() && choiceMatch.capturedStart() == cursor + 1) {
@@ -192,19 +209,17 @@
                     choiceName = " " + choiceName;
                 }
                 xlsx.write(row, 1, choiceName, mChoiceTextFmt);
-                html << "<tr><td " << mChoiceTextStyle << ">" << choiceName.toHtmlEscaped() << "</td>";
+                html << mChoiceTextStyle.arg(choiceName.toHtmlEscaped());
                 bool ok;
                 double percent = choiceMatch.captured(3).toDouble(&ok);
                 if (!ok) {
                     mErrors << "Unparsable number in string: " + choiceMatch.captured();
                 }
-                html << QStringLiteral("<td style='background:linear-gradient(to right,"
-                        "#ff9933, #ff9933 %1%, #ffffff %1%)'>").arg(percent);
-                html << "</td>";
+                makeBar(html, percent, 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));
-                html << "<td " << mChoiceVotesStyle << ">" << numVotesString.toHtmlEscaped() << "</td></tr>";
+                html << mChoiceVotesStyle.arg(numVotesString.toHtmlEscaped());
                 xlsx.write(row, 3, numVotesString, mChoiceVotesFmt);
                 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
                 /* As long as we can match a choice which is either before the next question
@@ -219,7 +234,7 @@
             QRegularExpressionMatch textMatch = freetxtEx.match(input, cursor);
             xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
             xlsx.write(row++, 1, "Answer", mAnswerTextFmt);
-            html << "<tr><td " << mAnswerTextStyle << ">" << "Answer" << "</td><td/><td/></tr>";
+            html << mAnswerTextStyle;
 
             /* To handle the workaround for quotes in answers we store
              * the number of rows and only afterwards create the html rows. */
@@ -280,16 +295,14 @@
                 textMatch = freetxtEx.match(input, cursor);
             }
             for (int i = firstFreeRow; i < row; i++) {
-                html << "<tr><td colspan='3' " << mFreeTextStyle << ">";
-                html << xlsx.read(i, 1).toString().toHtmlEscaped();
-                html << "</td></tr>";
+                html << mFreeTextStyle.arg(xlsx.read(i, 1).toString().toHtmlEscaped());
             }
         }
         /* Insert Empty row. */
         xlsx.setRowHeight(row++, CHOICE_ROW_HEIGHT);
         match = questionEx.match(input, cursor);
         cursor = match.capturedEnd();
-        html << QStringLiteral("<tr style='height: %1px'/>").arg(CHOICE_ROW_HEIGHT);
+        html << mEmptyRow;
     }
     xlsx.addConditionalFormatting(bars);
 
--- a/src/converter.h	Mon Apr 11 11:00:59 2016 +0200
+++ b/src/converter.h	Mon Apr 11 11:24:17 2016 +0200
@@ -82,7 +82,8 @@
             mAnswerTextStyle,
             mFreeTextStyle,
             mChoiceTextStyle,
-            mChoiceVotesStyle;
+            mChoiceVotesStyle,
+            mEmptyRow;
 };
 
 #endif // CONVERTER_H
--- a/src/l10n/main_de_DE.ts	Mon Apr 11 11:00:59 2016 +0200
+++ b/src/l10n/main_de_DE.ts	Mon Apr 11 11:24:17 2016 +0200
@@ -4,32 +4,32 @@
 <context>
     <name>Converter</name>
     <message>
-        <location filename="../converter.cpp" line="78"/>
+        <location filename="../converter.cpp" line="90"/>
         <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="84"/>
+        <location filename="../converter.cpp" line="96"/>
         <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="93"/>
+        <location filename="../converter.cpp" line="105"/>
         <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="99"/>
+        <location filename="../converter.cpp" line="111"/>
         <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="298"/>
+        <location filename="../converter.cpp" line="310"/>
         <source>Failed to parse input document.</source>
         <translation>Fehler bei der verarbeitung des Eingabedokuments.</translation>
     </message>
     <message>
-        <location filename="../converter.cpp" line="302"/>
+        <location filename="../converter.cpp" line="314"/>
         <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)