annotate src/converter.cpp @ 6:a10425e7ef98

Add free text handling
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 22 Mar 2016 15:15:39 +0100
parents 8b4c49c92451
children 21f11f988115
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 */
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
157 xlsx.write(row, 1, choiceMatch.captured(1), mChoiceTextFmt);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
158 bool ok;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
159 double percent = choiceMatch.captured(3).toDouble(&ok);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
160 if (!ok) {
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
161 mErrors << "Unparsable number in string: " + choiceMatch.captured();
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
162 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
163 xlsx.write(row, 2, percent);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
164 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
165 arg(choiceMatch.captured(3)).arg(choiceMatch.captured(2)),
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
166 mChoiceVotesFmt);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
167 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
168 /* 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
169 or before the end of the document */
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
170 choiceMatch = choiceEx.match(input, cursor);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
171 row++;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
172 lastChoiceRow++;
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
173 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
174 bars.addRange(QString("B%1:B%2").arg(firstChoiceRow).arg(lastChoiceRow));
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
175 } else if (answerLine == QStringLiteral(TEXT_IDENTIFIER)) {
6
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
176 QRegularExpressionMatch textMatch = freetxtEx.match(input, cursor);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
177 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
178 xlsx.write(row++, 1, tr("Answer"), mAnswerTextFmt);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
179 while (textMatch.hasMatch()) {
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
180 if (textMatch.capturedStart() != cursor + 1) {
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
181 /* The format allows unescaped quotes in the text.
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
182 This makes a workaround neccessary. If we have
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
183 an Unquoted string between the next quoted string
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
184 and that Unquoted string is before the next question
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
185 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
186 with Quotes in the Row.*/
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
187 QRegularExpressionMatch nextQuestion = questionEx.match(input, cursor);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
188 if (nextQuestion.hasMatch() &&
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
189 nextQuestion.capturedStart() < textMatch.capturedEnd()) {
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
190 /* 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
191 a new question. */
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
192 break;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
193 }
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
194 const QString lastRow = xlsx.read(row - 1, 26).toString();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
195 int unquotedLen = textMatch.capturedStart() - cursor;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
196 const QString unquoted = input.mid(cursor, unquotedLen);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
197 qDebug() << "Found inner quoted string: " << unquoted;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
198 /* Now combine */
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
199 const QString combined = QString("%1\"%2\"%3").arg(lastRow).
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
200 arg(unquoted).
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
201 arg(textMatch.captured(1).trimmed());
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
202 qDebug() << "Last row: " << lastRow;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
203 qDebug() << "Next Question is at: " << nextQuestion.capturedStart();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
204 qDebug() << "Text match is: " << textMatch.captured(1).trimmed();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
205 qDebug() << "cursor is at: " << cursor;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
206 qDebug() << "text match starts at: " << textMatch.capturedStart();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
207 xlsx.write(row - 1, 26, combined, mFreeTextFmt);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
208 cursor = textMatch.capturedEnd();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
209 textMatch = freetxtEx.match(input, cursor);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
210 continue;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
211 }
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
212 cursor = textMatch.capturedEnd();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
213
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
214 QString text = textMatch.captured(1).trimmed();
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
215 qDebug() << "Found free text: " << text;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
216
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
217 /* Merge the cells */
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
218 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
219
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
220 /* 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
221 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
222 */
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
223 /* Write the values */
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
224 xlsx.write(QString("Z%1").arg(row), text, mFreeTextFmt);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
225 xlsx.write(row, 1, QString("=Z%1").arg(row));
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
226 row++;
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
227 textMatch = freetxtEx.match(input, cursor);
a10425e7ef98 Add free text handling
Andre Heinecke <andre.heinecke@intevation.de>
parents: 3
diff changeset
228 }
3
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
229 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
230 /* Insert Empty row. */
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
231 xlsx.setRowHeight(row++, CHOICE_ROW_HEIGHT);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
232 match = questionEx.match(input, cursor);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
233 cursor = match.capturedEnd();
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
234 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
235 xlsx.addConditionalFormatting(bars);
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
236
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
237 if (!foundSomething) {
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
238 mErrors << tr("Failed to parse input document.");
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
239 }
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
240
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
241 if (!xlsx.saveAs(&output)) {
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
242 mErrors << tr("Saving the XLSX document failed.");
8b4c49c92451 Add initial implementation that handles choices
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
243 return;
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 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)