added genus model items

This commit is contained in:
mandlm 2018-05-22 10:57:25 +02:00
parent 59ff9712d8
commit 0937fc21e7
2 changed files with 168 additions and 45 deletions

View File

@ -1,5 +1,7 @@
#include "GenusModel.h"
#include <QDebug>
GenusModel::GenusModel(QObject *parent)
: QAbstractTableModel(parent)
{
@ -17,51 +19,64 @@ int GenusModel::columnCount(const QModelIndex &parent) const
QVariant GenusModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
if (!isValidIndex(index))
{
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end())
{
return m_tiere.find(index.column())->second.getText().c_str();
return {};
}
return "Hello";
try
{
auto &item = getItem(index);
if (role == Qt::DisplayRole)
{
return item.getText().c_str();
}
if (role == Qt::CheckStateRole)
{
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end())
return item.isChecked() ? Qt::Checked : Qt::Unchecked;
}
}
catch (std::runtime_error &e)
{
return m_tiere.find(index.column())->second.isChecked() ? Qt::Checked : Qt::Unchecked;
qDebug() << "GenusModel::data" << index << e.what();
}
return Qt::Unchecked;
}
return QVariant();
return {};
}
Qt::ItemFlags GenusModel::flags(const QModelIndex &index) const
{
return QAbstractTableModel::flags(index) | Qt::ItemIsEnabled
| Qt::ItemIsUserCheckable;
if (isValidIndex(index))
{
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
}
return Qt::NoItemFlags;
}
bool GenusModel::setData(
const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::CheckStateRole)
if (!isValidIndex(index))
{
return QAbstractTableModel::setData(index, value, role);
return false;
}
else
{
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end())
{
m_tiere.find(index.column())->second.setState(value.toBool());
try
{
if (role == Qt::CheckStateRole)
{
auto &item = getItem(index);
item.setState(value.toBool());
return true;
}
}
catch (std::runtime_error &e)
{
qDebug() << "GenusModel::setData" << index << e.what();
}
return false;
}
@ -69,9 +84,7 @@ bool GenusModel::setData(
QVariant GenusModel::headerData(
int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Vertical)
if (role == Qt::DisplayRole && orientation == Qt::Vertical)
{
switch (section)
{
@ -85,7 +98,6 @@ QVariant GenusModel::headerData(
return {};
}
}
}
return {};
}
@ -97,3 +109,76 @@ void GenusModel::write(QJsonObject &json) const
void GenusModel::read(const QJsonObject &json)
{
}
bool GenusModel::isValidIndex(const QModelIndex &index) const
{
switch (index.row())
{
case 0:
return m_tiere.find(index.column()) != m_tiere.end();
case 1:
return m_futter.find(index.column()) != m_futter.end();
case 2:
return m_zirkus.find(index.column()) != m_zirkus.end();
default:
return false;
}
}
GenusModel::CheckableItems &GenusModel::getItems(const QModelIndex &index)
{
switch (index.row())
{
case 0:
return m_tiere;
case 1:
return m_futter;
case 2:
return m_zirkus;
default:
break;
}
throw std::runtime_error("invalid index");
}
const GenusModel::CheckableItems &GenusModel::getItems(const QModelIndex &index) const
{
switch (index.row())
{
case 0:
return m_tiere;
case 1:
return m_futter;
case 2:
return m_zirkus;
default:
break;
}
throw std::runtime_error("invalid index");
}
CheckableItem &GenusModel::getItem(const QModelIndex &index)
{
auto &items = getItems(index);
auto entry = items.find(index.column());
if (entry != items.end())
{
return entry->second;
}
throw std::runtime_error("invalid index");
}
const CheckableItem &GenusModel::getItem(const QModelIndex &index) const
{
auto &items = getItems(index);
auto entry = items.find(index.column());
if (entry != items.end())
{
return entry->second;
}
throw std::runtime_error("invalid index");
}

View File

@ -38,10 +38,38 @@ class GenusModel : public QAbstractTableModel
Q_OBJECT
private:
std::map<size_t, CheckableItem> m_tiere =
using CheckableItems = std::map<size_t, CheckableItem>;
CheckableItems m_tiere =
{
{0, {"null"}},
{1, {"eins"}}
{0, {"Tiger"}},
{1, {"Bär"}},
{2, {"Katze"}},
{3, {"Pferd"}},
{4, {"Gans"}},
{5, {"Elefant"}},
{6, {"Katze"}},
{7, {"Hund"}}
};
CheckableItems m_futter =
{
{0, {"Salat"}},
{1, {"Fleisch"}},
{2, {"Knocken"}},
{3, {"Banane"}},
{4, {"Apfel"}},
{5, {"Möhre"}},
{6, {"Honig"}},
{7, {"Zucker"}}
};
CheckableItems m_zirkus =
{
{0, {"Kiste"}},
{1, {"Holz"}},
{2, {"Vorhang"}},
{3, {"Baum"}}
};
public:
@ -59,4 +87,14 @@ public:
void write(QJsonObject &json) const;
void read(const QJsonObject &json);
private:
bool isValidIndex(const QModelIndex &index) const;
CheckableItems &getItems(const QModelIndex &index);
const CheckableItems &getItems(const QModelIndex &index) const;
CheckableItem &getItem(const QModelIndex &index);
const CheckableItem &getItem(const QModelIndex &index) const;
};