198 lines
6.3 KiB
C++
198 lines
6.3 KiB
C++
|
#include <QUuid>
|
||
|
#include "core/gtl_selection_data_model.h"
|
||
|
#include "gtl_gui_spec_meas_model.h"
|
||
|
|
||
|
namespace gtl {
|
||
|
namespace gui {
|
||
|
|
||
|
spec_meas_model::spec_meas_model(QObject *parent, QAbstractItemModel *channels) :
|
||
|
QAbstractTableModel(parent),
|
||
|
_channels(channels)
|
||
|
{
|
||
|
_measures = SpecMeasParamsListPtr(new QListSpecMeasParams);
|
||
|
}
|
||
|
|
||
|
QVariant spec_meas_model::headerData(int section, Qt::Orientation orientation, int role) const
|
||
|
{
|
||
|
if(role != Qt::DisplayRole)
|
||
|
return QVariant();
|
||
|
|
||
|
if(orientation == Qt::Vertical)
|
||
|
return section;
|
||
|
|
||
|
switch(section) {
|
||
|
case CHAN:
|
||
|
return tr("channel");
|
||
|
case PARAM:
|
||
|
return tr("parameter");
|
||
|
case VALUE:
|
||
|
return tr("value");
|
||
|
}
|
||
|
|
||
|
return QVariant();
|
||
|
}
|
||
|
|
||
|
int spec_meas_model::rowCount(const QModelIndex &parent) const
|
||
|
{
|
||
|
if (parent.isValid())
|
||
|
return 0;
|
||
|
|
||
|
if(_measures)
|
||
|
return _measures->count();
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int spec_meas_model::columnCount(const QModelIndex &parent) const
|
||
|
{
|
||
|
if (parent.isValid())
|
||
|
return 0;
|
||
|
|
||
|
return VALUE + 1;
|
||
|
}
|
||
|
|
||
|
QVariant spec_meas_model::data(const QModelIndex &index, int role) const
|
||
|
{
|
||
|
if (!index.isValid())
|
||
|
return QVariant();
|
||
|
|
||
|
int col = index.column();
|
||
|
int row = index.row();
|
||
|
|
||
|
if(row >= _measures->size())
|
||
|
return QVariant();
|
||
|
|
||
|
switch (role){
|
||
|
case Qt::DisplayRole:
|
||
|
if(col == CHAN)
|
||
|
return _measures->at(row).chan;
|
||
|
else if(col == PARAM)
|
||
|
return static_cast<int>(_measures->at(row).type);
|
||
|
else if(col == VALUE)
|
||
|
return _measures->at(row).value;
|
||
|
break;
|
||
|
case CustomRoles::IdxRole:
|
||
|
{
|
||
|
selection_list* sel = static_cast<selection_list*>(_channels);
|
||
|
for(int i = 0; i < sel->size(); i++)
|
||
|
if(sel->at(i) == _measures->at(row).ad)
|
||
|
return i;
|
||
|
return QVariant();
|
||
|
}
|
||
|
break;
|
||
|
case Qt::BackgroundRole:
|
||
|
if(col == VALUE)
|
||
|
return QBrush(QColor(127,255,127,255));
|
||
|
default:
|
||
|
return QVariant();
|
||
|
}
|
||
|
|
||
|
return QVariant();
|
||
|
}
|
||
|
|
||
|
bool spec_meas_model::setData(const QModelIndex &index, const QVariant &value, int role)
|
||
|
{
|
||
|
if(data(index, role) != value)
|
||
|
{
|
||
|
int col = index.column();
|
||
|
int row = index.row();
|
||
|
|
||
|
if(row >= _measures->size() || col < 0)
|
||
|
return false;
|
||
|
|
||
|
math::spec_meas::params param = _measures->at(row);
|
||
|
bool param_flag = true;
|
||
|
switch (role){
|
||
|
case Qt::EditRole:
|
||
|
if(col == CHAN)
|
||
|
param.chan = value.toString();
|
||
|
else if(col == PARAM)
|
||
|
param.type = static_cast<math::spec_meas::types>(value.toInt());
|
||
|
else if(col == VALUE)
|
||
|
{
|
||
|
param.value = value.toDouble();
|
||
|
param_flag = false;
|
||
|
}
|
||
|
break;
|
||
|
case CustomRoles::IdxRole:
|
||
|
{
|
||
|
selection_list* chs = static_cast<selection_list*>(_channels);
|
||
|
if(chs->size() && chs->size() > value.toInt()) param.ad = chs->at(value.toInt());
|
||
|
else param.ad = nullptr;
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
_measures->replace(row, param);
|
||
|
if(param_flag)
|
||
|
emit measure_changed(row);
|
||
|
emit dataChanged(index, index, {role});
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
Qt::ItemFlags spec_meas_model::flags(const QModelIndex &index) const
|
||
|
{
|
||
|
if (!index.isValid())
|
||
|
return Qt::NoItemFlags;
|
||
|
|
||
|
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
||
|
}
|
||
|
|
||
|
bool spec_meas_model::insertRows(int row, int count, const QModelIndex &parent)
|
||
|
{
|
||
|
if(static_cast<selection_list*>(_channels)->size())
|
||
|
{
|
||
|
beginInsertRows(parent, row, row + count - 1);
|
||
|
math::spec_meas::params param;
|
||
|
param.chan = _channels->index(0, 0, QModelIndex()).data(Qt::DisplayRole).toString();
|
||
|
for(int i = 0; i < count; i++)
|
||
|
{
|
||
|
param.id = QUuid::createUuid().data1;
|
||
|
param.ad = static_cast<selection_list*>(_channels)->at(0);
|
||
|
_measures->insert(row, param);
|
||
|
}
|
||
|
endInsertRows();
|
||
|
emit measure_changed(row);
|
||
|
return true;
|
||
|
}
|
||
|
else
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
bool spec_meas_model::removeRows(int row, int count, const QModelIndex &parent)
|
||
|
{
|
||
|
if(_measures->count() && row >= 0 && _measures->count() >= (row + count))
|
||
|
{
|
||
|
beginRemoveRows(parent, row, row + count - 1);
|
||
|
_measures->remove(row,count);
|
||
|
endRemoveRows();
|
||
|
emit measure_changed(row);
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
SpecMeasParamsListPtr spec_meas_model::measures()
|
||
|
{
|
||
|
return _measures;
|
||
|
}
|
||
|
|
||
|
QAbstractItemModel *spec_meas_model::channels()
|
||
|
{
|
||
|
return _channels;
|
||
|
}
|
||
|
|
||
|
void spec_meas_model::all_done()
|
||
|
{
|
||
|
QModelIndex top_left = index(0,VALUE, QModelIndex());
|
||
|
QModelIndex bottom_right = index(_measures->count(), VALUE, QModelIndex());
|
||
|
emit dataChanged(top_left, bottom_right, {Qt::DisplayRole});
|
||
|
}
|
||
|
|
||
|
} // namespace gui
|
||
|
} // namespace gtl
|
||
|
|