GreyClusteringItemDelegate.h 3.1 KB

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