ESGRAF48/source/PrintableModel/PrintableModel.cpp

185 lines
5.1 KiB
C++
Raw Permalink Normal View History

#include "PrintableModel.h"
2019-02-05 21:16:30 +00:00
#include <regex>
2019-10-05 14:14:45 +00:00
PrintableModel::PrintableModel(QObject* parent)
2019-02-03 18:53:51 +00:00
: CheckableTestModel(parent)
{
}
2019-10-05 14:14:45 +00:00
void PrintableModel::printTo(QPainter& painter) const
2019-02-03 18:53:51 +00:00
{
2019-10-05 14:14:45 +00:00
printHeader(painter);
printTests(painter);
printSummary(painter);
2019-02-03 18:53:51 +00:00
}
QFont PrintableModel::h1Font()
{
2019-10-05 14:14:45 +00:00
return QFont("Helvetica", 16);
}
QFont PrintableModel::h2Font()
{
2019-10-05 14:14:45 +00:00
return QFont("Helvetica", 10);
}
QFont PrintableModel::tableFont()
{
2019-10-05 14:14:45 +00:00
return QFont("Helvetica", 8);
}
QPen PrintableModel::tablePen()
{
2019-10-05 14:14:45 +00:00
return QPen(Qt::black, 1, Qt::SolidLine);
}
2019-02-05 20:32:13 +00:00
QPen PrintableModel::resultPen()
{
2019-10-05 14:14:45 +00:00
return QPen(Qt::black, 2, Qt::SolidLine);
2019-02-05 20:32:13 +00:00
}
2019-02-07 19:03:22 +00:00
double PrintableModel::headerWidthFactor()
{
2019-10-05 14:14:45 +00:00
return 0.17;
2019-02-07 19:03:22 +00:00
}
double PrintableModel::cellWidthFactor()
{
2019-10-05 14:14:45 +00:00
return headerWidthFactor() / 2;
2019-02-07 19:03:22 +00:00
}
2019-10-05 14:14:45 +00:00
void PrintableModel::drawTextSquare(QPainter& painter, const QRectF& cell, const QString& text)
{
2019-10-05 14:14:45 +00:00
auto prevPen = painter.pen();
painter.setPen(tablePen());
2019-10-05 14:14:45 +00:00
painter.drawText(cell, Qt::AlignCenter, text);
2019-10-05 14:14:45 +00:00
painter.drawLine(cell.topLeft(), cell.topRight());
painter.drawLine(cell.topRight(), cell.bottomRight());
painter.drawLine(cell.bottomRight(), cell.bottomLeft());
painter.drawLine(cell.bottomLeft(), cell.topLeft());
2019-10-05 14:14:45 +00:00
painter.setPen(prevPen);
}
2019-10-05 14:14:45 +00:00
void PrintableModel::drawNumberSquare(QPainter& painter, double x, double y, int number)
2019-02-05 20:32:13 +00:00
{
2019-10-05 14:14:45 +00:00
QRectF cell = {x, y, 0.03 * painter.device()->width(),
1.5 * painter.fontMetrics().lineSpacing()};
2019-02-05 20:32:13 +00:00
2019-10-05 14:14:45 +00:00
painter.drawText(cell, Qt::AlignCenter, QString::number(number));
2019-02-05 20:32:13 +00:00
2019-10-05 14:14:45 +00:00
painter.drawLine(cell.topLeft(), cell.topRight());
painter.drawLine(cell.topRight(), cell.bottomRight());
painter.drawLine(cell.bottomRight(), cell.bottomLeft());
painter.drawLine(cell.bottomLeft(), cell.topLeft());
2019-02-05 20:32:13 +00:00
}
2019-10-05 14:14:45 +00:00
void PrintableModel::PrintableModel::drawCheckSquare(QPainter& painter, const QRectF& cell,
bool checked)
{
2019-02-08 20:03:52 +00:00
drawTextSquare(painter, cell, checked ? u8"\u2612" : u8"\u2610");
}
2019-10-05 14:14:45 +00:00
void PrintableModel::drawResultSquare(QPainter& painter, double y, bool rightCell,
unsigned int value)
{
2019-10-05 14:14:45 +00:00
double pageWidth = painter.device()->width();
double cellWidth = 0.03 * pageWidth;
double cellHeight = 1.5 * painter.fontMetrics().lineSpacing();
double x = pageWidth - cellWidth - (rightCell ? 0 : 0.04 * pageWidth);
2019-10-05 14:14:45 +00:00
drawTextSquare(painter, {x, y, cellWidth, cellHeight}, QString::number(value));
}
2019-10-05 14:14:45 +00:00
void PrintableModel::drawGreySquare(QPainter& painter, const QRectF& cell)
{
2019-10-05 14:14:45 +00:00
auto prevBrush = painter.brush();
auto prevPen = painter.pen();
2019-10-05 14:14:45 +00:00
painter.setBrush(QBrush(QColor(224, 224, 224)));
painter.setPen(QPen(Qt::NoPen));
QPointF points[4] = {cell.topLeft(), cell.topRight(), cell.bottomRight(), cell.bottomLeft()};
painter.drawPolygon(points, 4);
2019-10-05 14:14:45 +00:00
painter.setPen(tablePen());
painter.drawLine(cell.topLeft(), cell.topRight());
painter.drawLine(cell.topRight(), cell.bottomRight());
painter.drawLine(cell.bottomRight(), cell.bottomLeft());
painter.drawLine(cell.bottomLeft(), cell.topLeft());
2019-10-05 14:14:45 +00:00
painter.setBrush(prevBrush);
painter.setPen(prevPen);
}
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
void PrintableModel::drawHeader2(QPainter& painter, const QString& text)
2019-02-03 18:53:51 +00:00
{
2019-10-05 14:14:45 +00:00
painter.setFont(h2Font());
painter.drawText(0, 0, text);
painter.translate(0, 0.5 * painter.fontMetrics().lineSpacing());
2019-02-03 18:53:51 +00:00
}
2019-10-05 14:14:45 +00:00
void PrintableModel::printHeader(QPainter& painter) const
2019-02-03 18:53:51 +00:00
{
2019-10-05 14:14:45 +00:00
auto title = getTitle();
if (!title.isEmpty())
{
drawHeader2(painter, getTitle());
}
2019-02-03 18:53:51 +00:00
}
2019-10-05 14:14:45 +00:00
void PrintableModel::printTests(QPainter& painter) const
2019-02-03 18:53:51 +00:00
{
2019-10-05 14:14:45 +00:00
painter.setFont(tableFont());
painter.setPen(tablePen());
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
auto width = painter.device()->width();
auto height = 1.5 * painter.fontMetrics().lineSpacing();
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
double headerWidth = headerWidthFactor() * width;
double cellWidth = cellWidthFactor() * width;
double rowHeight = height;
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
double x = 0;
double y = 0;
for (const auto& test : m_tests)
{
QString testName = QString::fromStdString(
std::regex_replace(test.name().toStdString(), std::regex("\\s"), "\n"));
2019-02-05 21:16:30 +00:00
2019-10-05 14:14:45 +00:00
drawTextSquare(painter, {0, y, headerWidth, 2 * rowHeight}, testName);
x = headerWidth;
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
for (const auto& item : test.items())
{
drawTextSquare(painter, {x, y, cellWidth, rowHeight}, item.getText().c_str());
drawCheckSquare(painter, {x, y + rowHeight, cellWidth, rowHeight}, item.isChecked());
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
x += cellWidth;
}
y += rowHeight;
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
drawResultSquare(painter, y, true, test.getPoints());
y += rowHeight;
}
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
painter.translate(0, y + rowHeight);
2019-02-03 18:53:51 +00:00
}
2019-10-05 14:14:45 +00:00
void PrintableModel::printSummary(QPainter& painter) const
2019-02-03 18:53:51 +00:00
{
2019-10-05 14:14:45 +00:00
painter.setFont(tableFont());
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
auto width = painter.device()->width();
auto height = 1.5 * painter.fontMetrics().lineSpacing();
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
painter.drawText(0, 0, 0.95 * width, height, Qt::AlignRight | Qt::AlignVCenter,
"Rohwertpunkte Total:");
painter.setPen(resultPen());
drawNumberSquare(painter, 0.97 * width, 0, getPoints());
2019-02-03 18:53:51 +00:00
2019-10-05 14:14:45 +00:00
painter.translate(0, 3 * height);
2019-02-03 18:53:51 +00:00
}