MatterElementItemDelegate.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "MatterElementItemDelegate.h"
  2. #include <QComboBox>
  3. #include <QMessageBox>
  4. // #include <QDebug>
  5. MatterElementItemSpinDelegate::MatterElementItemSpinDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
  6. QWidget *MatterElementItemSpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  7. const QModelIndex & /* index */) const
  8. {
  9. QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
  10. editor->setFrame(false);
  11. editor->setMinimum(-10000);
  12. editor->setMaximum(10000);
  13. return editor;
  14. }
  15. void MatterElementItemSpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  16. {
  17. double value = index.model()->data(index, Qt::EditRole).toDouble();
  18. QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox *>(editor);
  19. spinBox->setValue(value);
  20. }
  21. void MatterElementItemSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  22. const QModelIndex &index) const
  23. {
  24. QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox *>(editor);
  25. spinBox->interpretText();
  26. double value = spinBox->value();
  27. model->setData(index, value, Qt::EditRole);
  28. }
  29. void MatterElementItemSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  30. const QModelIndex & /*index*/) const
  31. {
  32. editor->setGeometry(option.rect);
  33. }
  34. MatterElementItemInsideDelegate::MatterElementItemInsideDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
  35. QWidget *MatterElementItemInsideDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /*option*/,
  36. const QModelIndex & /*index*/) const
  37. {
  38. InsideWidget *editor = new InsideWidget(parent);
  39. return editor;
  40. }
  41. void MatterElementItemInsideDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  42. {
  43. QString value = index.model()->data(index, Qt::EditRole).toString();
  44. InsideWidget *iw = static_cast<InsideWidget *>(editor);
  45. iw->setValue(value);
  46. }
  47. void MatterElementItemInsideDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  48. const QModelIndex &index) const
  49. {
  50. InsideWidget *iw = static_cast<InsideWidget *>(editor);
  51. QString value = iw->result();
  52. model->setData(index, value, Qt::EditRole);
  53. }
  54. void MatterElementItemInsideDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  55. const QModelIndex & /*index*/) const
  56. {
  57. QRect rect = option.rect;
  58. rect.adjust(-100, 0, 0, 50);
  59. editor->setGeometry(rect);
  60. }
  61. MatterElementItemRangeDelegate::MatterElementItemRangeDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
  62. QWidget *MatterElementItemRangeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /*option*/,
  63. const QModelIndex & /*index*/) const
  64. {
  65. RangeWidget *editor = new RangeWidget(m_originItems, parent);
  66. editor->setMinimumSize(180, 60);
  67. return editor;
  68. }
  69. void MatterElementItemRangeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  70. {
  71. QString value = index.model()->data(index, Qt::EditRole).toString();
  72. RangeWidget *rw = static_cast<RangeWidget *>(editor);
  73. if (!value.isEmpty()) {
  74. QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(",");
  75. int id1 = m_originValues.indexOf(tmp.at(0).toDouble());
  76. int id2 = m_originValues.indexOf(tmp.at(1).toDouble());
  77. if (id1 != -1) {
  78. tmp[0] = m_originItems.at(id1);
  79. }
  80. if (id2 != -1) {
  81. tmp[1] = m_originItems.at(id2);
  82. }
  83. bool leftClosed = value[0] == "[";
  84. bool rightClosed = value.right(1) == "]";
  85. rw->setValue(tmp.at(0), tmp.at(1), leftClosed, rightClosed);
  86. } else {
  87. rw->setValue(m_originItems.at(0), m_originItems.at(1));
  88. }
  89. }
  90. void MatterElementItemRangeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  91. const QModelIndex &index) const
  92. {
  93. RangeWidget *rw = static_cast<RangeWidget *>(editor);
  94. QStringList list = rw->result();
  95. int id1 = m_originItems.indexOf(list.at(0));
  96. int id2 = m_originItems.indexOf(list.at(1));
  97. double t1, t2;
  98. bool valid = false;
  99. if (id1 == -1) {
  100. t1 = list.at(0).toDouble(&valid);
  101. if (!valid) {
  102. QMessageBox::warning(rw, "警告", QString("左侧值%1不是数值\n配置失败").arg(list.at(0)));
  103. return;
  104. }
  105. }
  106. if (id2 == -1) {
  107. t2 = list.at(1).toDouble(&valid);
  108. if (!valid) {
  109. QMessageBox::warning(rw, "警告", QString("右侧值%1不是数值\n配置失败").arg(list.at(1)));
  110. return;
  111. }
  112. }
  113. if (id1 != -1) {
  114. t1 = m_originValues.at(id1);
  115. }
  116. if (id2 != -1) {
  117. t2 = m_originValues.at(id2);
  118. }
  119. QString value = QString("%1%2, %3%4").arg(list.at(2)).arg(t1).arg(t2).arg(list.at(3));
  120. model->setData(index, value, Qt::EditRole);
  121. }
  122. void MatterElementItemRangeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  123. const QModelIndex & /*index*/) const
  124. {
  125. QRect rect = option.rect;
  126. rect.adjust(-80, 10, 10, 10);
  127. editor->setGeometry(rect);
  128. }
  129. void MatterElementItemRangeDelegate::setOriginItems(const QStringList &originItems, const QVector<double> &originValues)
  130. {
  131. m_originItems = originItems;
  132. m_originValues = originValues;
  133. }
  134. void MatterElementItemRangeDelegate::updateOriginValues(const QVector<double> &values)
  135. {
  136. m_originValues = values;
  137. }