Mercurial > clickerconvert
diff src/converter.cpp @ 25:e5c5ebfa4205
Refactor to centralise html tags
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 11 Apr 2016 11:24:17 +0200 |
parents | 927794e3cc52 |
children | 5acd601356ba |
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);