DiagramDesigner/diagramCavas/source/propertyContentDlg.cpp

365 lines
13 KiB
C++
Raw Normal View History

#include "propertyContentDlg.h"
2025-07-22 20:00:46 +08:00
#include "baseProperty.h"
#include <QScrollArea>
#include <QFormLayout>
#include <QLabel>
#include <QSpinBox>
#include <QLineEdit>
#include <QDateEdit>
PropertyContentDlg::PropertyContentDlg(QWidget *parent)
2025-05-16 19:20:46 +08:00
: BaseContentDlg(parent)
{
_layout = new QVBoxLayout(this);
}
PropertyContentDlg::~PropertyContentDlg()
{
}
void PropertyContentDlg::createGroupView(groupStateInfo infos)
{
QScrollArea* scrollArea = new QScrollArea(this);
QWidget* content = new QWidget();
2025-04-09 16:20:34 +08:00
QFormLayout* formLayout = createFormLayout(content);
// 动态生成字段
for(auto& info:infos.info) {
2025-04-09 16:20:34 +08:00
QLabel* label = new QLabel(info.name,this);
QWidget* editor = createEditor(info);
formLayout->addRow(label, editor);
}
scrollArea->setWidget(content);
scrollArea->setWidgetResizable(true);
_layout->addWidget(scrollArea);
}
QWidget* PropertyContentDlg::createEditor(propertyStateInfo pro)
{
QWidget* pWidget = nullptr;
if(pro.type.contains("SMALLINT"))
{
QSpinBox* spin = new QSpinBox(this);
2025-07-23 17:27:35 +08:00
if(pro.lengthPrecision > 0)
spin->setRange(-pro.lengthPrecision,pro.lengthPrecision);
else
spin->setRange(-32768, 32767);
pWidget = spin;
}
else if(pro.type.contains("INTEGER"))
{
QSpinBox* spin = new QSpinBox(this);
2025-07-22 20:00:46 +08:00
if(pro.lengthPrecision > 0)
spin->setRange(-pro.lengthPrecision,pro.lengthPrecision);
2025-07-23 17:27:35 +08:00
else
spin->setRange(-32768,32767);
pWidget = spin;
}
else if(pro.type.contains("BIGINT"))
{
QLineEdit *lineEdit = new QLineEdit(this);
QRegularExpression regExp("^[+-]?(0|[1-9][0-9]{0,18})$");
QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, this);
lineEdit->setValidator(validator);
pWidget = lineEdit;
}
else if(pro.type.contains("REAL"))
{
QDoubleSpinBox* dbSpin = new QDoubleSpinBox(this);
2025-07-23 17:27:35 +08:00
dbSpin->setDecimals(4);
if(pro.lengthPrecision > 0)
dbSpin->setRange(-pro.lengthPrecision,pro.lengthPrecision);
else
dbSpin->setRange(-9999999,9999999);
pWidget = dbSpin;
}
else if(pro.type.contains("DOUBLE PRECISION"))
{
QDoubleSpinBox* dbSpin = new QDoubleSpinBox(this);
2025-07-23 17:27:35 +08:00
dbSpin->setDecimals(8);
2025-07-22 20:00:46 +08:00
if(pro.lengthPrecision > 0)
dbSpin->setRange(-pro.lengthPrecision,pro.lengthPrecision);
2025-07-23 17:27:35 +08:00
else
dbSpin->setRange(-9999999,9999999);
pWidget = dbSpin;
}
else if(pro.type.contains("NUMERIC") || pro.type.contains("DECIMAL"))
{
QLineEdit *lineEdit = new QLineEdit(this);
// 正则表达式:支持正负号、整数/小数、科学计数法
QRegularExpression regExp(
"^[+-]?" // 可选正负号
"(?:0|[1-9]\\d*)(?:\\.\\d+)?" // 整数部分(避免前导零)和小数部分
"(?:[eE][+-]?\\d+)?" // 科学计数法如e5, E-3
);
QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, this);
lineEdit->setValidator(validator);
pWidget = lineEdit;
}
else if(pro.type.contains("SERIAL") || pro.type.contains("BIGSERIAL"))
{
QLabel *label = new QLabel(this);
pWidget = label;
}
else if(pro.type.contains("CHAR") || pro.type.contains("VARCHAR"))
{
QLineEdit *lineEdit = new QLineEdit(this);
lineEdit->setMaxLength(pro.lengthPrecision);
pWidget = lineEdit;
}
else if(pro.type.contains("TEXT"))
{
QLineEdit *lineEdit = new QLineEdit(this);
pWidget = lineEdit;
}
else if(pro.type.contains("BYTEA"))
{
//todo:二进制文件上传
QLabel *label = new QLabel(this);
pWidget = label;
}
else if(pro.type.contains("DATE"))
{
QDateEdit* dateEdit = new QDateEdit(this);
dateEdit->setDisplayFormat("yyyy-MM-dd");
pWidget = dateEdit;
}
else if(pro.type.contains("TIME"))
{
QTimeEdit* timeEdit = new QTimeEdit(this);
timeEdit->setDisplayFormat("HH:mm:ss");
pWidget = timeEdit;
}
else if(pro.type.contains("TIMESTAMP"))
{
QDateTimeEdit* dateTimeEidt = new QDateTimeEdit(this);
dateTimeEidt->setDisplayFormat("yyyy-MM-dd HH:mm:ss");
pWidget = dateTimeEidt;
}
else if(pro.type.contains("UUID"))
{
QLineEdit *lineEdit = new QLineEdit(this);
QRegularExpression regExp("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, this);
lineEdit->setValidator(validator);
pWidget = lineEdit;
}
else if(pro.type.contains("JSON") || pro.type.contains("JSONB"))
{
QLineEdit *lineEdit = new QLineEdit(this);
pWidget = lineEdit;
}
if(pWidget)
{
pWidget->setProperty("name",pro.name);
propertyContentInfo info;
info.proName = pro.name;
info.proType = pro.type;
info.proEditer = pWidget;
_mapPro.insert(pro.name,info);
}
return pWidget;
}
2025-05-23 10:30:52 +08:00
QMap<QString,propertyStateInfo> PropertyContentDlg::getPropertyValue(BaseProperty* pPro)
{
QMap<QString,propertyStateInfo> map;
for(auto &pro:_mapPro)
{
propertyStateInfo info;
info.type = pro.proType;
info.name = pro.proName;
if(pro.proEditer != nullptr)
{
if(pro.proType.contains("SMALLINT") || pro.proType.contains("INTEGER"))
{
QSpinBox* spin = qobject_cast<QSpinBox*>(pro.proEditer);
if(spin)
info.defaultValue = spin->value();
}
else if(pro.proType.contains("BIGINT"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
info.defaultValue = lineEdit->text();
}
else if(pro.proType.contains("REAL") || pro.proType.contains("DOUBLE PRECISION"))
{
QDoubleSpinBox* dbSpin = qobject_cast<QDoubleSpinBox*>(pro.proEditer);
if(dbSpin)
info.defaultValue = dbSpin->value();
}
else if(pro.proType.contains("NUMERIC") || pro.proType.contains("DECIMAL"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
info.defaultValue = lineEdit->text();
}
else if(pro.proType.contains("SERIAL") || pro.proType.contains("BIGSERIAL"))
{
QLabel* label = qobject_cast<QLabel*>(pro.proEditer);
if(label)
info.defaultValue = label->text();
}
else if(pro.proType.contains("CHAR") || pro.proType.contains("VARCHAR"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
info.defaultValue = lineEdit->text();
}
else if(pro.proType.contains("TEXT"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
info.defaultValue = lineEdit->text();
}
else if(pro.proType.contains("BYTEA"))
{
QLabel* label = qobject_cast<QLabel*>(pro.proEditer);
if(label)
info.defaultValue = label->text();
}
else if(pro.proType.contains("DATE"))
{
QDateEdit* dateEdit = qobject_cast<QDateEdit*>(pro.proEditer);
if(dateEdit)
info.defaultValue = dateEdit->text();
}
else if(pro.proType.contains("TIME"))
{
QTimeEdit* timeEdit = qobject_cast<QTimeEdit*>(pro.proEditer);
if(timeEdit)
info.defaultValue = timeEdit->text();
}
else if(pro.proType.contains("TIMESTAMP"))
{
QDateTimeEdit* dateTimeEidt = qobject_cast<QDateTimeEdit*>(pro.proEditer);
if(dateTimeEidt)
info.defaultValue = dateTimeEidt->text();
}
else if(pro.proType.contains("UUID"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
info.defaultValue = lineEdit->text();
}
else if(pro.proType.contains("JSON") || pro.proType.contains("JSONB"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
info.defaultValue = lineEdit->text();
}
map.insert(pro.proName,info);
}
}
2025-07-18 18:26:13 +08:00
for(auto it = map.begin();it != map.end();++it) //值被手动改变过,锁定(保存后解除锁定
{
if(_curValue.contains(it.key()))
{
if(it->defaultValue != _curValue.value(it.key()).defaultValue)
{
it->lock = true;
}
}
}
2025-07-22 20:00:46 +08:00
pPro->setDataChanged(true);
return map;
}
2025-05-23 10:30:52 +08:00
void PropertyContentDlg::setPropertyValue(QVariant var)
{
2025-05-23 10:30:52 +08:00
QMap<QString,propertyStateInfo> map = var.value<QMap<QString,propertyStateInfo>>();
2025-07-18 18:26:13 +08:00
_curValue = map;
for(auto &info:map)
{
propertyContentInfo pro = _mapPro[info.name];
if(info.type.contains("SMALLINT") || info.type.contains("INTEGER"))
{
QSpinBox* spin = qobject_cast<QSpinBox*>(pro.proEditer);
if(spin)
spin->setValue(info.defaultValue.toInt());
}
else if(info.type.contains("BIGINT"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
lineEdit->setText(info.defaultValue.toString());
}
else if(info.type.contains("REAL") || info.type.contains("DOUBLE PRECISION"))
{
QDoubleSpinBox* dbSpin = qobject_cast<QDoubleSpinBox*>(pro.proEditer);
if(dbSpin)
dbSpin->setValue(info.defaultValue.toDouble());
}
else if(info.type.contains("NUMERIC") || info.type.contains("DECIMAL"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
lineEdit->setText(info.defaultValue.toString());
}
else if(info.type.contains("SERIAL") || info.type.contains("BIGSERIAL"))
{
QLabel* label = qobject_cast<QLabel*>(pro.proEditer);
if(label)
label->setText(info.defaultValue.toString());
}
else if(info.type.contains("CHAR") || info.type.contains("VARCHAR"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
lineEdit->setText(info.defaultValue.toString());
}
else if(info.type.contains("TEXT"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
lineEdit->setText(info.defaultValue.toString());
}
else if(info.type.contains("BYTEA"))
{
QLabel* label = qobject_cast<QLabel*>(pro.proEditer);
if(label)
label->setText(info.defaultValue.toString());
}
else if(info.type.contains("DATE"))
{
QDateEdit* dateEdit = qobject_cast<QDateEdit*>(pro.proEditer);
QDate date = QDate::fromString(info.defaultValue.toString(), "yyyy-MM-dd");
if(dateEdit)
dateEdit->setDate(date);
}
else if(info.type.contains("TIME"))
{
QTimeEdit* timeEdit = qobject_cast<QTimeEdit*>(pro.proEditer);
QTime time = QTime::fromString(info.defaultValue.toString(), "HH:mm:ss");
if(timeEdit)
timeEdit->setTime(time);
}
else if(info.type.contains("TIMESTAMP"))
{
QDateTimeEdit* dateTimeEidt = qobject_cast<QDateTimeEdit*>(pro.proEditer);
QDateTime dateTime = QDateTime::fromString(info.defaultValue.toString(), "yyyy-MM-dd HH:mm:ss");
if(dateTimeEidt)
dateTimeEidt->setDateTime(dateTime);
}
else if(info.type.contains("UUID"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
lineEdit->setText(info.defaultValue.toString());
}
else if(info.type.contains("JSON") || info.type.contains("JSONB"))
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(pro.proEditer);
if(lineEdit)
lineEdit->setText(info.defaultValue.toString());
}
}
}