From 59c409c872772203b2d3dce8501d23336bed87ae Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 12 Jan 2019 16:50:04 +0100 Subject: [PATCH] Print headline and meta-data --- source/CMakeLists.txt | 1 + source/DataModel.cpp | 26 +++---------- source/DataModel.h | 31 +++------------- source/MetaData/MetaDataModel.cpp | 62 +++++++++++++++---------------- source/MetaData/MetaDataModel.h | 7 ++-- source/PrintableModel.cpp | 12 ++++++ source/PrintableModel.h | 13 +++++++ source/mainwindow.cpp | 23 +++++++----- source/mainwindow.h | 1 + 9 files changed, 87 insertions(+), 89 deletions(-) create mode 100644 source/PrintableModel.cpp create mode 100644 source/PrintableModel.h diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 0a0f40f..895a7a4 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -35,6 +35,7 @@ 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} diff --git a/source/DataModel.cpp b/source/DataModel.cpp index 216eb8c..d108729 100644 --- a/source/DataModel.cpp +++ b/source/DataModel.cpp @@ -59,28 +59,14 @@ void DataModel::read(std::istream &inStream) m_genitiv.read(dataModel.lateskillsgenitiv()); m_passiv.read(dataModel.lateskillspassiv()); } - -std::string DataModel::toHtml() const + +void DataModel::printTo(QPainter &painter) const { - std::stringstream out; + painter.setFont(h1Font()); + painter.drawText(0, painter.fontMetrics().lineSpacing(), "ESGRAF 4-8 Auswertungsbogen"); + painter.translate(0, 3 * painter.fontMetrics().lineSpacing()); - out << "" << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "

ESGRAF 4-8 Auswertungsbogen

" << std::endl; - out << "

" << std::endl; - out << m_metaData.toHtml(); - out << "

" << std::endl; - out << "" << std::endl; - out << "" << std::endl; - - return out.str(); + m_metaData.printTo(painter); } void DataModel::pluralModelChanged() diff --git a/source/DataModel.h b/source/DataModel.h index 71a9352..3469e3e 100644 --- a/source/DataModel.h +++ b/source/DataModel.h @@ -1,5 +1,7 @@ #pragma once +#include "PrintableModel.h" + #include "MetaData/MetaDataModel.h" #include "GenusModel.h" #include "VerbEndModel.h" @@ -12,9 +14,9 @@ #include "ResultModel.h" -#include +#include -class DataModel : public QObject +class DataModel : public QObject, public PrintableModel { Q_OBJECT @@ -34,35 +36,14 @@ public: public: DataModel(QObject *parent); - std::string toHtml() const; - void write(std::ostream &outStream) const; void read(std::istream &inStream); + void printTo(QPainter &painter) const override; + signals: void modelChanged(); -private: - template - void write( - const ModelType &model, QJsonObject &target, const char *name) const - { - QJsonObject jsonObject; - model.write(jsonObject); - target[name] = jsonObject; - } - - template - void read( - ModelType &model, const QJsonObject &source, const char *name) const - { - const auto &jsonObject = source[name]; - if (jsonObject.isObject()) - { - model.read(jsonObject.toObject()); - } - } - private slots: void pluralModelChanged(); void metaDataChanged(); diff --git a/source/MetaData/MetaDataModel.cpp b/source/MetaData/MetaDataModel.cpp index 94c2a62..32ab815 100644 --- a/source/MetaData/MetaDataModel.cpp +++ b/source/MetaData/MetaDataModel.cpp @@ -130,39 +130,37 @@ void MetaDataModel::write(ESGRAF48::MetaDataModel &model) const model.set_remarks(m_remarks.toStdString()); } -std::string MetaDataModel::toHtml() const +void MetaDataModel::printTo(QPainter &painter) const { - std::ostringstream out; + painter.setFont(tableFont()); - out << "" - << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" - << std::endl; - out << "" << std::endl; - out << "" - << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" - << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" - << std::endl; - out << "" - << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "" << std::endl; - out << "
Name, Vorname" << m_participant.toHtmlEscaped().toStdString() << "Untersucher(in)" << m_instructor.toHtmlEscaped().toStdString() << "
Geburtsdatum" << m_dateOfBirth.toString("dd.MM.yyyy").toHtmlEscaped().toStdString() << "Bemerkungen
Untersuchungsdatum" << m_dateOfTest.toString("dd.MM.yyyy").toHtmlEscaped().toStdString() << "" - << m_remarks.trimmed().toHtmlEscaped().replace("\n", "
").toStdString() << "
Alter am Testtag" << getAge().toString() << "
" << std::endl; + auto width = painter.device()->width(); + auto height = 1.5 * painter.fontMetrics().lineSpacing(); - return out.str(); + auto hasRemarks = !m_remarks.trimmed().isEmpty(); + + painter.drawText(0, 0, "Name, Vorname"); + painter.drawText(0.25 * width, 0, m_participant); + painter.drawText(0.5 * width, 0, "Untersucher(in)"); + painter.drawText(0.75 * width, 0, m_instructor); + + painter.translate(0, height); + + painter.drawText(0, 0, "Geburtsdatum"); + painter.drawText(0.25 * width, 0, m_dateOfBirth.toString("dd.MM.yyyy")); + if (hasRemarks) + { + painter.drawText(0.5 * width, 0, "Bemerkungen:"); + painter.drawText(QRect(0.5 * width, 0.5 * height, width, 2 * height), m_remarks); + } + + painter.translate(0, height); + + painter.drawText(0, 0, "Untersuchungsdatum"); + painter.drawText(0.25 * width, 0, m_dateOfTest.toString("dd.MM.yyyy")); + + painter.translate(0, height); + + painter.drawText(0, 0, "Alter am Testtag"); + painter.drawText(0.25 * width, 0, getAge().toString().c_str()); } diff --git a/source/MetaData/MetaDataModel.h b/source/MetaData/MetaDataModel.h index 0c8bbeb..3164b90 100644 --- a/source/MetaData/MetaDataModel.h +++ b/source/MetaData/MetaDataModel.h @@ -1,5 +1,7 @@ #pragma once +#include "../PrintableModel.h" + #include "Age.h" #include "MetaDataModel.pb.h" @@ -7,9 +9,8 @@ #include #include #include -#include -class MetaDataModel : public QAbstractTableModel +class MetaDataModel : public QAbstractTableModel, public PrintableModel { Q_OBJECT @@ -33,7 +34,7 @@ public: void read(const ESGRAF48::MetaDataModel &model); void write(ESGRAF48::MetaDataModel &model) const; - std::string toHtml() const; + void printTo(QPainter &painter) const override; Age getAge() const { diff --git a/source/PrintableModel.cpp b/source/PrintableModel.cpp new file mode 100644 index 0000000..ba9c0a1 --- /dev/null +++ b/source/PrintableModel.cpp @@ -0,0 +1,12 @@ +#include "PrintableModel.h" + +QFont PrintableModel::h1Font() +{ + return QFont("Helvetica", 16); +} + +QFont PrintableModel::tableFont() +{ + return QFont("Helvetica", 10); +} + diff --git a/source/PrintableModel.h b/source/PrintableModel.h new file mode 100644 index 0000000..46d2c65 --- /dev/null +++ b/source/PrintableModel.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +class PrintableModel +{ +public: + virtual void printTo(QPainter &painter) const = 0; + +protected: + static QFont h1Font(); + static QFont tableFont(); +}; diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index e91549a..0a5c8de 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include @@ -150,10 +151,9 @@ void MainWindow::closeFile() void MainWindow::print() const { - //std::ofstream htmlfile("print.html"); - //htmlfile << m_dataModel.toHtml(); - QPrinter printer; + printer.setPaperSize(QPrinter::A4); + printer.setPageMargins(30, 20, 30, 20, QPrinter::Millimeter); QPrintDialog dialog(&printer); if (dialog.exec() != QDialog::Accepted) @@ -161,10 +161,12 @@ void MainWindow::print() const return; } - QTextDocument printDoc; - printDoc.setHtml(QString::fromStdString(m_dataModel.toHtml())); + QPainter painter; + painter.begin(&printer); - printDoc.print(&printer); + m_dataModel.printTo(painter); + + painter.end(); } void MainWindow::dataModelChanged() @@ -213,10 +215,13 @@ void MainWindow::savePdf(const QString &filename) QPrinter printer; printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); + printer.setPageMargins(30, 20, 30, 20, QPrinter::Millimeter); printer.setOutputFileName(filename); - QTextDocument printDoc; - printDoc.setHtml(QString::fromStdString(m_dataModel.toHtml())); + QPainter painter; + painter.begin(&printer); - printDoc.print(&printer); + m_dataModel.printTo(painter); + + painter.end(); } diff --git a/source/mainwindow.h b/source/mainwindow.h index 9675fb7..006262f 100644 --- a/source/mainwindow.h +++ b/source/mainwindow.h @@ -4,6 +4,7 @@ #include #include +#include class DataModel;