add common project model module

This commit is contained in:
baiYue 2025-06-06 18:57:37 +08:00
parent dd5d158ea5
commit 9f38ffd14d
11 changed files with 974 additions and 1073 deletions

View File

@ -8,6 +8,7 @@
#include <QUuid> #include <QUuid>
#include <QJsonObject> #include <QJsonObject>
#include <QWidget> #include <QWidget>
#include <QStandardItemModel>
const double g_dGriaphicsScene_Width = 800; const double g_dGriaphicsScene_Width = 800;
const double g_dGriaphicsScene_Height = 600; const double g_dGriaphicsScene_Height = 600;
@ -180,6 +181,47 @@ struct projectManager //工程模管理类
QJsonObject checkState; //属性选择状态 QJsonObject checkState; //属性选择状态
}; };
enum projectState
{
Err = -1,
NotExist = 0,
Exist,
Changed
};
struct FormerName //曾用名,记录修改前名称
{
QString sName;
bool bChanged = false; //是否改变过
};
struct PropertyState //每个属性的状态
{
bool checkState = false;
QString dataType;
bool editable = true; //可勾选状态
};
struct PropertyPage //属性信息
{
QMap<QString,PropertyState> mCheckState; //属性选择状态
bool isPublic = false;
};
typedef QMap<QString,PropertyPage> MapProperty; //属性组
struct PropertyModel //工程模
{
MapProperty mapProperty;
int nType = 0; //工程模类型,选择图标后确定
QStandardItemModel* pBase; //基础属性
QStandardItemModel* pSelect; //已选择属性
FormerName formerMeta; //曾用元模名
FormerName formerProject; //曾用工程模名
QMap<QString,projectManager> dataInfo; //存放数据库内容
};
typedef QMap<QString,PropertyModel> MapProject; //str为工程名PropertyModel为工程属性
typedef QMap<QString,MapProject> MapMeta; //str为元模名,PropertyModel为工程模集
//===================================== //=====================================
struct propertyGroupState //模型属性组信息 struct propertyGroupState //模型属性组信息
{ {

View File

@ -27,8 +27,6 @@ public:
signals: signals:
void signalAddItem(QGraphicsItem*); void signalAddItem(QGraphicsItem*);
public slots:
void onDeleteClicked();
protected: protected:
void drawBackground(QPainter*, const QRectF&) override; void drawBackground(QPainter*, const QRectF&) override;
void mousePressEvent(QGraphicsSceneMouseEvent*) override; void mousePressEvent(QGraphicsSceneMouseEvent*) override;

View File

@ -58,6 +58,7 @@ public:
virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event, DesignerScene*); virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event, DesignerScene*);
virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event, DesignerScene*); virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event, DesignerScene*);
virtual void dropEvent(QGraphicsSceneDragDropEvent *event, DesignerScene*); virtual void dropEvent(QGraphicsSceneDragDropEvent *event, DesignerScene*);
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event,DesignerScene*);
SelectorType getSelectorType() { return m_type; } SelectorType getSelectorType() { return m_type; }
//void setOperationMode(OperationMode m) { m_opMode = m; } //void setOperationMode(OperationMode m) { m_opMode = m; }
@ -71,7 +72,6 @@ public:
void updateConnectLineByTopology(QList<QGraphicsItem *>); //通过拓扑关系更新位置 void updateConnectLineByTopology(QList<QGraphicsItem *>); //通过拓扑关系更新位置
signals: signals:
void setWorkingSelector(SelectorType); void setWorkingSelector(SelectorType);
protected: protected:
//静态变量用于不同类型selector间的成员共享 //静态变量用于不同类型selector间的成员共享
static OperationMode ms_opMode; static OperationMode ms_opMode;

View File

@ -131,18 +131,17 @@ void DesignerScene::keyReleaseEvent(QKeyEvent* event)
QGraphicsScene::keyReleaseEvent(event); QGraphicsScene::keyReleaseEvent(event);
} }
void DesignerScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent) void DesignerScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{ {
QList<QGraphicsItem*> listItem = selectedItems(); if(m_pDrawingPanel)
if(listItem.isEmpty())
return;
else if(listItem.count() == 1)
{ {
QMenu menu; if(m_pDrawingPanel->getMode() == DM_run)
QAction *removeAction = menu.addAction(QString::fromWCharArray(L"删除")); return;
connect(removeAction,&QAction::triggered,this,&DesignerScene::onDeleteClicked); m_pDrawingPanel->selectorManager()->getWorkingSelector()->contextMenuEvent(event, this);
menu.exec(QCursor::pos()); update();
} }
else
QGraphicsScene::contextMenuEvent(event);
} }
void DesignerScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event) void DesignerScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
@ -184,26 +183,6 @@ void DesignerScene::dropEvent(QGraphicsSceneDragDropEvent *event)
QGraphicsScene::dropEvent(event); QGraphicsScene::dropEvent(event);
} }
void DesignerScene::onDeleteClicked()
{
QList<QGraphicsItem*> listItem = selectedItems();
if(listItem.isEmpty())
return;
else if(listItem.count() == 1)
{
GraphicsBaseItem* item = qgraphicsitem_cast<GraphicsBaseItem*>(listItem.first());
if(item)
{
GraphicsItemType tpe = item->getItemType();
if(tpe > QGraphicsItem::UserType && tpe < QGraphicsItem::UserType+1000)
{
if(_graphModel)
_graphModel->deleteNodeItem(item);
}
}
}
}
void DesignerScene::setGridVisible(bool bVisible) void DesignerScene::setGridVisible(bool bVisible)
{ {
m_bGridVisible = bVisible; m_bGridVisible = bVisible;

View File

@ -421,6 +421,38 @@ void BaseSelector::dropEvent(QGraphicsSceneDragDropEvent *event, DesignerScene*)
} }
} }
void BaseSelector::contextMenuEvent(QGraphicsSceneContextMenuEvent *event,DesignerScene* scene)
{
QList<QGraphicsItem*> listItem = scene->selectedItems();
if(listItem.isEmpty())
return;
else if(listItem.count() == 1)
{
QMenu menu;
QAction *removeAction = menu.addAction(QString::fromWCharArray(L"删除"));
//connect(removeAction,&QAction::triggered,this,&DesignerScene::onDeleteClicked);
connect(removeAction,&QAction::triggered,[&,scene](){
QList<QGraphicsItem*> listItem = scene->selectedItems();
if(listItem.isEmpty())
return;
else if(listItem.count() == 1)
{
GraphicsBaseItem* item = qgraphicsitem_cast<GraphicsBaseItem*>(listItem.first());
if(item)
{
GraphicsItemType tpe = item->getItemType();
if(tpe > QGraphicsItem::UserType && tpe < QGraphicsItem::UserType+1000)
{
if(_model)
_model->deleteNodeItem(item);
}
}
}
});
menu.exec(QCursor::pos());
}
}
void BaseSelector::setCursor(DesignerScene *scene, const QCursor &cursor) void BaseSelector::setCursor(DesignerScene *scene, const QCursor &cursor)
{ {
QGraphicsView *view = scene->getView(); QGraphicsView *view = scene->getView();
@ -463,3 +495,4 @@ void BaseSelector::updateConnectLineByTopology(QList<QGraphicsItem *> lst)
} }
} }
} }

View File

@ -5,6 +5,7 @@ set(DIAGRAMUTILS_HEADER_FILES
include/dataManager.h include/dataManager.h
include/componentIconManager.h include/componentIconManager.h
include/basePropertyManager.h include/basePropertyManager.h
include/projectModelManager.h
../common/include/global.h ../common/include/global.h
../common/include/baseProperty.h ../common/include/baseProperty.h
../common/include/compiler.hpp ../common/include/compiler.hpp
@ -17,6 +18,7 @@ set(DIAGRAMUTILS_SOURCE_FILES
source/dataBase.cpp source/dataBase.cpp
source/dataManager.cpp source/dataManager.cpp
source/basePropertyManager.cpp source/basePropertyManager.cpp
source/projectModelManager.cpp
source/componentIconManager.cpp source/componentIconManager.cpp
../common/source/global.cpp ../common/source/global.cpp
../common/source/baseProperty.cpp ../common/source/baseProperty.cpp

View File

@ -0,0 +1,49 @@
#ifndef PROJECTMODELMANAGER_H
#define PROJECTMODELMANAGER_H
#include <QObject>
#include <QMap>
#include "export.hpp"
#include "global.h"
/****工程模管理类*****/
class DIAGRAM_DESIGNER_PUBLIC ProjectModelManager : public QObject
{
Q_OBJECT
public:
explicit ProjectModelManager(QObject *parent = nullptr);
~ProjectModelManager();
static ProjectModelManager& instance();
public:
void initialModel();
void generate(const QString& sMeta,const QString& sPro); //根据输入名称生成表
MapProperty addNewProject(const QString& sMeta,const QString& sProject,PropertyModel&); //根据元模型、工程模名称生成工程模对象
QList<QStandardItem*> getGroupSub(QStandardItemModel*,const QString&); //返回指定组下的属性(如果存在)
MapMeta& getData() {return m_mapTotalData;}
void deleteData(const QString& meta,const QString& proj);
public:
QStringList getModelList() const; //获取元模型列表
QStringList getGroupList(const QString& model) const; //返回该元模下的属性组列表
QStringList getPublicGroupList() const; //返回公共属性组列表
QStringList getAttributeList(const QString& model,const QString& group) const; //根据元模名和组名返回属性列表
QStringList getPublicAttributeList(const QString& group); //返回公共属性组的属性列表
void setItemAttribute(const QString&,QStandardItem*); //设置item的属性(数据库表字段名)
QPair<QString,QString> combinePropertySql(const QStandardItem*); //根据item属性生成sql
bool ifProjectEqual(const QString& sMeta,const QString& sPro,QMap<QString,QJsonObject>); //根据每个属性组的勾选状态判断两个模型是否相同
QString modifyProjectModel(const QString& sMeta,const QString& sProject,QMap<QString,QJsonObject>); //修改工程模
bool renameProjectModel(const QString& strCur,QMap<QString,projectManager> datas); //重命名工程模
void updateComponentModelName(const QString& strOld,const QString& strNew); //更新component中的工程模
bool ifProjectExsit(const QString&); //判断工程模存在
signals:
void modelChange(); //模型改变信号
private:
int createPropertyTable(const QString& sMeta,const QString& sProject,const QString& sGroup,QList<QStandardItem*> lstSelect,QList<QStandardItem*> lstBase,int nLinkType,bool isPublic = false); //创建属性组表并插入记录到管理表(工程名,当前项迭代器,关联图元类型)
QJsonObject getSelectedState(QList<QStandardItem*> select,QList<QStandardItem*> base); //返回json格式的选中状态
QString getItemDataType(const QStandardItem* pItem); //返回数据类型
private:
MapMeta m_mapTotalData;
bool _bInitialised;
};
#endif // PROJECTMODELMANAGER_H

View File

@ -0,0 +1,778 @@
#include <QJsonArray>
#include "projectModelManager.h"
#include "dataBase.h"
#include "logger.h"
const QSet<QString> stringDataTypes = {"VARCHAR", "CHAR", "TEXT", "DATE", "TIME", "TIMESTAMP","JSONB","JSON"};
ProjectModelManager& ProjectModelManager::instance()
{
//采用静态局部变量的方式,静态局部变量的初始化是在第一次访问时,以后的调用不会多次初始化,并且生命周期和程序一致
static ProjectModelManager instance;
return instance;
}
ProjectModelManager::ProjectModelManager(QObject *parent)
: QObject(parent)
{
_bInitialised = false;
}
ProjectModelManager::~ProjectModelManager()
{
}
void ProjectModelManager::initialModel()
{
if(_bInitialised)
return;
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_mapTotalData.insert(model,mp);
}
_bInitialised = true;
}
void ProjectModelManager::generate(const QString& sMeta,const QString& sPro)
{
MapMeta::Iterator iter = m_mapTotalData.find(sMeta); //获取元模下的工程
if(iter != m_mapTotalData.end())
{
MapProject mp = iter.value();
MapProject::Iterator ite = mp.find(sPro); //获取工程下的属性组
if(ite != mp.end())
{
MapProperty mapProperty = ite.value().mapProperty;
int createRes = 0; //动态表生成结果
for(MapProperty::Iterator it = mapProperty.begin();it != mapProperty.end();++it){ //每个属性组单独生成表
int nType = ite.value().nType;
QStandardItemModel* pSelectModel = ite->pSelect;
QStandardItemModel* pBaseModel = ite->pBase;
QList<QStandardItem*> lstSelected = getGroupSub(pSelectModel,it.key());
QList<QStandardItem*> lstBase = getGroupSub(pBaseModel,it.key());
if(!lstSelected.isEmpty())
{
bool isPub = it->isPublic;
int res = createPropertyTable(sMeta,sPro,it.key(),lstSelected,lstBase,nType,isPub);
switch (res){
case int(AlertInfo::success):
LOG_INFO("DB", QString("create %1 dynamicTable success").arg(sPro));
break;
case int(AlertInfo::fail):
LOG_WARN("DB", QString("create %1 dynamicTable fail").arg(sPro));
break;
case int(AlertInfo::exist):
LOG_WARN("DB", QString("%1 dynamicTable exist").arg(sPro));
break;
default:
break;
}
createRes = createRes | res;
}
}
if(!(createRes & int(AlertInfo::fail))) //结果不含失败就成功
{
//QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"创建表成功"));
emit modelChange();
}
else //创建失败
{
//QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"创建表失败"));
}
}
}
}
MapProperty ProjectModelManager::addNewProject(const QString& sMeta,const QString& sProject,PropertyModel& model)
{
MapProperty mt;
QStringList lstProperty = getGroupList(sMeta); //返回元模下的属性组名
QStringList lstProPublic = getPublicGroupList(); //返回公共属性组
//lstProperty<<QString("base")<<QString("seperation");
QMap<QString,QJsonObject> mapCheckState = DataBase::GetInstance()->getCheckStateFromManager(sProject); //获取选择状态
for(auto &property:lstProperty)
{
PropertyPage struProperty;
struProperty.isPublic = false;
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);
}
MapProperty pubMap;
for(auto &pro:lstProPublic) //公共属性组
{
PropertyPage struProperty;
struProperty.isPublic = true;
QStandardItem* pGroup = nullptr;
pGroup = new QStandardItem(pro);
//model.pSelect->appendRow(pGroup); //公共属性组默认都包含
model.pSelect->insertRow(0,pGroup);
QStringList lstName = getPublicAttributeList(pro);
for(auto &name:lstName)
{
QStandardItem* pItem = new QStandardItem(name);
pItem->setData(QColor(60,140,180,180), Qt::BackgroundRole);
setItemAttribute(name,pItem);
pGroup->appendRow(pItem);
QString dataType = getItemDataType(pItem);
PropertyState sta;
sta.dataType = dataType;
sta.checkState = true;
sta.editable = false;
struProperty.mCheckState.insert(name,sta); //初始都已经选择
}
pubMap.insert(pro,struProperty);
}
for (auto it = mt.begin(); it != mt.end(); ++it) { //将正常属性添到公共属性后
if(pubMap.contains(it.key()))
continue; //公共属性组已有的不重复添加
pubMap.insert(it.key(), it.value());
}
return pubMap;
}
QList<QStandardItem*> ProjectModelManager::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 ProjectModelManager::deleteData(const QString& sMeta,const QString& sProject)
{
delete m_mapTotalData[sMeta][sProject].pBase; //手动释放
delete m_mapTotalData[sMeta][sProject].pSelect;
m_mapTotalData[sMeta].remove(sProject);
}
QStringList ProjectModelManager::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 ProjectModelManager::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 ProjectModelManager::getPublicGroupList() const
{
QMap<int,modelAttributePublic> modelAttPublic = DataBase::GetInstance()->ModelAttributePublic();
QMap<int,attributeGroup> groupMap = DataBase::GetInstance()->AttributeGroup();
QSet<int> setGroup;
for(auto &model:modelAttPublic)
{
setGroup.insert(model.attributeGroupId);
}
QStringList groupList;
for(auto &id:setGroup)
{
groupList.append(groupMap[id].groupType);
}
return groupList;
}
QStringList ProjectModelManager::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;
}
QStringList ProjectModelManager::getPublicAttributeList(const QString& group)
{
QMap<int,modelAttributePublic> modelAttPublic = DataBase::GetInstance()->ModelAttributePublic();
QMap<int,attributeGroup> groupMap = DataBase::GetInstance()->AttributeGroup();
QMap<int,attribute> attMap = DataBase::GetInstance()->Attribute();
int groupId = -1;
for(auto &attGroup:groupMap)
{
if(attGroup.groupType == group) //返回参数属性组名对应的id
{
groupId = attGroup.id;
break;
}
}
QStringList lst;
for(auto &mt:modelAttPublic)
{
if(mt.attributeGroupId == groupId)
{
lst.append(attMap[mt.attributeId].attribute);
}
}
return lst;
}
void ProjectModelManager::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);
p->setData(att.isVisible,IsVisible);
return;
}
}
}
QPair<QString,QString> ProjectModelManager::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 != -1) {
sql += " NOT NULL";
}
if (!defaultValue.isEmpty()) {
QString defValue = defaultValue;
if (needsQuotes && defValue != "null" && defValue != "NULL") {
// 转义单引号并包裹
defValue.replace("'", "''");
defValue = QString("'%1'").arg(defValue);
}
sql += QString(" DEFAULT %1").arg(defValue);
}
/*if (isPrimaryKey != 0) {
sql += " PRIMARY KEY";
}*/
return qMakePair(sql,dataTypePart);
}
bool ProjectModelManager::ifProjectEqual(const QString& sMeta,const QString& sProject,QMap<QString,QJsonObject> map)
{
//todo:判断关联的模型类型
MapProperty curPro = m_mapTotalData[sMeta][sProject].mapProperty;
QMap<QString,PropertyPage>::Iterator iter;
for(iter = curPro.begin();iter != curPro.end();++iter)
{
if(iter->isPublic) //公共组跳过判断
continue;
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 ProjectModelManager::modifyProjectModel(const QString& sMeta,const QString& sProject,QMap<QString,QJsonObject> mapOld)
{
QString sRes;
MapProperty curPro = m_mapTotalData[sMeta][sProject].mapProperty;
QStandardItemModel* pSelectModel = m_mapTotalData[sMeta][sProject].pSelect;
QStandardItemModel* pBaseModel = m_mapTotalData[sMeta][sProject].pBase;
int nType = m_mapTotalData[sMeta][sProject].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(sProject);
bool res = DataBase::GetInstance()->deleteTable(map[iter.key()]);
if(res){
DataBase::GetInstance()->deleteRecordFromManager(sProject,iter.key());
sRes = QString::fromWCharArray(L"修改模型成功");
}
}
}
else{
QList<QStandardItem*> lstSelected = ProjectModelManager::instance().getGroupSub(pSelectModel,iter.key());
QList<QStandardItem*> lstBase = ProjectModelManager::instance().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(sProject);
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(sMeta,sProject,iter.key(),lstSelected,lstBase,nType);
if(res)
{
sRes = QString::fromWCharArray(L"修改模型成功");
}
}
}
}
return sRes;
}
bool ProjectModelManager::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);
}
return true;
}
void ProjectModelManager::updateComponentModelName(const QString& strOld,const QString& strNew)
{
DataBase::GetInstance()->updateComponentModelName(strOld,strNew);
}
bool ProjectModelManager::ifProjectExsit(const QString& sPro)
{
for(auto &meta:m_mapTotalData)
{
if(meta.contains(sPro))
{
return true;
}
}
return false;
}
int ProjectModelManager::createPropertyTable(const QString& sMeta,const QString& sProject,const QString& sGroup,QList<QStandardItem*> lstSelect,QList<QStandardItem*> lstBase,int nLinkType,bool isPublic)
{
if(!isPublic)
{
QString sName = sMeta + 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 int(AlertInfo::fail);
}
else
{
DataBase::GetInstance()->insertProjectManager(sName,sProject,sMeta,sGroup,nLinkType,objState);
return int(AlertInfo::success);
}
}
else
{
QString sName = 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)
{
QString attribute = item->data(Attribute).toString();
if(attribute.contains("global_uuid"))
continue;
QPair<QString,QString> pair = combinePropertySql(item); //拼接单句sql
fields.append(pair.first);
}
QJsonObject objState = getSelectedState(lstSelect,lstBase);
if(!DataBase::GetInstance()->ifDynamicTableExist(sName))
{
bool val = DataBase::GetInstance()->createDynamicTable(sName,fields);
if(val)
{
DataBase::GetInstance()->insertProjectManager(sName,sName,"NULL",sGroup,0,objState,true);
return int(AlertInfo::success);
}
else {
return int(AlertInfo::fail);
}
}
else
{
return int(AlertInfo::exist);
}
}
}
QJsonObject ProjectModelManager::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();
int isVisible = item->data(IsVisible).toInt();
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;
node["isVisible"] = isVisible;
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();
int isVisible = item->data(IsVisible).toInt();
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;
node["isVisible"] = isVisible;
arrState.append(node);
}
objState["checkState"] = arrState;
return objState;
}
QString ProjectModelManager::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;
}

View File

@ -12,46 +12,6 @@ QT_BEGIN_NAMESPACE
namespace Ui { class projectModelDlg; } namespace Ui { class projectModelDlg; }
QT_END_NAMESPACE QT_END_NAMESPACE
enum projectState
{
Err = -1,
NotExist = 0,
Exist,
Changed
};
struct FormerName //曾用名,记录修改前名称
{
QString sName;
bool bChanged = false; //是否改变过
};
struct PropertyState //每个属性的状态
{
bool checkState = false;
QString dataType;
bool editable = true; //可勾选状态
};
struct PropertyPage //属性信息
{
QMap<QString,PropertyState> mCheckState; //属性选择状态
bool isPublic = false;
};
typedef QMap<QString,PropertyPage> MapProperty; //属性组
struct PropertyModel //工程模
{
MapProperty mapProperty;
int nType = 0; //工程模类型,选择图标后确定
QStandardItemModel* pBase; //基础属性
QStandardItemModel* pSelect; //已选择属性
FormerName formerMeta; //曾用元模名
FormerName formerProject; //曾用工程模名
QMap<QString,projectManager> dataInfo; //存放数据库内容
};
typedef QMap<QString,PropertyModel> MapProject; //str为工程名PropertyModel为工程属性
typedef QMap<QString,MapProject> MapMeta; //str为元模名,PropertyModel为工程模集
class projectModelDlg : public QDialog class projectModelDlg : public QDialog
{ {
@ -64,20 +24,13 @@ public:
void initial(); void initial();
void initialModel(); void initialModel();
void initialList(); void initialList();
MapProperty addNewProject(const QString& sMeta,const QString& sProject,PropertyModel&); //根据元模型、工程模名称生成工程模对象
//void update(); //void update();
void generate(const QString&); //根据输入名称生成表
public slots: public slots:
void onSaveClicked(); void onSaveClicked();
void onCancelClicked(); void onCancelClicked();
void onApplyClicked(); void onApplyClicked();
void onRevokeClicked(); void onRevokeClicked();
//void onBaseModelIndexChanged(const QString&);
//void onProjectIndexChanged(const QString&);
//void onPropertyIndexChanged(const QString&);
//void onIconClicked(const QModelIndex &index); //关联图元改变
//void onIndexClicked(const QModelIndex &index); //索引列表点击
void onIndexRbtnClicked(const QPoint &pos); //索引列表右键菜单 void onIndexRbtnClicked(const QPoint &pos); //索引列表右键菜单
void onDeleteProjectClicked(); //删除选中工程模 void onDeleteProjectClicked(); //删除选中工程模
@ -85,28 +38,11 @@ public slots:
void onTableItemClicked(QTableWidgetItem *item); //模型item点击事件 void onTableItemClicked(QTableWidgetItem *item); //模型item点击事件
void onDelegateFinishEdit(const QModelIndex &index, const QString &value); //lineEdit代理编辑完成事件 void onDelegateFinishEdit(const QModelIndex &index, const QString &value); //lineEdit代理编辑完成事件
public: public:
QStringList getModelList() const; //获取元模型列表
QStringList getGroupList(const QString& model) const; //返回该元模下的属性组列表
QStringList getPublicGroupList() const; //返回公共属性组列表
QStringList getAttributeList(const QString& model,const QString& group) const; //根据元模名和组名返回属性列表
QStringList getPublicAttributeList(const QString& group); //返回公共属性组的属性列表
void setItemAttribute(const QString&,QStandardItem*); //设置item的属性(数据库表字段名)
QPair<QString,QString> combinePropertySql(const QStandardItem*); //根据item属性生成sql
QString getProjectName() const; //返回当前选择项目的名称 QString getProjectName() const; //返回当前选择项目的名称
QString getMetaName() const; //返回当前元模型名 QString getMetaName() const; //返回当前元模型名
bool ifProjectEqual(QMap<QString,QJsonObject>); //根据每个属性组的勾选状态判断两个模型是否相同
QString modifyProjectModel(QMap<QString,QJsonObject>); //修改工程模
bool renameProjectModel(const QString& strCur,QMap<QString,projectManager> datas); //重命名工程模
void updateComponentModelName(const QString& strOld,const QString& strNew); //更新component中的工程模
signals:
void modelChange(); //模型改变信号
private: private:
//void updateIconList(); //选择工程模后刷新关联图标
void removeProjectData(const QString&,const QString&,int role); //移除对应的project层级结构 void removeProjectData(const QString&,const QString&,int role); //移除对应的project层级结构
QString getItemDataType(const QStandardItem* pItem); //返回数据类型
int createPropertyTable(const QString& sProject,const QString& sGroup,QList<QStandardItem*> lstSelect,QList<QStandardItem*> lstBase,int nLinkType,bool isPublic = false); //创建属性组表并插入记录到管理表(工程名,当前项迭代器,关联图元类型)
QJsonObject getSelectedState(QList<QStandardItem*> select,QList<QStandardItem*> base); //返回json格式的选中状态
QList<QStandardItem*> getGroupSub(QStandardItemModel*,const QString&); //返回指定组下的属性(如果存在)
void setupUI(); void setupUI();
void initialTypeMap(); void initialTypeMap();
@ -115,11 +51,8 @@ private:
void setTableItemState(int row,TableItemState state); //设置状态 void setTableItemState(int row,TableItemState state); //设置状态
int getModelEditState(); //返回工程模编辑状态(选择、编辑、新建) int getModelEditState(); //返回工程模编辑状态(选择、编辑、新建)
bool ifProjectExsit(const QString&); //判断工程模存在
private: private:
Ui::projectModelDlg *ui; Ui::projectModelDlg *ui;
MapMeta m_mapTotal;
QString _curMeta; //当前元模型 QString _curMeta; //当前元模型
QString _curProject; //当前工程模 QString _curProject; //当前工程模
QString _curType; //当前关联类型 QString _curType; //当前关联类型

View File

@ -27,6 +27,7 @@
#include "topologyView.h" #include "topologyView.h"
#include "diagramView.h" #include "diagramView.h"
#include "createEditor.h" #include "createEditor.h"
#include "projectModelManager.h"
//using namespace ads; //using namespace ads;
@ -100,7 +101,7 @@ void CMainWindow::initializeDockUi()
connect(m_pElectricElementsBox,&ElectricElementsBox::addEletricItem,m_pDiagramCavas,&DiagramCavas::onSignal_addGraphicsItem); connect(m_pElectricElementsBox,&ElectricElementsBox::addEletricItem,m_pDiagramCavas,&DiagramCavas::onSignal_addGraphicsItem);
m_pProjectModelDlg = new projectModelDlg(this); m_pProjectModelDlg = new projectModelDlg(this);
connect(m_pProjectModelDlg,&projectModelDlg::modelChange,m_pElectricElementsBox,&ElectricElementsBox::onSignal_modelChanged); connect(&ProjectModelManager::instance(),&ProjectModelManager::modelChange,m_pElectricElementsBox,&ElectricElementsBox::onSignal_modelChanged);
} }
void CMainWindow::initializeAction() void CMainWindow::initializeAction()

File diff suppressed because it is too large Load Diff