From 2fa520e6a61e435ac85c9bb5611c90a59059cc24 Mon Sep 17 00:00:00 2001 From: duanshengchao <519970194@qq.com> Date: Wed, 4 Jun 2025 18:41:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E2=80=98=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E4=BF=A1=E6=81=AF=E2=80=99=E6=97=B6=E6=9B=B4?= =?UTF-8?q?=E6=94=B9=E4=BA=86=E6=A8=A1=E5=9E=8B=E5=90=8D=E7=A7=B0=EF=BC=8C?= =?UTF-8?q?=E8=AF=A5=E6=A8=A1=E5=9E=8B=E4=B8=8B=E8=A2=AB=E6=89=93=E5=BC=80?= =?UTF-8?q?=E7=9A=84=E5=B1=9E=E6=80=A7=E7=BB=84=E6=B5=8F=E8=A7=88tab?= =?UTF-8?q?=E7=9A=84=E6=96=87=E5=AD=97=E4=B9=9F=E5=81=9A=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/dbBrowser.h | 1 + include/dbStructureModel.h | 1 + include/mainwindow.h | 3 ++- include/modelInfoEditDialog.h | 3 ++- source/dbBrowser.cpp | 13 +++++++++ source/dbStructureModel.cpp | 48 ++++++++++++++++++++++++++++++++-- source/dbStructureView.cpp | 6 ++++- source/mainwindow.cpp | 18 +++++++++++-- source/modelInfoEditDialog.cpp | 4 +-- 9 files changed, 88 insertions(+), 9 deletions(-) diff --git a/include/dbBrowser.h b/include/dbBrowser.h index ced2293..33b88fc 100644 --- a/include/dbBrowser.h +++ b/include/dbBrowser.h @@ -25,6 +25,7 @@ public: void addTab_attribute(const QString&, ModelAttributeGroup&); void closeTab_attribute(ModelAttributeGroup&); void closeAllTab_attribute(); + void updateTabTextByModelNameChanged(const QString&, const QString&); protected: bool eventFilter(QObject*, QEvent*) override; diff --git a/include/dbStructureModel.h b/include/dbStructureModel.h index 73d4d10..6c1eb02 100644 --- a/include/dbStructureModel.h +++ b/include/dbStructureModel.h @@ -41,6 +41,7 @@ signals: public slots: void refreshStructure_Connection(const QString& connection); //刷新某个链接的数据(比如刷新、关闭或者手动刷新时) + void refreshStructure_Model(const QString& connection, int modelID); private: DBStructureNode* getNode(const QModelIndex& index) const; diff --git a/include/mainwindow.h b/include/mainwindow.h index 8508573..e3cde12 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -67,7 +67,8 @@ private slots: void onSIG_connectionStatusChanged(const QString& strConnectionName, bool bConnected); void onSIG_addModel(Model&); void onSIG_addGroups(int, QVector); - void onSIG_updateModelNodeText(int, const QString&); + void onSIG_updateModelNodeText(int, const QString&, const QString&); + void onSIG_updateModelGroups(int, const QVector&, const QVector&); //void onSIG_addAttributes(); void onSIG_openAttributeInfo(const QString&, ModelAttributeGroup&); void onSIG_closeAttributeInfo(ModelAttributeGroup&); diff --git a/include/modelInfoEditDialog.h b/include/modelInfoEditDialog.h index 5453463..20cdd33 100644 --- a/include/modelInfoEditDialog.h +++ b/include/modelInfoEditDialog.h @@ -57,7 +57,8 @@ private: signals: void addModel(Model&); - void updateModelNodeText(int, const QString&); + void updateModelNodeText(int, const QString&, const QString&); + void updateModelGroups(int, const QVector&, const QVector&); public slots: void onBtnClicked_selectImage(); diff --git a/source/dbBrowser.cpp b/source/dbBrowser.cpp index d3e9a46..aec0eb4 100644 --- a/source/dbBrowser.cpp +++ b/source/dbBrowser.cpp @@ -152,6 +152,19 @@ void DatabaseBrowser::closeAllTab_attribute() onTabCloseRequested(0); } +void DatabaseBrowser::updateTabTextByModelNameChanged(const QString& oldName, const QString& newName) +{ + for(int i = 0; i < ui->tabWidget->count(); i++) + { + QStringList textList = ui->tabWidget->tabText(i).split("/"); + if(textList.size() == 2 && textList.at(0) == oldName) + { + QString newTabText = newName + "/" + textList.at(1); + ui->tabWidget->setTabText(i, newTabText); + } + } +} + void DatabaseBrowser::onTabCloseRequested(int index) { /*QObject* sender = QObject::sender(); diff --git a/source/dbStructureModel.cpp b/source/dbStructureModel.cpp index 39356eb..e0eae3a 100644 --- a/source/dbStructureModel.cpp +++ b/source/dbStructureModel.cpp @@ -265,14 +265,14 @@ void DBStructureModel::addDataGroup(const QString& connection, int modelID, QVec DBStructureNode* connNode = getConnectionNode(connection); if(!connNode) { - LOG_ERROR("DBStructureModel", QString::fromWCharArray(L"添加数组时获取ConnectionNode失败,节点名称:%1").arg(connection)); + LOG_ERROR("DBStructureModel", QString::fromWCharArray(L"添加属性组时获取ConnectionNode失败,节点名称:%1").arg(connection)); return; } DBStructureNode* modelNode = getModelNode(connNode, modelID); if(!connNode) { - LOG_ERROR("DBStructureModel", QString::fromWCharArray(L"添加数组时获取ModelNode失败,节点ID:%1").arg(modelID)); + LOG_ERROR("DBStructureModel", QString::fromWCharArray(L"添加属性组时获取ModelNode失败,节点ID:%1").arg(modelID)); return; } @@ -409,5 +409,49 @@ void DBStructureModel::refreshStructure_Connection(const QString& connection) QModelIndex bottomRight = index(connNode->row(), 0, QModelIndex()); emit dataChanged(topLeft, bottomRight, {Qt::DecorationRole}); } +} + +void DBStructureModel::refreshStructure_Model(const QString& connection, int modelID) +{ + DBStructureNode* connNode = getConnectionNode(connection); + if(!connNode) + { + LOG_ERROR("DBStructureModel", QString::fromWCharArray(L"刷新模型节点时获取ConnectionNode失败,节点名称:%1").arg(connection)); + return; + } + QModelIndex connIndex = index(connNode->row(), 0, QModelIndex()); + + DBStructureNode* modelNode = getModelNode(connNode, modelID); + if(!connNode) + { + LOG_ERROR("DBStructureModel", QString::fromWCharArray(L"刷新模型节点时获取ModelNode失败,节点ID:%1").arg(modelID)); + return; + } + + //先删除 + if(modelNode->childCount() > 0) + { + beginRemoveRows(index(modelNode->row(), 0, connIndex), 0, modelNode->childCount() - 1); + modelNode->removeAllChildren(); + endRemoveRows(); //该语句之后会触发rowsRemoved(const QModelIndex &parent, int first, int last)信号,通知视图刷新对应行 + } + + //读取最新数据重新添加 + QVector groups = SqlQueryExecutor::instance().getModelGroups(connection, modelID); + beginInsertRows(index(modelNode->row(), 0, connIndex), 0, groups.count() - 1); + + for(int groupID : groups) + { + //QString groupName = SqlQueryExecutor::instance().getAttributeGroupName(connection, groupID); + AttributeGroup group = SqlQueryExecutor::instance().getAttributeGroupData(connection, groupID); + if(group.name.isEmpty()) + continue; + DBStructureNode* groupNode = new DBStructureNode(GroupNode, group.name, modelNode); + groupNode->setData(Qt::UserRole + NodeDataRole::ID, groupID); + groupNode->setData(Qt::UserRole + NodeDataRole::Type, group.isPublic); + modelNode->appendChild(groupNode); + } + + endInsertRows(); } diff --git a/source/dbStructureView.cpp b/source/dbStructureView.cpp index ba1ab3f..9d9c5f6 100644 --- a/source/dbStructureView.cpp +++ b/source/dbStructureView.cpp @@ -307,7 +307,11 @@ void DBStructureView::showContextMenu(const QPoint& pos) closeAttributeGroup(node); removeNode(node); }); - menu.addAction(QString::fromWCharArray(L"刷新"), []{}); + menu.addAction(QString::fromWCharArray(L"刷新"), [this, node]{ + DBStructureModel* model = dynamic_cast(this->model()); + if(model && node->parentNode()) + model->refreshStructure_Model(node->parentNode()->name(), node->data(Qt::UserRole + NodeDataRole::ID).toInt()); + }); menu.addSeparator(); menu.addAction(QString::fromWCharArray(L"修改"), [this, node]{ emit acitonTrigger_updateModel(node->data(Qt::UserRole + NodeDataRole::ID).toInt()); diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index 57ff968..f6217b6 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -351,10 +351,24 @@ void MainWindow::onSIG_addGroups(int modelID, QVector groups) m_pDBStrutureModel->addDataGroup(connection, modelID, groups); } -void MainWindow::onSIG_updateModelNodeText(int modelID, const QString& text) +void MainWindow::onSIG_updateModelNodeText(int modelID, const QString& oldText, const QString& newText) { QString connection = m_pDBStrutureView->curConnection(); - m_pDBStrutureModel->updateDataModelName(connection, modelID, text); + m_pDBStrutureModel->updateDataModelName(connection, modelID, newText); + //更新tab的text(若打开) + if(m_dbBrowser) + m_dbBrowser->updateTabTextByModelNameChanged(oldText, newText); +} + +void MainWindow::onSIG_updateModelGroups(int modelID, const QVector& removed, const QVector& added) +{ + //执行步骤:1、删除相应数据行组. 2、添加相应属性组. 3、刷新模型节点 + QString connection = m_pDBStrutureView->curConnection(); + for(int groupID : removed) + { + // if(SqlQueryExecutor::instance().removeAttributeGroup(connection, modelID, groupID) && m_dbBrowser) + // m_dbBrowser->closeTab_attribute() + } } void MainWindow::onSIG_openAttributeInfo(const QString& connection, ModelAttributeGroup& attributeGroup) diff --git a/source/modelInfoEditDialog.cpp b/source/modelInfoEditDialog.cpp index 4f1f263..c9b191f 100644 --- a/source/modelInfoEditDialog.cpp +++ b/source/modelInfoEditDialog.cpp @@ -372,14 +372,14 @@ void ModelInfoEditDialog::onBtnClicked_save() { if(modelName != m_oldName) //更新对应树节点 { - emit updateModelNodeText(m_curModelID, modelName); + emit updateModelNodeText(m_curModelID, m_oldName, modelName); } //计算删除和添加的属性组-转化为QSet,然后利用QSet的‘-’运算符操作 QSet oldSet(m_oldGroups.begin(), m_oldGroups.end()); QSet newSet(model.groups.begin(), model.groups.end()); QSet added = newSet - oldSet; QSet removed = oldSet - newSet; - + emit updateModelGroups(m_curModelID, removed.values(), added.values()); close(); }