diff 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
line wrap: on
line diff
--- a/src/converter.cpp	Tue Mar 22 15:14:58 2016 +0100
+++ b/src/converter.cpp	Tue Mar 22 15:15:39 2016 +0100
@@ -36,22 +36,23 @@
     mQuestionFmt.setFontName("Calibri");
     mQuestionFmt.setFontBold(true);
     mQuestionFmt.setTopBorderStyle(Format::BorderThin);
+    mQuestionFmt.setBottomBorderStyle(Format::BorderThin);
     mQuestionFmt.setTextWarp(true);
 
     mAnswerChoiceFmt.setFontSize(11);
     mAnswerChoiceFmt.setFontName("Calibri");
     mAnswerChoiceFmt.setHorizontalAlignment(Format::AlignRight);
+    mAnswerChoiceFmt.setTextWarp(true);
 
     mChoiceTextFmt = mAnswerChoiceFmt;
     mChoiceTextFmt.setVerticalAlignment(Format::AlignVCenter);
 
-    mChoiceBarFmt = mChoiceTextFmt;
-    mChoiceBarFmt.setFontName("Webdings");
-    mChoiceBarFmt.setFontSize(9);
-    mChoiceBarFmt.setFontColor(QColor(0xFF, 0x99, 0x33));
+    mFreeTextFmt = mQuestionFmt;
+    mFreeTextFmt.setFontBold(false);
 
-    mChoiceBarInactiveFmt = mChoiceBarFmt;
-    mChoiceBarInactiveFmt.setFontColor(QColor(0xD9, 0xD9, 0xD9));
+    mAnswerTextFmt = mQuestionFmt;
+    mAnswerTextFmt.setVerticalAlignment(Format::AlignVCenter);
+    mAnswerTextFmt.setHorizontalAlignment(Format::AlignLeft);
 }
 
 void Converter::run()
@@ -94,13 +95,20 @@
 
     ConditionalFormatting bars;
 
-    bars.addDataBarRule(QColor(0xFF, 0x99, 0x33), ConditionalFormatting::VOT_Num, "0", ConditionalFormatting::VOT_Num, "100", false);
+    bars.addDataBarRule(QColor(0xFF, 0x99, 0x33), ConditionalFormatting::VOT_Num,
+                        "0", ConditionalFormatting::VOT_Num, "100", false);
 
     const double colWidth[] = COLUMN_WIDTHS;
+    double sum = 0;
     for (int i = 1; i <= COLUMN_CNT; i++) {
         xlsx.setColumnWidth(i, colWidth[i-1]);
+        sum += colWidth[i-1];
     }
 
+    /* For the merged cell wordwrap trick. */
+    xlsx.setColumnWidth(26, sum - 1);
+    xlsx.setColumnHidden(26, true);
+
     int row = 1;
     if (!mTitle.isEmpty()) {
         // Set the title of the Questionaire
@@ -129,6 +137,8 @@
         foundSomething = true;
         const QString question = match.captured(1).trimmed();
         const QString answerLine = match.captured(2).trimmed();
+        xlsx.write(row, 2, QString(" "), mQuestionFmt);
+        xlsx.write(row, 3, QString(" "), mQuestionFmt);
         xlsx.write(row++, 1, question, mQuestionFmt);
 
         if (answerLine == QStringLiteral(CHOICE_IDENTIFIER)) {
@@ -163,7 +173,59 @@
             }
             bars.addRange(QString("B%1:B%2").arg(firstChoiceRow).arg(lastChoiceRow));
         } else if (answerLine == QStringLiteral(TEXT_IDENTIFIER)) {
-            
+            QRegularExpressionMatch textMatch = freetxtEx.match(input, cursor);
+            xlsx.setRowHeight(row, CHOICE_ROW_HEIGHT);
+            xlsx.write(row++, 1, tr("Answer"), mAnswerTextFmt);
+            while (textMatch.hasMatch()) {
+                if (textMatch.capturedStart() != cursor + 1) {
+                    /* The format allows unescaped quotes in the text.
+                       This makes a workaround neccessary. If we have
+                       an Unquoted string between the next quoted string
+                       and that Unquoted string is before the next question
+                       we append the unquoted string and the next quoted string
+                       with Quotes in the Row.*/
+                    QRegularExpressionMatch nextQuestion = questionEx.match(input, cursor);
+                    if (nextQuestion.hasMatch() &&
+                        nextQuestion.capturedStart() < textMatch.capturedEnd()) {
+                        /* The next question comes before the textMatch so we really have
+                           a new question. */
+                        break;
+                    }
+                    const QString lastRow = xlsx.read(row - 1, 26).toString();
+                    int unquotedLen = textMatch.capturedStart() - cursor;
+                    const QString unquoted = input.mid(cursor, unquotedLen);
+                    qDebug() << "Found inner quoted string: " << unquoted;
+                    /* Now combine */
+                    const QString combined = QString("%1\"%2\"%3").arg(lastRow).
+                                                                   arg(unquoted).
+                                                                   arg(textMatch.captured(1).trimmed());
+                    qDebug() << "Last row: " << lastRow;
+                    qDebug() << "Next Question is at: " << nextQuestion.capturedStart();
+                    qDebug() << "Text match is: " << textMatch.captured(1).trimmed();
+                    qDebug() << "cursor is at: " << cursor;
+                    qDebug() << "text match starts at: " << textMatch.capturedStart();
+                    xlsx.write(row - 1, 26, combined, mFreeTextFmt);
+                    cursor = textMatch.capturedEnd();
+                    textMatch = freetxtEx.match(input, cursor);
+                    continue;
+                }
+                cursor = textMatch.capturedEnd();
+
+                QString text = textMatch.captured(1).trimmed();
+                qDebug() << "Found free text: " << text;
+
+                /* Merge the cells */
+                xlsx.mergeCells(QString("A%1:C%1").arg(row), mFreeTextFmt);
+
+                /* Merged cells ignore wordwrap the following trick is based on:
+                   http://excel.tips.net/T003207_Automatic_Row_Height_For_Merged_Cells_with_Text_Wrap.html
+                */
+                /* Write the values */
+                xlsx.write(QString("Z%1").arg(row), text, mFreeTextFmt);
+                xlsx.write(row, 1, QString("=Z%1").arg(row));
+                row++;
+                textMatch = freetxtEx.match(input, cursor);
+            }
         }
         /* Insert Empty row. */
         xlsx.setRowHeight(row++, CHOICE_ROW_HEIGHT);
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)