2025-03-19 11:07:25 +08:00

127 lines
3.2 KiB
C++

#include "CTableDelegate.h"
#include "CLineEditWithBt.h"
#include <QComboBox>
#include <QLineEdit>
CTableDelegate::CTableDelegate(QObject *parent, DelegateWidget type, bool editable)
: QStyledItemDelegate(parent),
m_type(type),
m_editable(editable)
{
}
CTableDelegate::~CTableDelegate()
{
}
void CTableDelegate::setComboMap(const QMap<QVariant, QString> &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<QVariant, QString>::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<QLineEdit*>(editor);
edit->setText(index.data(Qt::DisplayRole).toString());
break;
}
case ComboBox:
{
QComboBox *comb = static_cast<QComboBox*>(editor);
comb->setCurrentIndex(comb->findText(index.data(Qt::DisplayRole).toString()));
break;
}
case PicFile:
case IconFile:
{
CLineEditWithBt *edit = static_cast<CLineEditWithBt*>(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<QLineEdit*>(editor);
model->setData(index, edit->text(), Qt::DisplayRole);
break;
}
case ComboBox:
{
QComboBox *comb = static_cast<QComboBox*>(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<CLineEditWithBt*>(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);
}