#include "ParamTypeDelegate.h" #include #include "TypeSelectionItemModel.h" ParamTypeDelegate::ParamTypeDelegate() {} ParamTypeDelegate::~ParamTypeDelegate() {} void ParamTypeDelegate::setTypeSelectionModel(TypeSelectionItemModel* model) { m_typeSelectionModel = model; } QWidget *ParamTypeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QWidget *w = nullptr; QComboBox *cmbbx = new QComboBox(parent); cmbbx->setMaxVisibleItems(32); cmbbx->setModel(m_typeSelectionModel); w = cmbbx; // ... // m_ComboBox->setView(m_ColumnView); // m_ComboBox->view()->setCornerWidget(new QSizeGrip(m_ColumnView)); // m_ComboBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); // ... return w; } void ParamTypeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { // if (index.data().canConvert()) { // StarRating starRating = qvariant_cast(index.data()); // StarEditor *starEditor = qobject_cast(editor); // starEditor->setStarRating(starRating); // } else { // QStyledItemDelegate::setEditorData(editor, index); // } if (index.column() == 1) { QComboBox *cmbbx = dynamic_cast(editor); if (cmbbx) { auto data = index.data(); if (data.canConvert()) { QModelIndexList indexes = m_typeSelectionModel->match( m_typeSelectionModel->index(0, 1), Qt::DisplayRole, data, 1, Qt::MatchFlags( Qt::MatchExactly )); if (!indexes.empty()) { cmbbx->setCurrentIndex(indexes.at(0).row()); } else { cmbbx->setCurrentIndex(-1); } } } } else { QStyledItemDelegate::setEditorData(editor, index); } } void ParamTypeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { // if (index.data().canConvert()) { // StarEditor *starEditor = qobject_cast(editor); // model->setData(index, QVariant::fromValue(starEditor->starRating())); // } else { // QStyledItemDelegate::setModelData(editor, model, index); // } if (index.column() == 1) { QComboBox *cmbbx = dynamic_cast(editor); if (cmbbx) { auto data = index.data(); if (data.canConvert()) { QVariant d = m_typeSelectionModel->data( m_typeSelectionModel->index(cmbbx->currentIndex(), 0)); model->setData(index, d); } } } else { QStyledItemDelegate::setModelData(editor, model, index); } } // triggered by editing finished from editor void ParamTypeDelegate::commitAndCloseEditor() { // StarEditor *editor = qobject_cast(sender()); // emit commitData(editor); // emit closeEditor(editor); }