Mercurial > clickerconvert
annotate src/converter.cpp @ 16:d8140d64fa76
Added tag 0.7 for changeset de342d45ed92
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Tue, 22 Mar 2016 17:49:16 +0100 |
parents | 03dad1cff5b9 |
children | df7936065450 |
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> |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
13 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
14 #include "xlsxdocument.h" |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
15 #include "xlsxconditionalformatting.h" |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
16 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
17 #include "constants.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 QTXLSX_USE_NAMESPACE |
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 Converter::Converter(const QString &input, const QString &output, |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
22 ConvertFormat fmt, const QString &title): |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
23 QThread(Q_NULLPTR), |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
24 mInput(input), |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
25 mOutput(output), |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
26 mFmt(fmt), |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
27 mTitle(title) |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
28 { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
29 mTitleFmt.setFontUnderline(Format::FontUnderlineSingle); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
30 mTitleFmt.setFontSize(18); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
31 mTitleFmt.setFontName("Calibri"); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
32 mTitleFmt.setFontBold(true); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
33 mTitleFmt.setVerticalAlignment(Format::AlignTop); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
34 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
35 mQuestionFmt.setFontSize(11); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
36 mQuestionFmt.setFontName("Calibri"); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
37 mQuestionFmt.setFontBold(true); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
38 mQuestionFmt.setTopBorderStyle(Format::BorderThin); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
39 mQuestionFmt.setBottomBorderStyle(Format::BorderThin); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
40 mQuestionFmt.setTextWarp(true); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
41 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
42 mAnswerChoiceFmt.setFontSize(11); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
43 mAnswerChoiceFmt.setFontName("Calibri"); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
44 mAnswerChoiceFmt.setHorizontalAlignment(Format::AlignRight); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
45 mAnswerChoiceFmt.setTextWarp(true); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
46 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
47 mChoiceTextFmt = mAnswerChoiceFmt; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
48 mChoiceTextFmt.setVerticalAlignment(Format::AlignVCenter); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
49 |
15
03dad1cff5b9
Minor layout fixes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
13
diff
changeset
|
50 mChoiceVotesFmt = mChoiceTextFmt; |
03dad1cff5b9
Minor layout fixes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
13
diff
changeset
|
51 mChoiceVotesFmt.setFontSize(10); |
03dad1cff5b9
Minor layout fixes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
13
diff
changeset
|
52 mAnswerChoiceFmt.setHorizontalAlignment(Format::AlignLeft); |
03dad1cff5b9
Minor layout fixes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
13
diff
changeset
|
53 |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
54 mFreeTextFmt = mQuestionFmt; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
55 mFreeTextFmt.setFontBold(false); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
56 |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
57 mAnswerTextFmt = mQuestionFmt; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
58 mAnswerTextFmt.setVerticalAlignment(Format::AlignVCenter); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
59 mAnswerTextFmt.setHorizontalAlignment(Format::AlignLeft); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
60 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
61 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
62 void Converter::run() |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
63 { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
64 QFile infile; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
65 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
66 if (mInput.isEmpty()) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
67 if (!infile.open(stdin, QIODevice::ReadOnly)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
68 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
|
69 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
70 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
71 } else { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
72 infile.setFileName(mInput); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
73 if (!infile.open(QIODevice::ReadOnly)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
74 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
|
75 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
76 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
77 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
78 QTextStream instream(&infile); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
79 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
80 QFile outfile; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
81 if (mOutput.isEmpty()) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
82 if (!outfile.open(stdout, QIODevice::WriteOnly)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
83 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
|
84 return; |
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 } else { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
87 outfile.setFileName(mOutput); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
88 if (!outfile.open(QIODevice::WriteOnly)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
89 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
|
90 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
91 } |
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 convertToXSLX(instream, outfile); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
94 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
95 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
96 void Converter::convertToXSLX(QTextStream& instream, QFile &output) |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
97 { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
98 Document xlsx; |
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 ConditionalFormatting bars; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
101 |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
102 bars.addDataBarRule(QColor(0xFF, 0x99, 0x33), ConditionalFormatting::VOT_Num, |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
103 "0", ConditionalFormatting::VOT_Num, "100", false); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
104 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
105 const double colWidth[] = COLUMN_WIDTHS; |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
106 double sum = 0; |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
107 for (int i = 1; i <= COLUMN_CNT; i++) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
108 xlsx.setColumnWidth(i, colWidth[i-1]); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
109 sum += colWidth[i-1]; |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
110 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
111 |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
112 /* For the merged cell wordwrap trick. */ |
15
03dad1cff5b9
Minor layout fixes
Andre Heinecke <andre.heinecke@intevation.de>
parents:
13
diff
changeset
|
113 xlsx.setColumnWidth(26, sum + 1); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
114 xlsx.setColumnHidden(26, true); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
115 |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
116 int row = 1; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
117 if (!mTitle.isEmpty()) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
118 // Set the title of the Questionaire |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
119 xlsx.write(row++, 1, mTitle, mTitleFmt); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
120 xlsx.mergeCells("A1:C1"); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
121 xlsx.setRowHeight(1, TITLE_ROW_HEIGHT); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
122 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
123 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
124 const QString input = instream.readAll(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
125 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
126 QRegularExpression questionEx(QUESTION_PATTERN); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
127 QRegularExpression choiceEx(CHOICE_PATTERN); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
128 QRegularExpression freetxtEx (FREETXT_PATTERN); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
129 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
130 QRegularExpressionMatch match = questionEx.match(input); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
131 bool foundSomething = false; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
132 int cursor = match.capturedEnd(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
133 while (match.hasMatch() && cursor != -1) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
134 /* 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
|
135 line */ |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
136 if (!match.lastCapturedIndex() == 2) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
137 /* Should not happen without misconfiguration. */ |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
138 mErrors << "Internal parser error."; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
139 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
140 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
141 foundSomething = true; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
142 const QString question = match.captured(1).trimmed(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
143 const QString answerLine = match.captured(2).trimmed(); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
144 xlsx.write(row, 2, QString(" "), mQuestionFmt); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
145 xlsx.write(row, 3, QString(" "), mQuestionFmt); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
146 xlsx.write(row++, 1, question, mQuestionFmt); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
147 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
148 if (answerLine == QStringLiteral(CHOICE_IDENTIFIER)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
149 QRegularExpressionMatch choiceMatch = choiceEx.match(input, cursor); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
150 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
151 xlsx.write(row++, 1, tr("Answer"), mAnswerChoiceFmt); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
152 int firstChoiceRow = row; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
153 int lastChoiceRow = row; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
154 while (choiceMatch.hasMatch() && choiceMatch.capturedStart() == cursor + 1) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
155 /* 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
|
156 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
|
157 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
|
158 cursor = choiceMatch.capturedEnd(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
159 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
160 /* 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
|
161 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
|
162 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
|
163 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
|
164 } |
21f11f988115
Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents:
6
diff
changeset
|
165 xlsx.write(row, 1, choiceName, mChoiceTextFmt); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
166 bool ok; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
167 double percent = choiceMatch.captured(3).toDouble(&ok); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
168 if (!ok) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
169 mErrors << "Unparsable number in string: " + choiceMatch.captured(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
170 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
171 xlsx.write(row, 2, percent); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
172 xlsx.write(row, 3, tr("%1% | %2 Number of votes"). |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
173 arg(choiceMatch.captured(3)).arg(choiceMatch.captured(2)), |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
174 mChoiceVotesFmt); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
175 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
176 /* 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
|
177 or before the end of the document */ |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
178 choiceMatch = choiceEx.match(input, cursor); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
179 row++; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
180 lastChoiceRow++; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
181 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
182 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
|
183 // xlsx.groupRows(firstChoiceRow - 2, lastChoiceRow - 1, false); |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
184 } else if (answerLine == QStringLiteral(TEXT_IDENTIFIER)) { |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
185 QRegularExpressionMatch textMatch = freetxtEx.match(input, cursor); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
186 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
187 xlsx.write(row++, 1, tr("Answer"), mAnswerTextFmt); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
188 while (textMatch.hasMatch()) { |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
189 if (textMatch.capturedStart() != cursor + 1) { |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
190 /* The format allows unescaped quotes in the text. |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
191 This makes a workaround neccessary. If we have |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
192 an Unquoted string between the next quoted string |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
193 and that Unquoted string is before the next question |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
194 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
|
195 with Quotes in the Row.*/ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
196 QRegularExpressionMatch nextQuestion = questionEx.match(input, cursor); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
197 if (nextQuestion.hasMatch() && |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
198 nextQuestion.capturedStart() < textMatch.capturedEnd()) { |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
199 /* 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
|
200 a new question. */ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
201 break; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
202 } |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
203 const QString lastRow = xlsx.read(row - 1, 26).toString(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
204 int unquotedLen = textMatch.capturedStart() - cursor; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
205 const QString unquoted = input.mid(cursor, unquotedLen); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
206 qDebug() << "Found inner quoted string: " << unquoted; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
207 /* Now combine */ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
208 const QString combined = QString("%1\"%2\"%3").arg(lastRow). |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
209 arg(unquoted). |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
210 arg(textMatch.captured(1).trimmed()); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
211 qDebug() << "Last row: " << lastRow; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
212 qDebug() << "Next Question is at: " << nextQuestion.capturedStart(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
213 qDebug() << "Text match is: " << textMatch.captured(1).trimmed(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
214 qDebug() << "cursor is at: " << cursor; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
215 qDebug() << "text match starts at: " << textMatch.capturedStart(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
216 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
|
217 xlsx.write(row - 1, 1, combined, mFreeTextFmt); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
218 cursor = textMatch.capturedEnd(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
219 textMatch = freetxtEx.match(input, cursor); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
220 continue; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
221 } |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
222 cursor = textMatch.capturedEnd(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
223 |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
224 QString text = textMatch.captured(1).trimmed(); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
225 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
|
226 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
|
227 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
|
228 } |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
229 |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
230 /* Merge the cells */ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
231 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
|
232 |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
233 /* 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
|
234 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
|
235 */ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
236 /* Write the values */ |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
237 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
|
238 xlsx.write(row, 1, text, mFreeTextFmt); |
6
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
239 row++; |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
240 textMatch = freetxtEx.match(input, cursor); |
a10425e7ef98
Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents:
3
diff
changeset
|
241 } |
3
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
242 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
243 /* Insert Empty row. */ |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
244 xlsx.setRowHeight(row++, CHOICE_ROW_HEIGHT); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
245 match = questionEx.match(input, cursor); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
246 cursor = match.capturedEnd(); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
247 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
248 xlsx.addConditionalFormatting(bars); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
249 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
250 if (!foundSomething) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
251 mErrors << tr("Failed to parse input document."); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
252 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
253 |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
254 if (!xlsx.saveAs(&output)) { |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
255 mErrors << tr("Saving the XLSX document failed."); |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
256 return; |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
257 } |
8b4c49c92451
Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
258 } |