This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
ESGRAF48/source/MetaData/MetaDataModel.cpp

154 lines
3.1 KiB
C++
Raw Normal View History

#include "MetaDataModel.h"
#include <QDebug>
MetaDataModel::MetaDataModel(QObject *parent) : QAbstractTableModel(parent)
{
m_dateOfBirth = QDate::currentDate().addYears(-9);
2018-05-20 10:04:21 +00:00
m_dateOfTest = QDate::currentDate();
}
int MetaDataModel::rowCount(const QModelIndex &parent) const
{
return 1;
}
int MetaDataModel::columnCount(const QModelIndex &parent) const
{
2018-06-14 17:52:19 +00:00
return 6;
}
2018-06-14 17:52:19 +00:00
QVariant MetaDataModel::data(const QModelIndex &modelIndex, int role) const
{
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
2018-06-14 17:52:19 +00:00
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;
2018-06-14 17:52:19 +00:00
case 5:
return QString::fromStdString(getAge().toString());
default:
return QVariant();
}
}
return QVariant();
}
2018-05-15 12:37:16 +00:00
2018-06-14 17:52:19 +00:00
Qt::ItemFlags MetaDataModel::flags(const QModelIndex &modelIndex) const
2018-05-17 17:42:35 +00:00
{
2018-06-14 17:52:19 +00:00
return QAbstractTableModel::flags(modelIndex) | Qt::ItemIsEditable;
2018-05-17 17:42:35 +00:00
}
bool MetaDataModel::setData(
2018-06-14 17:52:19 +00:00
const QModelIndex &modelIndex, const QVariant &value, int role)
2018-05-17 17:42:35 +00:00
{
if (role != Qt::EditRole)
{
2018-06-14 17:52:19 +00:00
return QAbstractTableModel::setData(modelIndex, value, role);
2018-05-17 17:42:35 +00:00
}
bool valueChanged = false;
2018-06-14 17:52:19 +00:00
switch (modelIndex.column())
2018-05-17 17:42:35 +00:00
{
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:
2018-05-18 18:36:48 +00:00
if (value.toDate() != m_dateOfBirth)
2018-05-17 17:42:35 +00:00
{
2018-05-18 18:36:48 +00:00
m_dateOfBirth = value.toDate();
2018-06-14 17:52:19 +00:00
emit dataChanged(index(0, 5), index(0, 5));
2018-05-17 17:42:35 +00:00
valueChanged = true;
}
break;
case 3:
2018-05-18 18:36:48 +00:00
if (value.toDate() != m_dateOfTest)
2018-05-17 17:42:35 +00:00
{
2018-05-18 18:36:48 +00:00
m_dateOfTest = value.toDate();
2018-06-14 17:52:19 +00:00
emit dataChanged(index(0, 5), index(0, 5));
2018-05-17 17:42:35 +00:00
valueChanged = true;
}
break;
case 4:
if (value.toString() != m_remarks)
{
m_remarks = value.toString();
valueChanged = true;
}
break;
default:
break;
}
if (valueChanged)
{
2018-06-14 17:52:19 +00:00
emit dataChanged(modelIndex, modelIndex);
}
2018-05-17 17:42:35 +00:00
return valueChanged;
}
2018-05-16 05:02:12 +00:00
void MetaDataModel::write(QJsonObject &json) const
2018-05-15 12:37:16 +00:00
{
json["participant name"] = m_participant;
json["instructor name"] = m_instructor;
2018-05-17 17:42:35 +00:00
json["date of birth"] = m_dateOfBirth.toString(Qt::ISODate);
json["date of test"] = m_dateOfTest.toString(Qt::ISODate);
2018-05-15 12:37:16 +00:00
json["remarks"] = m_remarks;
}
void MetaDataModel::read(const QJsonObject &json)
{
const auto &participant = json["participant name"];
if (participant.isString())
{
m_participant = participant.toString();
}
const auto &instructor = json["instructor name"];
if (instructor.isString())
{
m_instructor = instructor.toString();
}
const auto &dateOfBirth = json["date of birth"];
if (dateOfBirth.isString())
{
2018-05-17 17:42:35 +00:00
m_dateOfBirth = QDate::fromString(dateOfBirth.toString(), Qt::ISODate);
2018-05-15 12:37:16 +00:00
}
const auto &dateOfTest = json["date of test"];
if (dateOfTest.isString())
{
2018-05-17 17:42:35 +00:00
m_dateOfTest = QDate::fromString(dateOfTest.toString(), Qt::ISODate);
2018-05-15 12:37:16 +00:00
}
const auto &remarks = json["remarks"];
if (remarks.isString())
{
m_remarks = remarks.toString();
}
}