Print subtest 1

This commit is contained in:
mandlm 2019-01-13 18:44:07 +01:00
parent 463dc275d0
commit 61bcb7566c
12 changed files with 127 additions and 135 deletions

View File

@ -34,24 +34,3 @@ unsigned int CheckableItem::points() const
{ {
return m_checked ? m_value : 0; 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();
}
}

View File

@ -24,7 +24,4 @@ public:
void setValue(unsigned int value); void setValue(unsigned int value);
unsigned int points() const; unsigned int points() const;
void write(QJsonObject &json) const;
void read(const QJsonObject &json);
}; };

View File

@ -2,6 +2,8 @@
#include <QJsonArray> #include <QJsonArray>
#include <numeric>
CheckableItems::CheckableItems(std::initializer_list<std::string> itemNames) CheckableItems::CheckableItems(std::initializer_list<std::string> itemNames)
{ {
for (const auto &itemName : itemNames) for (const auto &itemName : itemNames)
@ -10,27 +12,9 @@ CheckableItems::CheckableItems(std::initializer_list<std::string> itemNames)
} }
} }
void CheckableItems::write(QJsonArray &json) const unsigned int CheckableItems::getPoints() const
{ {
for (const auto &item : *this) return std::accumulate(begin(), end(), 0, [](int base, const CheckableItem &item) {
{ return base + item.points();
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);
}
}
} }

View File

@ -10,6 +10,5 @@ class CheckableItems : public std::vector<CheckableItem>
public: public:
CheckableItems(std::initializer_list<std::string> itemNames); CheckableItems(std::initializer_list<std::string> itemNames);
void write(QJsonArray &json) const; unsigned int getPoints() const;
void read(const QJsonArray &json);
}; };

View File

@ -1,5 +1,7 @@
#include "CheckableTest.h" #include "CheckableTest.h"
#include <numeric>
CheckableTest::CheckableTest( CheckableTest::CheckableTest(
const char *name, std::initializer_list<std::string> items) const char *name, std::initializer_list<std::string> items)
: m_name(name) : m_name(name)
@ -26,3 +28,8 @@ CheckableItems &CheckableTest::items()
{ {
return m_items; return m_items;
} }
unsigned int CheckableTest::getPoints() const
{
return m_items.getPoints();
}

View File

@ -17,6 +17,8 @@ public:
const QString &name() const; const QString &name() const;
const CheckableItems &items() const; const CheckableItems &items() const;
CheckableItems &items(); CheckableItems &items();
unsigned int getPoints() const;
}; };
using CheckableTests = std::vector<CheckableTest>; using CheckableTests = std::vector<CheckableTest>;

View File

@ -107,30 +107,6 @@ QVariant CheckableTestModel::headerData(
return QAbstractTableModel::headerData(section, orientation, role); 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 bool CheckableTestModel::isValidIndex(const QModelIndex &index) const
{ {
if (index.row() < m_tests.size()) if (index.row() < m_tests.size())

View File

@ -25,9 +25,6 @@ public:
QVariant headerData(int section, Qt::Orientation orientation, QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const override; int role = Qt::DisplayRole) const override;
void write(QJsonObject &json) const;
void read(const QJsonObject &json);
unsigned int getPoints() const; unsigned int getPoints() const;
protected: protected:

View File

@ -12,7 +12,7 @@ QFont PrintableModel::h2Font()
QFont PrintableModel::tableFont() QFont PrintableModel::tableFont()
{ {
return QFont("Helvetica", 10); return QFont("Helvetica", 8);
} }
QPen PrintableModel::tablePen() QPen PrintableModel::tablePen()

View File

@ -29,7 +29,7 @@ V2SvkModel::V2SvkModel(QObject *parent)
}; };
} }
unsigned int V2SvkModel::getV2Points() unsigned int V2SvkModel::getV2Points() const
{ {
unsigned int points = 0; unsigned int points = 0;
@ -49,7 +49,7 @@ unsigned int V2SvkModel::getV2Points()
return points; return points;
} }
unsigned int V2SvkModel::getSvkPoints() unsigned int V2SvkModel::getSvkPoints() const
{ {
unsigned int points = 0; unsigned int points = 0;
@ -207,8 +207,6 @@ void V2SvkModel::printTo(QPainter &painter) const
painter.drawText( painter.drawText(
0, 0, "Subtest 1: Verbzweitstellungsregel (V2) und Subjekt-Verb-Kontrollregel (SVK)"); 0, 0, "Subtest 1: Verbzweitstellungsregel (V2) und Subjekt-Verb-Kontrollregel (SVK)");
painter.translate(0, 1.5 * painter.fontMetrics().lineSpacing());
painter.setFont(tableFont()); painter.setFont(tableFont());
painter.setPen(tablePen()); painter.setPen(tablePen());
@ -228,7 +226,7 @@ void V2SvkModel::printTo(QPainter &painter) const
drawTextSquare(left, top, width, height, checked ? "\u2612" : "\u2610"); 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 cellWidth = 0.03 * width;
double x = width - cellWidth - (right ? 0 : 0.04 * 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 drawGreySquare = [&](double left, double top, double width, double height) {
auto prevBrush = painter.brush(); auto prevBrush = painter.brush();
auto prevPen = painter.pen();
painter.setBrush(QBrush(QColor(224, 224, 224))); 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.setBrush(prevBrush);
painter.setPen(prevPen);
}; };
auto x = 0.0; std::set<unsigned int> blockStarters = {0, 3, 5, 7};
auto y = 0.0; std::set<unsigned int> v2Tests = {0, 1, 3, 5, 7, 8};
auto cellWidth = 0.2 * width; std::set<unsigned int> svkTests = {2, 4, 6, 9, 10};
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");
x = 0.2 * width; double x = 0;
y += height; double y = 0;
cellWidth = 0.05 * width; auto testIndex = 0;
for (int i = 0; i < 12; ++i) for (const auto &test : m_tests)
{ {
drawCheckSquare(x, y, cellWidth, height, true); double rowHeaderWidth = 0.2 * width;
x += cellWidth; 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<std::pair<std::string, unsigned int>> 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; x = 0;
y += height; 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; painter.drawText(x, y, 0.85 * width, height, Qt::AlignRight | Qt::AlignVCenter,
y += height; "Rohwertpunkte Total:");
cellWidth = 0.2 * width; drawResultSquare(false, getV2Points(), y);
drawTextSquare(x, y, cellWidth, height, "SVK: /-st/"); drawResultSquare(true, getSvkPoints(), y);
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);
} }

View File

@ -12,8 +12,8 @@ class V2SvkModel : public CheckableTestModel, public PrintableModel
public: public:
V2SvkModel(QObject *parent); V2SvkModel(QObject *parent);
unsigned int getV2Points(); unsigned int getV2Points() const;
unsigned int getSvkPoints(); unsigned int getSvkPoints() const;
void write(ESGRAF48::V2SvkModel &model) const; void write(ESGRAF48::V2SvkModel &model) const;
void read(const ESGRAF48::V2SvkModel &model); void read(const ESGRAF48::V2SvkModel &model);

View File

@ -153,7 +153,7 @@ void MainWindow::print() const
{ {
QPrinter printer; QPrinter printer;
printer.setPaperSize(QPrinter::A4); printer.setPaperSize(QPrinter::A4);
printer.setPageMargins(30, 20, 30, 20, QPrinter::Millimeter); printer.setPageMargins(20, 20, 20, 20, QPrinter::Millimeter);
QPrintDialog dialog(&printer); QPrintDialog dialog(&printer);
if (dialog.exec() != QDialog::Accepted) if (dialog.exec() != QDialog::Accepted)
@ -215,7 +215,7 @@ void MainWindow::savePdf(const QString &filename)
QPrinter printer; QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::A4); printer.setPaperSize(QPrinter::A4);
printer.setPageMargins(30, 20, 30, 20, QPrinter::Millimeter); printer.setPageMargins(20, 20, 20, 20, QPrinter::Millimeter);
printer.setOutputFileName(filename); printer.setOutputFileName(filename);
QPainter painter; QPainter painter;