test_sdk/gui/osc/gtl_gui_osc_meas_model.cpp

199 lines
6.8 KiB
C++
Raw Normal View History

#include <QUuid>
#include "core/gtl_selection_data_model.h"
#include "gtl_gui_osc_meas_model.h"
namespace gtl {
namespace gui {
namespace osc {
meas_model::meas_model(QObject *parent, QAbstractItemModel *channels) :
QAbstractTableModel(parent),
_channels(channels)
{
_measures = OscMeasParamsListPtr(new QListOscMeasParams);
}
QVariant 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 meas_model::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
if(_measures)
return _measures->count();
return 0;
}
int meas_model::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return VALUE + 1;
}
QVariant 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 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::osc_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::osc_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 meas_model::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
bool meas_model::insertRows(int row, int count, const QModelIndex &parent)
{
if(static_cast<selection_list*>(_channels)->size())
{
beginInsertRows(parent, row, row + count - 1);
math::osc_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 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;
}
OscMeasParamsListPtr meas_model::measures()
{
return _measures;
}
QAbstractItemModel *meas_model::channels()
{
return _channels;
}
void 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