123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- #include "MatterElementItemDelegate.h"
- #include <QComboBox>
- #include <QMessageBox>
- // #include <QDebug>
- 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<QDoubleSpinBox *>(editor);
- spinBox->setValue(value);
- }
- void MatterElementItemSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
- const QModelIndex &index) const
- {
- QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox *>(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<InsideWidget *>(editor);
- iw->setValue(value);
- }
- void MatterElementItemInsideDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
- const QModelIndex &index) const
- {
- InsideWidget *iw = static_cast<InsideWidget *>(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<RangeWidget *>(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<RangeWidget *>(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<double> &originValues)
- {
- m_originItems = originItems;
- m_originValues = originValues;
- }
- void MatterElementItemRangeDelegate::updateOriginValues(const QVector<double> &values)
- {
- m_originValues = values;
- }
|