#include "gtl_gui_osc_meas_delegate.h" #include "gtl_gui_osc_meas_model.h" namespace gtl { namespace gui { namespace osc { meas_delegate::meas_delegate(QObject *parent, QAbstractItemModel *channels) : QStyledItemDelegate(parent), _channels(channels) { } QWidget *meas_delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(option) Q_UNUSED(index) int col = index.column(); if(col == meas_model::CHAN) { meas_chan_widget *editor = new meas_chan_widget(parent, _channels); connect(editor, &meas_chan_widget::chan_changed , [=]{setModelData(editor, const_cast(index.model()), index);}); return editor; } else if(col == meas_model::PARAM) { meas_param_widget *editor = new meas_param_widget(parent); connect(editor, &meas_param_widget::param_changed , [=]{setModelData(editor, const_cast(index.model()), index);}); return editor; } return 0; } void meas_delegate::setEditorData(QWidget *editor, const QModelIndex &index) const { int col = index.column(); if(col == meas_model::CHAN) { meas_chan_widget *widget = static_cast(editor); widget->blockSignals(true); widget->setCurrentIndex(index.data(meas_model::CustomRoles::IdxRole).toInt()); widget->blockSignals(false); } else if(col == meas_model::PARAM) { meas_param_widget *widget = static_cast(editor); widget->blockSignals(true); widget->setCurrentIndex(index.data(Qt::DisplayRole).toInt()); widget->blockSignals(false); } } void meas_delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { int col = index.column(); editor->blockSignals(true); if(col == meas_model::CHAN) { meas_chan_widget *widget = static_cast(editor); model->setData(index, widget->currentIdx(), meas_model::CustomRoles::IdxRole); model->setData(index, widget->currentText(), Qt::EditRole); } else if(col == meas_model::PARAM) { meas_param_widget *widget = static_cast(editor); model->setData(index, widget->currentIndex(), Qt::EditRole); } editor->blockSignals(false); } void meas_delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(index) editor->setGeometry(option.rect); } void meas_delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(painter) Q_UNUSED(option) Q_UNUSED(index) } QSize 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