Refactored printing code from model to base-class

feature/qt-as-conan-package
mandlm 2019-01-13 19:54:09 +01:00
parent 61bcb7566c
commit d298238ae8
11 changed files with 146 additions and 91 deletions

View File

@ -35,7 +35,6 @@ protobuf_generate_cpp(DataModel_PROTO_SRCS DataModel_PROTO_HDRS
add_executable(${PROJECT_NAME} WIN32
ESGRAF48.cpp
PrintableModel.cpp
DataModel.cpp
mainwindow.cpp
${LOGO_TEST_UI}
@ -61,6 +60,7 @@ target_link_libraries(${PROJECT_NAME}
CheckableItem
CheckableTest
CheckableTestModel
PrintableModel
MetaData
VerbEnd
Plural
@ -78,6 +78,7 @@ add_subdirectory(Age)
add_subdirectory(CheckableItem)
add_subdirectory(CheckableTest)
add_subdirectory(CheckableTestModel)
add_subdirectory(PrintableModel)
add_subdirectory(MetaData)
add_subdirectory(SubTests)

View File

@ -42,6 +42,7 @@ target_include_directories(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME}
PRIVATE
Age
PrintableModel
Qt5::Widgets
${Protobuf_LIBRARIES}
)

View File

@ -1,6 +1,6 @@
#pragma once
#include "../PrintableModel.h"
#include "PrintableModel.h"
#include "Age.h"

View File

@ -1,21 +0,0 @@
#include "PrintableModel.h"
QFont PrintableModel::h1Font()
{
return QFont("Helvetica", 16);
}
QFont PrintableModel::h2Font()
{
return QFont("Helvetica", 12);
}
QFont PrintableModel::tableFont()
{
return QFont("Helvetica", 8);
}
QPen PrintableModel::tablePen()
{
return QPen(Qt::black, 1, Qt::SolidLine);
}

View File

@ -1,16 +0,0 @@
#pragma once
#include <QPainter>
class PrintableModel
{
public:
virtual void printTo(QPainter &painter) const = 0;
protected:
static QFont h1Font();
static QFont h2Font();
static QFont tableFont();
static QPen tablePen();
};

View File

@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.6)
project(PrintableModel LANGUAGES CXX)
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
add_library(${PROJECT_NAME}
PrintableModel.cpp
)
set_target_properties(${PROJECT_NAME}
PROPERTIES CXX_STANDARD 14
)
target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(${PROJECT_NAME}
PRIVATE
Qt5::Core
Qt5::Widgets
)

View File

@ -0,0 +1,78 @@
#include "PrintableModel.h"
QFont PrintableModel::h1Font()
{
return QFont("Helvetica", 16);
}
QFont PrintableModel::h2Font()
{
return QFont("Helvetica", 12);
}
QFont PrintableModel::tableFont()
{
return QFont("Helvetica", 8);
}
QPen PrintableModel::tablePen()
{
return QPen(Qt::black, 1, Qt::SolidLine);
}
void PrintableModel::drawTextSquare(QPainter &painter, const QRectF &cell, const QString &text)
{
auto prevPen = painter.pen();
painter.setPen(tablePen());
painter.drawText(cell, Qt::AlignCenter, text);
painter.drawLine(cell.topLeft(), cell.topRight());
painter.drawLine(cell.topRight(), cell.bottomRight());
painter.drawLine(cell.bottomRight(), cell.bottomLeft());
painter.drawLine(cell.bottomLeft(), cell.topLeft());
painter.setPen(prevPen);
}
void PrintableModel::PrintableModel::drawCheckSquare(QPainter &painter, const QRectF &cell,
bool checked)
{
drawTextSquare(painter, cell, checked ? "\u2612" : "\u2610");
}
void PrintableModel::drawResultSquare(QPainter &painter, double y, bool rightCell,
unsigned int value)
{
auto prevPen = painter.pen();
painter.setPen(tablePen());
double pageWidth = painter.device()->width();
double cellWidth = 0.03 * pageWidth;
double cellHeight = painter.fontMetrics().lineSpacing();
double x = pageWidth - cellWidth - (rightCell ? 0 : 0.04 * pageWidth);
drawTextSquare(painter, {x, y, cellWidth, cellHeight}, QString::number(value));
painter.setPen(prevPen);
}
void PrintableModel::drawGreySquare(QPainter &painter, const QRectF &cell)
{
auto prevBrush = painter.brush();
auto prevPen = painter.pen();
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);
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());
painter.setBrush(prevBrush);
painter.setPen(prevPen);
}

View File

@ -0,0 +1,25 @@
#pragma once
#include <QPainter>
#include <QFont>
#include <QPen>
#include <QRect>
#include <QString>
class PrintableModel
{
public:
virtual void printTo(QPainter &painter) const = 0;
protected:
static QFont h1Font();
static QFont h2Font();
static QFont tableFont();
static QPen tablePen();
static void drawTextSquare(QPainter &painter, const QRectF &cell, const QString &text);
static void drawCheckSquare(QPainter &painter, const QRectF &cell, bool checked);
static void drawResultSquare(QPainter &painter, double y, bool rightCell, unsigned int value);
static void drawGreySquare(QPainter &painter, const QRectF &cell);
};

View File

@ -42,6 +42,7 @@ target_link_libraries(${PROJECT_NAME}
CheckableItem
CheckableTest
CheckableTestModel
PrintableModel
Qt5::Widgets
${Protobuf_LIBRARIES}
)

View File

@ -213,45 +213,6 @@ void V2SvkModel::printTo(QPainter &painter) const
auto width = painter.device()->width();
auto height = 1.5 * painter.fontMetrics().lineSpacing();
auto drawTextSquare = [&](double left, double top, double width, double height,
const QString &text) {
painter.drawText(left, top, width, height, Qt::AlignCenter, text);
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);
};
auto drawCheckSquare = [&](double left, double top, double width, double height, bool checked) {
drawTextSquare(left, top, width, height, checked ? "\u2612" : "\u2610");
};
auto drawResultSquare = [&](bool right, unsigned int value, double y) {
double cellWidth = 0.03 * width;
double x = width - cellWidth - (right ? 0 : 0.04 * width);
drawTextSquare(x, y, cellWidth, height, QString::number(value));
};
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.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);
};
std::set<unsigned int> blockStarters = {0, 3, 5, 7};
std::set<unsigned int> v2Tests = {0, 1, 3, 5, 7, 8};
std::set<unsigned int> svkTests = {2, 4, 6, 9, 10};
@ -268,7 +229,7 @@ void V2SvkModel::printTo(QPainter &painter) const
if (blockStarters.find(testIndex) != blockStarters.end())
{
y += rowHeight;
drawTextSquare(x, y, rowHeaderWidth, 2 * rowHeight, test.name());
drawTextSquare(painter, {x, y, rowHeaderWidth, 2 * rowHeight}, test.name());
x += rowHeaderWidth;
std::vector<std::pair<std::string, unsigned int>> columnHeaders;
@ -288,7 +249,7 @@ void V2SvkModel::printTo(QPainter &painter) const
for (const auto &columnHeader : columnHeaders)
{
double cellWidth = columnHeader.second * resultCellWidth;
drawTextSquare(x, y, cellWidth, rowHeight, columnHeader.first.c_str());
drawTextSquare(painter, {x, y, cellWidth, rowHeight}, columnHeader.first.c_str());
x += cellWidth;
}
x = rowHeaderWidth;
@ -296,7 +257,7 @@ void V2SvkModel::printTo(QPainter &painter) const
}
else
{
drawTextSquare(x, y, rowHeaderWidth, rowHeight, test.name());
drawTextSquare(painter, {x, y, rowHeaderWidth, rowHeight}, test.name());
x += rowHeaderWidth;
}
@ -311,30 +272,30 @@ void V2SvkModel::printTo(QPainter &painter) const
{
if (emptyItemsStack > 0)
{
drawGreySquare(x - emptyItemsStack * resultCellWidth, y,
emptyItemsStack * resultCellWidth, rowHeight);
drawGreySquare(painter, {x - emptyItemsStack * resultCellWidth, y,
emptyItemsStack * resultCellWidth, rowHeight});
emptyItemsStack = 0;
}
drawCheckSquare(x, y, resultCellWidth, rowHeight, item.isChecked());
drawCheckSquare(painter, {x, y, resultCellWidth, rowHeight}, item.isChecked());
}
x += resultCellWidth;
}
if (emptyItemsStack > 0)
{
drawGreySquare(x - emptyItemsStack * resultCellWidth, y,
emptyItemsStack * resultCellWidth, rowHeight);
drawGreySquare(painter, {x - emptyItemsStack * resultCellWidth, y,
emptyItemsStack * resultCellWidth, rowHeight});
emptyItemsStack = 0;
}
if (v2Tests.find(testIndex) != v2Tests.end())
{
drawResultSquare(false, test.getPoints(), y);
drawResultSquare(painter, y, false, test.getPoints());
}
if (svkTests.find(testIndex) != svkTests.end())
{
drawResultSquare(true, test.getPoints(), y);
drawResultSquare(painter, y, true, test.getPoints());
}
x = 0;
@ -348,6 +309,6 @@ void V2SvkModel::printTo(QPainter &painter) const
painter.drawText(x, y, 0.85 * width, height, Qt::AlignRight | Qt::AlignVCenter,
"Rohwertpunkte Total:");
drawResultSquare(false, getV2Points(), y);
drawResultSquare(true, getSvkPoints(), y);
drawResultSquare(painter, y, false, getV2Points());
drawResultSquare(painter, y, true, getSvkPoints());
}

View File

@ -1,6 +1,6 @@
#pragma once
#include "../../PrintableModel.h"
#include "PrintableModel.h"
#include "CheckableTestModel.h"
#include "V2SvkModel.pb.h"