DiagramDesigner/diagramCavas/source/structDataPropertyDelegate.cpp

211 lines
7.2 KiB
C++
Raw Normal View History

2026-01-09 17:43:58 +08:00
#include <QLineEdit>
#include <QComboBox>
#include <QSpinBox>
#include <QCheckBox>
#include <QCompleter>
#include <QEvent>
#include <QKeyEvent>
#include "structDataPropertyDelegate.h"
#include "structDataPropertyModel.h"
#include "uiCommunicationBus.h"
StructDataPropertyDelegate::StructDataPropertyDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
QWidget* StructDataPropertyDelegate::createEditor(QWidget* parent,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
Q_UNUSED(option);
int column = index.column();
switch (column) {
case StructDataPropertyModel::ColConnectPara:
return createConnectParaEditor(parent);
case StructDataPropertyModel::ColDefaultValue:
return createDefaultValueEditor(parent, index);
case StructDataPropertyModel::ColLengthPrecision:
return createLengthPrecisionEditor(parent);
}
return nullptr;
}
void StructDataPropertyDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
int column = index.column();
QVariant value = index.data(Qt::EditRole);
switch (column) {
case StructDataPropertyModel::ColConnectPara:
if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
lineEdit->setText(value.toString());
}
break;
case StructDataPropertyModel::ColDefaultValue:
setDefaultValueEditorData(editor, value, index);
break;
case StructDataPropertyModel::ColLengthPrecision:
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
spinBox->setValue(value.toInt());
}
break;
}
}
void StructDataPropertyDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index) const {
int column = index.column();
QVariant newValue;
switch (column) {
case StructDataPropertyModel::ColConnectPara:
if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
newValue = lineEdit->text().trimmed();
}
break;
case StructDataPropertyModel::ColDefaultValue:
newValue = getDefaultValueEditorData(editor, index);
break;
case StructDataPropertyModel::ColLengthPrecision:
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
newValue = spinBox->value();
}
break;
}
if (!newValue.isNull()) {
model->setData(index, newValue, Qt::EditRole);
}
}
void StructDataPropertyDelegate::updateEditorGeometry(QWidget* editor,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
QString StructDataPropertyDelegate::displayText(const QVariant& value, const QLocale& locale) const {
Q_UNUSED(locale);
// 格式化显示文本
if (value.userType() == QMetaType::Bool) {
return value.toBool() ? "" : "";
}
return QStyledItemDelegate::displayText(value, locale);
}
QWidget* StructDataPropertyDelegate::createConnectParaEditor(QWidget* parent) const {
QLineEdit* lineEdit = new QLineEdit(parent);
lineEdit->setMaxLength(150);
lineEdit->installEventFilter(const_cast<StructDataPropertyDelegate*>(this));
lineEdit->setPlaceholderText("空格获取初始值");
lineEdit->setCompleter(_connectCompleter);
connect(lineEdit, &QLineEdit::textChanged, this, [=](const QString &text) {
if (text.endsWith(".")) {
onConnectParamChanged(text);
}
});
return lineEdit;
}
QWidget* StructDataPropertyDelegate::createDefaultValueEditor(QWidget* parent, const QModelIndex& index) const {
// 根据数据类型创建不同的编辑器
QString dataType = index.sibling(index.row(), StructDataPropertyModel::ColDataType).data().toString().toLower();
if (dataType == "int" || dataType == "integer") {
QSpinBox* spinBox = new QSpinBox(parent);
spinBox->setRange(-999999, 999999);
return spinBox;
} else if (dataType == "float" || dataType == "double") {
QDoubleSpinBox* spinBox = new QDoubleSpinBox(parent);
spinBox->setRange(-999999.0, 999999.0);
spinBox->setDecimals(3);
return spinBox;
} else if (dataType == "bool" || dataType == "boolean") {
QComboBox* comboBox = new QComboBox(parent);
comboBox->addItems({"false", "true"});
return comboBox;
} else {
// 字符串或其他类型
QLineEdit* lineEdit = new QLineEdit(parent);
return lineEdit;
}
}
QWidget* StructDataPropertyDelegate::createLengthPrecisionEditor(QWidget* parent) const {
QSpinBox* spinBox = new QSpinBox(parent);
spinBox->setRange(0, 999);
spinBox->setSpecialValueText("无限制");
return spinBox;
}
void StructDataPropertyDelegate::setDefaultValueEditorData(QWidget* editor, const QVariant& value, const QModelIndex& index) const {
QString dataType = index.sibling(index.row(), StructDataPropertyModel::ColDataType).data().toString().toLower();
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
spinBox->setValue(value.toInt());
} else if (QDoubleSpinBox* doubleSpinBox = qobject_cast<QDoubleSpinBox*>(editor)) {
doubleSpinBox->setValue(value.toDouble());
} else if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
bool boolValue = value.toBool();
comboBox->setCurrentIndex(boolValue ? 1 : 0);
} else if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
lineEdit->setText(value.toString());
}
}
QVariant StructDataPropertyDelegate::getDefaultValueEditorData(QWidget* editor, const QModelIndex& index) const {
QString dataType = index.sibling(index.row(), StructDataPropertyModel::ColDataType).data().toString().toLower();
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
return spinBox->value();
} else if (QDoubleSpinBox* doubleSpinBox = qobject_cast<QDoubleSpinBox*>(editor)) {
return doubleSpinBox->value();
} else if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
return comboBox->currentIndex() == 1;
} else if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
return lineEdit->text();
}
return QVariant();
}
void StructDataPropertyDelegate::onConnectParamChanged(const QString& str) const
{
QVariantMap map;
map.insert("input",str);
UiCommunicationBus::instance()->sendHttpRequest("/measurement/recommend",QVariant(),"GET",map);
}
bool StructDataPropertyDelegate::eventFilter(QObject *obj, QEvent *event)
{
if (QLineEdit *editor = qobject_cast<QLineEdit*>(obj)){
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Space) {
qDebug() << "eventFilter:space";
onConnectParamChanged(editor->text());
_connectCompleter->complete();
return true;
}
if (keyEvent->text() == ".") {
qDebug() << "eventFilter:dot";
// 返回false让事件继续传播
}
}
}
return QStyledItemDelegate::eventFilter(obj, event);
}