EXDataViewDelegate.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(30);
  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->setProperty("number", v);
  30. }
  31. connect(btnGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *)>(&QButtonGroup::buttonClicked), this,
  32. &SchemeBar::barClicked);
  33. hlay->addWidget(l);
  34. hlay->addLayout(centerLay);
  35. hlay->addWidget(r);
  36. setLayout(hlay);
  37. }
  38. void SchemeBar::barClicked(QAbstractButton *btn)
  39. {
  40. emit setValue(btn->property("number").toString());
  41. this->close();
  42. }
  43. EXDataTableComboDelegate::EXDataTableComboDelegate(QObject *parent) : QStyledItemDelegate(parent)
  44. {
  45. QSettings config("config.ini", QSettings::IniFormat);
  46. config.setIniCodec("UTF-8");
  47. QStringList techMessaureValues = config.value("USERCONFIG/TechMessaureConfig", {}).toStringList();
  48. bool empty = true;
  49. if (!techMessaureValues.isEmpty()) {
  50. bool ok = false;
  51. for (auto &v : techMessaureValues) {
  52. v.toInt(&ok);
  53. if (!ok) {
  54. empty = true;
  55. break;
  56. } else {
  57. empty = false;
  58. }
  59. }
  60. }
  61. if (empty) {
  62. QMessageBox::warning(nullptr, "配置数据无效", "技术措施重要度输入参数配置无效,恢复默认值");
  63. m_items << "9"
  64. << "7"
  65. << "5"
  66. << "3"
  67. << "0"
  68. << "-3"
  69. << "-5";
  70. } else {
  71. m_items = techMessaureValues;
  72. }
  73. }
  74. QWidget *EXDataTableComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  75. const QModelIndex & /* index */) const
  76. {
  77. QComboBox *w = new QComboBox(parent);
  78. w->addItems(m_items);
  79. return w;
  80. }
  81. void EXDataTableComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  82. {
  83. QString value = index.model()->data(index, Qt::EditRole).toString();
  84. QComboBox *comboBox = static_cast<QComboBox *>(editor);
  85. comboBox->setCurrentText(value);
  86. }
  87. void EXDataTableComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  88. {
  89. QComboBox *comboBox = static_cast<QComboBox *>(editor);
  90. QString value = comboBox->currentText();
  91. if (!m_items.contains(value)) {
  92. QMessageBox::warning(nullptr, "输入无效", "只可填写9、7、5、3、0、-3、-5");
  93. value = "0";
  94. }
  95. model->setData(index, value, Qt::EditRole);
  96. }
  97. void EXDataTableComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  98. const QModelIndex & /*index*/) const
  99. {
  100. editor->setGeometry(option.rect);
  101. }