#ifndef MATTERELEMENTITEMDELEGATE_H #define MATTERELEMENTITEMDELEGATE_H #include #include #include #include #include #include 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 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 &originValues); void updateOriginValues(const QVector &values); private: QStringList m_originItems; QVector m_originValues; }; #endif // MATTERELEMENTITEMDELEGATE_H