1635 lines
61 KiB
C++
1635 lines
61 KiB
C++
#include <QMessageBox>
|
||
#include <QJsonArray>
|
||
#include <QMenu>
|
||
#include <QTableWidgetItem>
|
||
#include "ui_projectModelDlg.h"
|
||
#include "projectModelDlg.h"
|
||
#include "dataBase.h"
|
||
#include "global.h"
|
||
#include "ProjectTableDelegate.h"
|
||
|
||
const QSet<QString> stringDataTypes = {"varchar", "char", "text", "date", "time", "timestamp"};
|
||
|
||
projectModelDlg::projectModelDlg(QWidget *parent)
|
||
: QDialog(parent)
|
||
, ui(new Ui::projectModelDlg)
|
||
,_curRow(-1)
|
||
{
|
||
ui->setupUi(this);
|
||
this->setWindowFlags(Qt::FramelessWindowHint | windowFlags());
|
||
setWindowModality(Qt::WindowModal);
|
||
initialTypeMap();
|
||
setupUI();
|
||
initial();
|
||
}
|
||
|
||
projectModelDlg::~projectModelDlg()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void projectModelDlg::initial()
|
||
{
|
||
connect(ui->btn_save,&QPushButton::clicked,this,&projectModelDlg::onSaveClicked);
|
||
connect(ui->btn_cancel,&QPushButton::clicked,this,&projectModelDlg::onCancelClicked);
|
||
connect(ui->btn_apply,&QPushButton::clicked,this,&projectModelDlg::onApplyClicked);
|
||
connect(ui->btn_revoke,&QPushButton::clicked,this,&projectModelDlg::onRevokeClicked);
|
||
|
||
//connect(ui->cb_baseModel,&QComboBox::textActivated,this,&projectModelDlg::onBaseModelIndexChanged);
|
||
//connect(ui->cb_projectModel,&QComboBox::textActivated,this,&projectModelDlg::onProjectIndexChanged);
|
||
//connect(ui->cb_property,&QComboBox::textActivated,this,&projectModelDlg::onPropertyIndexChanged);
|
||
|
||
//ui->treeView_model->setContextMenuPolicy(Qt::CustomContextMenu);
|
||
connect(ui->tableWidget_model, &QTableWidget::customContextMenuRequested, this, &projectModelDlg::onIndexRbtnClicked);
|
||
//connect(ui->treeView_model, &QTreeView::clicked, this, &projectModelDlg::onIndexClicked);
|
||
|
||
connect(ui->btn_new, &QPushButton::clicked, this, &projectModelDlg::addPropertyGroup);
|
||
connect(ui->tableWidget_model, &QTableWidget::itemClicked, this, &projectModelDlg::onTableItemClicked);
|
||
initialModel();
|
||
initialList();
|
||
update();
|
||
}
|
||
|
||
MapProperty projectModelDlg::addNewProject(const QString& sMeta,const QString& sProject,PropertyModel& model)
|
||
{
|
||
MapProperty mt;
|
||
QStringList lstProperty = getGroupList(sMeta); //返回元模下的属性组名
|
||
//lstProperty<<QString("base")<<QString("seperation");
|
||
|
||
QMap<QString,QJsonObject> mapCheckState = DataBase::GetInstance()->getCheckStateFromManager(sProject); //获取选择状态
|
||
for(auto &property:lstProperty)
|
||
{
|
||
PropertyPage struProperty;
|
||
|
||
if(mapCheckState.contains(property) && !model.formerMeta.bChanged) //生成的模型中勾选了该属性组且元模未改变过
|
||
{
|
||
QJsonObject obj = mapCheckState[property];
|
||
QJsonArray nodesJsonArray = obj["checkState"].toArray();
|
||
|
||
for (QJsonValueRef nodeJson : nodesJsonArray)
|
||
{
|
||
QJsonObject node = nodeJson.toObject();
|
||
QString propertyName = node["name"].toString();
|
||
int nState = node["checked"].toInt();
|
||
QString dataType = node["type"].toString();
|
||
QString defaultValue = node["defaultValue"].toString();
|
||
|
||
QStandardItem* pItem = new QStandardItem(propertyName);
|
||
setItemAttribute(propertyName,pItem);
|
||
PropertyState sta;
|
||
sta.dataType = dataType;
|
||
if(nState)
|
||
{
|
||
QStandardItem* pGroup = nullptr;
|
||
if(model.pSelect->findItems(propertyName).isEmpty())
|
||
{
|
||
pGroup = new QStandardItem(property);
|
||
model.pSelect->appendRow(pGroup); //属性的组未存在,将组添加到model
|
||
}
|
||
else
|
||
{
|
||
pGroup = model.pSelect->findItems(propertyName)[0];
|
||
}
|
||
|
||
if(pGroup){
|
||
pGroup->appendRow(pItem);
|
||
sta.checkState = true;
|
||
struProperty.mCheckState.insert(propertyName,sta);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
QStandardItem* pGroup = nullptr;
|
||
if(model.pBase->findItems(propertyName).isEmpty())
|
||
{
|
||
pGroup = new QStandardItem(property);
|
||
model.pBase->appendRow(pGroup); //属性的组未存在,将组添加到model
|
||
}
|
||
else
|
||
{
|
||
pGroup = model.pBase->findItems(propertyName)[0];
|
||
}
|
||
|
||
if(pGroup){
|
||
pGroup->appendRow(pItem);
|
||
sta.checkState = false;
|
||
struProperty.mCheckState.insert(propertyName,sta);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else //未勾选属性组或是新建或元模已改变
|
||
{
|
||
QStandardItem* pGroup = nullptr; //新建的不包含属性组
|
||
pGroup = new QStandardItem(property);
|
||
model.pBase->appendRow(pGroup);
|
||
|
||
QStringList lstName = getAttributeList(sMeta,property);
|
||
for(auto &name:lstName)
|
||
{
|
||
QStandardItem* pItem = new QStandardItem(name);
|
||
setItemAttribute(name,pItem);
|
||
pGroup->appendRow(pItem);
|
||
QString dataType = getItemDataType(pItem);
|
||
PropertyState sta;
|
||
sta.dataType = dataType;
|
||
sta.checkState = false;
|
||
struProperty.mCheckState.insert(name,sta); //初始都是未选择状态
|
||
}
|
||
}
|
||
mt.insert(property,struProperty);
|
||
}
|
||
|
||
return mt;
|
||
}
|
||
|
||
void projectModelDlg::initialModel()
|
||
{
|
||
QStringList lstModel = getModelList();
|
||
for(auto &model:lstModel)
|
||
{
|
||
MapProject mp;
|
||
QMap<QString,int> mapProject = DataBase::GetInstance()->getProjectFromManager(model);
|
||
|
||
|
||
QMap<QString,int>::Iterator iter;
|
||
for(iter = mapProject.begin();iter != mapProject.end(); ++iter)
|
||
{
|
||
QStandardItem* propertyItem = new QStandardItem(iter.key());
|
||
|
||
PropertyModel pm;
|
||
pm.pBase = new QStandardItemModel(this);
|
||
pm.pSelect = new QStandardItemModel(this);
|
||
pm.mapProperty = addNewProject(model,iter.key(),pm);
|
||
pm.nType = iter.value();
|
||
pm.formerMeta.sName = model;
|
||
pm.formerProject.sName = iter.key();
|
||
pm.dataInfo = DataBase::GetInstance()->getProjectModelGroupInfo(iter.key());
|
||
mp.insert(iter.key(),pm);
|
||
}
|
||
|
||
//todo:读取存储,按分类遍历名称
|
||
m_mapTotal.insert(model,mp);
|
||
}
|
||
}
|
||
|
||
void projectModelDlg::initialList()
|
||
{
|
||
MapMeta::Iterator iter;
|
||
for(iter = m_mapTotal.begin();iter != m_mapTotal.end();++iter)
|
||
{
|
||
MapProject proj = iter.value();
|
||
MapProject::Iterator it;
|
||
for(it = proj.begin();it != proj.end();++it)
|
||
{
|
||
int row = ui->tableWidget_model->rowCount();
|
||
ui->tableWidget_model->insertRow(row);
|
||
|
||
// 工程模列(可编辑)
|
||
QTableWidgetItem* projectItem = new QTableWidgetItem(it.key());
|
||
//projectItem->setFlags(projectItem->flags() | Qt::ItemIsEditable);
|
||
ui->tableWidget_model->setItem(row, 0, projectItem);
|
||
projectItem->setData(Qt::UserRole,TS_select);
|
||
|
||
// 元模型列(初始化值)
|
||
QTableWidgetItem* metaItem = new QTableWidgetItem(iter.key());
|
||
ui->tableWidget_model->setItem(row, 1, metaItem);
|
||
|
||
// 元件类型列(初始提示)
|
||
QString strType = _mapType.key(it->nType);
|
||
QTableWidgetItem* typeItem = new QTableWidgetItem(strType);
|
||
ui->tableWidget_model->setItem(row, 2, typeItem);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
/*void projectModelDlg::update()
|
||
{
|
||
for(MapMeta::Iterator iter = m_mapTotal.begin();iter != m_mapTotal.end();++iter)
|
||
{
|
||
ui->cb_baseModel->addItem(iter.key());
|
||
}
|
||
}*/
|
||
|
||
void projectModelDlg::generate(const QString& str)
|
||
{
|
||
MapMeta::Iterator iter = m_mapTotal.find(_curMeta); //获取元模下的工程
|
||
if(iter != m_mapTotal.end())
|
||
{
|
||
MapProject mp = iter.value();
|
||
MapProject::Iterator ite = mp.find(_curProject); //获取工程下的属性组
|
||
if(ite != mp.end())
|
||
{
|
||
MapProperty mapProperty = ite.value().mapProperty;
|
||
|
||
bool createRes = true; //动态表生成结果
|
||
for(MapProperty::Iterator it = mapProperty.begin();it != mapProperty.end();++it){ //每个属性组单独生成表
|
||
int nType = ite.value().nType;
|
||
QStandardItemModel* pSelectModel = ite->pSelect;
|
||
QStandardItemModel* pBaseModel = ite->pBase;
|
||
/*QStandardItem *rootItem = pSelectModel->invisibleRootItem();
|
||
if(rootItem->rowCount() == 0) //修改逻辑,未勾选的属性组不生成表 by/20250311
|
||
continue;*/
|
||
QList<QStandardItem*> lstSelected = getGroupSub(pSelectModel,it.key());
|
||
QList<QStandardItem*> lstBase = getGroupSub(pBaseModel,it.key());
|
||
if(!lstSelected.isEmpty())
|
||
{
|
||
bool res = createPropertyTable(str,it.key(),lstSelected,lstBase,nType);
|
||
createRes = createRes && res;
|
||
}
|
||
}
|
||
if(createRes)
|
||
{
|
||
/*PropertyModel pm = m_mapTotal[_curMeta].take(_curProject); //取出要保存的对象,另存为新索引
|
||
m_mapTotal[_curMeta].insert(str,pm);
|
||
|
||
_curProject = str;*/
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"创建表成功"));
|
||
|
||
}
|
||
else //创建失败
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"创建表失败"));
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
void projectModelDlg::onSaveClicked()
|
||
{
|
||
if(_curProject.isEmpty())
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"请选择操作的模型"));
|
||
}
|
||
else
|
||
{
|
||
if(m_mapTotal[_curMeta][_curProject].nType == 0)
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"请选择工程模类型"));
|
||
return;
|
||
}
|
||
projectState state = couldSave();
|
||
if(state == NotExist) //目前名称工程模不存在
|
||
{
|
||
PropertyModel proModel = m_mapTotal[_curMeta][_curProject];
|
||
if(proModel.formerProject.bChanged) //工程模改过名
|
||
{
|
||
if(proModel.formerMeta.bChanged) //元模改过类型
|
||
{
|
||
QStringList lst = DataBase::GetInstance()->ifModelOccupy(proModel.formerProject.sName);
|
||
if(!lst.isEmpty()){ //工程模被占用
|
||
QMessageBox msgBox;
|
||
msgBox.setText(QString::fromWCharArray(L"工程模将被修改"));
|
||
QString info = QString::fromWCharArray(L"该工程模已被元件%1使用,是否继续修改?"); //todo:修改工程模后相关处理
|
||
msgBox.setInformativeText(info);
|
||
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
|
||
msgBox.setDefaultButton(QMessageBox::Cancel);
|
||
int ret = msgBox.exec();
|
||
switch (ret) {
|
||
case QMessageBox::Yes:
|
||
{
|
||
bool val = DataBase::GetInstance()->deleteProjectModel(proModel.formerProject.sName); //删除旧工程模,生成新工程模
|
||
if(val)
|
||
{
|
||
generate(_curProject);
|
||
setTableItemState(_curRow,TS_select);
|
||
}
|
||
break;
|
||
}
|
||
case QMessageBox::Cancel:
|
||
// Cancel was clicked
|
||
break;
|
||
default:
|
||
// should never be reached
|
||
break;
|
||
}
|
||
}
|
||
else{ //工程模未被使用
|
||
bool val = DataBase::GetInstance()->deleteProjectModel(proModel.formerProject.sName); //删除旧工程模,生成新工程模
|
||
if(val)
|
||
{
|
||
generate(_curProject);
|
||
setTableItemState(_curRow,TS_select);
|
||
}
|
||
}
|
||
}
|
||
else //元模未改类型
|
||
{
|
||
QMap<QString,QJsonObject> map = DataBase::GetInstance()->getCheckStateFromManager(proModel.formerProject.sName);
|
||
bool val = ifProjectEqual(map);
|
||
|
||
QStringList lst = DataBase::GetInstance()->ifModelOccupy(proModel.formerProject.sName);
|
||
if(!lst.isEmpty()){ //工程模被使用
|
||
QMessageBox msgBox;
|
||
msgBox.setText(QString::fromWCharArray(L"工程模将被修改"));
|
||
QString info = QString::fromWCharArray(L"该工程模已被元件%1使用,是否继续修改?"); //todo:修改工程模后相关处理
|
||
msgBox.setInformativeText(info);
|
||
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
|
||
msgBox.setDefaultButton(QMessageBox::Cancel);
|
||
int ret = msgBox.exec();
|
||
switch (ret) {
|
||
case QMessageBox::Yes:
|
||
{
|
||
if(val) //旧工程模和新工程模数据相同,改名
|
||
{
|
||
renameProjectModel(_curProject,proModel.dataInfo);
|
||
updateComponentModelName(proModel.formerProject.sName,_curProject); //修改component中的模型名
|
||
setTableItemState(_curRow,TS_select);
|
||
}
|
||
else //不同,先改工程模名称,后修改数据
|
||
{
|
||
renameProjectModel(_curProject,proModel.dataInfo);
|
||
updateComponentModelName(proModel.formerProject.sName,_curProject); //修改component中的模型名
|
||
QString sRes = modifyProjectModel(map);
|
||
if(!sRes.isEmpty())
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"修改模型成功"));
|
||
setTableItemState(_curRow,TS_select);
|
||
}
|
||
else
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"修改模型失败"));
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
case QMessageBox::Cancel:
|
||
// Cancel was clicked
|
||
break;
|
||
default:
|
||
// should never be reached
|
||
break;
|
||
}
|
||
}
|
||
else //工程模未被使用
|
||
{
|
||
if(val) //旧工程模和新工程模数据相同,改名
|
||
{
|
||
renameProjectModel(_curProject,proModel.dataInfo);
|
||
setTableItemState(_curRow,TS_select);
|
||
}
|
||
else //不同,先改工程模名称,后修改数据
|
||
{
|
||
renameProjectModel(_curProject,proModel.dataInfo);
|
||
QString sRes = modifyProjectModel(map);
|
||
if(!sRes.isEmpty())
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"修改模型成功"));
|
||
setTableItemState(_curRow,TS_select);
|
||
}
|
||
else
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"修改模型失败"));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else //新建
|
||
{
|
||
generate(_curProject);
|
||
setTableItemState(_curRow,TS_select);
|
||
}
|
||
}
|
||
else if(state == Exist)
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"该模型已经存在"));
|
||
}
|
||
else if(state == Changed)
|
||
{
|
||
QMessageBox msgBox;
|
||
msgBox.setText(QString::fromWCharArray(L"提示"));
|
||
msgBox.setInformativeText(QString::fromWCharArray(L"该模型已存在且与同名模型不一致,是否替换库模型?"));
|
||
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
|
||
msgBox.setDefaultButton(QMessageBox::Cancel);
|
||
int ret = msgBox.exec();
|
||
if(ret == QMessageBox::Yes)
|
||
{
|
||
PropertyModel proModel = m_mapTotal[_curMeta][_curProject];
|
||
if(proModel.formerMeta.bChanged) //判断元模是否改变,元模变了删除旧工程模,添加新工程模
|
||
{
|
||
bool val = DataBase::GetInstance()->deleteProjectModel(proModel.formerProject.sName);
|
||
if(val)
|
||
{
|
||
generate(_curProject);
|
||
setTableItemState(_curRow,TS_select);
|
||
}
|
||
}
|
||
else //元模未变直接修改
|
||
{
|
||
QMap<QString,QJsonObject> mapCheckState = DataBase::GetInstance()->getCheckStateFromManager(_curProject); //获取选择状态
|
||
QString sRes = modifyProjectModel(mapCheckState);
|
||
if(!sRes.isEmpty())
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"修改模型成功"));
|
||
setTableItemState(_curRow,TS_select);
|
||
}
|
||
else
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"修改模型失败"));
|
||
}
|
||
}
|
||
}
|
||
else if(ret == QMessageBox::Cancel)
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
void projectModelDlg::onCancelClicked()
|
||
{
|
||
hide();
|
||
}
|
||
|
||
void projectModelDlg::onApplyClicked()
|
||
{
|
||
MapMeta::Iterator iter = m_mapTotal.find(_curMeta);
|
||
if(iter != m_mapTotal.end())
|
||
{
|
||
MapProject project = iter.value();
|
||
MapProject::Iterator it= project.find(_curProject);
|
||
if(it != project.end())
|
||
{
|
||
MapProperty property = it.value().mapProperty;
|
||
QModelIndex selected = ui->treeView_base->currentIndex();
|
||
QStandardItem* pItem = it->pBase->itemFromIndex(selected);
|
||
|
||
if(pItem)
|
||
{
|
||
int nLevel = getLevel(pItem);
|
||
if(nLevel != 1) //不是二级节点就返回
|
||
return;
|
||
else{
|
||
QString strGroup = pItem->parent()->text();
|
||
//QStandardItem* item = it->pBase->takeItem(selected.row());
|
||
QStandardItem *parentItem = pItem->parent();
|
||
if (parentItem) {
|
||
parentItem->takeRow(pItem->row());
|
||
}
|
||
|
||
QList<QStandardItem*> groupItem = it->pSelect->findItems(strGroup);
|
||
if(groupItem.isEmpty()) //已选对象中是否存在该组,没有则添加
|
||
{
|
||
QStandardItem* pGroup = new QStandardItem(strGroup);
|
||
it->pSelect->appendRow(pGroup);
|
||
pGroup->appendRow(pItem);
|
||
}
|
||
else{
|
||
QStandardItem* pGroup = groupItem[0];
|
||
pGroup->appendRow(pItem);
|
||
}
|
||
|
||
//it->pBase->removeRow(selected.row());
|
||
|
||
m_mapTotal[_curMeta][_curProject].mapProperty[strGroup].mCheckState[pItem->text()].checkState = true; //选择状态设为1
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
ui->treeView_sub->expandAll();
|
||
}
|
||
|
||
void projectModelDlg::onRevokeClicked()
|
||
{
|
||
MapMeta::Iterator iter = m_mapTotal.find(_curMeta);
|
||
if(iter != m_mapTotal.end())
|
||
{
|
||
MapProject project = iter.value();
|
||
MapProject::Iterator it= project.find(_curProject);
|
||
if(it != project.end())
|
||
{
|
||
MapProperty property = it.value().mapProperty;
|
||
QModelIndex selected = ui->treeView_sub->currentIndex();
|
||
QStandardItem* pItem = it->pSelect->itemFromIndex(selected);
|
||
|
||
if(pItem)
|
||
{
|
||
int nLevel = getLevel(pItem);
|
||
if(nLevel != 1) //不是二级节点就返回
|
||
return;
|
||
else{
|
||
QString strGroup = pItem->parent()->text();
|
||
//QStandardItem* item = it->pBase->takeItem(selected.row());
|
||
QStandardItem *parentItem = pItem->parent();
|
||
if (parentItem) {
|
||
parentItem->takeRow(pItem->row());
|
||
if(!parentItem->hasChildren()){
|
||
it->pSelect->takeRow(parentItem->row());
|
||
}
|
||
}
|
||
|
||
QList<QStandardItem*> groupItem = it->pBase->findItems(strGroup);
|
||
if(groupItem.isEmpty()) //已选对象中是否存在该组,没有则添加
|
||
{
|
||
QStandardItem* pGroup = new QStandardItem(strGroup);
|
||
it->pBase->appendRow(pGroup);
|
||
pGroup->appendRow(pItem);
|
||
}
|
||
else{
|
||
QStandardItem* pGroup = groupItem[0];
|
||
pGroup->appendRow(pItem);
|
||
}
|
||
|
||
//it->pSelect->removeRow(selected.row());
|
||
|
||
m_mapTotal[_curMeta][_curProject].mapProperty[strGroup].mCheckState[pItem->text()].checkState = false; //选择状态设为1
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
ui->treeView_base->expandAll();
|
||
}
|
||
|
||
/*void projectModelDlg::onBaseModelIndexChanged(const QString& str)
|
||
{
|
||
if(_curMeta == str) //选择未改变
|
||
{
|
||
return;
|
||
}
|
||
if(ui->stackedWidget->currentIndex() !=1) //选择元模时隐藏iconlist
|
||
{
|
||
ui->stackedWidget->setCurrentIndex(1);
|
||
}
|
||
ui->treeView_base->setModel(nullptr);
|
||
ui->treeView_sub->setModel(nullptr);
|
||
MapMeta::Iterator iter = m_mapTotal.find(str);
|
||
if(iter != m_mapTotal.end())
|
||
{
|
||
//先清空已有
|
||
_curMeta = str;
|
||
_curProject = "";
|
||
_curProperty = "";
|
||
ui->cb_projectModel->clear();
|
||
ui->cb_property->clear();
|
||
MapProject project = iter.value();
|
||
for(MapProject::Iterator it = project.begin();it != project.end();++it)
|
||
{
|
||
ui->cb_projectModel->addItem(it.key());
|
||
}
|
||
}
|
||
}*/
|
||
|
||
/*void projectModelDlg::onProjectIndexChanged(const QString& str)
|
||
{
|
||
if(_curMeta.isEmpty())
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"请先选择元模型"));
|
||
}
|
||
else
|
||
{
|
||
if(_curProject == str) //选择未改变
|
||
{
|
||
return;
|
||
}
|
||
ui->treeView_base->setModel(nullptr);
|
||
ui->treeView_sub->setModel(nullptr);
|
||
MapMeta::Iterator iter = m_mapTotal.find(_curMeta);
|
||
if(iter != m_mapTotal.end())
|
||
{
|
||
MapProject project = iter.value();
|
||
MapProject::Iterator it= project.find(str);
|
||
if(it != project.end())
|
||
{
|
||
_curProject = str;
|
||
_curProperty = "";
|
||
ui->cb_property->clear();
|
||
MapProperty property = it.value().mapProperty;
|
||
for(MapProperty::Iterator ite = property.begin();ite != property.end();++ite)
|
||
{
|
||
ui->cb_property->addItem(ite.key());
|
||
}
|
||
}
|
||
|
||
QString sFirst = ui->cb_property->itemText(0); //点选工程模后默认选中第一条属性
|
||
onPropertyIndexChanged(sFirst);
|
||
}
|
||
updateIconList();
|
||
}
|
||
}*/
|
||
|
||
/*void projectModelDlg::onPropertyIndexChanged(const QString& str)
|
||
{
|
||
if(_curProject.isEmpty())
|
||
{
|
||
QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"请先选择工程模型"));
|
||
}
|
||
else
|
||
{
|
||
if(_curProperty == str)
|
||
{
|
||
return;
|
||
}
|
||
ui->treeView_base->setModel(nullptr);
|
||
ui->treeView_sub->setModel(nullptr);
|
||
MapMeta::Iterator iter = m_mapTotal.find(_curMeta);
|
||
if(iter != m_mapTotal.end())
|
||
{
|
||
MapProject project = iter.value();
|
||
MapProject::Iterator it= project.find(_curProject);
|
||
if(it != project.end())
|
||
{
|
||
MapProperty property = it.value().mapProperty;
|
||
MapProperty::Iterator ite = property.find(str);
|
||
if(ite != property.end())
|
||
{
|
||
_curProperty = str;
|
||
ui->treeView_base->setModel(nullptr);
|
||
ui->treeView_sub->setModel(nullptr);
|
||
QStandardItemModel* pBase = ite.value().pBase;
|
||
QStandardItemModel* pSelect = ite.value().pSelect;
|
||
ui->treeView_base->setModel(pBase);
|
||
ui->treeView_sub->setModel(pSelect);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}*/
|
||
|
||
/*void projectModelDlg::onIconClicked(const QModelIndex &index)
|
||
{
|
||
QStandardItemModel *model = dynamic_cast<QStandardItemModel*>(ui->listView_icon->model());
|
||
if(model)
|
||
{
|
||
QStandardItem* pItem = model->itemFromIndex(index);
|
||
int id = pItem->data(Qt::UserRole).toInt();
|
||
m_mapTotal[_curMeta][_curProject].nType = id;
|
||
}
|
||
}*/
|
||
|
||
/*void projectModelDlg::onIndexClicked(const QModelIndex &index)
|
||
{
|
||
// 获取模型
|
||
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->treeView_model->model());
|
||
if (!model) {
|
||
return;
|
||
}
|
||
|
||
// 判断是否为第二级节点
|
||
if (index.parent().isValid()) { // 父索引有效,说明是第二级节点
|
||
QString sMeta = model->data(index.parent(), Qt::DisplayRole).toString();
|
||
QString sProject = model->data(index, Qt::DisplayRole).toString();
|
||
|
||
ui->cb_baseModel->setCurrentText(sMeta); //无法编辑combobox的会跳转到该内容
|
||
onBaseModelIndexChanged(sMeta); //Base的改变会清空_project
|
||
|
||
ui->cb_projectModel->setCurrentText(sProject);
|
||
onProjectIndexChanged(sProject);
|
||
|
||
_curMeta = sMeta;
|
||
_curProject = sProject;
|
||
} else {
|
||
QString sMeta = model->data(index, Qt::DisplayRole).toString();
|
||
ui->cb_baseModel->setCurrentText(sMeta); //无法编辑combobox的会跳转到该内容
|
||
onBaseModelIndexChanged(sMeta);
|
||
_curMeta = sMeta;
|
||
_curProject = "";
|
||
}
|
||
}*/
|
||
|
||
void projectModelDlg::onIndexRbtnClicked(const QPoint &pos)
|
||
{
|
||
// 获取当前点击的位置对应的索引
|
||
QModelIndex index = ui->tableWidget_model->indexAt(pos);
|
||
if (!index.isValid()) {
|
||
return; // 如果点击的是空白区域,直接返回
|
||
}
|
||
|
||
// 如果是第二级节点,创建右键菜单
|
||
QMenu menu;
|
||
QAction *deleteAction = new QAction("删除模型", this);
|
||
menu.addAction(deleteAction);
|
||
|
||
// 连接删除菜单项的触发信号与槽函数
|
||
connect(deleteAction, &QAction::triggered, this, &projectModelDlg::onDeleteProjectClicked);
|
||
|
||
// 在点击位置显示菜单
|
||
menu.exec(ui->tableWidget_model->mapToGlobal(pos));
|
||
}
|
||
|
||
void projectModelDlg::onDeleteProjectClicked()
|
||
{
|
||
// 获取当前选中的索引
|
||
QModelIndexList selectedIndexes = ui->tableWidget_model->selectionModel()->selectedRows();
|
||
if (selectedIndexes.isEmpty()) {
|
||
return; // 没有选中任何行
|
||
}
|
||
|
||
// 获取当前选中的第一项索引
|
||
QModelIndex index = selectedIndexes.first();
|
||
|
||
// 确保索引有效
|
||
if (!index.isValid()) {
|
||
return;
|
||
}
|
||
|
||
int currentRow = ui->tableWidget_model->currentRow();
|
||
if (currentRow == -1) {
|
||
return; // 没有选中行
|
||
}
|
||
QString strProj = ui->tableWidget_model->item(currentRow, 0)->text();
|
||
QString strMeta = ui->tableWidget_model->item(currentRow, 1)->text();
|
||
|
||
_curProject = strProj;
|
||
_curMeta = strMeta;
|
||
|
||
// 删除当前选中的项
|
||
|
||
QStringList lst = DataBase::GetInstance()->ifModelOccupy(_curProject);
|
||
QString info = QString::fromWCharArray(L"删除后不可恢复,是否删除?");
|
||
if(!lst.empty()) //非空,被占用
|
||
{
|
||
info = QString::fromWCharArray(L"工程模被%1元件使用中,是否继续删除?").arg(lst.join(","));
|
||
}
|
||
QMessageBox msgBox;
|
||
msgBox.setText(QString::fromWCharArray(L"工程模将被删除"));
|
||
msgBox.setInformativeText(info);
|
||
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
|
||
msgBox.setDefaultButton(QMessageBox::Cancel);
|
||
int ret = msgBox.exec();
|
||
switch (ret) {
|
||
case QMessageBox::Yes:
|
||
{
|
||
int role = index.data(Qt::UserRole).toInt();
|
||
if(role != TS_create) //create状态的数据未创建数据库
|
||
{
|
||
bool val = DataBase::GetInstance()->deleteProjectModel(_curProject);
|
||
}
|
||
|
||
removeProjectData(_curMeta,_curProject,role);
|
||
|
||
ui->tableWidget_model->removeRow(currentRow);
|
||
_curMeta = "";
|
||
_curProject = "";
|
||
//QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"删除失败"));
|
||
break;
|
||
}
|
||
case QMessageBox::Cancel:
|
||
// Cancel was clicked
|
||
break;
|
||
default:
|
||
// should never be reached
|
||
break;
|
||
}
|
||
}
|
||
|
||
void projectModelDlg::addPropertyGroup()
|
||
{
|
||
int state = getModelEditState();
|
||
if(state & TS_create)
|
||
{
|
||
QMessageBox::information(NULL, QString("提示"), QString::fromWCharArray(L"请先完成编辑"));
|
||
return;
|
||
}
|
||
int row = ui->tableWidget_model->rowCount();
|
||
ui->tableWidget_model->insertRow(row);
|
||
|
||
// 工程模列(可编辑)
|
||
QTableWidgetItem* projectItem = new QTableWidgetItem("未命名");
|
||
//projectItem->setFlags(projectItem->flags() | Qt::ItemIsEditable);
|
||
ui->tableWidget_model->setItem(row, 0, projectItem);
|
||
projectItem->setData(Qt::UserRole,TS_create);
|
||
|
||
// 元模型列(初始化值)
|
||
QTableWidgetItem* metaItem = new QTableWidgetItem("未关联");
|
||
ui->tableWidget_model->setItem(row, 1, metaItem);
|
||
|
||
// 元件类型列(初始提示)
|
||
QTableWidgetItem* typeItem = new QTableWidgetItem("未选择");
|
||
ui->tableWidget_model->setItem(row, 2, typeItem);
|
||
|
||
ui->tableWidget_model->selectRow(row);
|
||
|
||
PropertyModel pm;
|
||
pm.pBase = new QStandardItemModel(this);
|
||
pm.pSelect = new QStandardItemModel(this);
|
||
_curModel = pm;
|
||
}
|
||
|
||
void projectModelDlg::onTableItemClicked(QTableWidgetItem *item)
|
||
{
|
||
// 获取选中行的索引
|
||
int row = item->row();
|
||
_curRow = row;
|
||
QString strProj = ui->tableWidget_model->item(row, 0)->text();
|
||
QString strMeta = ui->tableWidget_model->item(row, 1)->text();
|
||
|
||
QTableWidgetItem *firstColumnItem = ui->tableWidget_model->item(row,0);
|
||
if(firstColumnItem)
|
||
{
|
||
TableItemState state = TableItemState(firstColumnItem->data(Qt::UserRole).toInt());
|
||
if(state == TS_create)
|
||
{
|
||
int col = item->column();
|
||
if(col == TD_ProjectModel) //工程模输入框,不做特殊处理
|
||
{
|
||
if(strProj != "未命名")
|
||
{
|
||
_curProject = strProj;
|
||
}
|
||
if(strMeta != "未选择")
|
||
{
|
||
_curMeta = strMeta;
|
||
}
|
||
return;
|
||
}
|
||
else if(col == TD_MetaModel) //元模选择
|
||
{
|
||
if(strProj == "未命名")
|
||
{
|
||
QMessageBox::information(NULL, QString("提示"), QString::fromWCharArray(L"工程模未命名"));
|
||
return;
|
||
}
|
||
else
|
||
{
|
||
_curProject = strProj;
|
||
}
|
||
if(strMeta != "未选择")
|
||
{
|
||
_curMeta = strMeta;
|
||
}
|
||
}
|
||
else if(col == TD_ComponentType) //类型选择
|
||
{
|
||
if(strMeta == "未选择")
|
||
{
|
||
QMessageBox::information(NULL, QString("提示"), QString::fromWCharArray(L"元模未选择"));
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
else if(state == TS_select)
|
||
{
|
||
/*if(_curProject == strProj) //点击当前的返回
|
||
{
|
||
return;
|
||
}*/
|
||
ui->treeView_base->setModel(nullptr);
|
||
ui->treeView_sub->setModel(nullptr);
|
||
|
||
MapMeta::Iterator it;
|
||
for(it = m_mapTotal.begin();it != m_mapTotal.end();++it)
|
||
{
|
||
MapProject proj = it.value();
|
||
MapProject::Iterator iter;
|
||
for(iter = proj.begin();iter != proj.end();++iter)
|
||
{
|
||
if(iter.key() == strProj)
|
||
{
|
||
ui->treeView_base->setModel(iter->pBase);
|
||
ui->treeView_sub->setModel(iter->pSelect);
|
||
ui->treeView_base->expandAll();
|
||
ui->treeView_sub->expandAll();
|
||
_curMeta = it.key();
|
||
_curProject = strProj;
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else if(state == TS_edit)
|
||
{
|
||
/*if(_curProject == strProj) //点击当前的返回
|
||
{
|
||
return;
|
||
}*/
|
||
ui->treeView_base->setModel(nullptr);
|
||
ui->treeView_sub->setModel(nullptr);
|
||
|
||
MapMeta::Iterator it;
|
||
for(it = m_mapTotal.begin();it != m_mapTotal.end();++it)
|
||
{
|
||
MapProject proj = it.value();
|
||
MapProject::Iterator iter;
|
||
for(iter = proj.begin();iter != proj.end();++iter)
|
||
{
|
||
if(iter.key() == strProj)
|
||
{
|
||
ui->treeView_base->setModel(iter->pBase);
|
||
ui->treeView_sub->setModel(iter->pSelect);
|
||
ui->treeView_base->expandAll();
|
||
ui->treeView_sub->expandAll();
|
||
_curMeta = it.key();
|
||
_curProject = strProj;
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void projectModelDlg::onDelegateFinishEdit(const QModelIndex &index, const QString &value)
|
||
{
|
||
int row = index.row();
|
||
QTableWidgetItem *firstColumnItem = ui->tableWidget_model->item(row, 0);
|
||
if(firstColumnItem)
|
||
{
|
||
TableItemState state = TableItemState(firstColumnItem->data(Qt::UserRole).toInt());
|
||
if(state == TS_create)
|
||
{
|
||
int col = index.column();
|
||
if(col == TD_ProjectModel)
|
||
{
|
||
if(m_mapTotal.contains(_curMeta)) //判断元模存在
|
||
{
|
||
if(m_mapTotal[_curMeta].contains(_curProject)) //已经存在
|
||
{
|
||
if(_curProject == value) //工程模名称未变
|
||
return;
|
||
else{ //工程模名称变化,更新存储
|
||
PropertyModel proModel = m_mapTotal[_curMeta].take(_curProject);
|
||
m_mapTotal[_curMeta].insert(value,proModel);
|
||
_curProject = value;
|
||
}
|
||
}
|
||
else{ //新建
|
||
_curProject = value;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_curProject = value;
|
||
}
|
||
}
|
||
else if(col == TD_MetaModel)
|
||
{
|
||
if(m_mapTotal.contains(_curMeta)) //判断元模存在
|
||
{
|
||
if(m_mapTotal[_curMeta].contains(_curProject)) //工程模已经存在
|
||
{
|
||
if(_curMeta == value) //选择相同元模
|
||
return;
|
||
else{ //元模变化,删除旧model
|
||
PropertyModel proModel = m_mapTotal[_curMeta].take(_curProject); //从结构中取出
|
||
ui->treeView_base->setModel(nullptr);
|
||
ui->treeView_sub->setModel(nullptr);
|
||
delete proModel.pBase; //置空旧model
|
||
delete proModel.pSelect;
|
||
proModel.pBase = new QStandardItemModel(this);
|
||
proModel.pSelect = new QStandardItemModel(this);
|
||
proModel.mapProperty = addNewProject(value,_curProject,proModel); //挂接新元模数据
|
||
m_mapTotal[value].insert(_curProject,proModel); //插入到新位置
|
||
ui->treeView_base->setModel(proModel.pBase);
|
||
ui->treeView_sub->setModel(proModel.pSelect);
|
||
ui->treeView_base->expandAll();
|
||
ui->treeView_sub->expandAll();
|
||
_curMeta = value;
|
||
}
|
||
}
|
||
else //新建
|
||
{
|
||
_curModel.mapProperty = addNewProject(value,_curProject,_curModel);
|
||
m_mapTotal[value].insert(_curProject,_curModel);
|
||
|
||
ui->treeView_base->setModel(nullptr);
|
||
ui->treeView_sub->setModel(nullptr);
|
||
ui->treeView_base->setModel(_curModel.pBase);
|
||
ui->treeView_sub->setModel(_curModel.pSelect);
|
||
ui->treeView_base->expandAll();
|
||
ui->treeView_sub->expandAll();
|
||
_curMeta = value;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_curModel.mapProperty = addNewProject(value,_curProject,_curModel);
|
||
m_mapTotal[value].insert(_curProject,_curModel);
|
||
|
||
ui->treeView_base->setModel(nullptr);
|
||
ui->treeView_sub->setModel(nullptr);
|
||
ui->treeView_base->setModel(_curModel.pBase);
|
||
ui->treeView_sub->setModel(_curModel.pSelect);
|
||
ui->treeView_base->expandAll();
|
||
ui->treeView_sub->expandAll();
|
||
_curMeta = value;
|
||
}
|
||
}
|
||
else if(col == TD_ComponentType)
|
||
{
|
||
m_mapTotal[_curMeta][_curProject].nType = _mapType.value(value);
|
||
firstColumnItem->setData(Qt::UserRole,TS_edit);
|
||
}
|
||
}
|
||
else if(state == TS_select || state == TS_edit)
|
||
{
|
||
int col = index.column();
|
||
if(col == TD_ProjectModel)
|
||
{
|
||
if(m_mapTotal[_curMeta].contains(_curProject)) //已经存在
|
||
{
|
||
if(_curProject == value) //工程模名称未变
|
||
return;
|
||
else{ //工程模名称变化,更新存储
|
||
PropertyModel proModel = m_mapTotal[_curMeta].take(_curProject);
|
||
if(!proModel.formerProject.sName.isEmpty()) //曾用工程模名不为空
|
||
proModel.formerProject.bChanged = true;
|
||
m_mapTotal[_curMeta].insert(value,proModel);
|
||
_curProject = value;
|
||
firstColumnItem->setData(Qt::UserRole,TS_edit);
|
||
}
|
||
}
|
||
}
|
||
else if(col == TD_MetaModel)
|
||
{
|
||
if(m_mapTotal[_curMeta].contains(_curProject)) //已经存在
|
||
{
|
||
if(_curMeta == value) //选择相同元模
|
||
return;
|
||
else{ //元模变化,删除旧model
|
||
PropertyModel proModel = m_mapTotal[_curMeta].take(_curProject); //从结构中取出
|
||
if(!proModel.formerMeta.sName.isEmpty()) //曾用元模名不为空
|
||
proModel.formerMeta.bChanged = true;
|
||
ui->treeView_base->setModel(nullptr);
|
||
ui->treeView_sub->setModel(nullptr);
|
||
delete proModel.pBase; //置空旧model
|
||
delete proModel.pSelect;
|
||
proModel.pBase = new QStandardItemModel(this);
|
||
proModel.pSelect = new QStandardItemModel(this);
|
||
proModel.mapProperty = addNewProject(value,_curProject,proModel); //挂接新元模数据
|
||
m_mapTotal[value].insert(_curProject,proModel); //插入到新位置
|
||
ui->treeView_base->setModel(proModel.pBase);
|
||
ui->treeView_sub->setModel(proModel.pSelect);
|
||
ui->treeView_base->expandAll();
|
||
ui->treeView_sub->expandAll();
|
||
_curMeta = value;
|
||
firstColumnItem->setData(Qt::UserRole,TS_edit);
|
||
}
|
||
}
|
||
}
|
||
else if(col == TD_ComponentType)
|
||
{
|
||
m_mapTotal[_curMeta][_curProject].nType = _mapType.value(value);
|
||
firstColumnItem->setData(Qt::UserRole,TS_edit);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//=============================================================================
|
||
QStringList projectModelDlg::getModelList() const
|
||
{
|
||
QMap<int,modelType> modelMap = DataBase::GetInstance()->ModelType();
|
||
|
||
QSet<QString> modelSet;
|
||
for(auto &model:modelMap)
|
||
{
|
||
modelSet.insert(model.modelType);
|
||
}
|
||
|
||
return QStringList(modelSet.values());
|
||
}
|
||
|
||
QStringList projectModelDlg::getGroupList(const QString& sM) const
|
||
{
|
||
QMap<int,modelType> modelType = DataBase::GetInstance()->ModelType();
|
||
QMap<int,modelGroup> modelGroupMap = DataBase::GetInstance()->ModelGroup();
|
||
QMap<int,attributeGroup> groupMap = DataBase::GetInstance()->AttributeGroup();
|
||
|
||
int metaId = 0;
|
||
for(auto &meta:modelType)
|
||
{
|
||
if(sM == meta.modelType) //查找元模对应的id
|
||
{
|
||
metaId = meta.id;
|
||
break;
|
||
}
|
||
}
|
||
|
||
QList<int> lstGroupId;
|
||
for(auto &group:modelGroupMap) //找到元模id对应的属性组id
|
||
{
|
||
if(group.modelTypeId == metaId)
|
||
{
|
||
lstGroupId.push_back(group.attributeGroupId);
|
||
}
|
||
}
|
||
|
||
QStringList groupList;
|
||
for(auto &id:lstGroupId) //从属性组中找到id对应的组名
|
||
{
|
||
groupList.append(groupMap[id].groupType);
|
||
}
|
||
|
||
return groupList;
|
||
}
|
||
|
||
QStringList projectModelDlg::getAttributeList(const QString& sM,const QString& sG) const
|
||
{
|
||
QMap<int,modelType> modelType = DataBase::GetInstance()->ModelType();
|
||
//QMap<int,modelGroup> modelGroupMap = DataBase::GetInstance()->ModelGroup();
|
||
QMap<int,attributeGroup> groupMap = DataBase::GetInstance()->AttributeGroup();
|
||
QMap<int,modelAttribute> modelAttMap = DataBase::GetInstance()->ModelAttribute();
|
||
QMap<int,attribute> attMap = DataBase::GetInstance()->Attribute();
|
||
|
||
int metaId = -1;
|
||
for(auto &meta:modelType)
|
||
{
|
||
if(sM == meta.modelType) //查找元模对应的id
|
||
{
|
||
metaId = meta.id;
|
||
break;
|
||
}
|
||
}
|
||
|
||
int groupId = -1;
|
||
for(auto &attGroup:groupMap)
|
||
{
|
||
if(attGroup.groupType == sG) //返回参数属性组名对应的id
|
||
{
|
||
groupId = attGroup.id;
|
||
break;
|
||
}
|
||
}
|
||
|
||
QStringList lst;
|
||
for(auto &mt:modelAttMap)
|
||
{
|
||
if(mt.modelTypeId == metaId && mt.attributeGroupId == groupId)
|
||
{
|
||
lst.append(attMap[mt.attributeId].attribute);
|
||
}
|
||
}
|
||
|
||
return lst;
|
||
}
|
||
|
||
void projectModelDlg::setItemAttribute(const QString& name,QStandardItem* p)
|
||
{
|
||
QMap<int,attribute> attMap = DataBase::GetInstance()->Attribute();
|
||
QMap<int,dataType> dt = DataBase::GetInstance()->DataType();
|
||
|
||
for(auto &att:attMap)
|
||
{
|
||
QString sType = dt[att.dataTypeId].dataType; //获得属性id对应的属性名
|
||
if(name == att.attribute)
|
||
{
|
||
p->setData(att.id,Id);
|
||
p->setData(att.attribute,Attribute);
|
||
p->setData(att.attributeName,AttributeName);
|
||
p->setData(sType,DataType); //不直接使用id,拼接完成str
|
||
p->setData(att.lengthPrecision,LengthPrecision);
|
||
p->setData(att.scale,Scale);
|
||
p->setData(att.isNotNull,IsNotNull);
|
||
p->setData(att.defaultValue,DefaultValue);
|
||
p->setData(att.valueRange,ValueRange);
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
QPair<QString,QString> projectModelDlg::combinePropertySql(const QStandardItem* pItem)
|
||
{
|
||
QMap<int,dataType> dt = DataBase::GetInstance()->DataType();
|
||
|
||
int id = pItem->data(Id).toInt();
|
||
QString attribute = pItem->data(Attribute).toString();
|
||
QString dataType = pItem->data(DataType).toString();
|
||
int lengthPrecision = pItem->data(LengthPrecision).toInt();
|
||
int scale = pItem->data(Scale).toInt();
|
||
QString defaultValue = pItem->data(DefaultValue).toString();
|
||
QString valueRange = pItem->data(ValueRange).toString();
|
||
int isNotNull = pItem->data(IsNotNull).toInt();
|
||
|
||
bool needsQuotes = stringDataTypes.contains(dataType);
|
||
// 处理数据类型及其长度精度
|
||
QString dataTypePart = dataType;
|
||
if (lengthPrecision > 0) {
|
||
dataTypePart += QString("(%1").arg(lengthPrecision);
|
||
if (scale > 0) {
|
||
dataTypePart += QString(",%1").arg(scale);
|
||
}
|
||
dataTypePart += ")";
|
||
}
|
||
|
||
// 开始拼接SQL
|
||
QString sql = QString("%1 %2").arg(attribute, dataTypePart);
|
||
|
||
// 处理约束条件
|
||
if (isNotNull != 0) {
|
||
sql += " NOT NULL";
|
||
}
|
||
|
||
if (!defaultValue.isEmpty()) {
|
||
QString defValue = defaultValue;
|
||
if (needsQuotes) {
|
||
// 转义单引号并包裹
|
||
defValue.replace("'", "''");
|
||
defValue = QString("'%1'").arg(defValue);
|
||
}
|
||
sql += QString(" DEFAULT %1").arg(defValue);
|
||
}
|
||
|
||
/*if (isPrimaryKey != 0) {
|
||
sql += " PRIMARY KEY";
|
||
}*/
|
||
|
||
return qMakePair(sql,dataTypePart);
|
||
}
|
||
|
||
QString projectModelDlg::getProjectName() const
|
||
{
|
||
return _curProject;
|
||
}
|
||
|
||
QString projectModelDlg::getMetaName() const
|
||
{
|
||
return _curMeta;
|
||
}
|
||
|
||
bool projectModelDlg::ifProjectEqual(QMap<QString,QJsonObject> map)
|
||
{
|
||
//todo:判断关联的模型类型
|
||
MapProperty curPro = m_mapTotal[_curMeta][_curProject].mapProperty;
|
||
|
||
QMap<QString,PropertyPage>::Iterator iter;
|
||
for(iter = curPro.begin();iter != curPro.end();++iter)
|
||
{
|
||
if(!map.contains(iter.key())) //已存在的模型中不包含该属性组
|
||
{
|
||
QMap<QString,PropertyState> curCheckState = iter.value().mCheckState;
|
||
for(auto &val:curCheckState)
|
||
{
|
||
if(val.checkState) //该属性组不在模型中且被勾选
|
||
return false;
|
||
}
|
||
continue; //该属性组不在模型中且未被勾选,不做判断
|
||
}
|
||
else
|
||
{
|
||
QJsonObject dbCheckState = map[iter.key()]; //数据库中该属性组的勾选状态
|
||
QMap<QString,PropertyState> curCheckState = iter.value().mCheckState; //当前程序中的勾选状态
|
||
|
||
QJsonArray nodesJsonArray = dbCheckState["checkState"].toArray();
|
||
if(nodesJsonArray.size() != curCheckState.size()) //属性个数对不上,模型不同
|
||
return false;
|
||
for (QJsonValueRef nodeJson : nodesJsonArray)
|
||
{
|
||
QJsonObject node = nodeJson.toObject();
|
||
QString propertyName = node["name"].toString();
|
||
int nState = node["checked"].toInt();
|
||
if(curCheckState[propertyName].checkState != nState) //相同属性选中状态不同
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
QString projectModelDlg::modifyProjectModel(QMap<QString,QJsonObject> mapOld)
|
||
{
|
||
QString sRes;
|
||
MapProperty curPro = m_mapTotal[_curMeta][_curProject].mapProperty;
|
||
QStandardItemModel* pSelectModel = m_mapTotal[_curMeta][_curProject].pSelect;
|
||
QStandardItemModel* pBaseModel = m_mapTotal[_curMeta][_curProject].pBase;
|
||
int nType = m_mapTotal[_curMeta][_curProject].nType;
|
||
|
||
QMap<QString,PropertyPage>::Iterator iter;
|
||
for(iter = curPro.begin();iter != curPro.end();++iter) //遍历当前模型所有属性组
|
||
{
|
||
QMap<QString,PropertyState> curCheckState = iter.value().mCheckState; //当前程序中的属性勾选状态
|
||
bool isNull = true; //当前属性组是否未空
|
||
for(auto &val:curCheckState)
|
||
{
|
||
if(val.checkState){
|
||
isNull = false; //当前程序模型有勾选,不为空
|
||
break;
|
||
}
|
||
}
|
||
if(isNull){
|
||
if(mapOld.contains(iter.key())){ //当前模型勾选为空且库模型中包含此属性组,移除库中该属性组表
|
||
QMap<QString,QString> map = DataBase::GetInstance()->getProjectTableName(_curProject);
|
||
bool res = DataBase::GetInstance()->deleteTable(map[iter.key()]);
|
||
if(res){
|
||
DataBase::GetInstance()->deleteRecordFromManager(_curProject,iter.key());
|
||
sRes = QString::fromWCharArray(L"修改模型成功");
|
||
}
|
||
}
|
||
}
|
||
else{
|
||
QList<QStandardItem*> lstSelected = getGroupSub(pSelectModel,iter.key());
|
||
QList<QStandardItem*> lstBase = getGroupSub(pBaseModel,iter.key());
|
||
if(mapOld.contains(iter.key())){ //新旧模型中都存在,修改模型
|
||
QMap<QString,QString> oldSchema; //库中模型 属性名/数据类型
|
||
QMap<QString,QString> newSchema; //现有模型
|
||
|
||
QJsonObject obj = mapOld[iter.key()];
|
||
QJsonArray nodesJsonArray = obj["checkState"].toArray();
|
||
|
||
for (QJsonValueRef nodeJson : nodesJsonArray)
|
||
{
|
||
QJsonObject node = nodeJson.toObject();
|
||
QString propertyName = node["name"].toString();
|
||
int nState = node["checked"].toInt();
|
||
QString dataType = node["type"].toString();
|
||
|
||
if(nState)
|
||
oldSchema.insert(propertyName,dataType);
|
||
}
|
||
|
||
QMap<QString,PropertyState>::Iterator it;
|
||
for(it = curCheckState.begin(); it != curCheckState.end();++it)
|
||
{
|
||
if(it->checkState){
|
||
newSchema.insert(it.key(),it->dataType);
|
||
}
|
||
}
|
||
if(oldSchema == newSchema)
|
||
continue;
|
||
|
||
QMap<QString,QString> map = DataBase::GetInstance()->getProjectTableName(_curProject);
|
||
|
||
bool res = DataBase::GetInstance()->modifyProjectTable(map[iter.key()],oldSchema,newSchema);
|
||
|
||
QJsonObject objState = getSelectedState(lstSelected,lstBase);
|
||
DataBase::GetInstance()->updateCheckState(map[iter.key()],objState);
|
||
if(res)
|
||
{
|
||
sRes = QString::fromWCharArray(L"修改模型成功");
|
||
}
|
||
}
|
||
else{ //非空且库模型中不存在,新增
|
||
bool res = createPropertyTable(_curProject,iter.key(),lstSelected,lstBase,nType);
|
||
if(res)
|
||
{
|
||
sRes = QString::fromWCharArray(L"修改模型成功");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return sRes;
|
||
}
|
||
|
||
bool projectModelDlg::renameProjectModel(const QString& strCur,QMap<QString,projectManager> datas)
|
||
{
|
||
for(auto &data:datas)
|
||
{
|
||
QString sTable = data.metaModel + QString("_") + strCur + QString("_")+data.groupName;
|
||
|
||
DataBase::GetInstance()->updateProjectName(sTable,strCur,data.name);
|
||
DataBase::GetInstance()->alterTableName(data.name,sTable);
|
||
}
|
||
}
|
||
|
||
void projectModelDlg::updateComponentModelName(const QString& strOld,const QString& strNew)
|
||
{
|
||
DataBase::GetInstance()->updateComponentModelName(strOld,strNew);
|
||
}
|
||
/*void projectModelDlg::updateIconList()
|
||
{
|
||
if(ui->stackedWidget->currentIndex() !=0)
|
||
{
|
||
ui->stackedWidget->setCurrentIndex(0);
|
||
}
|
||
MapProject mp = m_mapTotal[_curMeta];
|
||
int nType = mp[_curProject].nType;
|
||
QStandardItemModel *model = dynamic_cast<QStandardItemModel*>(ui->listView_icon->model());
|
||
if(model)
|
||
{
|
||
for (int row = 0; row < model->rowCount(); ++row)
|
||
{
|
||
QStandardItem* pItem = model->item(row);
|
||
int id = pItem->data(Qt::UserRole).toInt();
|
||
if(nType == id) //使用存储的序列号更新list
|
||
{
|
||
QModelIndex index = model->index(id,0);
|
||
ui->listView_icon->setCurrentIndex(index);
|
||
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}*/
|
||
|
||
void projectModelDlg::removeProjectData(const QString& sMeta,const QString& sProject,int role)
|
||
{
|
||
if(role == TS_create && !m_mapTotal.contains(sMeta)){ //创建且未加入到管理
|
||
delete _curModel.pBase;
|
||
delete _curModel.pSelect;
|
||
}
|
||
else{
|
||
delete m_mapTotal[sMeta][sProject].pBase; //手动释放
|
||
delete m_mapTotal[sMeta][sProject].pSelect;
|
||
m_mapTotal[sMeta].remove(sProject);
|
||
}
|
||
}
|
||
|
||
QString projectModelDlg::getItemDataType(const QStandardItem* pItem)
|
||
{
|
||
QString dataType = pItem->data(DataType).toString();
|
||
int lengthPrecision = pItem->data(LengthPrecision).toInt();
|
||
int scale = pItem->data(Scale).toInt();
|
||
|
||
QString dataTypePart = dataType;
|
||
if (lengthPrecision > 0) {
|
||
dataTypePart += QString("(%1").arg(lengthPrecision);
|
||
if (scale > 0) {
|
||
dataTypePart += QString(",%1").arg(scale);
|
||
}
|
||
dataTypePart += ")";
|
||
}
|
||
return dataTypePart;
|
||
}
|
||
|
||
bool projectModelDlg::createPropertyTable(const QString& sProject,const QString& sGroup,QList<QStandardItem*> lstSelect,QList<QStandardItem*> lstBase,int nLinkType)
|
||
{
|
||
QString sName = _curMeta + QString("_") + sProject + QString("_")+sGroup;
|
||
|
||
QStringList fields;
|
||
fields.append("id SERIAL NOT NULL PRIMARY KEY");
|
||
fields.append("global_uuid uuid NOT NULL DEFAULT gen_random_uuid()");
|
||
fields.append("attribute_group VARCHAR(64) NOT NULL");
|
||
|
||
for(auto &item:lstSelect)
|
||
{
|
||
QPair<QString,QString> pair = combinePropertySql(item); //拼接单句sql
|
||
fields.append(pair.first);
|
||
}
|
||
|
||
QJsonObject objState = getSelectedState(lstSelect,lstBase);
|
||
|
||
if(!DataBase::GetInstance()->createDynamicTable(sName,fields))
|
||
{
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
DataBase::GetInstance()->insertProjectManager(sName,sProject,_curMeta,sGroup,nLinkType,objState);
|
||
return true;
|
||
}
|
||
}
|
||
|
||
QJsonObject projectModelDlg::getSelectedState(QList<QStandardItem*> select,QList<QStandardItem*> base)
|
||
{
|
||
QJsonObject objState;
|
||
QJsonArray arrState;
|
||
|
||
for(auto &item:select)
|
||
{
|
||
QString dataType = item->data(DataType).toString();
|
||
int lengthPrecision = item->data(LengthPrecision).toInt();
|
||
int scale = item->data(Scale).toInt();
|
||
QString defaultValue = item->data(DefaultValue).toString();
|
||
|
||
QString dataTypePart = dataType; //拼接数据类型
|
||
if (lengthPrecision > 0) {
|
||
dataTypePart += QString("(%1").arg(lengthPrecision);
|
||
if (scale > 0) {
|
||
dataTypePart += QString(",%1").arg(scale);
|
||
}
|
||
dataTypePart += ")";
|
||
}
|
||
|
||
QJsonObject node; //保存已选择状态
|
||
node["name"] = item->text();
|
||
node["checked"] = 1;
|
||
node["type"] = dataTypePart;
|
||
node["defaultValue"] = defaultValue;
|
||
node["lengthPrecision"] = lengthPrecision;
|
||
arrState.append(node);
|
||
}
|
||
|
||
for(auto &item:base)
|
||
{
|
||
QString dataType = item->data(DataType).toString();
|
||
int lengthPrecision = item->data(LengthPrecision).toInt();
|
||
int scale = item->data(Scale).toInt();
|
||
QString defaultValue = item->data(DefaultValue).toString();
|
||
|
||
QString dataTypePart = dataType; //拼接数据类型
|
||
if (lengthPrecision > 0) {
|
||
dataTypePart += QString("(%1").arg(lengthPrecision);
|
||
if (scale > 0) {
|
||
dataTypePart += QString(",%1").arg(scale);
|
||
}
|
||
dataTypePart += ")";
|
||
}
|
||
|
||
QJsonObject node; //保存未选择状态
|
||
node["name"] = item->text();
|
||
node["checked"] = 0;
|
||
node["type"] = dataTypePart;
|
||
node["defaultValue"] = defaultValue;
|
||
node["lengthPrecision"] = lengthPrecision;
|
||
arrState.append(node);
|
||
}
|
||
|
||
objState["checkState"] = arrState;
|
||
return objState;
|
||
}
|
||
|
||
QList<QStandardItem*> projectModelDlg::getGroupSub(QStandardItemModel* pModel,const QString& str)
|
||
{
|
||
QList<QStandardItem*> lst;
|
||
if(!pModel)
|
||
return lst;
|
||
QList<QStandardItem*> items = pModel->findItems(str);
|
||
if (!items.isEmpty()) {
|
||
// 存在该文本的项
|
||
QStandardItem* item = items[0];
|
||
for (int row = 0; row < item->rowCount(); ++row) {
|
||
// 默认获取第 0 列的子项
|
||
QStandardItem* child = item->child(row, 0);
|
||
if (child) {
|
||
lst.append(child);
|
||
}
|
||
}
|
||
}
|
||
return lst;
|
||
}
|
||
|
||
void projectModelDlg::setupUI()
|
||
{
|
||
QStringList headerText;
|
||
headerText<<"工程模"<<"元模型"<<"元件类型";
|
||
ui->tableWidget_model->setContextMenuPolicy(Qt::CustomContextMenu);
|
||
ui->tableWidget_model->setSelectionMode(QAbstractItemView::SingleSelection);
|
||
ui->tableWidget_model->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||
ui->tableWidget_model->setColumnCount(headerText.count());
|
||
ui->tableWidget_model->setHorizontalHeaderLabels(headerText);
|
||
ui->tableWidget_model->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
||
|
||
// 设置列代理
|
||
ProjectTableDelegate* delegate = new ProjectTableDelegate(this);
|
||
ui->tableWidget_model->setItemDelegate(delegate);
|
||
|
||
connect(delegate,&ProjectTableDelegate::editingFinished,this,&projectModelDlg::onDelegateFinishEdit);
|
||
}
|
||
|
||
void projectModelDlg::initialTypeMap()
|
||
{
|
||
_mapType.insert(QString::fromWCharArray(L"断路器"),1);
|
||
_mapType.insert(QString::fromWCharArray(L"母线"),2);
|
||
_mapType.insert(QString::fromWCharArray(L"异步电动机"),3);
|
||
}
|
||
|
||
int projectModelDlg::getLevel(QStandardItem *item) {
|
||
int level = 0;
|
||
QStandardItem *parent = item->parent();
|
||
if(parent)
|
||
{
|
||
while (parent) {
|
||
level++;
|
||
parent = parent->parent();
|
||
}
|
||
return level;
|
||
}
|
||
else{
|
||
return -1;
|
||
}
|
||
|
||
}
|
||
|
||
projectState projectModelDlg::couldSave()
|
||
{
|
||
QMap<QString,QJsonObject> map = DataBase::GetInstance()->getCheckStateFromManager(_curProject);
|
||
if(map.isEmpty())
|
||
{
|
||
return NotExist;
|
||
}
|
||
else
|
||
{
|
||
bool val = ifProjectEqual(map);
|
||
if(val){
|
||
return Exist;
|
||
}
|
||
else{
|
||
return Changed;
|
||
}
|
||
}
|
||
}
|
||
|
||
void projectModelDlg::setTableItemState(int row,TableItemState state)
|
||
{
|
||
QTableWidgetItem *item = ui->tableWidget_model->item(row, 0);
|
||
if(item)
|
||
{
|
||
item->setData(Qt::UserRole,state);
|
||
}
|
||
}
|
||
|
||
int projectModelDlg::getModelEditState()
|
||
{
|
||
int res = 0;
|
||
int totalRows = ui->tableWidget_model->rowCount();
|
||
|
||
// 遍历每一行
|
||
for (int row = 0; row < totalRows; ++row) {
|
||
// 获取当前行第一列(列索引 0)的 QTableWidgetItem
|
||
QTableWidgetItem *item = ui->tableWidget_model->item(row, 0);
|
||
|
||
// 检查项是否存在
|
||
if (item != nullptr) {
|
||
int role = item->data(Qt::UserRole).toInt();
|
||
res |= role;
|
||
}
|
||
}
|
||
return res;
|
||
}
|