123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- #ifndef GREYCLUSTERINGITEMDELEGATE_H
- #define GREYCLUSTERINGITEMDELEGATE_H
- #include <QStyledItemDelegate>
- #include <QHBoxLayout>
- #include <QLabel>
- #include <QDoubleSpinBox>
- #include <QPushButton>
- 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_leftSpin->setRange(-1000, 1000);
- m_rightSpin->setRange(-1000, 1000);
- hLayout->addWidget(minLabel);
- hLayout->addWidget(m_leftSpin);
- hLayout->addWidget(m_rightSpin);
- 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(",");
- m_leftSpin->setValue(tmp[0].toDouble());
- m_rightSpin->setValue(tmp[1].toDouble());
- }
- }
- private slots:
- void onConfirm() { m_result = QString("[%1, %2]").arg(m_leftSpin->value()).arg(m_rightSpin->value()); }
- private:
- QDoubleSpinBox *m_leftSpin;
- QDoubleSpinBox *m_rightSpin;
- 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
|