comparison 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
comparison
equal deleted inserted replaced
5:2559010c1202 6:a10425e7ef98
34 34
35 mQuestionFmt.setFontSize(11); 35 mQuestionFmt.setFontSize(11);
36 mQuestionFmt.setFontName("Calibri"); 36 mQuestionFmt.setFontName("Calibri");
37 mQuestionFmt.setFontBold(true); 37 mQuestionFmt.setFontBold(true);
38 mQuestionFmt.setTopBorderStyle(Format::BorderThin); 38 mQuestionFmt.setTopBorderStyle(Format::BorderThin);
39 mQuestionFmt.setBottomBorderStyle(Format::BorderThin);
39 mQuestionFmt.setTextWarp(true); 40 mQuestionFmt.setTextWarp(true);
40 41
41 mAnswerChoiceFmt.setFontSize(11); 42 mAnswerChoiceFmt.setFontSize(11);
42 mAnswerChoiceFmt.setFontName("Calibri"); 43 mAnswerChoiceFmt.setFontName("Calibri");
43 mAnswerChoiceFmt.setHorizontalAlignment(Format::AlignRight); 44 mAnswerChoiceFmt.setHorizontalAlignment(Format::AlignRight);
45 mAnswerChoiceFmt.setTextWarp(true);
44 46
45 mChoiceTextFmt = mAnswerChoiceFmt; 47 mChoiceTextFmt = mAnswerChoiceFmt;
46 mChoiceTextFmt.setVerticalAlignment(Format::AlignVCenter); 48 mChoiceTextFmt.setVerticalAlignment(Format::AlignVCenter);
47 49
48 mChoiceBarFmt = mChoiceTextFmt; 50 mFreeTextFmt = mQuestionFmt;
49 mChoiceBarFmt.setFontName("Webdings"); 51 mFreeTextFmt.setFontBold(false);
50 mChoiceBarFmt.setFontSize(9); 52
51 mChoiceBarFmt.setFontColor(QColor(0xFF, 0x99, 0x33)); 53 mAnswerTextFmt = mQuestionFmt;
52 54 mAnswerTextFmt.setVerticalAlignment(Format::AlignVCenter);
53 mChoiceBarInactiveFmt = mChoiceBarFmt; 55 mAnswerTextFmt.setHorizontalAlignment(Format::AlignLeft);
54 mChoiceBarInactiveFmt.setFontColor(QColor(0xD9, 0xD9, 0xD9));
55 } 56 }
56 57
57 void Converter::run() 58 void Converter::run()
58 { 59 {
59 QFile infile; 60 QFile infile;
92 { 93 {
93 Document xlsx; 94 Document xlsx;
94 95
95 ConditionalFormatting bars; 96 ConditionalFormatting bars;
96 97
97 bars.addDataBarRule(QColor(0xFF, 0x99, 0x33), ConditionalFormatting::VOT_Num, "0", ConditionalFormatting::VOT_Num, "100", false); 98 bars.addDataBarRule(QColor(0xFF, 0x99, 0x33), ConditionalFormatting::VOT_Num,
99 "0", ConditionalFormatting::VOT_Num, "100", false);
98 100
99 const double colWidth[] = COLUMN_WIDTHS; 101 const double colWidth[] = COLUMN_WIDTHS;
102 double sum = 0;
100 for (int i = 1; i <= COLUMN_CNT; i++) { 103 for (int i = 1; i <= COLUMN_CNT; i++) {
101 xlsx.setColumnWidth(i, colWidth[i-1]); 104 xlsx.setColumnWidth(i, colWidth[i-1]);
102 } 105 sum += colWidth[i-1];
106 }
107
108 /* For the merged cell wordwrap trick. */
109 xlsx.setColumnWidth(26, sum - 1);
110 xlsx.setColumnHidden(26, true);
103 111
104 int row = 1; 112 int row = 1;
105 if (!mTitle.isEmpty()) { 113 if (!mTitle.isEmpty()) {
106 // Set the title of the Questionaire 114 // Set the title of the Questionaire
107 xlsx.write(row++, 1, mTitle, mTitleFmt); 115 xlsx.write(row++, 1, mTitle, mTitleFmt);
127 return; 135 return;
128 } 136 }
129 foundSomething = true; 137 foundSomething = true;
130 const QString question = match.captured(1).trimmed(); 138 const QString question = match.captured(1).trimmed();
131 const QString answerLine = match.captured(2).trimmed(); 139 const QString answerLine = match.captured(2).trimmed();
140 xlsx.write(row, 2, QString(" "), mQuestionFmt);
141 xlsx.write(row, 3, QString(" "), mQuestionFmt);
132 xlsx.write(row++, 1, question, mQuestionFmt); 142 xlsx.write(row++, 1, question, mQuestionFmt);
133 143
134 if (answerLine == QStringLiteral(CHOICE_IDENTIFIER)) { 144 if (answerLine == QStringLiteral(CHOICE_IDENTIFIER)) {
135 QRegularExpressionMatch choiceMatch = choiceEx.match(input, cursor); 145 QRegularExpressionMatch choiceMatch = choiceEx.match(input, cursor);
136 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT); 146 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
161 row++; 171 row++;
162 lastChoiceRow++; 172 lastChoiceRow++;
163 } 173 }
164 bars.addRange(QString("B%1:B%2").arg(firstChoiceRow).arg(lastChoiceRow)); 174 bars.addRange(QString("B%1:B%2").arg(firstChoiceRow).arg(lastChoiceRow));
165 } else if (answerLine == QStringLiteral(TEXT_IDENTIFIER)) { 175 } else if (answerLine == QStringLiteral(TEXT_IDENTIFIER)) {
166 176 QRegularExpressionMatch textMatch = freetxtEx.match(input, cursor);
177 xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
178 xlsx.write(row++, 1, tr("Answer"), mAnswerTextFmt);
179 while (textMatch.hasMatch()) {
180 if (textMatch.capturedStart() != cursor + 1) {
181 /* The format allows unescaped quotes in the text.
182 This makes a workaround neccessary. If we have
183 an Unquoted string between the next quoted string
184 and that Unquoted string is before the next question
185 we append the unquoted string and the next quoted string
186 with Quotes in the Row.*/
187 QRegularExpressionMatch nextQuestion = questionEx.match(input, cursor);
188 if (nextQuestion.hasMatch() &&
189 nextQuestion.capturedStart() < textMatch.capturedEnd()) {
190 /* The next question comes before the textMatch so we really have
191 a new question. */
192 break;
193 }
194 const QString lastRow = xlsx.read(row - 1, 26).toString();
195 int unquotedLen = textMatch.capturedStart() - cursor;
196 const QString unquoted = input.mid(cursor, unquotedLen);
197 qDebug() << "Found inner quoted string: " << unquoted;
198 /* Now combine */
199 const QString combined = QString("%1\"%2\"%3").arg(lastRow).
200 arg(unquoted).
201 arg(textMatch.captured(1).trimmed());
202 qDebug() << "Last row: " << lastRow;
203 qDebug() << "Next Question is at: " << nextQuestion.capturedStart();
204 qDebug() << "Text match is: " << textMatch.captured(1).trimmed();
205 qDebug() << "cursor is at: " << cursor;
206 qDebug() << "text match starts at: " << textMatch.capturedStart();
207 xlsx.write(row - 1, 26, combined, mFreeTextFmt);
208 cursor = textMatch.capturedEnd();
209 textMatch = freetxtEx.match(input, cursor);
210 continue;
211 }
212 cursor = textMatch.capturedEnd();
213
214 QString text = textMatch.captured(1).trimmed();
215 qDebug() << "Found free text: " << text;
216
217 /* Merge the cells */
218 xlsx.mergeCells(QString("A%1:C%1").arg(row), mFreeTextFmt);
219
220 /* Merged cells ignore wordwrap the following trick is based on:
221 http://excel.tips.net/T003207_Automatic_Row_Height_For_Merged_Cells_with_Text_Wrap.html
222 */
223 /* Write the values */
224 xlsx.write(QString("Z%1").arg(row), text, mFreeTextFmt);
225 xlsx.write(row, 1, QString("=Z%1").arg(row));
226 row++;
227 textMatch = freetxtEx.match(input, cursor);
228 }
167 } 229 }
168 /* Insert Empty row. */ 230 /* Insert Empty row. */
169 xlsx.setRowHeight(row++, CHOICE_ROW_HEIGHT); 231 xlsx.setRowHeight(row++, CHOICE_ROW_HEIGHT);
170 match = questionEx.match(input, cursor); 232 match = questionEx.match(input, cursor);
171 cursor = match.capturedEnd(); 233 cursor = match.capturedEnd();
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)