formatting

This commit is contained in:
Michael Mandl 2018-05-22 09:07:51 +02:00
parent 0937fc21e7
commit 0a1c570ffd
3 changed files with 142 additions and 163 deletions

View file

@ -19,85 +19,85 @@ 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 (!isValidIndex(index)) if (!isValidIndex(index))
{ {
return {}; return {};
} }
try try
{ {
auto &item = getItem(index); auto &item = getItem(index);
if (role == Qt::DisplayRole) if (role == Qt::DisplayRole)
{ {
return item.getText().c_str(); return item.getText().c_str();
} }
if (role == Qt::CheckStateRole) if (role == Qt::CheckStateRole)
{ {
return item.isChecked() ? Qt::Checked : Qt::Unchecked; return item.isChecked() ? Qt::Checked : Qt::Unchecked;
} }
} }
catch (std::runtime_error &e) catch (std::runtime_error &e)
{ {
qDebug() << "GenusModel::data" << index << e.what(); qDebug() << "GenusModel::data" << index << e.what();
} }
return {}; return {};
} }
Qt::ItemFlags GenusModel::flags(const QModelIndex &index) const Qt::ItemFlags GenusModel::flags(const QModelIndex &index) const
{ {
if (isValidIndex(index)) if (isValidIndex(index))
{ {
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable; return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
} }
return Qt::NoItemFlags; 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 (!isValidIndex(index)) if (!isValidIndex(index))
{ {
return false; return false;
} }
try try
{ {
if (role == Qt::CheckStateRole) if (role == Qt::CheckStateRole)
{ {
auto &item = getItem(index); auto &item = getItem(index);
item.setState(value.toBool()); item.setState(value.toBool());
return true; return true;
} }
} }
catch (std::runtime_error &e) catch (std::runtime_error &e)
{ {
qDebug() << "GenusModel::setData" << index << e.what(); qDebug() << "GenusModel::setData" << index << e.what();
} }
return false; return false;
} }
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 && orientation == Qt::Vertical) if (role == Qt::DisplayRole && orientation == Qt::Vertical)
{ {
switch (section) switch (section)
{ {
case 0: case 0:
return "Tiere"; return "Tiere";
case 1: case 1:
return "Futter"; return "Futter";
case 2: case 2:
return "Zirkus"; return "Zirkus";
default: default:
return {}; return {};
} }
} }
return {}; return {};
} }
@ -112,73 +112,74 @@ void GenusModel::read(const QJsonObject &json)
bool GenusModel::isValidIndex(const QModelIndex &index) const bool GenusModel::isValidIndex(const QModelIndex &index) const
{ {
switch (index.row()) switch (index.row())
{ {
case 0: case 0:
return m_tiere.find(index.column()) != m_tiere.end(); return m_tiere.find(index.column()) != m_tiere.end();
case 1: case 1:
return m_futter.find(index.column()) != m_futter.end(); return m_futter.find(index.column()) != m_futter.end();
case 2: case 2:
return m_zirkus.find(index.column()) != m_zirkus.end(); return m_zirkus.find(index.column()) != m_zirkus.end();
default: default:
return false; return false;
} }
} }
GenusModel::CheckableItems &GenusModel::getItems(const QModelIndex &index) GenusModel::CheckableItems &GenusModel::getItems(const QModelIndex &index)
{ {
switch (index.row()) switch (index.row())
{ {
case 0: case 0:
return m_tiere; return m_tiere;
case 1: case 1:
return m_futter; return m_futter;
case 2: case 2:
return m_zirkus; return m_zirkus;
default: default:
break; break;
} }
throw std::runtime_error("invalid index"); throw std::runtime_error("invalid index");
} }
const GenusModel::CheckableItems &GenusModel::getItems(const QModelIndex &index) const const GenusModel::CheckableItems &GenusModel::getItems(
const QModelIndex &index) const
{ {
switch (index.row()) switch (index.row())
{ {
case 0: case 0:
return m_tiere; return m_tiere;
case 1: case 1:
return m_futter; return m_futter;
case 2: case 2:
return m_zirkus; return m_zirkus;
default: default:
break; break;
} }
throw std::runtime_error("invalid index"); throw std::runtime_error("invalid index");
} }
CheckableItem &GenusModel::getItem(const QModelIndex &index) CheckableItem &GenusModel::getItem(const QModelIndex &index)
{ {
auto &items = getItems(index); auto &items = getItems(index);
auto entry = items.find(index.column()); auto entry = items.find(index.column());
if (entry != items.end()) if (entry != items.end())
{ {
return entry->second; return entry->second;
} }
throw std::runtime_error("invalid index"); throw std::runtime_error("invalid index");
} }
const CheckableItem &GenusModel::getItem(const QModelIndex &index) const const CheckableItem &GenusModel::getItem(const QModelIndex &index) const
{ {
auto &items = getItems(index); auto &items = getItems(index);
auto entry = items.find(index.column()); auto entry = items.find(index.column());
if (entry != items.end()) if (entry != items.end())
{ {
return entry->second; return entry->second;
} }
throw std::runtime_error("invalid index"); throw std::runtime_error("invalid index");
} }

View file

@ -5,29 +5,29 @@
class CheckableItem class CheckableItem
{ {
private: private:
bool m_checked = false; bool m_checked = false;
std::string m_text; std::string m_text;
public: public:
CheckableItem(const std::string &text) CheckableItem(const std::string &text)
: m_text(text) : m_text(text)
{ {
} }
std::string getText() const std::string getText() const
{ {
return m_text; return m_text;
} }
bool isChecked() const bool isChecked() const
{ {
return m_checked; return m_checked;
} }
void setState(bool checked) void setState(bool checked)
{ {
m_checked = checked; m_checked = checked;
} }
}; };
#include <QAbstractTableModel> #include <QAbstractTableModel>
@ -38,39 +38,18 @@ class GenusModel : public QAbstractTableModel
Q_OBJECT Q_OBJECT
private: private:
using CheckableItems = std::map<size_t, CheckableItem>; using CheckableItems = std::map<size_t, CheckableItem>;
CheckableItems m_tiere = CheckableItems m_tiere = {{0, {"Tiger"}}, {1, {"Bär"}}, {2, {"Katze"}},
{ {3, {"Pferd"}}, {4, {"Gans"}}, {5, {"Elefant"}}, {6, {"Katze"}},
{0, {"Tiger"}}, {7, {"Hund"}}};
{1, {"Bär"}},
{2, {"Katze"}},
{3, {"Pferd"}},
{4, {"Gans"}},
{5, {"Elefant"}},
{6, {"Katze"}},
{7, {"Hund"}}
};
CheckableItems m_futter = CheckableItems m_futter = {{0, {"Salat"}}, {1, {"Fleisch"}},
{ {2, {"Knocken"}}, {3, {"Banane"}}, {4, {"Apfel"}}, {5, {"Möhre"}},
{0, {"Salat"}}, {6, {"Honig"}}, {7, {"Zucker"}}};
{1, {"Fleisch"}},
{2, {"Knocken"}},
{3, {"Banane"}},
{4, {"Apfel"}},
{5, {"Möhre"}},
{6, {"Honig"}},
{7, {"Zucker"}}
};
CheckableItems m_zirkus = CheckableItems m_zirkus = {
{ {0, {"Kiste"}}, {1, {"Holz"}}, {2, {"Vorhang"}}, {3, {"Baum"}}};
{0, {"Kiste"}},
{1, {"Holz"}},
{2, {"Vorhang"}},
{3, {"Baum"}}
};
public: public:
GenusModel(QObject *parent); GenusModel(QObject *parent);
@ -89,12 +68,11 @@ public:
void read(const QJsonObject &json); void read(const QJsonObject &json);
private: private:
bool isValidIndex(const QModelIndex &index) const; bool isValidIndex(const QModelIndex &index) const;
CheckableItems &getItems(const QModelIndex &index); CheckableItems &getItems(const QModelIndex &index);
const CheckableItems &getItems(const QModelIndex &index) const; const CheckableItems &getItems(const QModelIndex &index) const;
CheckableItem &getItem(const QModelIndex &index); CheckableItem &getItem(const QModelIndex &index);
const CheckableItem &getItem(const QModelIndex &index) const; const CheckableItem &getItem(const QModelIndex &index) const;
}; };

View file

@ -5,7 +5,7 @@
class GenusModel; class GenusModel;
namespace Ui { namespace Ui {
class GenusWidget; class GenusWidget;
}; };
class GenusWidget : public QWidget class GenusWidget : public QWidget