#include "CTableDelegate.h" #include "CLineEditWithBt.h" #include #include CTableDelegate::CTableDelegate(QObject *parent, DelegateWidget type, bool editable) : QStyledItemDelegate(parent), m_type(type), m_editable(editable) { } CTableDelegate::~CTableDelegate() { } void CTableDelegate::setComboMap(const QMap &map) { m_mapComboBox = map; } void CTableDelegate::setFilePath(const QString &path) { m_strFilePath = path; } QString CTableDelegate::getFilePath() { return m_strFilePath; } QWidget *CTableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { switch(m_type){ case ReadOnly: return NULL; case LineEdit: return new QLineEdit(parent); case ComboBox: { QComboBox *box= new QComboBox(parent); box->setEditable(m_editable); QMap::const_iterator iter = m_mapComboBox.constBegin(); for(; iter != m_mapComboBox.constEnd(); iter ++) { box->addItem(iter.value(), iter.key()); } return box; } case PicFile: case IconFile: { return new CLineEditWithBt(parent, m_strFilePath); } default: return QStyledItemDelegate::createEditor(parent, option, index); } } void CTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { switch(m_type){ case LineEdit: { QLineEdit *edit = static_cast(editor); edit->setText(index.data(Qt::DisplayRole).toString()); break; } case ComboBox: { QComboBox *comb = static_cast(editor); comb->setCurrentIndex(comb->findText(index.data(Qt::DisplayRole).toString())); break; } case PicFile: case IconFile: { CLineEditWithBt *edit = static_cast(editor); edit->setText(index.data(Qt::DisplayRole).toString()); break; } default: QStyledItemDelegate::setEditorData(editor, index); } } void CTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { switch (m_type) { case LineEdit: { QLineEdit *edit = static_cast(editor); model->setData(index, edit->text(), Qt::DisplayRole); break; } case ComboBox: { QComboBox *comb = static_cast(editor); if(model->data(index, Qt::DisplayRole) != comb->currentText()) { model->setData(index, comb->currentText(), Qt::DisplayRole); model->setData(index, comb->currentData(), Qt::UserRole); } break; } case PicFile: case IconFile: { CLineEditWithBt *edit = static_cast(editor); model->setData(index, edit->text(), Qt::DisplayRole); model->setData(index, edit->text(), Qt::UserRole); break; } default: QStyledItemDelegate::setModelData(editor, model, index); } } void CTableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(index) editor->setGeometry(option.rect); }