added genus model items

pull/12/head
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 "GenusModel.h"
#include <QDebug>
GenusModel::GenusModel(QObject *parent) GenusModel::GenusModel(QObject *parent)
: QAbstractTableModel(parent) : QAbstractTableModel(parent)
{ {
@ -17,51 +19,64 @@ int GenusModel::columnCount(const QModelIndex &parent) const
QVariant GenusModel::data(const QModelIndex &index, int role) 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 {};
}
try
{
auto &item = getItem(index);
if (role == Qt::DisplayRole)
{ {
return m_tiere.find(index.column())->second.getText().c_str(); return item.getText().c_str();
} }
return "Hello"; if (role == Qt::CheckStateRole)
}
if (role == Qt::CheckStateRole)
{
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end())
{ {
return m_tiere.find(index.column())->second.isChecked() ? Qt::Checked : Qt::Unchecked; return item.isChecked() ? Qt::Checked : Qt::Unchecked;
} }
}
catch (std::runtime_error &e)
{
qDebug() << "GenusModel::data" << index << e.what();
}
return Qt::Unchecked; return {};
}
return QVariant();
} }
Qt::ItemFlags GenusModel::flags(const QModelIndex &index) const Qt::ItemFlags GenusModel::flags(const QModelIndex &index) const
{ {
return QAbstractTableModel::flags(index) | Qt::ItemIsEnabled if (isValidIndex(index))
| Qt::ItemIsUserCheckable; {
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
}
return Qt::NoItemFlags;
} }
bool GenusModel::setData( bool GenusModel::setData(
const QModelIndex &index, const QVariant &value, int role) const QModelIndex &index, const QVariant &value, int role)
{ {
if (role != Qt::CheckStateRole) if (!isValidIndex(index))
{
return QAbstractTableModel::setData(index, value, role);
}
else
{ {
if (index.row() == 0 && m_tiere.find(index.column()) != m_tiere.end()) return false;
{ }
m_tiere.find(index.column())->second.setState(value.toBool());
try
{
if (role == Qt::CheckStateRole)
{
auto &item = getItem(index);
item.setState(value.toBool());
return true; return true;
} }
} }
catch (std::runtime_error &e)
{
qDebug() << "GenusModel::setData" << index << e.what();
}
return false; return false;
} }
@ -69,23 +84,20 @@ bool GenusModel::setData(
QVariant GenusModel::headerData( QVariant GenusModel::headerData(
int section, Qt::Orientation orientation, int role) const int section, Qt::Orientation orientation, int role) const
{ {
if (role == Qt::DisplayRole) if (role == Qt::DisplayRole && orientation == Qt::Vertical)
{ {
if (orientation == Qt::Vertical) switch (section)
{ {
switch (section) case 0:
{ return "Tiere";
case 0: case 1:
return "Tiere"; return "Futter";
case 1: case 2:
return "Futter"; return "Zirkus";
case 2: default:
return "Zirkus"; return {};
default: }
return {}; }
}
}
}
return {}; return {};
} }
@ -97,3 +109,76 @@ void GenusModel::write(QJsonObject &json) const
void GenusModel::read(const QJsonObject &json) 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 Q_OBJECT
private: private:
std::map<size_t, CheckableItem> m_tiere = using CheckableItems = std::map<size_t, CheckableItem>;
CheckableItems m_tiere =
{ {
{0, {"null"}}, {0, {"Tiger"}},
{1, {"eins"}} {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: public:
@ -59,4 +87,14 @@ public:
void write(QJsonObject &json) const; void write(QJsonObject &json) const;
void read(const QJsonObject &json); 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;
}; };