test_sdk/script/gtl_scr_specs_model.cpp

100 lines
2.6 KiB
C++
Raw Normal View History

#include "gtl_scr_specs_model.h"
namespace gtl
{
namespace scr
{
specs_model::specs_model(QObject *parent)
: QAbstractListModel(parent)
{
}
int specs_model::rowCount(const QModelIndex &parent) const
{
return _specs.size();
}
Qt::ItemFlags specs_model::flags(const QModelIndex &index) const
{
return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable;
}
QVariant specs_model::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if(role == Qt::DisplayRole)
return _specs[index.row()]->name();
else if(role == Qt::ForegroundRole)
return QBrush(_specs[index.row()]->color());
if(role == Qt::CheckStateRole)
return _specs[index.row()]->is_visible() ? Qt::Checked : Qt::Unchecked;
return QVariant();
}
bool specs_model::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(role == Qt::CheckStateRole)
{
_specs[index.row()]->set_visible(value.toBool());
return true;
}
return false;
}
void specs_model::add_spec(spec *s)
{
beginInsertRows(QModelIndex(), _specs.size(), _specs.size());
_specs.push_back(s);
connect(s, &gtl::scr::spec::deleting, this, &gtl::scr::specs_model::deleting_spec);
connect(s, &gtl::scr::spec::visible_changed, this, &gtl::scr::specs_model::visibility_spec_chagned);
if(s->is_visible())
emit show_spec(s);
endInsertRows();
}
void specs_model::deleting_spec()
{
auto it = std::find(_specs.begin(), _specs.end(), sender());
if(it == _specs.end())
return;
int idx = std::distance(_specs.begin(), it);
beginRemoveRows(QModelIndex(), idx, idx);
_specs.erase(it);
endRemoveRows();
}
void specs_model::visibility_spec_chagned()
{
auto it = std::find(_specs.begin(), _specs.end(), sender());
if(it == _specs.end())
return;
int idx = std::distance(_specs.begin(), it);
spec* s = _specs[idx];
if(s->is_visible())
emit show_spec(s);
else
emit hide_spec(s);
emit dataChanged(index(idx), index(idx), QList<int>() << Qt::CheckStateRole);
}
}
}