From da2aa8905920e1d0127aeedda28cf7ee9ddbda60 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 9 Jun 2018 13:13:00 +0200 Subject: [PATCH] added age to result --- source/Age.cpp | 48 +++++++++++++++++++++++++++++ source/Age.h | 13 ++++++++ source/CMakeLists.txt | 1 + source/DataModel.cpp | 1 + source/MetaData/MetaDataModel.h | 2 +- source/ResultWidget/ResultModel.cpp | 3 +- source/ResultWidget/ResultModel.h | 6 ++-- 7 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 source/Age.cpp diff --git a/source/Age.cpp b/source/Age.cpp new file mode 100644 index 0000000..1069357 --- /dev/null +++ b/source/Age.cpp @@ -0,0 +1,48 @@ +#include "Age.h" + +#include + +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; +} diff --git a/source/Age.h b/source/Age.h index bdaccae..93b4e5e 100644 --- a/source/Age.h +++ b/source/Age.h @@ -1,5 +1,18 @@ #pragma once +#include + 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; }; diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 8c0dd44..d7525a5 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable(${PROJECT_NAME} LogoTest.cpp DataModel.cpp mainwindow.cpp + Age.cpp ${LOGO_TEST_UI} ${LOGO_TEST_QRC} ) diff --git a/source/DataModel.cpp b/source/DataModel.cpp index 2cf7723..7cde0b9 100644 --- a/source/DataModel.cpp +++ b/source/DataModel.cpp @@ -40,4 +40,5 @@ void DataModel::pluralModelChanged() void DataModel::metaDataChanged() { + m_results.setAge(m_metaData.getAge()); } diff --git a/source/MetaData/MetaDataModel.h b/source/MetaData/MetaDataModel.h index 7a6e80f..8d5f614 100644 --- a/source/MetaData/MetaDataModel.h +++ b/source/MetaData/MetaDataModel.h @@ -33,6 +33,6 @@ public: Age getAge() const { - return {}; + return { m_dateOfBirth, m_dateOfTest }; } }; diff --git a/source/ResultWidget/ResultModel.cpp b/source/ResultWidget/ResultModel.cpp index c6aa500..6eb8216 100644 --- a/source/ResultWidget/ResultModel.cpp +++ b/source/ResultWidget/ResultModel.cpp @@ -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; } diff --git a/source/ResultWidget/ResultModel.h b/source/ResultWidget/ResultModel.h index 7b65006..f626d3b 100644 --- a/source/ResultWidget/ResultModel.h +++ b/source/ResultWidget/ResultModel.h @@ -1,8 +1,8 @@ #pragma once +#include "../Age.h" #include #include -#include class TestResult { @@ -38,7 +38,7 @@ class ResultModel : public QAbstractTableModel Q_OBJECT private: - QDate m_age; + Age m_age; std::vector 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); };