annotate src/converter.cpp @ 21:0b66b10a287d

Add german translation
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 30 Mar 2016 17:08:46 +0200
parents df7936065450
children 927794e3cc52
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);
20
df7936065450 Add default title
Andre Heinecke <andre.heinecke@intevation.de>
parents: 15
diff changeset
120 } else {
df7936065450 Add default title
Andre Heinecke <andre.heinecke@intevation.de>
parents: 15
diff changeset
121 xlsx.write(row++, 1, DEFAULT_TITLE, mTitleFmt);
3
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
122 }
20
df7936065450 Add default title
Andre Heinecke <andre.heinecke@intevation.de>
parents: 15
diff changeset
123 xlsx.mergeCells("A1:C1");
df7936065450 Add default title
Andre Heinecke <andre.heinecke@intevation.de>
parents: 15
diff changeset
124 xlsx.setRowHeight(1, TITLE_ROW_HEIGHT);
3
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 const QString input = instream.readAll();
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
127
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
128 QRegularExpression questionEx(QUESTION_PATTERN);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
129 QRegularExpression choiceEx(CHOICE_PATTERN);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
130 QRegularExpression freetxtEx (FREETXT_PATTERN);
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 QRegularExpressionMatch match = questionEx.match(input);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
133 bool foundSomething = false;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
134 int cursor = match.capturedEnd();
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
135 while (match.hasMatch() && cursor != -1) {
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
136 /* 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
137 line */
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
138 if (!match.lastCapturedIndex() == 2) {
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
139 /* Should not happen without misconfiguration. */
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
140 mErrors << "Internal parser error.";
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
141 return;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
142 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
143 foundSomething = true;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
144 const QString question = match.captured(1).trimmed();
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
145 const QString answerLine = match.captured(2).trimmed();
6
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
146 xlsx.write(row, 2, QString(" "), mQuestionFmt);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
147 xlsx.write(row, 3, QString(" "), mQuestionFmt);
3
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
148 xlsx.write(row++, 1, question, mQuestionFmt);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
149
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
150 if (answerLine == QStringLiteral(CHOICE_IDENTIFIER)) {
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
151 QRegularExpressionMatch choiceMatch = choiceEx.match(input, cursor);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
152 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
21
0b66b10a287d Add german translation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 20
diff changeset
153 xlsx.write(row++, 1, "Answer", mAnswerChoiceFmt);
3
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
154 int firstChoiceRow = row;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
155 int lastChoiceRow = row;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
156 while (choiceMatch.hasMatch() && choiceMatch.capturedStart() == cursor + 1) {
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
157 /* 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
158 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
159 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
160 cursor = choiceMatch.capturedEnd();
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
161
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
162 /* 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
163 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
164 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
165 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
166 }
21f11f988115 Make sure strings don't start with = and duplicate text instead of formula
Andre Heinecke <andre.heinecke@intevation.de>
parents: 6
diff changeset
167 xlsx.write(row, 1, choiceName, mChoiceTextFmt);
3
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
168 bool ok;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
169 double percent = choiceMatch.captured(3).toDouble(&ok);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
170 if (!ok) {
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
171 mErrors << "Unparsable number in string: " + choiceMatch.captured();
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
172 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
173 xlsx.write(row, 2, percent);
21
0b66b10a287d Add german translation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 20
diff changeset
174 xlsx.write(row, 3, QString("%1% | %2 Number of votes").
3
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
175 arg(choiceMatch.captured(3)).arg(choiceMatch.captured(2)),
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
176 mChoiceVotesFmt);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
177 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
178 /* 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
179 or before the end of the document */
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
180 choiceMatch = choiceEx.match(input, cursor);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
181 row++;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
182 lastChoiceRow++;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
183 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
184 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
185 // xlsx.groupRows(firstChoiceRow - 2, lastChoiceRow - 1, false);
3
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
186 } else if (answerLine == QStringLiteral(TEXT_IDENTIFIER)) {
6
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
187 QRegularExpressionMatch textMatch = freetxtEx.match(input, cursor);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
188 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
21
0b66b10a287d Add german translation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 20
diff changeset
189 xlsx.write(row++, 1, "Answer", mAnswerTextFmt);
6
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
190 while (textMatch.hasMatch()) {
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
191 if (textMatch.capturedStart() != cursor + 1) {
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
192 /* The format allows unescaped quotes in the text.
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
193 This makes a workaround neccessary. If we have
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
194 an Unquoted string between the next quoted string
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
195 and that Unquoted string is before the next question
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
196 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
197 with Quotes in the Row.*/
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
198 QRegularExpressionMatch nextQuestion = questionEx.match(input, cursor);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
199 if (nextQuestion.hasMatch() &&
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
200 nextQuestion.capturedStart() < textMatch.capturedEnd()) {
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
201 /* 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
202 a new question. */
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
203 break;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
204 }
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
205 const QString lastRow = xlsx.read(row - 1, 26).toString();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
206 int unquotedLen = textMatch.capturedStart() - cursor;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
207 const QString unquoted = input.mid(cursor, unquotedLen);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
208 qDebug() << "Found inner quoted string: " << unquoted;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
209 /* Now combine */
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
210 const QString combined = QString("%1\"%2\"%3").arg(lastRow).
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
211 arg(unquoted).
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
212 arg(textMatch.captured(1).trimmed());
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
213 qDebug() << "Last row: " << lastRow;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
214 qDebug() << "Next Question is at: " << nextQuestion.capturedStart();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
215 qDebug() << "Text match is: " << textMatch.captured(1).trimmed();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
216 qDebug() << "cursor is at: " << cursor;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
217 qDebug() << "text match starts at: " << textMatch.capturedStart();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
218 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
219 xlsx.write(row - 1, 1, combined, mFreeTextFmt);
6
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
220 cursor = textMatch.capturedEnd();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
221 textMatch = freetxtEx.match(input, cursor);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
222 continue;
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 cursor = textMatch.capturedEnd();
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 QString text = textMatch.captured(1).trimmed();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
227 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
228 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
229 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
230 }
6
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 /* Merge the cells */
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
233 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
234
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
235 /* 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
236 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
237 */
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
238 /* Write the values */
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
239 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
240 xlsx.write(row, 1, text, mFreeTextFmt);
6
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
241 row++;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
242 textMatch = freetxtEx.match(input, cursor);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
243 }
3
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
244 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
245 /* Insert Empty row. */
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
246 xlsx.setRowHeight(row++, CHOICE_ROW_HEIGHT);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
247 match = questionEx.match(input, cursor);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
248 cursor = match.capturedEnd();
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 xlsx.addConditionalFormatting(bars);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
251
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
252 if (!foundSomething) {
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
253 mErrors << tr("Failed to parse input document.");
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
254 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
255
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
256 if (!xlsx.saveAs(&output)) {
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
257 mErrors << tr("Saving the XLSX document failed.");
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
258 return;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
259 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
260 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)