123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- #ifndef GREYCLUSTERINGITEMDELEGATE_H
- #define GREYCLUSTERINGITEMDELEGATE_H
- #include <QStyledItemDelegate>
- #include <QHBoxLayout>
- #include <QLabel>
- #include <QDoubleSpinBox>
- #include <QPushButton>
- #include <QCheckBox>
- 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
|