#include "MatterElementItemDelegate.h" #include #include // #include MatterElementItemSpinDelegate::MatterElementItemSpinDelegate(QObject *parent) : QStyledItemDelegate(parent) { } QWidget *MatterElementItemSpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex & /* index */) const { QDoubleSpinBox *editor = new QDoubleSpinBox(parent); editor->setFrame(false); editor->setMinimum(-10000); editor->setMaximum(10000); return editor; } void MatterElementItemSpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { double value = index.model()->data(index, Qt::EditRole).toDouble(); QDoubleSpinBox *spinBox = static_cast(editor); spinBox->setValue(value); } void MatterElementItemSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QDoubleSpinBox *spinBox = static_cast(editor); spinBox->interpretText(); double value = spinBox->value(); model->setData(index, value, Qt::EditRole); } void MatterElementItemSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/) const { editor->setGeometry(option.rect); } MatterElementItemInsideDelegate::MatterElementItemInsideDelegate(QObject *parent) : QStyledItemDelegate(parent) { } QWidget *MatterElementItemInsideDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex & /*index*/) const { InsideWidget *editor = new InsideWidget(parent); return editor; } void MatterElementItemInsideDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QString value = index.model()->data(index, Qt::EditRole).toString(); InsideWidget *iw = static_cast(editor); iw->setValue(value); } void MatterElementItemInsideDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { InsideWidget *iw = static_cast(editor); QString value = iw->result(); model->setData(index, value, Qt::EditRole); } void MatterElementItemInsideDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/) const { QRect rect = option.rect; rect.adjust(-100, 0, 0, 50); editor->setGeometry(rect); } MatterElementItemRangeDelegate::MatterElementItemRangeDelegate(QObject *parent) : QStyledItemDelegate(parent) { } QWidget *MatterElementItemRangeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex & /*index*/) const { RangeWidget *editor = new RangeWidget(m_originItems, parent); editor->setMinimumSize(180, 60); return editor; } void MatterElementItemRangeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QString value = index.model()->data(index, Qt::EditRole).toString(); RangeWidget *rw = static_cast(editor); if (!value.isEmpty()) { QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(","); int id1 = m_originValues.indexOf(tmp.at(0).toDouble()); int id2 = m_originValues.indexOf(tmp.at(1).toDouble()); if (id1 != -1) { tmp[0] = m_originItems.at(id1); } if (id2 != -1) { tmp[1] = m_originItems.at(id2); } bool leftClosed = value[0] == "["; bool rightClosed = value.right(1) == "]"; rw->setValue(tmp.at(0), tmp.at(1), leftClosed, rightClosed); } else { rw->setValue(m_originItems.at(0), m_originItems.at(1)); } } void MatterElementItemRangeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { RangeWidget *rw = static_cast(editor); QStringList list = rw->result(); int id1 = m_originItems.indexOf(list.at(0)); int id2 = m_originItems.indexOf(list.at(1)); double t1, t2; bool valid = false; if (id1 == -1) { t1 = list.at(0).toDouble(&valid); if (!valid) { QMessageBox::warning(rw, "警告", QString("左侧值%1不是数值\n配置失败").arg(list.at(0))); return; } } if (id2 == -1) { t2 = list.at(1).toDouble(&valid); if (!valid) { QMessageBox::warning(rw, "警告", QString("右侧值%1不是数值\n配置失败").arg(list.at(1))); return; } } if (id1 != -1) { t1 = m_originValues.at(id1); } if (id2 != -1) { t2 = m_originValues.at(id2); } QString value = QString("%1%2, %3%4").arg(list.at(2)).arg(t1).arg(t2).arg(list.at(3)); model->setData(index, value, Qt::EditRole); } void MatterElementItemRangeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/) const { QRect rect = option.rect; rect.adjust(-80, 10, 10, 10); editor->setGeometry(rect); } void MatterElementItemRangeDelegate::setOriginItems(const QStringList &originItems, const QVector &originValues) { m_originItems = originItems; m_originValues = originValues; } void MatterElementItemRangeDelegate::updateOriginValues(const QVector &values) { m_originValues = values; }