GreyClusteringItemDelegate.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_minSpin = new QDoubleSpinBox;
  21. m_maxSpin = new QDoubleSpinBox;
  22. m_minSpin->setRange(-1000, 1000);
  23. m_maxSpin->setRange(-1000, 1000);
  24. hLayout->addWidget(minLabel);
  25. hLayout->addWidget(m_minSpin);
  26. hLayout->addWidget(m_maxSpin);
  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. if (m_minSpin->value() < m_maxSpin->value()) {
  39. return true;
  40. }
  41. return false;
  42. }
  43. void setValue(const QString &value)
  44. {
  45. if (!value.isEmpty()) {
  46. QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(",");
  47. m_minSpin->setValue(tmp[0].toDouble());
  48. m_maxSpin->setValue(tmp[1].toDouble());
  49. }
  50. }
  51. private slots:
  52. void onConfirm() { m_result = QString("[%1, %2]").arg(m_minSpin->value()).arg(m_maxSpin->value()); }
  53. private:
  54. QDoubleSpinBox *m_minSpin;
  55. QDoubleSpinBox *m_maxSpin;
  56. QString m_result;
  57. };
  58. class GreyClusteringItemDelegate : public QStyledItemDelegate
  59. {
  60. Q_OBJECT
  61. public:
  62. GreyClusteringItemDelegate(QObject *parent = 0);
  63. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  64. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  65. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  66. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  67. const QModelIndex &index) const override;
  68. };
  69. #endif // GREYCLUSTERINGITEMDELEGATE_H