test_sdk/gui/gtl_gui_spec_meas_delegate.cpp

102 lines
3.4 KiB
C++

#include "gtl_gui_spec_meas_delegate.h"
#include "gui/gtl_gui_spec_meas_model.h"
namespace gtl {
namespace gui {
spec_meas_delegate::spec_meas_delegate(QObject *parent, QAbstractItemModel *channels) :
QStyledItemDelegate(parent),
_channels(channels)
{
}
QWidget *spec_meas_delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option)
Q_UNUSED(index)
int col = index.column();
if(col == spec_meas_model::CHAN)
{
spec_meas_chan_widget *editor = new spec_meas_chan_widget(parent, _channels);
connect(editor, &spec_meas_chan_widget::chan_changed , [=]{setModelData(editor, const_cast<QAbstractItemModel *>(index.model()), index);});
return editor;
}
else if(col == spec_meas_model::PARAM)
{
spec_meas_param_widget *editor = new spec_meas_param_widget(parent);
connect(editor, &spec_meas_param_widget::param_changed , [=]{setModelData(editor, const_cast<QAbstractItemModel *>(index.model()), index);});
return editor;
}
return 0;
}
void spec_meas_delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int col = index.column();
if(col == spec_meas_model::CHAN)
{
spec_meas_chan_widget *widget = static_cast<spec_meas_chan_widget*>(editor);
widget->blockSignals(true);
widget->setCurrentIndex(index.data(spec_meas_model::CustomRoles::IdxRole).toInt());
widget->blockSignals(false);
}
else if(col == spec_meas_model::PARAM)
{
spec_meas_param_widget *widget = static_cast<spec_meas_param_widget*>(editor);
widget->blockSignals(true);
widget->setCurrentIndex(index.data(Qt::DisplayRole).toInt());
widget->blockSignals(false);
}
}
void spec_meas_delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
int col = index.column();
editor->blockSignals(true);
if(col == spec_meas_model::CHAN)
{
spec_meas_chan_widget *widget = static_cast<spec_meas_chan_widget *>(editor);
model->setData(index, widget->currentIdx(), spec_meas_model::CustomRoles::IdxRole);
model->setData(index, widget->currentText(), Qt::EditRole);
}
else if(col == spec_meas_model::PARAM)
{
spec_meas_param_widget *widget = static_cast<spec_meas_param_widget *>(editor);
model->setData(index, widget->currentIndex(), Qt::EditRole);
}
editor->blockSignals(false);
}
void spec_meas_delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index)
editor->setGeometry(option.rect);
}
void spec_meas_delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(painter)
Q_UNUSED(option)
Q_UNUSED(index)
}
QSize spec_meas_delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSize s = index.data(Qt::SizeHintRole).toSize();
return s.isValid() ? s: QStyledItemDelegate::sizeHint(option, index);
}
} // namespace gui
} // namespace gtl