EXDataViewDelegate.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "EXDataViewDelegate.h"
  2. #include <QHBoxLayout>
  3. #include <QLabel>
  4. #include <QPushButton>
  5. #include <QButtonGroup>
  6. #include <QDebug>
  7. #include <QComboBox>
  8. #include <QSettings>
  9. #include <QMessageBox>
  10. SchemeBar::SchemeBar(const QString &lLabel, const QString &rLabel, const QStringList &vlist, QWidget *parent)
  11. : QDialog(parent), leftLabel(lLabel), rightLabel(rLabel), barValueList(vlist)
  12. {
  13. setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
  14. QHBoxLayout *hlay = new QHBoxLayout;
  15. QLabel *l = new QLabel(leftLabel.trimmed());
  16. QLabel *r = new QLabel(rightLabel.trimmed());
  17. QHBoxLayout *centerLay = new QHBoxLayout;
  18. QButtonGroup *btnGroup = new QButtonGroup(this);
  19. for (QString &v : barValueList) {
  20. QPushButton *btn = new QPushButton(v);
  21. btn->setMaximumWidth(40);
  22. centerLay->addWidget(btn);
  23. btnGroup->addButton(btn);
  24. if (v.startsWith("1/")) {
  25. btn->setText(v.split("/")[1]);
  26. } else {
  27. btn->setText(v);
  28. }
  29. btn->setText(v);
  30. btn->setProperty("number", v);
  31. }
  32. connect(btnGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *)>(&QButtonGroup::buttonClicked), this,
  33. &SchemeBar::barClicked);
  34. hlay->addWidget(l);
  35. hlay->addLayout(centerLay);
  36. hlay->addWidget(r);
  37. setLayout(hlay);
  38. }
  39. void SchemeBar::barClicked(QAbstractButton *btn)
  40. {
  41. emit setValue(btn->property("number").toString());
  42. this->close();
  43. }
  44. EXDataTableComboDelegate::EXDataTableComboDelegate(QObject *parent) : QStyledItemDelegate(parent)
  45. {
  46. QSettings config("config.ini", QSettings::IniFormat);
  47. config.setIniCodec("UTF-8");
  48. QStringList techMessaureValues = config.value("USERCONFIG/TechMessaureConfig", {}).toStringList();
  49. bool empty = true;
  50. if (!techMessaureValues.isEmpty()) {
  51. bool ok = false;
  52. for (auto &v : techMessaureValues) {
  53. v.toInt(&ok);
  54. if (!ok) {
  55. empty = true;
  56. break;
  57. } else {
  58. empty = false;
  59. }
  60. }
  61. }
  62. if (empty) {
  63. QMessageBox::warning(nullptr, "配置数据无效", "技术措施重要度输入参数配置无效,恢复默认值");
  64. m_items << "9"
  65. << "7"
  66. << "5"
  67. << "3"
  68. << "0"
  69. << "-3"
  70. << "-5";
  71. } else {
  72. m_items = techMessaureValues;
  73. }
  74. }
  75. QWidget *EXDataTableComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  76. const QModelIndex & /* index */) const
  77. {
  78. QComboBox *w = new QComboBox(parent);
  79. w->addItems(m_items);
  80. return w;
  81. }
  82. void EXDataTableComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  83. {
  84. QString value = index.model()->data(index, Qt::EditRole).toString();
  85. QComboBox *comboBox = static_cast<QComboBox *>(editor);
  86. comboBox->setCurrentText(value);
  87. }
  88. void EXDataTableComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  89. {
  90. QComboBox *comboBox = static_cast<QComboBox *>(editor);
  91. QString value = comboBox->currentText();
  92. if (!m_items.contains(value)) {
  93. QMessageBox::warning(nullptr, "输入无效", "只可填写9、7、5、3、0、-3、-5");
  94. value = "0";
  95. }
  96. model->setData(index, value, Qt::EditRole);
  97. }
  98. void EXDataTableComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  99. const QModelIndex & /*index*/) const
  100. {
  101. editor->setGeometry(option.rect);
  102. }