Mercurial > clickerconvert
annotate 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 |
rev | line source |
---|---|
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
1 /* Copyright (C) 2016 by ETH Zürich |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
2 * Software engineering by Intevation GmbH |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
3 * |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
4 * This file is Free Software under the GNU GPL (v>=2) |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
5 * and comes with ABSOLUTELY NO WARRANTY! |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
6 * See LICENSE.txt for details. |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
7 */ |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
8 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
9 #include "converter.h" |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
10 #include <QDebug> |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
11 #include <QRegularExpression> |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
12 #include <QRegularExpressionMatch> |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
13 #include <QTextDocument> |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
14 #include <QPrinter> |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
15 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
16 #include "xlsxdocument.h" |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
17 #include "xlsxconditionalformatting.h" |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
18 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
19 #include "constants.h" |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
20 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
21 QTXLSX_USE_NAMESPACE |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
22 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
23 Converter::Converter(const QString &input, const QString &output, |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
24 ConvertFormat fmt, const QString &title): |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
25 QThread(Q_NULLPTR), |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
26 mInput(input), |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
27 mOutput(output), |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
28 mFmt(fmt), |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
29 mTitle(title) |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
30 { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
31 mTitleFmt.setFontUnderline(Format::FontUnderlineSingle); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
32 mTitleFmt.setFontSize(18); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
33 mTitleFmt.setFontName("Calibri"); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
34 mTitleFmt.setFontBold(true); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
35 mTitleFmt.setVerticalAlignment(Format::AlignTop); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
36 |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
37 |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
38 mQuestionFmt.setFontSize(11); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
39 mQuestionFmt.setFontName("Calibri"); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
40 mQuestionFmt.setFontBold(true); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
41 mQuestionFmt.setTopBorderStyle(Format::BorderThin); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
42 mQuestionFmt.setBottomBorderStyle(Format::BorderThin); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
43 mQuestionFmt.setTextWarp(true); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
44 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
45 mAnswerChoiceFmt.setFontSize(11); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
46 mAnswerChoiceFmt.setFontName("Calibri"); |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
47 mAnswerChoiceFmt.setHorizontalAlignment(Format::AlignLeft); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
48 mAnswerChoiceFmt.setTextWarp(true); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
49 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
50 mChoiceTextFmt = mAnswerChoiceFmt; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
51 mChoiceTextFmt.setVerticalAlignment(Format::AlignVCenter); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
52 |
15
03dad1cff5b9
Minor layout fixes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
13
diff
changeset
|
53 mChoiceVotesFmt = mChoiceTextFmt; |
03dad1cff5b9
Minor layout fixes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
13
diff
changeset
|
54 mChoiceVotesFmt.setFontSize(10); |
03dad1cff5b9
Minor layout fixes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
13
diff
changeset
|
55 |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
56 mFreeTextFmt = mQuestionFmt; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
57 mFreeTextFmt.setFontBold(false); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
58 |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
59 mAnswerTextFmt = mQuestionFmt; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
60 mAnswerTextFmt.setVerticalAlignment(Format::AlignVCenter); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
61 mAnswerTextFmt.setHorizontalAlignment(Format::AlignLeft); |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
62 |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
63 mTitleStyle = QStringLiteral("<tr><td colspan='3' style='vertical-align: top;" |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
64 "font-weight: bold;font-size:18; text-decoration:" |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
65 "underline;'>%1</td></tr>"); |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
66 const QString questionBorders = QStringLiteral("style='border-bottom: 1pt solid black;" |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
67 "border-top: 1pt solid black;font-weight:" |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
68 "bold;font-size:11;'"); |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
69 mQuestionStyle = QStringLiteral("<tr><td ") + questionBorders + QStringLiteral(">%1</td>") + |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
70 QStringLiteral("<td %1</td>").arg(questionBorders) + |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
71 QStringLiteral("<td %1</td>").arg(questionBorders); |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
72 mAnswerChoiceStyle= QStringLiteral("<tr><td style='text-align: left;font-size:11;'>Answer</td><td/><td/></tr>"); |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
73 mChoiceTextStyle= QStringLiteral("<tr><td style='text-align: right;vertical-algin: center; font-size:11;'>%1</td>"); |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
74 mChoiceVotesStyle = QStringLiteral("<td style='text-align: left;vertical-algin: center;" |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
75 "font-size:10;'</td>%1</td></tr>"); |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
76 mAnswerTextStyle = QStringLiteral("<tr><td style='text-align: left;font-weight: bold;vertical-algin: center;" |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
77 "border-bottom: 1pt solid black;border-top: 1pt solid black;font-size:11;'" |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
78 ">Answer</td><td/></td></tr>"); |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
79 mFreeTextStyle = QStringLiteral("<tr><td colspan='3' style='border-bottom: 1pt solid black;" |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
80 "border-top: 1pt solid black;font-size:11;'>%1</td></tr>"); |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
81 mEmptyRow = QStringLiteral("<tr style='height: %1px'/>").arg(CHOICE_ROW_HEIGHT); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
82 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
83 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
84 void Converter::run() |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
85 { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
86 QFile infile; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
87 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
88 if (mInput.isEmpty()) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
89 if (!infile.open(stdin, QIODevice::ReadOnly)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
90 mErrors << tr("Failed to open standard input and no input file provided."); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
91 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
92 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
93 } else { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
94 infile.setFileName(mInput); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
95 if (!infile.open(QIODevice::ReadOnly)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
96 mErrors << tr("Failed to open %1 for reading.").arg(mInput); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
97 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
98 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
99 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
100 QTextStream instream(&infile); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
101 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
102 QFile outfile; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
103 if (mOutput.isEmpty()) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
104 if (!outfile.open(stdout, QIODevice::WriteOnly)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
105 mErrors << tr("Failed to open standard output and no output file provided."); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
106 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
107 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
108 } else { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
109 outfile.setFileName(mOutput); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
110 if (!outfile.open(QIODevice::WriteOnly)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
111 mErrors << tr("Failed to open %1 for writing.").arg(mOutput); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
112 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
113 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
114 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
115 convertToXSLX(instream, outfile); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
116 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
117 |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
118 static void makeBar(QTextStream &html, double percent, QTextDocument &doc, bool forPDF) |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
119 { |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
120 html << QStringLiteral("<td style='background:linear-gradient(to right," |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
121 "#ff9933, #ff9933 %1%, #ffffff %1%)'></td>").arg(percent); |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
122 return; |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
123 } |
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
124 |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
125 void Converter::convertToXSLX(QTextStream& instream, QFile &output) |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
126 { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
127 Document xlsx; |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
128 QTextDocument doc; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
129 QString htmlString; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
130 QTextStream html (&htmlString); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
131 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
132 ConditionalFormatting bars; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
133 |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
134 bars.addDataBarRule(QColor(0xFF, 0x99, 0x33), ConditionalFormatting::VOT_Num, |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
135 "0", ConditionalFormatting::VOT_Num, "100", false); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
136 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
137 const double colWidth[] = COLUMN_WIDTHS; |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
138 double sum = 0; |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
139 |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
140 for (int i = 1; i <= COLUMN_CNT; i++) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
141 xlsx.setColumnWidth(i, colWidth[i-1]); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
142 sum += colWidth[i-1]; |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
143 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
144 |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
145 double xlsx2htmlFactor = HTML_WIDTH / sum; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
146 int col1Width = colWidth[0] * xlsx2htmlFactor; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
147 int col2Width = colWidth[1] * xlsx2htmlFactor; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
148 int col3Width = colWidth[2] * xlsx2htmlFactor; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
149 |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
150 /* For the merged cell wordwrap trick. */ |
15
03dad1cff5b9
Minor layout fixes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
13
diff
changeset
|
151 xlsx.setColumnWidth(26, sum + 1); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
152 xlsx.setColumnHidden(26, true); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
153 |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
154 html << "<html><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">" |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
155 "<body><table border=\"0\" style='width:\"" << HTML_WIDTH << "px\";border-collapse:collapse'>"; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
156 |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
157 int row = 1; |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
158 html << "<tr><th width=\"" << col1Width << "\"</th>"; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
159 html << "<th width=\"" << col2Width << "\"</th>"; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
160 html << "<th width=\"" << col3Width << "\"</th>"; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
161 const QString title = mTitle.isEmpty() ? DEFAULT_TITLE : mTitle; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
162 // Set the title of the Questionaire |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
163 xlsx.write(row++, 1, title, mTitleFmt); |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
164 html << mTitleStyle.arg(title.toHtmlEscaped()); |
20
df7936065450
Add default title
Andre Heinecke <andre.heinecke@intevation.de>
parents:
15
diff
changeset
|
165 xlsx.mergeCells("A1:C1"); |
df7936065450
Add default title
Andre Heinecke <andre.heinecke@intevation.de>
parents:
15
diff
changeset
|
166 xlsx.setRowHeight(1, TITLE_ROW_HEIGHT); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
167 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
168 const QString input = instream.readAll(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
169 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
170 QRegularExpression questionEx(QUESTION_PATTERN); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
171 QRegularExpression choiceEx(CHOICE_PATTERN); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
172 QRegularExpression freetxtEx (FREETXT_PATTERN); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
173 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
174 QRegularExpressionMatch match = questionEx.match(input); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
175 bool foundSomething = false; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
176 int cursor = match.capturedEnd(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
177 while (match.hasMatch() && cursor != -1) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
178 /* We've matched a question pattern. With the answer |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
179 line */ |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
180 if (!match.lastCapturedIndex() == 2) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
181 /* Should not happen without misconfiguration. */ |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
182 mErrors << "Internal parser error."; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
183 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
184 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
185 foundSomething = true; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
186 const QString question = match.captured(1).trimmed(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
187 const QString answerLine = match.captured(2).trimmed(); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
188 xlsx.write(row, 2, QString(" "), mQuestionFmt); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
189 xlsx.write(row, 3, QString(" "), mQuestionFmt); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
190 xlsx.write(row++, 1, question, mQuestionFmt); |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
191 html << mQuestionStyle.arg(question.toHtmlEscaped()); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
192 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
193 if (answerLine == QStringLiteral(CHOICE_IDENTIFIER)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
194 QRegularExpressionMatch choiceMatch = choiceEx.match(input, cursor); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
195 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT); |
21
0b66b10a287d
Add german translation
Andre Heinecke <andre.heinecke@intevation.de>
parents:
20
diff
changeset
|
196 xlsx.write(row++, 1, "Answer", mAnswerChoiceFmt); |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
197 html << mAnswerChoiceStyle; |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
198 int firstChoiceRow = row; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
199 int lastChoiceRow = row; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
200 while (choiceMatch.hasMatch() && choiceMatch.capturedStart() == cursor + 1) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
201 /* We use the cursor here to keep track of the state. Only if an answer |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
202 follows immediately behind the last answer we treat it as valid as |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
203 otherwise we can't figure out when the next question begins. */ |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
204 cursor = choiceMatch.capturedEnd(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
205 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
206 /* Write the values */ |
12
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
207 QString choiceName = choiceMatch.captured(1).trimmed(); |
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
208 if (choiceName.startsWith("=")) { |
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
209 choiceName = " " + choiceName; |
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
210 } |
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
211 xlsx.write(row, 1, choiceName, mChoiceTextFmt); |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
212 html << mChoiceTextStyle.arg(choiceName.toHtmlEscaped()); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
213 bool ok; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
214 double percent = choiceMatch.captured(3).toDouble(&ok); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
215 if (!ok) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
216 mErrors << "Unparsable number in string: " + choiceMatch.captured(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
217 } |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
218 makeBar(html, percent, doc, mFmt == Format_PDF); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
219 xlsx.write(row, 2, percent); |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
220 const QString numVotesString = QString("%1% | %2 Number of votes"). |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
221 arg(choiceMatch.captured(3)).arg(choiceMatch.captured(2)); |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
222 html << mChoiceVotesStyle.arg(numVotesString.toHtmlEscaped()); |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
223 xlsx.write(row, 3, numVotesString, mChoiceVotesFmt); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
224 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
225 /* As long as we can match a choice which is either before the next question |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
226 or before the end of the document */ |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
227 choiceMatch = choiceEx.match(input, cursor); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
228 row++; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
229 lastChoiceRow++; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
230 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
231 bars.addRange(QString("B%1:B%2").arg(firstChoiceRow).arg(lastChoiceRow)); |
13
101ee762f021
Add commented out Grouping
Andre Heinecke <andre.heinecke@intevation.de>
parents:
12
diff
changeset
|
232 // xlsx.groupRows(firstChoiceRow - 2, lastChoiceRow - 1, false); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
233 } else if (answerLine == QStringLiteral(TEXT_IDENTIFIER)) { |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
234 QRegularExpressionMatch textMatch = freetxtEx.match(input, cursor); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
235 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT); |
21
0b66b10a287d
Add german translation
Andre Heinecke <andre.heinecke@intevation.de>
parents:
20
diff
changeset
|
236 xlsx.write(row++, 1, "Answer", mAnswerTextFmt); |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
237 html << mAnswerTextStyle; |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
238 |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
239 /* To handle the workaround for quotes in answers we store |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
240 * the number of rows and only afterwards create the html rows. */ |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
241 int firstFreeRow = row; |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
242 while (textMatch.hasMatch()) { |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
243 if (textMatch.capturedStart() != cursor + 1) { |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
244 /* The format allows unescaped quotes in the text. |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
245 This makes a workaround neccessary. If we have |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
246 an Unquoted string between the next quoted string |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
247 and that Unquoted string is before the next question |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
248 we append the unquoted string and the next quoted string |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
249 with Quotes in the Row.*/ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
250 QRegularExpressionMatch nextQuestion = questionEx.match(input, cursor); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
251 if (nextQuestion.hasMatch() && |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
252 nextQuestion.capturedStart() < textMatch.capturedEnd()) { |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
253 /* The next question comes before the textMatch so we really have |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
254 a new question. */ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
255 break; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
256 } |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
257 const QString lastRow = xlsx.read(row - 1, 26).toString(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
258 int unquotedLen = textMatch.capturedStart() - cursor; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
259 const QString unquoted = input.mid(cursor, unquotedLen); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
260 qDebug() << "Found inner quoted string: " << unquoted; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
261 /* Now combine */ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
262 const QString combined = QString("%1\"%2\"%3").arg(lastRow). |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
263 arg(unquoted). |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
264 arg(textMatch.captured(1).trimmed()); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
265 qDebug() << "Last row: " << lastRow; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
266 qDebug() << "Next Question is at: " << nextQuestion.capturedStart(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
267 qDebug() << "Text match is: " << textMatch.captured(1).trimmed(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
268 qDebug() << "cursor is at: " << cursor; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
269 qDebug() << "text match starts at: " << textMatch.capturedStart(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
270 xlsx.write(row - 1, 26, combined, mFreeTextFmt); |
12
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
271 xlsx.write(row - 1, 1, combined, mFreeTextFmt); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
272 cursor = textMatch.capturedEnd(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
273 textMatch = freetxtEx.match(input, cursor); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
274 continue; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
275 } |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
276 cursor = textMatch.capturedEnd(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
277 |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
278 QString text = textMatch.captured(1).trimmed(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
279 qDebug() << "Found free text: " << text; |
12
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
280 if (text.startsWith("=")) { |
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
281 text = " " + text; |
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
282 } |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
283 |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
284 /* Merge the cells */ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
285 xlsx.mergeCells(QString("A%1:C%1").arg(row), mFreeTextFmt); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
286 |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
287 /* Merged cells ignore wordwrap the following trick is based on: |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
288 http://excel.tips.net/T003207_Automatic_Row_Height_For_Merged_Cells_with_Text_Wrap.html |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
289 */ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
290 /* Write the values */ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
291 xlsx.write(QString("Z%1").arg(row), text, mFreeTextFmt); |
12
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
292 xlsx.write(row, 1, text, mFreeTextFmt); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
293 row++; |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
294 |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
295 textMatch = freetxtEx.match(input, cursor); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
296 } |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
297 for (int i = firstFreeRow; i < row; i++) { |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
298 html << mFreeTextStyle.arg(xlsx.read(i, 1).toString().toHtmlEscaped()); |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
299 } |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
300 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
301 /* Insert Empty row. */ |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
302 xlsx.setRowHeight(row++, CHOICE_ROW_HEIGHT); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
303 match = questionEx.match(input, cursor); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
304 cursor = match.capturedEnd(); |
25
e5c5ebfa4205
Refactor to centralise html tags
Andre Heinecke <andre.heinecke@intevation.de>
parents:
23
diff
changeset
|
305 html << mEmptyRow; |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
306 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
307 xlsx.addConditionalFormatting(bars); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
308 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
309 if (!foundSomething) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
310 mErrors << tr("Failed to parse input document."); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
311 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
312 |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
313 if (mFmt == Format_XLSX && !xlsx.saveAs(&output)) { |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
314 mErrors << tr("Saving the XLSX document failed."); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
315 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
316 } |
23
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
317 |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
318 html << "</table></body></html>"; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
319 |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
320 if (mFmt == Format_HTML) { |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
321 QTextStream outstream(&output); |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
322 outstream << htmlString; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
323 return; |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
324 } |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
325 |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
326 if (mFmt == Format_PDF) { |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
327 output.close(); |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
328 QPrinter printer(QPrinter::PrinterResolution); |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
329 printer.setOutputFormat(QPrinter::PdfFormat); |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
330 printer.setPaperSize(QPrinter::A4); |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
331 printer.setOutputFileName(output.fileName()); |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
332 doc.setHtml(htmlString); |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
333 doc.print(&printer); |
927794e3cc52
Add HTML output and some pdf support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
21
diff
changeset
|
334 } |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
335 } |