GreyClusteringItemDelegate.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef GREYCLUSTERINGITEMDELEGATE_H
  2. #define GREYCLUSTERINGITEMDELEGATE_H
  3. #include <QStyledItemDelegate>
  4. #include <QHBoxLayout>
  5. #include <QLabel>
  6. #include <QDoubleSpinBox>
  7. #include <QPushButton>
  8. #include <QCheckBox>
  9. class RangeSpin : public QFrame
  10. {
  11. Q_OBJECT
  12. public:
  13. RangeSpin(QWidget *parent = nullptr) : QFrame(parent)
  14. {
  15. QHBoxLayout *hLayout = new QHBoxLayout;
  16. setStyleSheet("RangeSpin{background-color:gray} QLabel{color:white}");
  17. hLayout->setMargin(0);
  18. hLayout->setSpacing(0);
  19. QLabel *minLabel = new QLabel("左");
  20. QLabel *maxLabel = new QLabel("右");
  21. m_leftSpin = new QDoubleSpinBox;
  22. m_rightSpin = new QDoubleSpinBox;
  23. m_leftChb = new QCheckBox;
  24. m_leftChb->setCheckable(true);
  25. m_leftChb->setChecked(true);
  26. m_rightChb = new QCheckBox;
  27. m_rightChb->setCheckable(true);
  28. m_rightChb->setChecked(true);
  29. m_leftSpin->setRange(-1000000, 1000000);
  30. m_rightSpin->setRange(-1000000, 1000000);
  31. hLayout->addWidget(minLabel);
  32. hLayout->addWidget(m_leftChb);
  33. hLayout->addWidget(m_leftSpin);
  34. hLayout->addWidget(m_rightSpin);
  35. hLayout->addWidget(m_rightChb);
  36. hLayout->addWidget(maxLabel);
  37. setLayout(hLayout);
  38. }
  39. virtual ~RangeSpin() { }
  40. QString result()
  41. {
  42. this->onConfirm();
  43. return m_result;
  44. }
  45. bool valid() const
  46. {
  47. // 值好像有方向,不好控制
  48. // if (m_leftSpin->value() < m_rightSpin->value()) {
  49. // return true;
  50. // }
  51. // return false;
  52. return true;
  53. }
  54. void setValue(const QString &value)
  55. {
  56. if (!value.isEmpty()) {
  57. QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(",");
  58. if (value[0] == "[") {
  59. m_leftChb->setChecked(true);
  60. } else {
  61. m_leftChb->setChecked(false);
  62. }
  63. m_leftSpin->setValue(tmp[0].toDouble());
  64. m_rightSpin->setValue(tmp[1].toDouble());
  65. if (value.right(1) == "]") {
  66. m_rightChb->setChecked(true);
  67. } else {
  68. m_rightChb->setChecked(false);
  69. }
  70. }
  71. }
  72. private slots:
  73. void onConfirm()
  74. {
  75. m_result = QString("%1%2, %3%4")
  76. .arg(m_leftChb->isChecked() ? "[" : "(")
  77. .arg(m_leftSpin->value())
  78. .arg(m_rightSpin->value())
  79. .arg(m_rightChb->isChecked() ? "]" : ")");
  80. }
  81. private:
  82. QDoubleSpinBox *m_leftSpin;
  83. QDoubleSpinBox *m_rightSpin;
  84. QCheckBox *m_leftChb;
  85. QCheckBox *m_rightChb;
  86. QString m_result;
  87. };
  88. class GreyClusteringItemRangeDelegate : public QStyledItemDelegate
  89. {
  90. Q_OBJECT
  91. public:
  92. GreyClusteringItemRangeDelegate(QObject *parent = 0);
  93. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  94. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  95. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  96. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  97. const QModelIndex &index) const override;
  98. };
  99. class GreyClusteringItemSpinDelegate : public QStyledItemDelegate
  100. {
  101. Q_OBJECT
  102. public:
  103. GreyClusteringItemSpinDelegate(QObject *parent = 0);
  104. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  105. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  106. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  107. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  108. const QModelIndex &index) const override;
  109. };
  110. #endif // GREYCLUSTERINGITEMDELEGATE_H