ESGRAF48/source/MetaData/MetaDataModel.cpp

174 lines
4.7 KiB
C++
Raw Permalink Normal View History

#include "MetaDataModel.h"
#include <QDebug>
2018-11-03 21:29:38 +00:00
#include <sstream>
2019-10-05 14:14:45 +00:00
MetaDataModel::MetaDataModel(QObject* parent)
2019-02-03 18:53:51 +00:00
: PrintableModel(parent)
{
2019-10-05 14:14:45 +00:00
m_dateOfBirth = QDate::currentDate().addYears(-9);
m_dateOfTest = QDate::currentDate();
}
2019-10-05 14:14:45 +00:00
int MetaDataModel::rowCount(const QModelIndex& parent) const
{
2019-10-05 14:14:45 +00:00
return 1;
}
2019-10-05 14:14:45 +00:00
int MetaDataModel::columnCount(const QModelIndex& parent) const
{
2019-10-05 14:14:45 +00:00
return 6;
}
2019-10-05 14:14:45 +00:00
QVariant MetaDataModel::data(const QModelIndex& modelIndex, int role) const
{
2019-10-05 14:14:45 +00:00
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
switch (modelIndex.column())
{
case 0:
return m_participant;
case 1:
return m_instructor;
case 2:
return m_dateOfBirth;
case 3:
return m_dateOfTest;
case 4:
return m_remarks;
case 5:
return QString::fromStdString(getAge().toString());
default:
return QVariant();
}
}
return QVariant();
}
2018-05-15 12:37:16 +00:00
2019-10-05 14:14:45 +00:00
Qt::ItemFlags MetaDataModel::flags(const QModelIndex& modelIndex) const
2018-05-17 17:42:35 +00:00
{
2019-10-05 14:14:45 +00:00
return QAbstractTableModel::flags(modelIndex) | Qt::ItemIsEditable;
2018-05-17 17:42:35 +00:00
}
2019-10-05 14:14:45 +00:00
bool MetaDataModel::setData(const QModelIndex& modelIndex, const QVariant& value, int role)
2018-05-17 17:42:35 +00:00
{
2019-10-05 14:14:45 +00:00
if (role != Qt::EditRole)
{
return QAbstractTableModel::setData(modelIndex, value, role);
}
bool valueChanged = false;
switch (modelIndex.column())
{
case 0:
if (value.toString() != m_participant)
{
m_participant = value.toString();
valueChanged = true;
}
break;
case 1:
if (value.toString() != m_instructor)
{
m_instructor = value.toString();
valueChanged = true;
}
break;
case 2:
if (value.toDate() != m_dateOfBirth)
{
m_dateOfBirth = value.toDate();
emit dataChanged(index(0, 5), index(0, 5));
valueChanged = true;
}
break;
case 3:
if (value.toDate() != m_dateOfTest)
{
m_dateOfTest = value.toDate();
emit dataChanged(index(0, 5), index(0, 5));
valueChanged = true;
}
break;
case 4:
if (value.toString() != m_remarks)
{
m_remarks = value.toString();
valueChanged = true;
}
break;
default:
break;
}
if (valueChanged)
{
emit dataChanged(modelIndex, modelIndex);
}
return valueChanged;
2018-05-17 17:42:35 +00:00
}
2019-10-05 14:14:45 +00:00
void MetaDataModel::read(const ESGRAF48::MetaDataModel& model)
2018-11-16 22:21:02 +00:00
{
2019-10-05 14:14:45 +00:00
setData(index(0, 0), QString::fromStdString(model.participantname()));
setData(index(0, 1), QString::fromStdString(model.instructorname()));
setData(index(0, 2), QString::fromStdString(model.dateofbirth()));
setData(index(0, 3), QString::fromStdString(model.dateoftest()));
setData(index(0, 4), QString::fromStdString(model.remarks()));
2018-11-16 22:21:02 +00:00
}
2019-10-05 14:14:45 +00:00
void MetaDataModel::write(ESGRAF48::MetaDataModel& model) const
2018-11-16 22:21:02 +00:00
{
2019-10-05 14:14:45 +00:00
model.set_participantname(m_participant.toStdString());
model.set_instructorname(m_instructor.toStdString());
model.set_dateofbirth(m_dateOfBirth.toString(Qt::ISODate).toStdString());
model.set_dateoftest(m_dateOfTest.toString(Qt::ISODate).toStdString());
model.set_remarks(m_remarks.toStdString());
2018-11-16 22:21:02 +00:00
}
2018-11-13 23:17:29 +00:00
2019-10-05 14:14:45 +00:00
void MetaDataModel::printTo(QPainter& painter) const
2018-11-03 21:29:38 +00:00
{
2019-10-05 14:14:45 +00:00
painter.setFont(tableFont());
2019-01-12 15:50:04 +00:00
2019-10-05 14:14:45 +00:00
auto width = painter.device()->width();
auto height = 1.5 * painter.fontMetrics().lineSpacing();
2019-01-12 15:50:04 +00:00
2019-10-05 14:14:45 +00:00
auto hasRemarks = !m_remarks.trimmed().isEmpty();
2019-01-12 15:50:04 +00:00
2019-10-05 14:14:45 +00:00
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);
2019-01-12 15:50:04 +00:00
2019-10-05 14:14:45 +00:00
painter.translate(0, height);
2019-01-12 15:50:04 +00:00
2019-10-05 14:14:45 +00:00
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);
}
2019-01-12 15:50:04 +00:00
2019-10-05 14:14:45 +00:00
painter.translate(0, height);
2019-01-12 15:50:04 +00:00
2019-10-05 14:14:45 +00:00
painter.drawText(0, 0, "Untersuchungsdatum");
painter.drawText(0.25 * width, 0, m_dateOfTest.toString("dd.MM.yyyy"));
2019-01-12 15:50:04 +00:00
2019-10-05 14:14:45 +00:00
painter.translate(0, height);
2019-01-12 15:50:04 +00:00
2019-10-05 14:14:45 +00:00
painter.drawText(0, 0, "Alter am Testtag");
painter.drawText(0.25 * width, 0, getAge().toString().c_str());
2019-01-12 20:42:52 +00:00
2019-10-05 14:14:45 +00:00
painter.translate(0, 2 * height);
2018-11-03 21:29:38 +00:00
}
Age MetaDataModel::getAge() const
{
2019-10-05 14:14:45 +00:00
return {m_dateOfBirth, m_dateOfTest};
}