2018-05-24 18:35:16 +00:00
|
|
|
#include "CheckableTestModel.h"
|
|
|
|
|
|
|
|
#include <QJsonArray>
|
2018-08-26 12:24:05 +00:00
|
|
|
#include <QSize>
|
2018-05-24 18:35:16 +00:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
CheckableTestModel::CheckableTestModel(QObject *parent)
|
2019-02-02 13:44:01 +00:00
|
|
|
: QAbstractTableModel(parent)
|
2018-05-24 18:35:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-26 12:24:05 +00:00
|
|
|
int CheckableTestModel::rowCount(const QModelIndex &) const
|
2018-05-24 18:35:16 +00:00
|
|
|
{
|
2019-02-02 13:44:01 +00:00
|
|
|
return static_cast<int>(m_tests.size());
|
2018-05-24 18:35:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 12:24:05 +00:00
|
|
|
int CheckableTestModel::columnCount(const QModelIndex &) const
|
2018-05-24 18:35:16 +00:00
|
|
|
{
|
|
|
|
int columnCount = 0;
|
|
|
|
|
|
|
|
for (const auto &test : m_tests)
|
|
|
|
{
|
2019-02-02 13:44:01 +00:00
|
|
|
columnCount = std::max(columnCount, static_cast<int>(test.size()));
|
2018-05-24 18:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return columnCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant CheckableTestModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!isValidIndex(index))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &item = getItem(index);
|
|
|
|
|
2019-02-02 13:44:01 +00:00
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
{
|
|
|
|
return item.getText().c_str();
|
|
|
|
}
|
|
|
|
case Qt::CheckStateRole:
|
|
|
|
{
|
|
|
|
return item.isChecked() ? Qt::Checked : Qt::Unchecked;
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 18:35:16 +00:00
|
|
|
}
|
|
|
|
catch (std::runtime_error &e)
|
|
|
|
{
|
|
|
|
qDebug() << "CheckableTestModel::data" << index << e.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags CheckableTestModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (isValidIndex(index))
|
|
|
|
{
|
|
|
|
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qt::NoItemFlags;
|
|
|
|
}
|
|
|
|
|
2019-02-02 13:44:01 +00:00
|
|
|
bool CheckableTestModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
2018-05-24 18:35:16 +00:00
|
|
|
{
|
|
|
|
if (!isValidIndex(index))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (role == Qt::CheckStateRole)
|
|
|
|
{
|
|
|
|
auto &item = getItem(index);
|
|
|
|
item.setState(value.toBool());
|
2018-06-08 18:25:21 +00:00
|
|
|
emit dataChanged(index, index);
|
2018-05-24 18:35:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::runtime_error &e)
|
|
|
|
{
|
|
|
|
qDebug() << "CheckableTestModel::setData" << index << e.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-02 13:44:01 +00:00
|
|
|
QVariant CheckableTestModel::headerData(int section, Qt::Orientation orientation, int role) const
|
2018-05-24 18:35:16 +00:00
|
|
|
{
|
2019-02-02 13:44:01 +00:00
|
|
|
switch (orientation)
|
2018-05-24 18:35:16 +00:00
|
|
|
{
|
2019-02-02 13:44:01 +00:00
|
|
|
case Qt::Vertical:
|
2018-05-24 18:35:16 +00:00
|
|
|
{
|
2019-02-02 13:44:01 +00:00
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
{
|
|
|
|
if (section < m_tests.size())
|
|
|
|
{
|
|
|
|
return m_tests.at(section).name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Qt::SizeHintRole:
|
|
|
|
{
|
|
|
|
return QSize(200, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-05-24 18:35:16 +00:00
|
|
|
}
|
2019-02-02 13:44:01 +00:00
|
|
|
default:
|
|
|
|
break;
|
2018-05-24 18:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-02 13:44:01 +00:00
|
|
|
return QAbstractTableModel::headerData(section, orientation, role);
|
2018-05-24 18:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckableTestModel::isValidIndex(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.row() < m_tests.size())
|
|
|
|
{
|
|
|
|
return index.column() < m_tests.at(index.row()).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckableItems &CheckableTestModel::getItems(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
if (index.row() < m_tests.size())
|
|
|
|
{
|
|
|
|
return m_tests.at(index.row()).items();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("invalid index");
|
|
|
|
}
|
|
|
|
|
2019-02-02 13:44:01 +00:00
|
|
|
const CheckableItems &CheckableTestModel::getItems(const QModelIndex &index) const
|
2018-05-24 18:35:16 +00:00
|
|
|
{
|
|
|
|
if (index.row() < m_tests.size())
|
|
|
|
{
|
|
|
|
return m_tests.at(index.row()).items();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("invalid index");
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckableItem &CheckableTestModel::getItem(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
auto &items = getItems(index);
|
|
|
|
if (index.column() < items.size())
|
|
|
|
{
|
|
|
|
return items.at(index.column());
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("invalid index");
|
|
|
|
}
|
|
|
|
|
|
|
|
const CheckableItem &CheckableTestModel::getItem(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
auto &items = getItems(index);
|
|
|
|
if (index.column() < items.size())
|
|
|
|
{
|
|
|
|
return items.at(index.column());
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("invalid index");
|
|
|
|
}
|
2018-06-15 15:53:43 +00:00
|
|
|
|
|
|
|
unsigned int CheckableTestModel::getPoints() const
|
|
|
|
{
|
2019-02-02 13:44:01 +00:00
|
|
|
unsigned int points = 0;
|
2018-06-15 15:53:43 +00:00
|
|
|
|
|
|
|
for (const auto &test : m_tests)
|
|
|
|
{
|
|
|
|
for (const auto &item : test.items())
|
|
|
|
{
|
2018-06-29 17:31:28 +00:00
|
|
|
points += item.points();
|
2018-06-15 15:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return points;
|
|
|
|
}
|
2019-02-03 18:53:51 +00:00
|
|
|
|
|
|
|
QString CheckableTestModel::getTitle() const
|
|
|
|
{
|
|
|
|
return m_title;
|
|
|
|
}
|