notify result widget about plural test changes

This commit is contained in:
mandlm 2018-06-08 20:25:21 +02:00
parent 1f9a051382
commit 077efaac32
11 changed files with 107 additions and 5 deletions

5
source/Age.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
class Age
{
};

View File

@ -78,6 +78,7 @@ bool CheckableTestModel::setData(
{ {
auto &item = getItem(index); auto &item = getItem(index);
item.setState(value.toBool()); item.setState(value.toBool());
emit dataChanged(index, index);
return true; return true;
} }
} }

View File

@ -1,5 +1,7 @@
#include "DataModel.h" #include "DataModel.h"
#include <QDebug>
DataModel::DataModel(QObject *parent) DataModel::DataModel(QObject *parent)
: QObject(parent) : QObject(parent)
, m_metaData(this) , m_metaData(this)
@ -8,6 +10,11 @@ DataModel::DataModel(QObject *parent)
, m_plural(this) , m_plural(this)
, m_results(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 void DataModel::write(QJsonObject &target) const
@ -25,3 +32,12 @@ void DataModel::read(const QJsonObject &source)
read(m_genus, source, "Genus"); read(m_genus, source, "Genus");
read(m_plural, source, "Plural"); read(m_plural, source, "Plural");
} }
void DataModel::pluralModelChanged()
{
m_results.setPluralResult(m_plural.getPoints());
}
void DataModel::metaDataChanged()
{
}

View File

@ -47,4 +47,8 @@ private:
model.read(jsonObject.toObject()); model.read(jsonObject.toObject());
} }
} }
private slots:
void pluralModelChanged();
void metaDataChanged();
}; };

View File

@ -98,6 +98,11 @@ bool MetaDataModel::setData(
break; break;
} }
if (valueChanged)
{
emit dataChanged(index, index);
}
return valueChanged; return valueChanged;
} }

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "../Age.h"
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QString> #include <QString>
#include <QDate> #include <QDate>
@ -28,4 +30,9 @@ public:
void write(QJsonObject &json) const; void write(QJsonObject &json) const;
void read(const QJsonObject &json); void read(const QJsonObject &json);
Age getAge() const
{
return {};
}
}; };

View File

@ -5,7 +5,8 @@
ResultModel::ResultModel(QObject *parent) ResultModel::ResultModel(QObject *parent)
: QAbstractTableModel(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 int ResultModel::rowCount(const QModelIndex &parent) const
@ -22,7 +23,16 @@ QVariant ResultModel::data(const QModelIndex &index, int role) const
{ {
if (role == Qt::DisplayRole) 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<uint>(points);
}
}
return "-";
} }
return {}; return {};
@ -59,3 +69,17 @@ QVariant ResultModel::headerData(
return {}; 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));
}
}

View File

@ -2,22 +2,35 @@
#include <vector> #include <vector>
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QDate>
class TestResult class TestResult
{ {
private: private:
QString m_name; QString m_name;
size_t m_points;
public: public:
TestResult(const char *name) TestResult(const char *name)
: m_name(name) : m_name(name)
, m_points(0)
{ {
} }
void operator=(const size_t &points)
{
m_points = points;
}
const QString &name() const const QString &name() const
{ {
return m_name; return m_name;
} }
const size_t points() const
{
return m_points;
}
}; };
class ResultModel : public QAbstractTableModel class ResultModel : public QAbstractTableModel
@ -25,6 +38,7 @@ class ResultModel : public QAbstractTableModel
Q_OBJECT Q_OBJECT
private: private:
QDate m_age;
std::vector<TestResult> m_results; std::vector<TestResult> m_results;
public: public:
@ -38,4 +52,7 @@ public:
QVariant headerData(int section, Qt::Orientation orientation, QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const override; int role = Qt::DisplayRole) const override;
void setAge(const QDate &age);
void setPluralResult(size_t points);
}; };

View File

@ -4,6 +4,24 @@ PluralModel::PluralModel(QObject *parent)
: CheckableTestModel(parent) : CheckableTestModel(parent)
{ {
m_tests = { { "", 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" } } }; "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;
}

View File

@ -8,4 +8,9 @@ class PluralModel : public CheckableTestModel
public: public:
PluralModel(QObject *parent); PluralModel(QObject *parent);
size_t getPoints() const;
signals:
void resultChanged(size_t result) const;
}; };

View File

@ -6,7 +6,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>905</width> <width>1125</width>
<height>572</height> <height>572</height>
</rect> </rect>
</property> </property>
@ -89,7 +89,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>905</width> <width>1125</width>
<height>19</height> <height>19</height>
</rect> </rect>
</property> </property>