From 077efaac329ba9518ce3359195061dc0ffec51de Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Fri, 8 Jun 2018 20:25:21 +0200 Subject: [PATCH] notify result widget about plural test changes --- source/Age.h | 5 ++++ .../CheckableTestModel/CheckableTestModel.cpp | 1 + source/DataModel.cpp | 16 +++++++++++ source/DataModel.h | 4 +++ source/MetaData/MetaDataModel.cpp | 5 ++++ source/MetaData/MetaDataModel.h | 7 +++++ source/ResultWidget/ResultModel.cpp | 28 +++++++++++++++++-- source/ResultWidget/ResultModel.h | 17 +++++++++++ source/SubTests/Plural/PluralModel.cpp | 20 ++++++++++++- source/SubTests/Plural/PluralModel.h | 5 ++++ source/mainwindow.ui | 4 +-- 11 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 source/Age.h diff --git a/source/Age.h b/source/Age.h new file mode 100644 index 0000000..bdaccae --- /dev/null +++ b/source/Age.h @@ -0,0 +1,5 @@ +#pragma once + +class Age +{ +}; diff --git a/source/CheckableTestModel/CheckableTestModel.cpp b/source/CheckableTestModel/CheckableTestModel.cpp index f8df7ef..b4ae54a 100644 --- a/source/CheckableTestModel/CheckableTestModel.cpp +++ b/source/CheckableTestModel/CheckableTestModel.cpp @@ -78,6 +78,7 @@ bool CheckableTestModel::setData( { auto &item = getItem(index); item.setState(value.toBool()); + emit dataChanged(index, index); return true; } } diff --git a/source/DataModel.cpp b/source/DataModel.cpp index ef2ba72..2cf7723 100644 --- a/source/DataModel.cpp +++ b/source/DataModel.cpp @@ -1,5 +1,7 @@ #include "DataModel.h" +#include + DataModel::DataModel(QObject *parent) : QObject(parent) , m_metaData(this) @@ -8,6 +10,11 @@ DataModel::DataModel(QObject *parent) , m_plural(this) , m_results(this) { + connect(&m_plural, &PluralModel::dataChanged, + this, &DataModel::pluralModelChanged); + + connect(&m_metaData, &PluralModel::dataChanged, + this, &DataModel::metaDataChanged); } void DataModel::write(QJsonObject &target) const @@ -25,3 +32,12 @@ void DataModel::read(const QJsonObject &source) read(m_genus, source, "Genus"); read(m_plural, source, "Plural"); } + +void DataModel::pluralModelChanged() +{ + m_results.setPluralResult(m_plural.getPoints()); +} + +void DataModel::metaDataChanged() +{ +} diff --git a/source/DataModel.h b/source/DataModel.h index a7737a2..295eede 100644 --- a/source/DataModel.h +++ b/source/DataModel.h @@ -47,4 +47,8 @@ private: model.read(jsonObject.toObject()); } } + +private slots: + void pluralModelChanged(); + void metaDataChanged(); }; diff --git a/source/MetaData/MetaDataModel.cpp b/source/MetaData/MetaDataModel.cpp index cd70d41..6b873b5 100644 --- a/source/MetaData/MetaDataModel.cpp +++ b/source/MetaData/MetaDataModel.cpp @@ -98,6 +98,11 @@ bool MetaDataModel::setData( break; } + if (valueChanged) + { + emit dataChanged(index, index); + } + return valueChanged; } diff --git a/source/MetaData/MetaDataModel.h b/source/MetaData/MetaDataModel.h index 94de6a3..7a6e80f 100644 --- a/source/MetaData/MetaDataModel.h +++ b/source/MetaData/MetaDataModel.h @@ -1,5 +1,7 @@ #pragma once +#include "../Age.h" + #include #include #include @@ -28,4 +30,9 @@ public: void write(QJsonObject &json) const; void read(const QJsonObject &json); + + Age getAge() const + { + return {}; + } }; diff --git a/source/ResultWidget/ResultModel.cpp b/source/ResultWidget/ResultModel.cpp index 3b997b4..cd0dadd 100644 --- a/source/ResultWidget/ResultModel.cpp +++ b/source/ResultWidget/ResultModel.cpp @@ -5,7 +5,8 @@ ResultModel::ResultModel(QObject *parent) : QAbstractTableModel(parent) { - m_results = {{ "V2", "SVK", "VE", "Passiv", "Genus", "Akkusativ", "Dativ", "Genitiv", "Plural" }}; + m_results = { { "V2", "SVK", "VE", "Passiv", "Genus", "Akkusativ", "Dativ", + "Genitiv", "Plural" } }; } int ResultModel::rowCount(const QModelIndex &parent) const @@ -22,7 +23,16 @@ QVariant ResultModel::data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole) { - return "data"; + if (index.column() < m_results.size()) + { + size_t points = m_results[index.column()].points(); + if (points != 0) + { + return static_cast(points); + } + } + + return "-"; } return {}; @@ -59,3 +69,17 @@ QVariant ResultModel::headerData( return {}; } } + +void ResultModel::setAge(const QDate &age) +{ + m_age = age; +} + +void ResultModel::setPluralResult(size_t points) +{ + if (m_results[8].points() != points) + { + m_results[8] = points; + emit dataChanged(index(0, 8), index(2, 8)); + } +} diff --git a/source/ResultWidget/ResultModel.h b/source/ResultWidget/ResultModel.h index e3b0f54..7b65006 100644 --- a/source/ResultWidget/ResultModel.h +++ b/source/ResultWidget/ResultModel.h @@ -2,22 +2,35 @@ #include #include +#include class TestResult { private: QString m_name; + size_t m_points; public: TestResult(const char *name) : m_name(name) + , m_points(0) { } + void operator=(const size_t &points) + { + m_points = points; + } + const QString &name() const { return m_name; } + + const size_t points() const + { + return m_points; + } }; class ResultModel : public QAbstractTableModel @@ -25,6 +38,7 @@ class ResultModel : public QAbstractTableModel Q_OBJECT private: + QDate m_age; std::vector m_results; public: @@ -38,4 +52,7 @@ public: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + + void setAge(const QDate &age); + void setPluralResult(size_t points); }; diff --git a/source/SubTests/Plural/PluralModel.cpp b/source/SubTests/Plural/PluralModel.cpp index 20af866..d5bb899 100644 --- a/source/SubTests/Plural/PluralModel.cpp +++ b/source/SubTests/Plural/PluralModel.cpp @@ -4,6 +4,24 @@ PluralModel::PluralModel(QObject *parent) : CheckableTestModel(parent) { m_tests = { { "", - { "Fisch /-e/", "Banane /-n/", "Bonbon /-s/", "Ei /-er/", "Eimer /-o/", + { "Fisch /-e/", "Banane /-n/", "Bonbon /-s/", "Ei /-er/", "Eimer /-ø/", "Korn UML+/-er/", "Nuss UML+/-e/", "Bär /-en/", "Apfel UML" } } }; } + +size_t PluralModel::getPoints() const +{ + size_t points = 0; + + for (const auto &test : m_tests) + { + for (const auto &item : test.items()) + { + if (item.isChecked()) + { + points++; + } + } + } + + return points; +} diff --git a/source/SubTests/Plural/PluralModel.h b/source/SubTests/Plural/PluralModel.h index 60427c0..5d7273d 100644 --- a/source/SubTests/Plural/PluralModel.h +++ b/source/SubTests/Plural/PluralModel.h @@ -8,4 +8,9 @@ class PluralModel : public CheckableTestModel public: PluralModel(QObject *parent); + + size_t getPoints() const; + +signals: + void resultChanged(size_t result) const; }; diff --git a/source/mainwindow.ui b/source/mainwindow.ui index 45a66d0..31baf67 100644 --- a/source/mainwindow.ui +++ b/source/mainwindow.ui @@ -6,7 +6,7 @@ 0 0 - 905 + 1125 572 @@ -89,7 +89,7 @@ 0 0 - 905 + 1125 19