Print headline and meta-data

This commit is contained in:
mandlm 2019-01-12 16:50:04 +01:00
parent bdf9706e58
commit 59c409c872
9 changed files with 87 additions and 89 deletions

View File

@ -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}

View File

@ -60,27 +60,13 @@ void DataModel::read(std::istream &inStream)
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 << "<html>" << std::endl;
out << "<head>" << std::endl;
out << "<style>" << std::endl;
out << "body {" << std::endl;
out << "font-family:sans-serif;" << std::endl;
out << "}" << std::endl;
out << "</style>" << std::endl;
out << "</head>" << std::endl;
out << "<body>" << std::endl;
out << "<h2>ESGRAF 4-8 Auswertungsbogen</h2>" << std::endl;
out << "<p>" << std::endl;
out << m_metaData.toHtml();
out << "</p>" << std::endl;
out << "</body>" << std::endl;
out << "</html>" << std::endl;
return out.str();
m_metaData.printTo(painter);
}
void DataModel::pluralModelChanged()

View File

@ -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 <QJsonObject>
#include <QPainter>
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 <class ModelType>
void write(
const ModelType &model, QJsonObject &target, const char *name) const
{
QJsonObject jsonObject;
model.write(jsonObject);
target[name] = jsonObject;
}
template <class ModelType>
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();

View File

@ -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 << "<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" frame=\"box\" rules=\"all\">"
<< std::endl;
out << "<tr>" << std::endl;
out << "<td width=\"25%\">Name, Vorname</td>" << std::endl;
out << "<td width=\"25%\">" << m_participant.toHtmlEscaped().toStdString() << "</td>"
<< std::endl;
out << "<td width=\"25%\">Untersucher(in)</td>" << std::endl;
out << "<td width=\"25%\">" << m_instructor.toHtmlEscaped().toStdString() << "</td>"
<< std::endl;
out << "</tr>" << std::endl;
out << "<tr>" << std::endl;
out << "<td>Geburtsdatum</td>" << std::endl;
out << "<td>" << m_dateOfBirth.toString("dd.MM.yyyy").toHtmlEscaped().toStdString() << "</td>"
<< std::endl;
out << "<td colspan=\"2\">Bemerkungen</td>" << std::endl;
out << "</tr>" << std::endl;
out << "<tr>" << std::endl;
out << "<td>Untersuchungsdatum</td>" << std::endl;
out << "<td>" << m_dateOfTest.toString("dd.MM.yyyy").toHtmlEscaped().toStdString() << "</td>"
<< std::endl;
out << "<td colspan=\"2\" rowspan=\"2\">"
<< m_remarks.trimmed().toHtmlEscaped().replace("\n", "<br>").toStdString() << "</td>"
<< std::endl;
out << "</tr>" << std::endl;
out << "<tr>" << std::endl;
out << "<td>Alter am Testtag</td>" << std::endl;
out << "<td>" << getAge().toString() << "</td>" << std::endl;
out << "</tr>" << std::endl;
out << "</table>" << 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());
}

View File

@ -1,5 +1,7 @@
#pragma once
#include "../PrintableModel.h"
#include "Age.h"
#include "MetaDataModel.pb.h"
@ -7,9 +9,8 @@
#include <QAbstractTableModel>
#include <QString>
#include <QDate>
#include <QJsonObject>
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
{

12
source/PrintableModel.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "PrintableModel.h"
QFont PrintableModel::h1Font()
{
return QFont("Helvetica", 16);
}
QFont PrintableModel::tableFont()
{
return QFont("Helvetica", 10);
}

13
source/PrintableModel.h Normal file
View File

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

View File

@ -11,6 +11,7 @@
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
#include <QTextDocument>
#include <QPainter>
#include <QDebug>
@ -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();
}

View File

@ -4,6 +4,7 @@
#include <QMainWindow>
#include <QString>
#include <QFont>
class DataModel;