#ifndef GREYCLUSTERINGITEMDELEGATE_H #define GREYCLUSTERINGITEMDELEGATE_H #include #include #include #include #include #include class RangeSpin : public QFrame { Q_OBJECT public: RangeSpin(QWidget *parent = nullptr) : QFrame(parent) { QHBoxLayout *hLayout = new QHBoxLayout; setStyleSheet("RangeSpin{background-color:gray} QLabel{color:white}"); hLayout->setMargin(0); hLayout->setSpacing(0); QLabel *minLabel = new QLabel("左"); QLabel *maxLabel = new QLabel("右"); m_leftSpin = new QDoubleSpinBox; m_rightSpin = new QDoubleSpinBox; m_leftChb = new QCheckBox; m_leftChb->setCheckable(true); m_leftChb->setChecked(true); m_rightChb = new QCheckBox; m_rightChb->setCheckable(true); m_rightChb->setChecked(true); m_leftSpin->setRange(-1000000, 1000000); m_rightSpin->setRange(-1000000, 1000000); hLayout->addWidget(minLabel); hLayout->addWidget(m_leftChb); hLayout->addWidget(m_leftSpin); hLayout->addWidget(m_rightSpin); hLayout->addWidget(m_rightChb); hLayout->addWidget(maxLabel); setLayout(hLayout); } virtual ~RangeSpin() { } QString result() { this->onConfirm(); return m_result; } bool valid() const { // 值好像有方向,不好控制 // if (m_leftSpin->value() < m_rightSpin->value()) { // return true; // } // return false; return true; } void setValue(const QString &value) { if (!value.isEmpty()) { QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(","); if (value[0] == "[") { m_leftChb->setChecked(true); } else { m_leftChb->setChecked(false); } m_leftSpin->setValue(tmp[0].toDouble()); m_rightSpin->setValue(tmp[1].toDouble()); if (value.right(1) == "]") { m_rightChb->setChecked(true); } else { m_rightChb->setChecked(false); } } } private slots: void onConfirm() { m_result = QString("%1%2, %3%4") .arg(m_leftChb->isChecked() ? "[" : "(") .arg(m_leftSpin->value()) .arg(m_rightSpin->value()) .arg(m_rightChb->isChecked() ? "]" : ")"); } private: QDoubleSpinBox *m_leftSpin; QDoubleSpinBox *m_rightSpin; QCheckBox *m_leftChb; QCheckBox *m_rightChb; QString m_result; }; class GreyClusteringItemRangeDelegate : public QStyledItemDelegate { Q_OBJECT public: GreyClusteringItemRangeDelegate(QObject *parent = 0); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; }; class GreyClusteringItemSpinDelegate : public QStyledItemDelegate { Q_OBJECT public: GreyClusteringItemSpinDelegate(QObject *parent = 0); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; }; #endif // GREYCLUSTERINGITEMDELEGATE_H