Print headline and meta-data

This commit is contained in:
Michael Mandl 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 add_executable(${PROJECT_NAME} WIN32
ESGRAF48.cpp ESGRAF48.cpp
PrintableModel.cpp
DataModel.cpp DataModel.cpp
mainwindow.cpp mainwindow.cpp
${LOGO_TEST_UI} ${LOGO_TEST_UI}

View file

@ -59,28 +59,14 @@ void DataModel::read(std::istream &inStream)
m_genitiv.read(dataModel.lateskillsgenitiv()); m_genitiv.read(dataModel.lateskillsgenitiv());
m_passiv.read(dataModel.lateskillspassiv()); 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; m_metaData.printTo(painter);
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();
} }
void DataModel::pluralModelChanged() void DataModel::pluralModelChanged()

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "PrintableModel.h"
#include "MetaData/MetaDataModel.h" #include "MetaData/MetaDataModel.h"
#include "GenusModel.h" #include "GenusModel.h"
#include "VerbEndModel.h" #include "VerbEndModel.h"
@ -12,9 +14,9 @@
#include "ResultModel.h" #include "ResultModel.h"
#include <QJsonObject> #include <QPainter>
class DataModel : public QObject class DataModel : public QObject, public PrintableModel
{ {
Q_OBJECT Q_OBJECT
@ -34,35 +36,14 @@ public:
public: public:
DataModel(QObject *parent); DataModel(QObject *parent);
std::string toHtml() const;
void write(std::ostream &outStream) const; void write(std::ostream &outStream) const;
void read(std::istream &inStream); void read(std::istream &inStream);
void printTo(QPainter &painter) const override;
signals: signals:
void modelChanged(); 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: private slots:
void pluralModelChanged(); void pluralModelChanged();
void metaDataChanged(); void metaDataChanged();

View file

@ -130,39 +130,37 @@ void MetaDataModel::write(ESGRAF48::MetaDataModel &model) const
model.set_remarks(m_remarks.toStdString()); 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\">" auto width = painter.device()->width();
<< std::endl; auto height = 1.5 * painter.fontMetrics().lineSpacing();
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;
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 #pragma once
#include "../PrintableModel.h"
#include "Age.h" #include "Age.h"
#include "MetaDataModel.pb.h" #include "MetaDataModel.pb.h"
@ -7,9 +9,8 @@
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QString> #include <QString>
#include <QDate> #include <QDate>
#include <QJsonObject>
class MetaDataModel : public QAbstractTableModel class MetaDataModel : public QAbstractTableModel, public PrintableModel
{ {
Q_OBJECT Q_OBJECT
@ -33,7 +34,7 @@ public:
void read(const ESGRAF48::MetaDataModel &model); void read(const ESGRAF48::MetaDataModel &model);
void write(ESGRAF48::MetaDataModel &model) const; void write(ESGRAF48::MetaDataModel &model) const;
std::string toHtml() const; void printTo(QPainter &painter) const override;
Age getAge() const 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/QPrinter>
#include <QtPrintSupport/QPrintDialog> #include <QtPrintSupport/QPrintDialog>
#include <QTextDocument> #include <QTextDocument>
#include <QPainter>
#include <QDebug> #include <QDebug>
@ -150,10 +151,9 @@ void MainWindow::closeFile()
void MainWindow::print() const void MainWindow::print() const
{ {
//std::ofstream htmlfile("print.html");
//htmlfile << m_dataModel.toHtml();
QPrinter printer; QPrinter printer;
printer.setPaperSize(QPrinter::A4);
printer.setPageMargins(30, 20, 30, 20, QPrinter::Millimeter);
QPrintDialog dialog(&printer); QPrintDialog dialog(&printer);
if (dialog.exec() != QDialog::Accepted) if (dialog.exec() != QDialog::Accepted)
@ -161,10 +161,12 @@ void MainWindow::print() const
return; return;
} }
QTextDocument printDoc; QPainter painter;
printDoc.setHtml(QString::fromStdString(m_dataModel.toHtml())); painter.begin(&printer);
printDoc.print(&printer); m_dataModel.printTo(painter);
painter.end();
} }
void MainWindow::dataModelChanged() void MainWindow::dataModelChanged()
@ -213,10 +215,13 @@ void MainWindow::savePdf(const QString &filename)
QPrinter printer; QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::A4); printer.setPaperSize(QPrinter::A4);
printer.setPageMargins(30, 20, 30, 20, QPrinter::Millimeter);
printer.setOutputFileName(filename); printer.setOutputFileName(filename);
QTextDocument printDoc; QPainter painter;
printDoc.setHtml(QString::fromStdString(m_dataModel.toHtml())); painter.begin(&printer);
printDoc.print(&printer); m_dataModel.printTo(painter);
painter.end();
} }

View file

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