Refactored printing code from model to base-class

This commit is contained in:
Michael Mandl 2019-01-13 19:54:09 +01:00
parent 61bcb7566c
commit d298238ae8
11 changed files with 146 additions and 91 deletions

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);
};