formatting

This commit is contained in:
mandlm 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
{
if (!isValidIndex(index))
{
return {};
}
if (!isValidIndex(index))
{
return {};
}
try
{
auto &item = getItem(index);
try
{
auto &item = getItem(index);
if (role == Qt::DisplayRole)
{
return item.getText().c_str();
}
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();
}
if (role == Qt::CheckStateRole)
{
return item.isChecked() ? Qt::Checked : Qt::Unchecked;
}
}
catch (std::runtime_error &e)
{
qDebug() << "GenusModel::data" << index << e.what();
}
return {};
return {};
}
Qt::ItemFlags GenusModel::flags(const QModelIndex &index) const
{
if (isValidIndex(index))
{
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
}
if (isValidIndex(index))
{
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
}
return Qt::NoItemFlags;
return Qt::NoItemFlags;
}
bool GenusModel::setData(
const QModelIndex &index, const QVariant &value, int role)
{
if (!isValidIndex(index))
{
return false;
}
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();
}
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;
return false;
}
QVariant GenusModel::headerData(
int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Vertical)
{
switch (section)
{
case 0:
return "Tiere";
case 1:
return "Futter";
case 2:
return "Zirkus";
default:
return {};
}
}
if (role == Qt::DisplayRole && orientation == Qt::Vertical)
{
switch (section)
{
case 0:
return "Tiere";
case 1:
return "Futter";
case 2:
return "Zirkus";
default:
return {};
}
}
return {};
}
@ -112,73 +112,74 @@ 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;
}
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;
}
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");
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())
{
case 0:
return m_tiere;
case 1:
return m_futter;
case 2:
return m_zirkus;
default:
break;
}
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");
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;
}
auto &items = getItems(index);
auto entry = items.find(index.column());
if (entry != items.end())
{
return entry->second;
}
throw std::runtime_error("invalid index");
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;
}
auto &items = getItems(index);
auto entry = items.find(index.column());
if (entry != items.end())
{
return entry->second;
}
throw std::runtime_error("invalid index");
throw std::runtime_error("invalid index");
}

View File

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

View File

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