From 61bcb7566c22f006f1f151c41ec9dfc0fb4e37c0 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sun, 13 Jan 2019 18:44:07 +0100 Subject: [PATCH] Print subtest 1 --- source/CheckableItem/CheckableItem.cpp | 21 --- source/CheckableItem/CheckableItem.h | 3 - source/CheckableItem/CheckableItems.cpp | 28 +-- source/CheckableItem/CheckableItems.h | 3 +- source/CheckableTest/CheckableTest.cpp | 7 + source/CheckableTest/CheckableTest.h | 2 + .../CheckableTestModel/CheckableTestModel.cpp | 24 --- .../CheckableTestModel/CheckableTestModel.h | 3 - source/PrintableModel.cpp | 2 +- source/SubTests/V2Svk/V2SvkModel.cpp | 161 ++++++++++++------ source/SubTests/V2Svk/V2SvkModel.h | 4 +- source/mainwindow.cpp | 4 +- 12 files changed, 127 insertions(+), 135 deletions(-) diff --git a/source/CheckableItem/CheckableItem.cpp b/source/CheckableItem/CheckableItem.cpp index 6d6a16a..faed665 100644 --- a/source/CheckableItem/CheckableItem.cpp +++ b/source/CheckableItem/CheckableItem.cpp @@ -34,24 +34,3 @@ unsigned int CheckableItem::points() const { return m_checked ? m_value : 0; } - -void CheckableItem::write(QJsonObject &json) const -{ - json["text"] = m_text.c_str(); - json["checked"] = m_checked; -} - -void CheckableItem::read(const QJsonObject &json) -{ - const auto &text = json["text"]; - if (text.isString()) - { - m_text = text.toString().toStdString(); - } - - const auto &checked = json["checked"]; - if (checked.isBool()) - { - m_checked = checked.toBool(); - } -} diff --git a/source/CheckableItem/CheckableItem.h b/source/CheckableItem/CheckableItem.h index a37086e..89feffe 100644 --- a/source/CheckableItem/CheckableItem.h +++ b/source/CheckableItem/CheckableItem.h @@ -24,7 +24,4 @@ public: void setValue(unsigned int value); unsigned int points() const; - - void write(QJsonObject &json) const; - void read(const QJsonObject &json); }; diff --git a/source/CheckableItem/CheckableItems.cpp b/source/CheckableItem/CheckableItems.cpp index 30532f3..f9b59f3 100644 --- a/source/CheckableItem/CheckableItems.cpp +++ b/source/CheckableItem/CheckableItems.cpp @@ -2,6 +2,8 @@ #include +#include + CheckableItems::CheckableItems(std::initializer_list itemNames) { for (const auto &itemName : itemNames) @@ -10,27 +12,9 @@ CheckableItems::CheckableItems(std::initializer_list itemNames) } } -void CheckableItems::write(QJsonArray &json) const +unsigned int CheckableItems::getPoints() const { - for (const auto &item : *this) - { - QJsonObject itemObject; - item.write(itemObject); - json.append(itemObject); - } -} - -void CheckableItems::read(const QJsonArray &json) -{ - clear(); - - for (const auto &itemObject : json) - { - if (itemObject.isObject()) - { - CheckableItem item; - item.read(itemObject.toObject()); - emplace_back(item); - } - } + return std::accumulate(begin(), end(), 0, [](int base, const CheckableItem &item) { + return base + item.points(); + }); } diff --git a/source/CheckableItem/CheckableItems.h b/source/CheckableItem/CheckableItems.h index 164f829..b147e27 100644 --- a/source/CheckableItem/CheckableItems.h +++ b/source/CheckableItem/CheckableItems.h @@ -10,6 +10,5 @@ class CheckableItems : public std::vector public: CheckableItems(std::initializer_list itemNames); - void write(QJsonArray &json) const; - void read(const QJsonArray &json); + unsigned int getPoints() const; }; diff --git a/source/CheckableTest/CheckableTest.cpp b/source/CheckableTest/CheckableTest.cpp index 0d0cbf5..fabf0c3 100644 --- a/source/CheckableTest/CheckableTest.cpp +++ b/source/CheckableTest/CheckableTest.cpp @@ -1,5 +1,7 @@ #include "CheckableTest.h" +#include + CheckableTest::CheckableTest( const char *name, std::initializer_list items) : m_name(name) @@ -26,3 +28,8 @@ CheckableItems &CheckableTest::items() { return m_items; } + +unsigned int CheckableTest::getPoints() const +{ + return m_items.getPoints(); +} diff --git a/source/CheckableTest/CheckableTest.h b/source/CheckableTest/CheckableTest.h index 32f6da0..f4a0c20 100644 --- a/source/CheckableTest/CheckableTest.h +++ b/source/CheckableTest/CheckableTest.h @@ -17,6 +17,8 @@ public: const QString &name() const; const CheckableItems &items() const; CheckableItems &items(); + + unsigned int getPoints() const; }; using CheckableTests = std::vector; diff --git a/source/CheckableTestModel/CheckableTestModel.cpp b/source/CheckableTestModel/CheckableTestModel.cpp index 493bd76..7b66014 100644 --- a/source/CheckableTestModel/CheckableTestModel.cpp +++ b/source/CheckableTestModel/CheckableTestModel.cpp @@ -107,30 +107,6 @@ QVariant CheckableTestModel::headerData( return QAbstractTableModel::headerData(section, orientation, role); } -void CheckableTestModel::write(QJsonObject &json) const -{ - for (const auto &test : m_tests) - { - QJsonArray testData; - test.items().write(testData); - json[test.name()] = testData; - } -} - -void CheckableTestModel::read(const QJsonObject &json) -{ - for (auto &test : m_tests) - { - auto testData = json[test.name()]; - if (testData.isArray()) - { - test.items().read(testData.toArray()); - } - } - - emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1)); -} - bool CheckableTestModel::isValidIndex(const QModelIndex &index) const { if (index.row() < m_tests.size()) diff --git a/source/CheckableTestModel/CheckableTestModel.h b/source/CheckableTestModel/CheckableTestModel.h index 7b4ed9d..2fd5434 100644 --- a/source/CheckableTestModel/CheckableTestModel.h +++ b/source/CheckableTestModel/CheckableTestModel.h @@ -25,9 +25,6 @@ public: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; - void write(QJsonObject &json) const; - void read(const QJsonObject &json); - unsigned int getPoints() const; protected: diff --git a/source/PrintableModel.cpp b/source/PrintableModel.cpp index 7da134b..2f5a16c 100644 --- a/source/PrintableModel.cpp +++ b/source/PrintableModel.cpp @@ -12,7 +12,7 @@ QFont PrintableModel::h2Font() QFont PrintableModel::tableFont() { - return QFont("Helvetica", 10); + return QFont("Helvetica", 8); } QPen PrintableModel::tablePen() diff --git a/source/SubTests/V2Svk/V2SvkModel.cpp b/source/SubTests/V2Svk/V2SvkModel.cpp index 17e4284..e621207 100644 --- a/source/SubTests/V2Svk/V2SvkModel.cpp +++ b/source/SubTests/V2Svk/V2SvkModel.cpp @@ -29,7 +29,7 @@ V2SvkModel::V2SvkModel(QObject *parent) }; } -unsigned int V2SvkModel::getV2Points() +unsigned int V2SvkModel::getV2Points() const { unsigned int points = 0; @@ -49,7 +49,7 @@ unsigned int V2SvkModel::getV2Points() return points; } -unsigned int V2SvkModel::getSvkPoints() +unsigned int V2SvkModel::getSvkPoints() const { unsigned int points = 0; @@ -207,8 +207,6 @@ void V2SvkModel::printTo(QPainter &painter) const painter.drawText( 0, 0, "Subtest 1: Verbzweitstellungsregel (V2) und Subjekt-Verb-Kontrollregel (SVK)"); - painter.translate(0, 1.5 * painter.fontMetrics().lineSpacing()); - painter.setFont(tableFont()); painter.setPen(tablePen()); @@ -228,7 +226,7 @@ void V2SvkModel::printTo(QPainter &painter) const drawTextSquare(left, top, width, height, checked ? "\u2612" : "\u2610"); }; - auto drawResultSquare = [&](bool right, bool bold, unsigned int value, double y) { + auto drawResultSquare = [&](bool right, unsigned int value, double y) { double cellWidth = 0.03 * width; double x = width - cellWidth - (right ? 0 : 0.04 * width); @@ -237,66 +235,119 @@ void V2SvkModel::printTo(QPainter &painter) const auto drawGreySquare = [&](double left, double top, double width, double height) { auto prevBrush = painter.brush(); + auto prevPen = painter.pen(); painter.setBrush(QBrush(QColor(224, 224, 224))); - painter.drawRect(left, top, width, height); - + painter.setPen(QPen(Qt::NoPen)); + QPointF points[4] = {{left, top}, {left + width, top}, {left + width, top + height}, {left, top + height}}; + painter.drawPolygon(points, 4); + + painter.setPen(tablePen()); + painter.drawLine(left, top, left + width, top); + painter.drawLine(left + width, top, left + width, top + height); + painter.drawLine(left + width, top + height, left, top + height); + painter.drawLine(left, top + height, left, top); + painter.setBrush(prevBrush); + painter.setPen(prevPen); }; - auto x = 0.0; - auto y = 0.0; - auto cellWidth = 0.2 * width; - drawTextSquare(x, y, cellWidth, 2 * height, "W-Fragen"); - x += cellWidth; - drawTextSquare(x, y, cellWidth, height, "Affe"); - x += cellWidth; - drawTextSquare(x, y, cellWidth, height, "Schwein"); - x += cellWidth; - drawTextSquare(x, y, cellWidth, height, "Gans"); + std::set blockStarters = {0, 3, 5, 7}; + std::set v2Tests = {0, 1, 3, 5, 7, 8}; + std::set svkTests = {2, 4, 6, 9, 10}; - x = 0.2 * width; - y += height; - cellWidth = 0.05 * width; - for (int i = 0; i < 12; ++i) + double x = 0; + double y = 0; + auto testIndex = 0; + for (const auto &test : m_tests) { - drawCheckSquare(x, y, cellWidth, height, true); - x += cellWidth; + double rowHeaderWidth = 0.2 * width; + double resultCellWidth = test.size() > 8 ? 0.05 * width : 0.1 * width; + double rowHeight = height; + + if (blockStarters.find(testIndex) != blockStarters.end()) + { + y += rowHeight; + drawTextSquare(x, y, rowHeaderWidth, 2 * rowHeight, test.name()); + x += rowHeaderWidth; + + std::vector> columnHeaders; + for (const auto &item : test.items()) + { + const auto &itemText = item.getText(); + if (!columnHeaders.empty() && columnHeaders.back().first == itemText) + { + columnHeaders.back().second++; + } + else + { + columnHeaders.emplace_back(itemText, 1); + } + } + + for (const auto &columnHeader : columnHeaders) + { + double cellWidth = columnHeader.second * resultCellWidth; + drawTextSquare(x, y, cellWidth, rowHeight, columnHeader.first.c_str()); + x += cellWidth; + } + x = rowHeaderWidth; + y += rowHeight; + } + else + { + drawTextSquare(x, y, rowHeaderWidth, rowHeight, test.name()); + x += rowHeaderWidth; + } + + unsigned int emptyItemsStack = 0; + for (const auto &item : test.items()) + { + if (item.getText().empty()) + { + emptyItemsStack++; + } + else + { + if (emptyItemsStack > 0) + { + drawGreySquare(x - emptyItemsStack * resultCellWidth, y, + emptyItemsStack * resultCellWidth, rowHeight); + emptyItemsStack = 0; + } + + drawCheckSquare(x, y, resultCellWidth, rowHeight, item.isChecked()); + } + x += resultCellWidth; + } + if (emptyItemsStack > 0) + { + drawGreySquare(x - emptyItemsStack * resultCellWidth, y, + emptyItemsStack * resultCellWidth, rowHeight); + emptyItemsStack = 0; + } + + if (v2Tests.find(testIndex) != v2Tests.end()) + { + drawResultSquare(false, test.getPoints(), y); + } + + if (svkTests.find(testIndex) != svkTests.end()) + { + drawResultSquare(true, test.getPoints(), y); + } + + x = 0; + y += rowHeight; + + testIndex++; } - drawResultSquare(false, false, 9, y); x = 0; y += height; - cellWidth = 0.2 * width; - drawTextSquare(x, y, cellWidth, height, "Verbtrennung"); - x += cellWidth; - cellWidth = 0.05 * width; - drawGreySquare(x, y, cellWidth, height); - x += cellWidth; - drawCheckSquare(x, y, cellWidth, height, true); - x += cellWidth; - drawGreySquare(x, y, 5 * cellWidth, height); - x += 5 * cellWidth; - drawCheckSquare(x, y, cellWidth, height, true); - x += cellWidth; - drawGreySquare(x, y, 2 * cellWidth, height); - x += 2 * cellWidth; - drawCheckSquare(x, y, cellWidth, height, true); - x += cellWidth; - drawGreySquare(x, y, cellWidth, height); - x += cellWidth; - drawResultSquare(false, false, 2, y); - x = 0; - y += height; - cellWidth = 0.2 * width; - drawTextSquare(x, y, cellWidth, height, "SVK: /-st/"); - x += cellWidth; - cellWidth = 0.05 * width; - for (int i = 0; i < 12; ++i) - { - drawCheckSquare(x, y, cellWidth, height, true); - x += cellWidth; - } - drawResultSquare(true, false, 8, y); + painter.drawText(x, y, 0.85 * width, height, Qt::AlignRight | Qt::AlignVCenter, + "Rohwertpunkte Total:"); + drawResultSquare(false, getV2Points(), y); + drawResultSquare(true, getSvkPoints(), y); } diff --git a/source/SubTests/V2Svk/V2SvkModel.h b/source/SubTests/V2Svk/V2SvkModel.h index becad53..c7090c3 100644 --- a/source/SubTests/V2Svk/V2SvkModel.h +++ b/source/SubTests/V2Svk/V2SvkModel.h @@ -12,8 +12,8 @@ class V2SvkModel : public CheckableTestModel, public PrintableModel public: V2SvkModel(QObject *parent); - unsigned int getV2Points(); - unsigned int getSvkPoints(); + unsigned int getV2Points() const; + unsigned int getSvkPoints() const; void write(ESGRAF48::V2SvkModel &model) const; void read(const ESGRAF48::V2SvkModel &model); diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index 0a5c8de..88a67e3 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -153,7 +153,7 @@ void MainWindow::print() const { QPrinter printer; printer.setPaperSize(QPrinter::A4); - printer.setPageMargins(30, 20, 30, 20, QPrinter::Millimeter); + printer.setPageMargins(20, 20, 20, 20, QPrinter::Millimeter); QPrintDialog dialog(&printer); if (dialog.exec() != QDialog::Accepted) @@ -215,7 +215,7 @@ void MainWindow::savePdf(const QString &filename) QPrinter printer; printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); - printer.setPageMargins(30, 20, 30, 20, QPrinter::Millimeter); + printer.setPageMargins(20, 20, 20, 20, QPrinter::Millimeter); printer.setOutputFileName(filename); QPainter painter;