2018-05-22 03:55:46 +00:00
|
|
|
#include "GenusModel.h"
|
|
|
|
|
2018-05-23 09:08:50 +00:00
|
|
|
#include <QJsonArray>
|
2018-05-22 08:57:25 +00:00
|
|
|
#include <QDebug>
|
|
|
|
|
2018-05-22 03:55:46 +00:00
|
|
|
GenusModel::GenusModel(QObject *parent)
|
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int GenusModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GenusModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant GenusModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2018-05-22 07:07:51 +00:00
|
|
|
if (!isValidIndex(index))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &item = getItem(index);
|
|
|
|
|
|
|
|
if (role == Qt::DisplayRole)
|
|
|
|
{
|
|
|
|
return item.getText().c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (role == Qt::CheckStateRole)
|
|
|
|
{
|
|
|
|
return item.isChecked() ? Qt::Checked : Qt::Unchecked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::runtime_error &e)
|
|
|
|
{
|
|
|
|
qDebug() << "GenusModel::data" << index << e.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2018-05-22 03:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags GenusModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
2018-05-22 07:07:51 +00:00
|
|
|
if (isValidIndex(index))
|
|
|
|
{
|
|
|
|
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
|
|
|
|
}
|
2018-05-22 08:57:25 +00:00
|
|
|
|
2018-05-22 07:07:51 +00:00
|
|
|
return Qt::NoItemFlags;
|
2018-05-22 03:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GenusModel::setData(
|
|
|
|
const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
2018-05-22 07:07:51 +00:00
|
|
|
if (!isValidIndex(index))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-05-22 03:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant GenusModel::headerData(
|
|
|
|
int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
2018-05-22 07:07:51 +00:00
|
|
|
if (role == Qt::DisplayRole && orientation == Qt::Vertical)
|
|
|
|
{
|
|
|
|
switch (section)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return "Tiere";
|
|
|
|
case 1:
|
|
|
|
return "Futter";
|
|
|
|
case 2:
|
|
|
|
return "Zirkus";
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 03:55:46 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenusModel::write(QJsonObject &json) const
|
|
|
|
{
|
2018-05-23 15:47:04 +00:00
|
|
|
QJsonArray tiere;
|
2018-05-22 18:23:03 +00:00
|
|
|
m_tiere.write(tiere);
|
|
|
|
json["Tiere"] = tiere;
|
|
|
|
|
2018-05-23 15:47:04 +00:00
|
|
|
QJsonArray futter;
|
2018-05-22 18:23:03 +00:00
|
|
|
m_futter.write(futter);
|
|
|
|
json["Futter"] = futter;
|
|
|
|
|
2018-05-23 15:47:04 +00:00
|
|
|
QJsonArray zirkus;
|
2018-05-22 18:23:03 +00:00
|
|
|
m_zirkus.write(zirkus);
|
|
|
|
json["Zirkus"] = zirkus;
|
2018-05-22 03:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GenusModel::read(const QJsonObject &json)
|
|
|
|
{
|
2018-05-23 15:47:04 +00:00
|
|
|
if (json["Tiere"].isArray())
|
|
|
|
{
|
|
|
|
m_tiere.read(json["Tiere"].toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json["Futter"].isArray())
|
|
|
|
{
|
|
|
|
m_futter.read(json["Futter"].toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json["Zirkus"].isArray())
|
|
|
|
{
|
|
|
|
m_zirkus.read(json["Zirkus"].toArray());
|
|
|
|
}
|
2018-05-22 03:55:46 +00:00
|
|
|
}
|
2018-05-22 08:57:25 +00:00
|
|
|
|
|
|
|
bool GenusModel::isValidIndex(const QModelIndex &index) const
|
|
|
|
{
|
2018-05-22 07:07:51 +00:00
|
|
|
switch (index.row())
|
|
|
|
{
|
|
|
|
case 0:
|
2018-05-23 15:47:04 +00:00
|
|
|
return index.column() < m_tiere.size();
|
2018-05-22 07:07:51 +00:00
|
|
|
case 1:
|
2018-05-23 15:47:04 +00:00
|
|
|
return index.column() < m_futter.size();
|
2018-05-22 07:07:51 +00:00
|
|
|
case 2:
|
2018-05-23 15:47:04 +00:00
|
|
|
return index.column() < m_zirkus.size();
|
2018-05-22 07:07:51 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-22 08:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 18:23:03 +00:00
|
|
|
CheckableItems &GenusModel::getItems(const QModelIndex &index)
|
2018-05-22 08:57:25 +00:00
|
|
|
{
|
2018-05-22 07:07:51 +00:00
|
|
|
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");
|
2018-05-22 08:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 18:23:03 +00:00
|
|
|
const CheckableItems &GenusModel::getItems(const QModelIndex &index) const
|
2018-05-22 08:57:25 +00:00
|
|
|
{
|
2018-05-22 07:07:51 +00:00
|
|
|
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");
|
2018-05-22 08:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CheckableItem &GenusModel::getItem(const QModelIndex &index)
|
|
|
|
{
|
2018-05-22 07:07:51 +00:00
|
|
|
auto &items = getItems(index);
|
2018-05-23 15:47:04 +00:00
|
|
|
if (index.column() < items.size())
|
|
|
|
{
|
|
|
|
return items.at(index.column());
|
|
|
|
}
|
2018-05-22 07:07:51 +00:00
|
|
|
|
|
|
|
throw std::runtime_error("invalid index");
|
2018-05-22 08:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const CheckableItem &GenusModel::getItem(const QModelIndex &index) const
|
|
|
|
{
|
2018-05-23 15:47:04 +00:00
|
|
|
auto &items = getItems(index);
|
|
|
|
if (index.column() < items.size())
|
|
|
|
{
|
|
|
|
return items.at(index.column());
|
|
|
|
}
|
2018-05-22 07:07:51 +00:00
|
|
|
|
|
|
|
throw std::runtime_error("invalid index");
|
2018-05-22 08:57:25 +00:00
|
|
|
}
|