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