From 189c24cf6180d0526ec13a2f657d50303d0501f6 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 7 Feb 2019 20:03:22 +0100 Subject: [PATCH] Wrap long row headers --- source/PrintableModel/PrintableModel.cpp | 14 ++++++-- source/PrintableModel/PrintableModel.h | 3 ++ source/SubTests/Plural/PluralModel.cpp | 41 ++++++++++++++++++++++++ source/SubTests/Plural/PluralModel.h | 3 ++ source/SubTests/V2Svk/V2SvkModel.cpp | 17 +++++++--- 5 files changed, 72 insertions(+), 6 deletions(-) diff --git a/source/PrintableModel/PrintableModel.cpp b/source/PrintableModel/PrintableModel.cpp index baabe85..3107942 100644 --- a/source/PrintableModel/PrintableModel.cpp +++ b/source/PrintableModel/PrintableModel.cpp @@ -39,6 +39,16 @@ QPen PrintableModel::resultPen() return QPen(Qt::black, 2, Qt::SolidLine); } +double PrintableModel::headerWidthFactor() +{ + return 0.17; +} + +double PrintableModel::cellWidthFactor() +{ + return 0.085; +} + void PrintableModel::drawTextSquare(QPainter &painter, const QRectF &cell, const QString &text) { auto prevPen = painter.pen(); @@ -128,8 +138,8 @@ void PrintableModel::printTests(QPainter &painter) const auto width = painter.device()->width(); auto height = 1.5 * painter.fontMetrics().lineSpacing(); - double headerWidth = 0.2 * width; - double cellWidth = 0.08 * width; + double headerWidth = headerWidthFactor() * width; + double cellWidth = cellWidthFactor() * width; double rowHeight = height; double x = 0; diff --git a/source/PrintableModel/PrintableModel.h b/source/PrintableModel/PrintableModel.h index e647585..eb1daeb 100644 --- a/source/PrintableModel/PrintableModel.h +++ b/source/PrintableModel/PrintableModel.h @@ -24,6 +24,9 @@ public: static QPen tablePen(); static QPen resultPen(); + static double headerWidthFactor(); + static double cellWidthFactor(); + static void drawTextSquare(QPainter &painter, const QRectF &cell, const QString &text); static void drawNumberSquare(QPainter &painter, double x, double y, int number); static void drawCheckSquare(QPainter &painter, const QRectF &cell, bool checked); diff --git a/source/SubTests/Plural/PluralModel.cpp b/source/SubTests/Plural/PluralModel.cpp index 5448ed3..b310ae2 100644 --- a/source/SubTests/Plural/PluralModel.cpp +++ b/source/SubTests/Plural/PluralModel.cpp @@ -2,6 +2,8 @@ #include +#include + PluralModel::PluralModel(QObject *parent) : PrintableModel(parent) { @@ -43,3 +45,42 @@ void PluralModel::write(ESGRAF48::PluralModel &model) const model.set_baer(testItems[7].isChecked()); model.set_apfel(testItems[8].isChecked()); } + +void PluralModel::printTests(QPainter &painter) const +{ + painter.setFont(tableFont()); + painter.setPen(tablePen()); + + auto width = painter.device()->width(); + auto height = 1.5 * painter.fontMetrics().lineSpacing(); + + double headerWidth = headerWidthFactor() * width; + double cellWidth = cellWidthFactor() * width; + double rowHeight = height; + + double x = 0; + double y = 0; + for (const auto &test : m_tests) + { + drawTextSquare(painter, {0, y, headerWidth, 3 * rowHeight}, test.name()); + x = headerWidth; + + for (const auto &item : test.items()) + { + QString itemText = + QString::fromStdString(std::regex_replace(item.getText(), std::regex("\\s"), "\n")); + + drawTextSquare(painter, {x, y, cellWidth, 2 * rowHeight}, itemText); + drawCheckSquare(painter, {x, y + 2 * rowHeight, cellWidth, rowHeight}, + item.isChecked()); + + x += cellWidth; + } + y += rowHeight; + + drawResultSquare(painter, y, true, test.getPoints()); + y += rowHeight; + } + + painter.translate(0, y + 2 * rowHeight); +} diff --git a/source/SubTests/Plural/PluralModel.h b/source/SubTests/Plural/PluralModel.h index 728258d..f7c3543 100644 --- a/source/SubTests/Plural/PluralModel.h +++ b/source/SubTests/Plural/PluralModel.h @@ -12,4 +12,7 @@ public: void read(const ESGRAF48::PluralModel &model); void write(ESGRAF48::PluralModel &model) const; + +protected: + virtual void printTests(QPainter &painter) const; }; diff --git a/source/SubTests/V2Svk/V2SvkModel.cpp b/source/SubTests/V2Svk/V2SvkModel.cpp index 5416064..ff0ab00 100644 --- a/source/SubTests/V2Svk/V2SvkModel.cpp +++ b/source/SubTests/V2Svk/V2SvkModel.cpp @@ -1,5 +1,7 @@ #include "V2SvkModel.h" +#include + V2SvkModel::V2SvkModel(QObject *parent) : PrintableModel(parent) { @@ -21,13 +23,20 @@ void V2SvkModel::printTests(QPainter &painter) const auto testIndex = 0; for (const auto &test : m_tests) { - double rowHeaderWidth = 0.2 * width; - double resultCellWidth = test.size() > 8 ? 0.04 * width : 0.08 * width; + double rowHeaderWidth = headerWidthFactor() * width; + double resultCellWidth = (test.size() > 8 ? 0.5 : 1) * cellWidthFactor() * width; double rowHeight = height; + QString testName = test.name(); + if (testName.length() > 20) + { + testName = QString::fromStdString( + std::regex_replace(testName.toStdString(), std::regex("[\\s-]"), "\n")); + } + if (testIndex == 0) { - drawTextSquare(painter, {x, y, rowHeaderWidth, 2 * rowHeight}, test.name()); + drawTextSquare(painter, {x, y, rowHeaderWidth, 2 * rowHeight}, testName); x += rowHeaderWidth; std::vector> columnHeaders; @@ -55,7 +64,7 @@ void V2SvkModel::printTests(QPainter &painter) const } else { - drawTextSquare(painter, {x, y, rowHeaderWidth, rowHeight}, test.name()); + drawTextSquare(painter, {x, y, rowHeaderWidth, rowHeight}, testName); x += rowHeaderWidth; }