From f6c2da5edc59fb567d5c314d882bdd29c66ce809 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Mon, 10 Dec 2018 21:11:46 +0100 Subject: [PATCH] Implemented plural-test printing --- .../CheckableTestModel/CheckableTestModel.cpp | 77 ++++++++----------- .../CheckableTestModel/CheckableTestModel.h | 8 +- source/DataModel.cpp | 2 +- source/SubTests/Plural/PluralModel.cpp | 39 ++++++++++ source/SubTests/Plural/PluralModel.h | 2 + 5 files changed, 78 insertions(+), 50 deletions(-) diff --git a/source/CheckableTestModel/CheckableTestModel.cpp b/source/CheckableTestModel/CheckableTestModel.cpp index 35d1579..fc5a4ab 100644 --- a/source/CheckableTestModel/CheckableTestModel.cpp +++ b/source/CheckableTestModel/CheckableTestModel.cpp @@ -154,30 +154,6 @@ void CheckableTestModel::printTableTo(QTextCursor &cursor) const QTextTable *table = cursor.insertTable(m_tests.size() * 2, 13, tableFormat); - const char *emptyBox = "\u2610"; - //const char *checkBox = "\u2611"; - const char *checkBox = "x"; - - auto insertText = [&table](int row, int column, const QString &text) { - auto cell = table->cellAt(row, column); - auto textCursor = cell.firstCursorPosition(); - - auto blockFormat = textCursor.blockFormat(); - blockFormat.setAlignment(Qt::AlignCenter); - textCursor.setBlockFormat(blockFormat); - - auto cellFormat = cell.format(); - cellFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle); - cell.setFormat(cellFormat); - - auto charFormat = textCursor.charFormat(); - charFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle); - charFormat.setFontPointSize(8); - textCursor.setCharFormat(charFormat); - - textCursor.insertText(text); - }; - int currentRow = 0; for (const auto &test : m_tests) { @@ -185,18 +161,18 @@ void CheckableTestModel::printTableTo(QTextCursor &cursor) const int currentColumn = 0; - insertText(currentRow, currentColumn, test.name()); + setCellText(*table, currentRow, currentColumn, test.name()); currentColumn++; for (const auto &item : test.items()) { - insertText(currentRow, currentColumn, item.getText().c_str()); - insertText(currentRow + 1, currentColumn, item.isChecked() ? checkBox : emptyBox); + setCellText(*table, currentRow, currentColumn, item.getText().c_str()); + setCellChecked(*table, currentRow + 1, currentColumn, item.isChecked()); currentColumn++; } - insertText(currentRow + 1, 12, QString::number(test.getPoints())); + setCellText(*table, currentRow + 1, 12, QString::number(test.getPoints())); currentRow += 2; } @@ -215,28 +191,34 @@ void CheckableTestModel::printSummaryTo(QTextCursor &cursor) const QTextTable *table = cursor.insertTable(1, 4, tableFormat); - auto insertText = [&table](int row, int column, const QString &text) { - auto cell = table->cellAt(row, column); - auto textCursor = cell.firstCursorPosition(); + setCellText(*table, 0, 1, "Rohwertpunkte Total:"); + setCellText(*table, 0, 3, QString::number(getPoints())); +} - auto blockFormat = textCursor.blockFormat(); - blockFormat.setAlignment(Qt::AlignCenter); - textCursor.setBlockFormat(blockFormat); +void CheckableTestModel::setCellText(QTextTable &table, int row, int column, const QString &text) +{ + auto cell = table.cellAt(row, column); + auto textCursor = cell.firstCursorPosition(); - auto cellFormat = cell.format(); - cellFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle); - cell.setFormat(cellFormat); + auto blockFormat = textCursor.blockFormat(); + blockFormat.setAlignment(Qt::AlignCenter); + textCursor.setBlockFormat(blockFormat); - auto charFormat = textCursor.charFormat(); - charFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle); - charFormat.setFontPointSize(8); - textCursor.setCharFormat(charFormat); + auto cellFormat = cell.format(); + cellFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle); + cell.setFormat(cellFormat); - textCursor.insertText(text); - }; + auto charFormat = textCursor.charFormat(); + charFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle); + charFormat.setFontPointSize(8); + textCursor.setCharFormat(charFormat); - insertText(0, 1, "Rohwertpunkte Total:"); - insertText(0, 3, QString::number(getPoints())); + textCursor.insertText(text); +} + +void CheckableTestModel::setCellChecked(QTextTable &table, int row, int column, bool check) +{ + setCellText(table, row, column, check ? "x" : "\u2610"); } CheckableItems &CheckableTestModel::getItems(const QModelIndex &index) @@ -283,6 +265,7 @@ const CheckableItem &CheckableTestModel::getItem(const QModelIndex &index) const unsigned int CheckableTestModel::getPoints() const { - return std::accumulate(std::begin(m_tests), std::end(m_tests), 0, - [](int base, const CheckableTest &test) { return base + test.getPoints(); }); + return std::accumulate( + std::begin(m_tests), std::end(m_tests), 0, + [](int base, const CheckableTest &test) { return base + test.getPoints(); }); } diff --git a/source/CheckableTestModel/CheckableTestModel.h b/source/CheckableTestModel/CheckableTestModel.h index 8f54220..1db2880 100644 --- a/source/CheckableTestModel/CheckableTestModel.h +++ b/source/CheckableTestModel/CheckableTestModel.h @@ -34,8 +34,12 @@ protected: virtual bool isValidIndex(const QModelIndex &index) const; virtual std::string getName() const = 0; - void printTableTo(QTextCursor &cursor) const; - void printSummaryTo(QTextCursor &cursor) const; + + virtual void printTableTo(QTextCursor &cursor) const; + virtual void printSummaryTo(QTextCursor &cursor) const; + + static void setCellText(QTextTable &table, int row, int column, const QString &text); + static void setCellChecked(QTextTable &table, int row, int column, bool check); private: CheckableItems &getItems(const QModelIndex &index); diff --git a/source/DataModel.cpp b/source/DataModel.cpp index 9f2b3c4..20398be 100644 --- a/source/DataModel.cpp +++ b/source/DataModel.cpp @@ -75,7 +75,7 @@ void DataModel::printTo(QTextCursor &cursor) const m_genus.printTo(cursor); m_akkusativ.printTo(cursor); m_dativ.printTo(cursor); - //m_plural.printTo(cursor); + m_plural.printTo(cursor); //m_genitiv.printTo(cursor); //m_passiv.printTo(cursor); diff --git a/source/SubTests/Plural/PluralModel.cpp b/source/SubTests/Plural/PluralModel.cpp index f7a5e2e..bc407dc 100644 --- a/source/SubTests/Plural/PluralModel.cpp +++ b/source/SubTests/Plural/PluralModel.cpp @@ -1,6 +1,10 @@ #include "PluralModel.h" #include +#include +#include + +#include PluralModel::PluralModel(QObject *parent) : CheckableTestModel(parent) @@ -56,3 +60,38 @@ std::string PluralModel::getName() const { return "Subtest 5: Plural"; } + +void PluralModel::printTableTo(QTextCursor &cursor) const +{ + QTextTableFormat tableFormat; + tableFormat.setCellPadding(2); + tableFormat.setCellSpacing(0); + + tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 10), + QTextLength(QTextLength::PercentageLength, 10), + QTextLength(QTextLength::PercentageLength, 10), + QTextLength(QTextLength::PercentageLength, 10), + QTextLength(QTextLength::PercentageLength, 10), + QTextLength(QTextLength::PercentageLength, 10), + QTextLength(QTextLength::PercentageLength, 10), + QTextLength(QTextLength::PercentageLength, 10), + QTextLength(QTextLength::PercentageLength, 10), + QTextLength(QTextLength::PercentageLength, 10)}); + + QTextTable *table = cursor.insertTable(2, 10, tableFormat); + + const auto &test = m_tests.front(); + + int currentColumn = 0; + for (const auto &item : test.items()) + { + std::string itemName = std::regex_replace(item.getText(), std::regex("\\s"), "\n", + std::regex_constants::format_first_only); + + setCellText(*table, 0, currentColumn, itemName.c_str()); + setCellChecked(*table, 1, currentColumn, item.isChecked()); + + currentColumn++; + } +} + diff --git a/source/SubTests/Plural/PluralModel.h b/source/SubTests/Plural/PluralModel.h index 60d0ae8..cfa977f 100644 --- a/source/SubTests/Plural/PluralModel.h +++ b/source/SubTests/Plural/PluralModel.h @@ -18,4 +18,6 @@ public: protected: std::string getName() const override; + + void printTableTo(QTextCursor &cursor) const override; };