added value to CheckableItem, implemented exclusive checking in PassivModel

pull/12/head
mandlm 2018-06-29 19:31:28 +02:00
parent 0a1eea0fe2
commit 02eaa7be16
6 changed files with 55 additions and 9 deletions

View File

@ -20,6 +20,21 @@ void CheckableItem::setState(bool checked)
m_checked = checked;
}
unsigned int CheckableItem::value() const
{
return m_value;
}
void CheckableItem::setValue(unsigned int value)
{
m_value = value;
}
unsigned int CheckableItem::points() const
{
return m_checked ? m_value : 0;
}
void CheckableItem::write(QJsonObject &json) const
{
json["text"] = m_text.c_str();

View File

@ -9,15 +9,22 @@ class CheckableItem
private:
bool m_checked = false;
std::string m_text;
unsigned int m_value = 1;
public:
CheckableItem() = default;
CheckableItem(const std::string &text);
std::string getText() const;
bool isChecked() const;
void setState(bool checked);
unsigned int value() const;
void setValue(unsigned int value);
unsigned int points() const;
void write(QJsonObject &json) const;
void read(const QJsonObject &json);
};

View File

@ -20,4 +20,3 @@ public:
};
using CheckableTests = std::vector<CheckableTest>;

View File

@ -187,10 +187,7 @@ unsigned int CheckableTestModel::getPoints() const
{
for (const auto &item : test.items())
{
if (item.isChecked())
{
points++;
}
points += item.points();
}
}

View File

@ -3,7 +3,33 @@
PassivModel::PassivModel(QObject *parent)
: CheckableTestModel(parent)
{
m_tests = {
{ "Passiv", { "Elefant", "Pferde", "Bälle", "Ball", "Fleisch" } }
};
m_tests = { { "Passiv",
{ "Elefant (1)", "Elefant (2)", "Pferde (1)", "Pferde (2)", "Bälle (1)",
"Bälle (2)", "Ball (1)", "Ball (2)", "Fleisch (1)",
"Fleisch (2)" } } };
for (auto index : { 1, 3, 5, 7, 9 })
{
m_tests[0].items()[index].setValue(2);
}
}
bool PassivModel::setData(
const QModelIndex &modelIndex, const QVariant &value, int role)
{
if (role == Qt::CheckStateRole && value.toBool() == true)
{
if (modelIndex.column() % 2 == 0)
{
CheckableTestModel::setData(
index(modelIndex.row(), modelIndex.column() + 1), false, role);
}
else
{
CheckableTestModel::setData(
index(modelIndex.row(), modelIndex.column() - 1), false, role);
}
}
return CheckableTestModel::setData(modelIndex, value, role);
}

View File

@ -7,5 +7,7 @@ class PassivModel : public CheckableTestModel
Q_OBJECT
public:
PassivModel(QObject *parent);
PassivModel(QObject *parent);
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
};