added age to result

pull/12/head
mandlm 2018-06-09 13:13:00 +02:00
parent bc7be14c65
commit da2aa89059
7 changed files with 69 additions and 5 deletions

48
source/Age.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "Age.h"
#include <QDebug>
Age::Age(unsigned int years, unsigned int months)
: m_years(years)
, m_months(months)
{
}
Age::Age(const QDate &birth, const QDate &reference)
{
if (reference < birth)
{
qDebug() << "test (" << reference << ") before birth (" << birth << ")";
m_years = 0;
m_months = 0;
return;
}
int years = reference.year() - birth.year();
int months = reference.month() - birth.month();
if (months == 0 && reference.day() < birth.day())
{
months--;
}
if (months < 0)
{
years--;
months = (months + 12) % 12;
}
m_years = years;
m_months = months;
}
unsigned int Age::years() const
{
return m_years;
}
unsigned int Age::months() const
{
return m_months;
}

View File

@ -1,5 +1,18 @@
#pragma once
#include <QDate>
class Age
{
private:
unsigned int m_years = 0;
unsigned int m_months = 0;
public:
Age() = default;
Age(unsigned int years, unsigned int months);
Age(const QDate &birth, const QDate &reference);
unsigned int years() const;
unsigned int months() const;
};

View File

@ -19,6 +19,7 @@ add_executable(${PROJECT_NAME}
LogoTest.cpp
DataModel.cpp
mainwindow.cpp
Age.cpp
${LOGO_TEST_UI}
${LOGO_TEST_QRC}
)

View File

@ -40,4 +40,5 @@ void DataModel::pluralModelChanged()
void DataModel::metaDataChanged()
{
m_results.setAge(m_metaData.getAge());
}

View File

@ -33,6 +33,6 @@ public:
Age getAge() const
{
return {};
return { m_dateOfBirth, m_dateOfTest };
}
};

View File

@ -80,8 +80,9 @@ QVariant ResultModel::headerData(
}
}
void ResultModel::setAge(const QDate &age)
void ResultModel::setAge(const Age &age)
{
qDebug() << "Age:" << age.years() << "years" << age.months() << "months";
m_age = age;
}

View File

@ -1,8 +1,8 @@
#pragma once
#include "../Age.h"
#include <vector>
#include <QAbstractTableModel>
#include <QDate>
class TestResult
{
@ -38,7 +38,7 @@ class ResultModel : public QAbstractTableModel
Q_OBJECT
private:
QDate m_age;
Age m_age;
std::vector<TestResult> m_results;
public:
@ -53,6 +53,6 @@ public:
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const override;
void setAge(const QDate &age);
void setAge(const Age &age);
void setPluralResult(size_t points);
};