#include "ModelTable.h"
#include "DaRecord.h"
#include "ScopeTailed.h"
ModelTable::ModelTable()
{
header_ <<.....;
}
ModelTable::~ModelTable()
{
}
QVariant ModelTable::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return header_[section];
return QVariant();
}
QVariant ModelTable::data(const QModelIndex &index, int role /*= Qt::DisplayRole*/) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::CheckStateRole) //..勾选
{
if (index.column() == 0)
{
if (checkStateMap_.contains(index.row()))
return checkStateMap_[index.row()] ? Qt::Checked : Qt::Unchecked;
return Qt::Unchecked;
}
}
if (role==Qt::DisplayRole)
{
RecordTree res = res_[index.row()];
switch (index.column())
{
case 0:return res.spmc_;
case 1:return res.zs_;
case 2:return res.slv_.toString();
}
}
return QVariant();
}
int ModelTable::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
return res_.count();
}
int ModelTable::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
return header_.count();
}
void ModelTable::loadData()
{
beginResetModel();
res_;
endResetModel();
}
void ModelTable::loadData(RecordTree &res)
{
beginResetModel();
res_.clear();
res_ << res;
checkStateMap_.clear();
endResetModel();
}
Qt::ItemFlags ModelTable::flags(const QModelIndex &index) const
{
const Qt::ItemFlags editable = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
const Qt::ItemFlags checkedtable = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
if (index.column() == 0)
return checkedtable;
if (index.isValid())
{
switch (index.column())
{
case 0:return editable;
case 1:return editable;
case 2:return editable;
}
}
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
bool ModelTable::setData(const QModelIndex &index, const QVariant &value, int role /* = Qt::EditRole */)
{
if (!index.isValid() && index.row() >= res_.count())
return false;
if (role == Qt::CheckStateRole && index.column() == 0)
checkStateMap_[index.row()] = (value == Qt::Checked ? Qt::Checked : Qt::Unchecked);
RecordTree & record = res_[index.row()];
//editable (可编辑)
if (index.isValid() && role == Qt::EditRole)
{
if (index.column() == 0)
record.zs_ = value.toString();
else if (index.column() == 1)
record.slv_ = value.toString();
}
//refresh full row (刷新全部行)
emit dataChanged(createIndex(index.row(), 0), createIndex(index.row(), header_.count()));
return true;
}
void ModelTable::Clear()
{
checkStateMap_.clear();
res_.clear();
}