123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #include "EXDataViewDelegate.h"
- #include <QHBoxLayout>
- #include <QLabel>
- #include <QPushButton>
- #include <QButtonGroup>
- #include <QDebug>
- #include <QComboBox>
- #include <QSettings>
- #include <QMessageBox>
- 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<void (QButtonGroup::*)(QAbstractButton *)>(&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<QComboBox *>(editor);
- comboBox->setCurrentText(value);
- }
- void EXDataTableComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
- {
- QComboBox *comboBox = static_cast<QComboBox *>(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);
- }
|