#include "comboboxdelegate.h" #include #include #include "model/metadata.h" ComboboxDelegate::ComboboxDelegate(const QStringList items, QObject* parent) : QStyledItemDelegate(parent) , m_types(new QStringListModel(items)) {} void ComboboxDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QStyledItemDelegate::paint(painter, option, index); } QSize ComboboxDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { return QStyledItemDelegate::sizeHint(option, index); } QWidget* ComboboxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const { QComboBox* editor = new QComboBox(parent); editor->setModel(m_types); return editor; // return QStyledItemDelegate::createEditor(parent, option, index); } void ComboboxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { /// Get the value via index of the Model const QAbstractItemModel* localModel = index.model(); const QString headerText = localModel->headerData(index.column(), Qt::Horizontal).toString(); const UserRoles role = GET_ROLE_FOR_COLUMN(index.column()); const bool isShareType = SHARE_TYPE_ROLES.contains(role); const bool isBiddingType = BIDDING_TYPE_ROLES.contains(role); /// Put the value into the SpinBox if (isShareType) { const QString valueString = index.model()->data(index, ShareTypeRole).toString(); int value = SHARE_TYPES.indexOf(valueString); QComboBox* combobox = static_cast(editor); combobox->setCurrentIndex(value); // QStyledItemDelegate::setEditorData(editor, index); } else if (isBiddingType) { const QString valueString = index.model()->data(index, BiddingTypeRole).toString(); int value = BIDDING_TYPES.indexOf(valueString); // Put the value into the SpinBox QComboBox* combobox = static_cast(editor); combobox->setCurrentIndex(value); // QStyledItemDelegate::setEditorData(editor, index); } else { qCritical() << "Could not find the correct type role for index:" << index << "!!!"; QComboBox* combobox = static_cast(editor); combobox->setCurrentIndex(-1); } } void ComboboxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { QStyledItemDelegate::setModelData(editor, model, index); }