MatterElementItemDelegate.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #ifndef MATTERELEMENTITEMDELEGATE_H
  2. #define MATTERELEMENTITEMDELEGATE_H
  3. #include <QStyledItemDelegate>
  4. #include <QFormLayout>
  5. #include <QDoubleSpinBox>
  6. #include <QComboBox>
  7. #include <QCheckBox>
  8. #include <QLabel>
  9. class InsideWidget : public QFrame
  10. {
  11. Q_OBJECT
  12. public:
  13. InsideWidget(QWidget *parent = nullptr) : QFrame(parent)
  14. {
  15. QHBoxLayout *vcenter = new QHBoxLayout;
  16. vcenter->setMargin(0);
  17. vcenter->setSpacing(0);
  18. QFormLayout *layout = new QFormLayout;
  19. setStyleSheet("InsideWidget{background-color:gray} QLabel{color:white}");
  20. layout->setLabelAlignment(Qt::AlignCenter);
  21. vcenter->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Expanding));
  22. vcenter->addLayout(layout);
  23. vcenter->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Expanding));
  24. layout->setMargin(2);
  25. layout->setSpacing(2);
  26. for (int i = 0; i < 4; ++i) {
  27. auto s = new QDoubleSpinBox;
  28. s->setFixedWidth(70);
  29. s->setFrame(false);
  30. s->setMinimum(-10000);
  31. s->setMaximum(10000);
  32. m_spins << s;
  33. }
  34. layout->addRow("[很差,交差]中间值:", m_spins.at(0));
  35. layout->addRow("[较差,一般]中间值:", m_spins.at(1));
  36. layout->addRow("[一般,较高]中间值:", m_spins.at(2));
  37. layout->addRow("[较高,很高]中间值:", m_spins.at(3));
  38. setLayout(vcenter);
  39. }
  40. virtual ~InsideWidget() { }
  41. QString result()
  42. {
  43. this->onConfirm();
  44. return m_result;
  45. }
  46. void setValue(const QString &value)
  47. {
  48. if (!value.isEmpty()) {
  49. QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(",");
  50. for (int i = 0; i < tmp.size(); ++i) {
  51. m_spins.at(i)->setValue(tmp[i].toDouble());
  52. }
  53. }
  54. }
  55. private slots:
  56. void onConfirm()
  57. {
  58. m_result = QString("[%1, %2, %3, %4]")
  59. .arg(m_spins.at(0)->value())
  60. .arg(m_spins.at(1)->value())
  61. .arg(m_spins.at(2)->value())
  62. .arg(m_spins.at(3)->value());
  63. }
  64. private:
  65. QList<QDoubleSpinBox *> m_spins;
  66. QString m_result;
  67. };
  68. class MatterElementItemSpinDelegate : public QStyledItemDelegate
  69. {
  70. Q_OBJECT
  71. public:
  72. MatterElementItemSpinDelegate(QObject *parent = 0);
  73. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  74. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  75. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  76. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  77. const QModelIndex &index) const override;
  78. };
  79. class MatterElementItemInsideDelegate : public QStyledItemDelegate
  80. {
  81. Q_OBJECT
  82. public:
  83. MatterElementItemInsideDelegate(QObject *parent = 0);
  84. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  85. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  86. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  87. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  88. const QModelIndex &index) const override;
  89. };
  90. class RangeWidget : public QFrame
  91. {
  92. Q_OBJECT
  93. public:
  94. RangeWidget(const QStringList &items, QWidget *parent = nullptr) : QFrame(parent)
  95. {
  96. QHBoxLayout *hLayout = new QHBoxLayout;
  97. setStyleSheet("RangeSpin{background-color:gray} QLabel{color:white}");
  98. hLayout->setMargin(0);
  99. hLayout->setSpacing(0);
  100. QLabel *minLabel = new QLabel("左");
  101. QLabel *maxLabel = new QLabel("右");
  102. m_leftChb = new QCheckBox;
  103. m_leftChb->setCheckable(true);
  104. m_leftChb->setChecked(true);
  105. m_rightChb = new QCheckBox;
  106. m_rightChb->setCheckable(true);
  107. m_rightChb->setChecked(true);
  108. QFormLayout *layout = new QFormLayout;
  109. setStyleSheet("RangeWidget{background-color:gray}");
  110. layout->setMargin(10);
  111. layout->setSpacing(2);
  112. m_leftCombInput = new QComboBox();
  113. m_leftCombInput->setMaxVisibleItems(15);
  114. m_leftCombInput->setEditable(true);
  115. m_leftCombInput->addItems(items);
  116. m_leftCombInput->setCurrentIndex(0);
  117. m_rightCombInput = new QComboBox();
  118. m_rightCombInput->setMaxVisibleItems(15);
  119. m_rightCombInput->setEditable(true);
  120. m_rightCombInput->addItems(items);
  121. m_rightCombInput->setCurrentIndex(0);
  122. layout->addRow("左:", m_leftCombInput);
  123. layout->addRow("右:", m_rightCombInput);
  124. hLayout->addWidget(minLabel);
  125. hLayout->addWidget(m_leftChb);
  126. hLayout->addLayout(layout);
  127. hLayout->addWidget(m_rightChb);
  128. hLayout->addWidget(maxLabel);
  129. setLayout(hLayout);
  130. }
  131. virtual ~RangeWidget() { }
  132. QStringList result()
  133. {
  134. QString leftClosed = m_leftChb->isChecked() ? "[" : "(";
  135. QString rightClosed = m_rightChb->isChecked() ? "]" : ")";
  136. return { m_leftCombInput->currentText(), m_rightCombInput->currentText(), leftClosed, rightClosed };
  137. }
  138. void setValue(const QString &lValue, const QString &rValue, const bool leftClosed = false,
  139. const bool rightClosed = false)
  140. {
  141. m_leftCombInput->setCurrentText(lValue);
  142. m_rightCombInput->setCurrentText(rValue);
  143. m_leftChb->setChecked(leftClosed);
  144. m_rightChb->setChecked(rightClosed);
  145. }
  146. private:
  147. QComboBox *m_leftCombInput;
  148. QComboBox *m_rightCombInput;
  149. QCheckBox *m_leftChb;
  150. QCheckBox *m_rightChb;
  151. };
  152. class MatterElementItemRangeDelegate : public QStyledItemDelegate
  153. {
  154. Q_OBJECT
  155. public:
  156. MatterElementItemRangeDelegate(QObject *parent = 0);
  157. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  158. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  159. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  160. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  161. const QModelIndex &index) const override;
  162. void setOriginItems(const QStringList &originItems, const QVector<double> &originValues);
  163. void updateOriginValues(const QVector<double> &values);
  164. private:
  165. QStringList m_originItems;
  166. QVector<double> m_originValues;
  167. };
  168. #endif // MATTERELEMENTITEMDELEGATE_H