2026-01-09 17:43:58 +08:00
|
|
|
#include "structDataPropertyModel.h"
|
|
|
|
|
#include "structDataSource.h"
|
|
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StructDataPropertyModel::StructDataPropertyModel(StructDataSource* dataManager, QObject* parent)
|
|
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
|
, m_dataManager(dataManager)
|
|
|
|
|
{
|
|
|
|
|
connect(m_dataManager, &StructDataSource::propertyUpdated,
|
|
|
|
|
this, &StructDataPropertyModel::onPropertyUpdated);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置要显示的code列表
|
|
|
|
|
void StructDataPropertyModel::setPropertyCodes(const QStringList& codes) {
|
|
|
|
|
beginResetModel();
|
|
|
|
|
m_propertyCodes = codes;
|
|
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
|
|
//emit propertiesLoaded(m_propertyCodes.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置要显示的属性
|
|
|
|
|
void StructDataPropertyModel::setProperties(const QVector<ExtraProperty>& properties) {
|
|
|
|
|
QStringList codes;
|
|
|
|
|
for (const auto& prop : properties) {
|
|
|
|
|
if (!prop.code.isEmpty()) {
|
|
|
|
|
codes.append(prop.code);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
setPropertyCodes(codes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int StructDataPropertyModel::rowCount(const QModelIndex& parent) const {
|
|
|
|
|
return parent.isValid() ? 0 : m_propertyCodes.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int StructDataPropertyModel::columnCount(const QModelIndex& parent) const {
|
|
|
|
|
return parent.isValid() ? 0 : ColumnCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant StructDataPropertyModel::data(const QModelIndex& index, int role) const {
|
|
|
|
|
if (!index.isValid() || index.row() >= m_propertyCodes.size()) {
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ExtraProperty* prop = getProperty(index.row());
|
|
|
|
|
if (!prop) {
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int col = index.column();
|
|
|
|
|
|
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
|
|
|
|
switch (col) {
|
|
|
|
|
case ColName: return prop->name;
|
|
|
|
|
case ColTag: return prop->tag;
|
|
|
|
|
case ColCode: return prop->code;
|
|
|
|
|
case ColSourceType: return "属性";
|
|
|
|
|
case ColConnectPara: return prop->connect_para;
|
|
|
|
|
case ColModelName: return prop->sourceConfig.value("modelName");
|
|
|
|
|
default: return getPropertyData(*prop, col);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (role == Qt::UserRole) {
|
|
|
|
|
return QVariant::fromValue(*prop);
|
|
|
|
|
}
|
|
|
|
|
else if (role == Qt::UserRole + 1) {
|
|
|
|
|
return prop->code; // 返回code用于查找
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StructDataPropertyModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
|
|
|
|
if (!index.isValid() || index.row() >= m_propertyCodes.size() || role != Qt::EditRole) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExtraProperty* prop = getProperty(index.row());
|
|
|
|
|
if (!prop) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int col = index.column();
|
|
|
|
|
bool changed = false;
|
|
|
|
|
ExtraProperty updatedProp = *prop; // 创建副本
|
|
|
|
|
|
|
|
|
|
if (col == ColConnectPara) {
|
|
|
|
|
// 连接参数自由文本输入
|
|
|
|
|
QString newValue = value.toString().trimmed();
|
|
|
|
|
if (updatedProp.connect_para != newValue) {
|
|
|
|
|
updatedProp.connect_para = newValue;
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 修改属性数据
|
|
|
|
|
propertyStateInfo* data = m_dataManager->getPropertyData(updatedProp);
|
|
|
|
|
if (data) {
|
|
|
|
|
changed = updatePropertyData(data, col, value);
|
|
|
|
|
if (changed) {
|
|
|
|
|
emit m_dataManager->dataChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
|
// 更新到DataManager
|
2026-01-16 18:40:46 +08:00
|
|
|
updatedProp.bDataChanged = true; //修改后设为真
|
2026-01-09 17:43:58 +08:00
|
|
|
if (m_dataManager->updateProperty(updatedProp)) {
|
|
|
|
|
emit dataChanged(index, index, {role});
|
|
|
|
|
emit propertyModified(index.row(), updatedProp);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Qt::ItemFlags StructDataPropertyModel::flags(const QModelIndex& index) const {
|
|
|
|
|
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
|
|
|
|
|
|
|
|
|
|
int col = index.column();
|
|
|
|
|
if (col == ColConnectPara ||
|
|
|
|
|
col == ColLengthPrecision ||
|
|
|
|
|
col == ColDefaultValue) {
|
|
|
|
|
flags |= Qt::ItemIsEditable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant StructDataPropertyModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
|
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
|
|
|
QStringList headers = {
|
|
|
|
|
"名称", "标签", "编码", "类型", "连接参数",
|
|
|
|
|
"模型名", "数据类型", "默认值", "长度精度"
|
|
|
|
|
};
|
|
|
|
|
return headers.value(section, "");
|
|
|
|
|
}
|
|
|
|
|
return QAbstractTableModel::headerData(section, orientation, role);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前显示的属性
|
|
|
|
|
QVector<ExtraProperty> StructDataPropertyModel::getDisplayedProperties() const {
|
|
|
|
|
QVector<ExtraProperty> result;
|
|
|
|
|
for (const QString& code : m_propertyCodes) {
|
|
|
|
|
ExtraProperty* prop = m_dataManager->getPropertyByCode(code);
|
|
|
|
|
if (prop) {
|
|
|
|
|
result.append(*prop);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前显示的code列表
|
|
|
|
|
QStringList StructDataPropertyModel::getDisplayedCodes() const {
|
|
|
|
|
return m_propertyCodes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据code查找行
|
|
|
|
|
int StructDataPropertyModel::findRowByCode(const QString& code) const {
|
|
|
|
|
return m_propertyCodes.indexOf(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 刷新指定code的行
|
|
|
|
|
void StructDataPropertyModel::refreshRow(const QString& code) {
|
|
|
|
|
int row = findRowByCode(code);
|
|
|
|
|
if (row >= 0) {
|
|
|
|
|
QModelIndex start = index(row, 0);
|
|
|
|
|
QModelIndex end = index(row, columnCount() - 1);
|
|
|
|
|
emit dataChanged(start, end);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StructDataPropertyModel::onPropertyUpdated(const ExtraProperty& updatedProp, bool isNew) {
|
|
|
|
|
Q_UNUSED(isNew);
|
|
|
|
|
|
|
|
|
|
// 如果这个属性在当前显示列表中,刷新对应行
|
|
|
|
|
int row = findRowByCode(updatedProp.code);
|
|
|
|
|
if (row >= 0) {
|
|
|
|
|
refreshRow(updatedProp.code);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExtraProperty* StructDataPropertyModel::getProperty(int displayRow) const {
|
|
|
|
|
if (displayRow < 0 || displayRow >= m_propertyCodes.size()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return m_dataManager->getPropertyByCode(m_propertyCodes[displayRow]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant StructDataPropertyModel::getPropertyData(const ExtraProperty& prop, int col) const {
|
|
|
|
|
propertyStateInfo* data = m_dataManager->getPropertyData(prop);
|
|
|
|
|
if (!data) return QVariant();
|
|
|
|
|
|
|
|
|
|
switch (col) {
|
|
|
|
|
case ColDataType: return data->type;
|
|
|
|
|
case ColDefaultValue: return data->defaultValue;
|
|
|
|
|
case ColLengthPrecision: return data->lengthPrecision;
|
|
|
|
|
}
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StructDataPropertyModel::updatePropertyData(propertyStateInfo* data, int col, const QVariant& value) {
|
|
|
|
|
switch (col) {
|
|
|
|
|
case ColLengthPrecision:
|
|
|
|
|
if (data->lengthPrecision != value.toInt()) {
|
|
|
|
|
data->lengthPrecision = value.toInt();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ColDefaultValue:
|
|
|
|
|
if (data->defaultValue != value) {
|
|
|
|
|
data->defaultValue = value;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|