From d298238ae88ca60ee295a5e79a024e262040e95c Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sun, 13 Jan 2019 19:54:09 +0100 Subject: [PATCH] Refactored printing code from model to base-class --- source/CMakeLists.txt | 3 +- source/MetaData/CMakeLists.txt | 1 + source/MetaData/MetaDataModel.h | 2 +- source/PrintableModel.cpp | 21 ------- source/PrintableModel.h | 16 ----- source/PrintableModel/CMakeLists.txt | 25 ++++++++ source/PrintableModel/PrintableModel.cpp | 78 ++++++++++++++++++++++++ source/PrintableModel/PrintableModel.h | 25 ++++++++ source/SubTests/V2Svk/CMakeLists.txt | 1 + source/SubTests/V2Svk/V2SvkModel.cpp | 63 ++++--------------- source/SubTests/V2Svk/V2SvkModel.h | 2 +- 11 files changed, 146 insertions(+), 91 deletions(-) delete mode 100644 source/PrintableModel.cpp delete mode 100644 source/PrintableModel.h create mode 100644 source/PrintableModel/CMakeLists.txt create mode 100644 source/PrintableModel/PrintableModel.cpp create mode 100644 source/PrintableModel/PrintableModel.h diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 895a7a4..f1329f6 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -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) diff --git a/source/MetaData/CMakeLists.txt b/source/MetaData/CMakeLists.txt index e00d8de..ab2b0f0 100644 --- a/source/MetaData/CMakeLists.txt +++ b/source/MetaData/CMakeLists.txt @@ -42,6 +42,7 @@ target_include_directories(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} PRIVATE Age + PrintableModel Qt5::Widgets ${Protobuf_LIBRARIES} ) diff --git a/source/MetaData/MetaDataModel.h b/source/MetaData/MetaDataModel.h index 3164b90..7774f99 100644 --- a/source/MetaData/MetaDataModel.h +++ b/source/MetaData/MetaDataModel.h @@ -1,6 +1,6 @@ #pragma once -#include "../PrintableModel.h" +#include "PrintableModel.h" #include "Age.h" diff --git a/source/PrintableModel.cpp b/source/PrintableModel.cpp deleted file mode 100644 index 2f5a16c..0000000 --- a/source/PrintableModel.cpp +++ /dev/null @@ -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); -} diff --git a/source/PrintableModel.h b/source/PrintableModel.h deleted file mode 100644 index 5d99c31..0000000 --- a/source/PrintableModel.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include - -class PrintableModel -{ -public: - virtual void printTo(QPainter &painter) const = 0; - -protected: - static QFont h1Font(); - static QFont h2Font(); - static QFont tableFont(); - - static QPen tablePen(); -}; diff --git a/source/PrintableModel/CMakeLists.txt b/source/PrintableModel/CMakeLists.txt new file mode 100644 index 0000000..0a09f00 --- /dev/null +++ b/source/PrintableModel/CMakeLists.txt @@ -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 +) diff --git a/source/PrintableModel/PrintableModel.cpp b/source/PrintableModel/PrintableModel.cpp new file mode 100644 index 0000000..21d57a2 --- /dev/null +++ b/source/PrintableModel/PrintableModel.cpp @@ -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); +} diff --git a/source/PrintableModel/PrintableModel.h b/source/PrintableModel/PrintableModel.h new file mode 100644 index 0000000..ab08433 --- /dev/null +++ b/source/PrintableModel/PrintableModel.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include +#include +#include + +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); +}; diff --git a/source/SubTests/V2Svk/CMakeLists.txt b/source/SubTests/V2Svk/CMakeLists.txt index caf6c8c..5cd3661 100644 --- a/source/SubTests/V2Svk/CMakeLists.txt +++ b/source/SubTests/V2Svk/CMakeLists.txt @@ -42,6 +42,7 @@ target_link_libraries(${PROJECT_NAME} CheckableItem CheckableTest CheckableTestModel + PrintableModel Qt5::Widgets ${Protobuf_LIBRARIES} ) diff --git a/source/SubTests/V2Svk/V2SvkModel.cpp b/source/SubTests/V2Svk/V2SvkModel.cpp index e621207..ab9a833 100644 --- a/source/SubTests/V2Svk/V2SvkModel.cpp +++ b/source/SubTests/V2Svk/V2SvkModel.cpp @@ -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 blockStarters = {0, 3, 5, 7}; std::set v2Tests = {0, 1, 3, 5, 7, 8}; std::set 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> 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()); } diff --git a/source/SubTests/V2Svk/V2SvkModel.h b/source/SubTests/V2Svk/V2SvkModel.h index c7090c3..2b282ee 100644 --- a/source/SubTests/V2Svk/V2SvkModel.h +++ b/source/SubTests/V2Svk/V2SvkModel.h @@ -1,6 +1,6 @@ #pragma once -#include "../../PrintableModel.h" +#include "PrintableModel.h" #include "CheckableTestModel.h" #include "V2SvkModel.pb.h"