comparison 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
comparison
equal deleted inserted replaced
25:e5c5ebfa4205 26:5acd601356ba
10 #include <QDebug> 10 #include <QDebug>
11 #include <QRegularExpression> 11 #include <QRegularExpression>
12 #include <QRegularExpressionMatch> 12 #include <QRegularExpressionMatch>
13 #include <QTextDocument> 13 #include <QTextDocument>
14 #include <QPrinter> 14 #include <QPrinter>
15 #include <QImage>
16 #include <QPainter>
15 17
16 #include "xlsxdocument.h" 18 #include "xlsxdocument.h"
17 #include "xlsxconditionalformatting.h" 19 #include "xlsxconditionalformatting.h"
18 20
19 #include "constants.h" 21 #include "constants.h"
59 mAnswerTextFmt = mQuestionFmt; 61 mAnswerTextFmt = mQuestionFmt;
60 mAnswerTextFmt.setVerticalAlignment(Format::AlignVCenter); 62 mAnswerTextFmt.setVerticalAlignment(Format::AlignVCenter);
61 mAnswerTextFmt.setHorizontalAlignment(Format::AlignLeft); 63 mAnswerTextFmt.setHorizontalAlignment(Format::AlignLeft);
62 64
63 mTitleStyle = QStringLiteral("<tr><td colspan='3' style='vertical-align: top;" 65 mTitleStyle = QStringLiteral("<tr><td colspan='3' style='vertical-align: top;"
64 "font-weight: bold;font-size:18; text-decoration:" 66 "font-weight: bold; text-decoration:underline; font-size: 18pt;'>"
65 "underline;'>%1</td></tr>"); 67 "%1</td></tr><tr/>");
66 const QString questionBorders = QStringLiteral("style='border-bottom: 1pt solid black;" 68 mQuestionStyle = QStringLiteral("<tr><td colspan='3' style='text-decoration: underline;"
67 "border-top: 1pt solid black;font-weight:" 69 "font-size: 11pt;font-weight: bold;'><hr/>%1<hr/></td></tr>");
68 "bold;font-size:11;'"); 70 mAnswerChoiceStyle= QStringLiteral("<tr><td colspan='3' style='text-align: left; font-size: 11pt;'>Answer</td></tr>");
69 mQuestionStyle = QStringLiteral("<tr><td ") + questionBorders + QStringLiteral(">%1</td>") + 71 mChoiceTextStyle= QStringLiteral("<tr><td align='right' style='vertical-align: middle; font-size:11pt;'>%1</td>");
70 QStringLiteral("<td %1</td>").arg(questionBorders) +
71 QStringLiteral("<td %1</td>").arg(questionBorders);
72 mAnswerChoiceStyle= QStringLiteral("<tr><td style='text-align: left;font-size:11;'>Answer</td><td/><td/></tr>");
73 mChoiceTextStyle= QStringLiteral("<tr><td style='text-align: right;vertical-algin: center; font-size:11;'>%1</td>");
74 mChoiceVotesStyle = QStringLiteral("<td style='text-align: left;vertical-algin: center;" 72 mChoiceVotesStyle = QStringLiteral("<td style='text-align: left;vertical-algin: center;"
75 "font-size:10;'</td>%1</td></tr>"); 73 "font-size:10pt;'</td>%1</td></tr>");
76 mAnswerTextStyle = QStringLiteral("<tr><td style='text-align: left;font-weight: bold;vertical-algin: center;" 74 mAnswerTextStyle = QStringLiteral("<tr><td colspan='3' style='font-weight: bold;vertical-algin: middle;"
77 "border-bottom: 1pt solid black;border-top: 1pt solid black;font-size:11;'" 75 "font-size:11pt;'>Answer<hr/></td></tr>");
78 ">Answer</td><td/></td></tr>"); 76 mFreeTextStyle = QStringLiteral("<tr><td colspan='3'; font-size:11pt;'>%1<hr/></td></tr>");
79 mFreeTextStyle = QStringLiteral("<tr><td colspan='3' style='border-bottom: 1pt solid black;"
80 "border-top: 1pt solid black;font-size:11;'>%1</td></tr>");
81 mEmptyRow = QStringLiteral("<tr style='height: %1px'/>").arg(CHOICE_ROW_HEIGHT); 77 mEmptyRow = QStringLiteral("<tr style='height: %1px'/>").arg(CHOICE_ROW_HEIGHT);
82 } 78 }
83 79
84 void Converter::run() 80 void Converter::run()
85 { 81 {
113 } 109 }
114 } 110 }
115 convertToXSLX(instream, outfile); 111 convertToXSLX(instream, outfile);
116 } 112 }
117 113
118 static void makeBar(QTextStream &html, double percent, QTextDocument &doc, bool forPDF) 114 static void makeBar(QTextStream &html, double percent, int width, QTextDocument &doc, bool forPDF)
119 { 115 {
120 html << QStringLiteral("<td style='background:linear-gradient(to right," 116 static int barCnt;
121 "#ff9933, #ff9933 %1%, #ffffff %1%)'></td>").arg(percent); 117 if (!forPDF) {
118 html << QStringLiteral("<td style='background:linear-gradient(to right,"
119 BAR_COLOR ", " BAR_COLOR " %1%, #ffffff %1%)'></td>").arg(percent);
120 return;
121 }
122 QImage image(QSize(width, 25), QImage::Format_RGB32);
123 QPainter painter(&image);
124 QRect rect = image.rect();
125 qDebug() << "Image of " << width;
126 rect.setRight(rect.right() / (100 / percent));
127 painter.fillRect(rect, QColor(BAR_COLOR));
128 qDebug() << "Filled " << rect << " with color";
129 rect.setLeft(rect.right());
130 rect.setRight(width);
131 painter.fillRect(rect, Qt::white);
132 qDebug() << "Filled " << rect << " with white";
133 doc.addResource(QTextDocument::ImageResource, QUrl(QStringLiteral("internal://bar%1.png").arg(barCnt)),
134 QVariant(image));
135 html << QStringLiteral("<td><img src=\"internal://bar%1.png\"/></td>").arg(barCnt++);
136 image.save(QStringLiteral("/tmp/foo.png"));
122 return; 137 return;
123 } 138 }
124 139
125 void Converter::convertToXSLX(QTextStream& instream, QFile &output) 140 void Converter::convertToXSLX(QTextStream& instream, QFile &output)
126 { 141 {
149 164
150 /* For the merged cell wordwrap trick. */ 165 /* For the merged cell wordwrap trick. */
151 xlsx.setColumnWidth(26, sum + 1); 166 xlsx.setColumnWidth(26, sum + 1);
152 xlsx.setColumnHidden(26, true); 167 xlsx.setColumnHidden(26, true);
153 168
169 int row = 1;
154 html << "<html><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">" 170 html << "<html><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"
155 "<body><table border=\"0\" style='width:\"" << HTML_WIDTH << "px\";border-collapse:collapse'>"; 171 "<body><table border=\"0\" style='width:\"" << HTML_WIDTH << "px\";border-collapse:collapse'>";
156 172 html << "<tr><th width=\"" << col1Width << "px\"</th>";
157 int row = 1; 173 html << "<th width=\"" << col2Width << "px\"</th>";
158 html << "<tr><th width=\"" << col1Width << "\"</th>"; 174 html << "<th width=\"" << col3Width << "px\"</th>";
159 html << "<th width=\"" << col2Width << "\"</th>"; 175
160 html << "<th width=\"" << col3Width << "\"</th>";
161 const QString title = mTitle.isEmpty() ? DEFAULT_TITLE : mTitle; 176 const QString title = mTitle.isEmpty() ? DEFAULT_TITLE : mTitle;
162 // Set the title of the Questionaire 177 // Set the title of the Questionaire
163 xlsx.write(row++, 1, title, mTitleFmt); 178 xlsx.write(row++, 1, title, mTitleFmt);
164 html << mTitleStyle.arg(title.toHtmlEscaped()); 179 html << mTitleStyle.arg(title.toHtmlEscaped());
165 xlsx.mergeCells("A1:C1"); 180 xlsx.mergeCells("A1:C1");
213 bool ok; 228 bool ok;
214 double percent = choiceMatch.captured(3).toDouble(&ok); 229 double percent = choiceMatch.captured(3).toDouble(&ok);
215 if (!ok) { 230 if (!ok) {
216 mErrors << "Unparsable number in string: " + choiceMatch.captured(); 231 mErrors << "Unparsable number in string: " + choiceMatch.captured();
217 } 232 }
218 makeBar(html, percent, doc, mFmt == Format_PDF); 233 makeBar(html, percent, col2Width, doc, mFmt == Format_PDF);
219 xlsx.write(row, 2, percent); 234 xlsx.write(row, 2, percent);
220 const QString numVotesString = QString("%1% | %2 Number of votes"). 235 const QString numVotesString = QString("%1% | %2 Number of votes").
221 arg(choiceMatch.captured(3)).arg(choiceMatch.captured(2)); 236 arg(choiceMatch.captured(3)).arg(choiceMatch.captured(2));
222 html << mChoiceVotesStyle.arg(numVotesString.toHtmlEscaped()); 237 html << mChoiceVotesStyle.arg(numVotesString.toHtmlEscaped());
223 xlsx.write(row, 3, numVotesString, mChoiceVotesFmt); 238 xlsx.write(row, 3, numVotesString, mChoiceVotesFmt);
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)