123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #ifndef MATTERELEMENTITEMDELEGATE_H
- #define MATTERELEMENTITEMDELEGATE_H
- #include <QStyledItemDelegate>
- #include <QFormLayout>
- #include <QDoubleSpinBox>
- #include <QComboBox>
- #include <QCheckBox>
- #include <QLabel>
- class InsideWidget : public QFrame
- {
- Q_OBJECT
- public:
- InsideWidget(QWidget *parent = nullptr) : QFrame(parent)
- {
- QHBoxLayout *vcenter = new QHBoxLayout;
- vcenter->setMargin(0);
- vcenter->setSpacing(0);
- QFormLayout *layout = new QFormLayout;
- setStyleSheet("InsideWidget{background-color:gray} QLabel{color:white}");
- layout->setLabelAlignment(Qt::AlignCenter);
- vcenter->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Expanding));
- vcenter->addLayout(layout);
- vcenter->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Expanding));
- layout->setMargin(2);
- layout->setSpacing(2);
- for (int i = 0; i < 4; ++i) {
- auto s = new QDoubleSpinBox;
- s->setFixedWidth(70);
- s->setFrame(false);
- s->setMinimum(-10000);
- s->setMaximum(10000);
- m_spins << s;
- }
- layout->addRow("[很差,交差]中间值:", m_spins.at(0));
- layout->addRow("[较差,一般]中间值:", m_spins.at(1));
- layout->addRow("[一般,较高]中间值:", m_spins.at(2));
- layout->addRow("[较高,很高]中间值:", m_spins.at(3));
- setLayout(vcenter);
- }
- virtual ~InsideWidget() { }
- QString result()
- {
- this->onConfirm();
- return m_result;
- }
- void setValue(const QString &value)
- {
- if (!value.isEmpty()) {
- QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(",");
- for (int i = 0; i < tmp.size(); ++i) {
- m_spins.at(i)->setValue(tmp[i].toDouble());
- }
- }
- }
- private slots:
- void onConfirm()
- {
- m_result = QString("[%1, %2, %3, %4]")
- .arg(m_spins.at(0)->value())
- .arg(m_spins.at(1)->value())
- .arg(m_spins.at(2)->value())
- .arg(m_spins.at(3)->value());
- }
- private:
- QList<QDoubleSpinBox *> m_spins;
- QString m_result;
- };
- class MatterElementItemSpinDelegate : public QStyledItemDelegate
- {
- Q_OBJECT
- public:
- MatterElementItemSpinDelegate(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 MatterElementItemInsideDelegate : public QStyledItemDelegate
- {
- Q_OBJECT
- public:
- MatterElementItemInsideDelegate(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 RangeWidget : public QFrame
- {
- Q_OBJECT
- public:
- RangeWidget(const QStringList &items, 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_leftChb = new QCheckBox;
- m_leftChb->setCheckable(true);
- m_leftChb->setChecked(true);
- m_rightChb = new QCheckBox;
- m_rightChb->setCheckable(true);
- m_rightChb->setChecked(true);
- QFormLayout *layout = new QFormLayout;
- setStyleSheet("RangeWidget{background-color:gray}");
- layout->setMargin(10);
- layout->setSpacing(2);
- m_leftCombInput = new QComboBox();
- m_leftCombInput->setMaxVisibleItems(15);
- m_leftCombInput->setEditable(true);
- m_leftCombInput->addItems(items);
- m_leftCombInput->setCurrentIndex(0);
- m_rightCombInput = new QComboBox();
- m_rightCombInput->setMaxVisibleItems(15);
- m_rightCombInput->setEditable(true);
- m_rightCombInput->addItems(items);
- m_rightCombInput->setCurrentIndex(0);
- layout->addRow("左:", m_leftCombInput);
- layout->addRow("右:", m_rightCombInput);
- hLayout->addWidget(minLabel);
- hLayout->addWidget(m_leftChb);
- hLayout->addLayout(layout);
- hLayout->addWidget(m_rightChb);
- hLayout->addWidget(maxLabel);
- setLayout(hLayout);
- }
- virtual ~RangeWidget() { }
- QStringList result()
- {
- QString leftClosed = m_leftChb->isChecked() ? "[" : "(";
- QString rightClosed = m_rightChb->isChecked() ? "]" : ")";
- return { m_leftCombInput->currentText(), m_rightCombInput->currentText(), leftClosed, rightClosed };
- }
- void setValue(const QString &lValue, const QString &rValue, const bool leftClosed = false,
- const bool rightClosed = false)
- {
- m_leftCombInput->setCurrentText(lValue);
- m_rightCombInput->setCurrentText(rValue);
- m_leftChb->setChecked(leftClosed);
- m_rightChb->setChecked(rightClosed);
- }
- private:
- QComboBox *m_leftCombInput;
- QComboBox *m_rightCombInput;
- QCheckBox *m_leftChb;
- QCheckBox *m_rightChb;
- };
- class MatterElementItemRangeDelegate : public QStyledItemDelegate
- {
- Q_OBJECT
- public:
- MatterElementItemRangeDelegate(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;
- void setOriginItems(const QStringList &originItems, const QVector<double> &originValues);
- void updateOriginValues(const QVector<double> &values);
- private:
- QStringList m_originItems;
- QVector<double> m_originValues;
- };
- #endif // MATTERELEMENTITEMDELEGATE_H
|