Wrap long row headers

This commit is contained in:
mandlm 2019-02-07 20:03:22 +01:00
parent 1f5c0de80a
commit 189c24cf61
5 changed files with 72 additions and 6 deletions

View File

@ -39,6 +39,16 @@ QPen PrintableModel::resultPen()
return QPen(Qt::black, 2, Qt::SolidLine); 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) void PrintableModel::drawTextSquare(QPainter &painter, const QRectF &cell, const QString &text)
{ {
auto prevPen = painter.pen(); auto prevPen = painter.pen();
@ -128,8 +138,8 @@ void PrintableModel::printTests(QPainter &painter) const
auto width = painter.device()->width(); auto width = painter.device()->width();
auto height = 1.5 * painter.fontMetrics().lineSpacing(); auto height = 1.5 * painter.fontMetrics().lineSpacing();
double headerWidth = 0.2 * width; double headerWidth = headerWidthFactor() * width;
double cellWidth = 0.08 * width; double cellWidth = cellWidthFactor() * width;
double rowHeight = height; double rowHeight = height;
double x = 0; double x = 0;

View File

@ -24,6 +24,9 @@ public:
static QPen tablePen(); static QPen tablePen();
static QPen resultPen(); static QPen resultPen();
static double headerWidthFactor();
static double cellWidthFactor();
static void drawTextSquare(QPainter &painter, const QRectF &cell, const QString &text); static void drawTextSquare(QPainter &painter, const QRectF &cell, const QString &text);
static void drawNumberSquare(QPainter &painter, double x, double y, int number); static void drawNumberSquare(QPainter &painter, double x, double y, int number);
static void drawCheckSquare(QPainter &painter, const QRectF &cell, bool checked); static void drawCheckSquare(QPainter &painter, const QRectF &cell, bool checked);

View File

@ -2,6 +2,8 @@
#include <QSize> #include <QSize>
#include <regex>
PluralModel::PluralModel(QObject *parent) PluralModel::PluralModel(QObject *parent)
: PrintableModel(parent) : PrintableModel(parent)
{ {
@ -43,3 +45,42 @@ void PluralModel::write(ESGRAF48::PluralModel &model) const
model.set_baer(testItems[7].isChecked()); model.set_baer(testItems[7].isChecked());
model.set_apfel(testItems[8].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);
}

View File

@ -12,4 +12,7 @@ public:
void read(const ESGRAF48::PluralModel &model); void read(const ESGRAF48::PluralModel &model);
void write(ESGRAF48::PluralModel &model) const; void write(ESGRAF48::PluralModel &model) const;
protected:
virtual void printTests(QPainter &painter) const;
}; };

View File

@ -1,5 +1,7 @@
#include "V2SvkModel.h" #include "V2SvkModel.h"
#include <regex>
V2SvkModel::V2SvkModel(QObject *parent) V2SvkModel::V2SvkModel(QObject *parent)
: PrintableModel(parent) : PrintableModel(parent)
{ {
@ -21,13 +23,20 @@ void V2SvkModel::printTests(QPainter &painter) const
auto testIndex = 0; auto testIndex = 0;
for (const auto &test : m_tests) for (const auto &test : m_tests)
{ {
double rowHeaderWidth = 0.2 * width; double rowHeaderWidth = headerWidthFactor() * width;
double resultCellWidth = test.size() > 8 ? 0.04 * width : 0.08 * width; double resultCellWidth = (test.size() > 8 ? 0.5 : 1) * cellWidthFactor() * width;
double rowHeight = height; 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) if (testIndex == 0)
{ {
drawTextSquare(painter, {x, y, rowHeaderWidth, 2 * rowHeight}, test.name()); drawTextSquare(painter, {x, y, rowHeaderWidth, 2 * rowHeight}, testName);
x += rowHeaderWidth; x += rowHeaderWidth;
std::vector<std::pair<std::string, unsigned int>> columnHeaders; std::vector<std::pair<std::string, unsigned int>> columnHeaders;
@ -55,7 +64,7 @@ void V2SvkModel::printTests(QPainter &painter) const
} }
else else
{ {
drawTextSquare(painter, {x, y, rowHeaderWidth, rowHeight}, test.name()); drawTextSquare(painter, {x, y, rowHeaderWidth, rowHeight}, testName);
x += rowHeaderWidth; x += rowHeaderWidth;
} }