#include "EXDataViewDelegate.h" #include #include #include #include #include #include #include #include SchemeBar::SchemeBar(const QString &lLabel, const QString &rLabel, const QStringList &vlist, QWidget *parent) : QDialog(parent), leftLabel(lLabel), rightLabel(rLabel), barValueList(vlist) { setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint); QHBoxLayout *hlay = new QHBoxLayout; QLabel *l = new QLabel(leftLabel.trimmed()); QLabel *r = new QLabel(rightLabel.trimmed()); QHBoxLayout *centerLay = new QHBoxLayout; QButtonGroup *btnGroup = new QButtonGroup(this); for (QString &v : barValueList) { QPushButton *btn = new QPushButton(v); btn->setMaximumWidth(40); centerLay->addWidget(btn); btnGroup->addButton(btn); if (v.startsWith("1/")) { btn->setText(v.split("/")[1]); } else { btn->setText(v); } btn->setText(v); btn->setProperty("number", v); } connect(btnGroup, static_cast(&QButtonGroup::buttonClicked), this, &SchemeBar::barClicked); hlay->addWidget(l); hlay->addLayout(centerLay); hlay->addWidget(r); setLayout(hlay); } void SchemeBar::barClicked(QAbstractButton *btn) { emit setValue(btn->property("number").toString()); this->close(); } EXDataTableComboDelegate::EXDataTableComboDelegate(QObject *parent) : QStyledItemDelegate(parent) { QSettings config("config.ini", QSettings::IniFormat); config.setIniCodec("UTF-8"); QStringList techMessaureValues = config.value("USERCONFIG/TechMessaureConfig", {}).toStringList(); bool empty = true; if (!techMessaureValues.isEmpty()) { bool ok = false; for (auto &v : techMessaureValues) { v.toInt(&ok); if (!ok) { empty = true; break; } else { empty = false; } } } if (empty) { QMessageBox::warning(nullptr, "配置数据无效", "技术措施重要度输入参数配置无效,恢复默认值"); m_items << "9" << "7" << "5" << "3" << "0" << "-3" << "-5"; } else { m_items = techMessaureValues; } } QWidget *EXDataTableComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex & /* index */) const { QComboBox *w = new QComboBox(parent); w->addItems(m_items); return w; } void EXDataTableComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QString value = index.model()->data(index, Qt::EditRole).toString(); QComboBox *comboBox = static_cast(editor); comboBox->setCurrentText(value); } void EXDataTableComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *comboBox = static_cast(editor); QString value = comboBox->currentText(); if (!m_items.contains(value)) { QMessageBox::warning(nullptr, "输入无效", "只可填写9、7、5、3、0、-3、-5"); value = "0"; } model->setData(index, value, Qt::EditRole); } void EXDataTableComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/) const { editor->setGeometry(option.rect); }