PowerModeler/source/dbStructureView.cpp

346 lines
12 KiB
C++
Raw Normal View History

2025-03-14 16:06:20 +08:00
#include "dbStructureView.h"
#include "dbStructureNode.h"
#include "dbStructureModel.h"
#include "mainwindow.h"
2025-03-14 16:06:20 +08:00
#include "dbManager.h"
#include "customMenu.h"
#include "dataSyncManager.h"
2025-03-14 16:06:20 +08:00
#include <QMenu>
#include <QPoint>
#include <QMouseEvent>
DBStructureView::DBStructureView(DatabaseManager* dbManager, QWidget* parent)
: QTreeView(parent), m_dbManager(dbManager), m_pMainWindow(nullptr)
2025-03-14 16:06:20 +08:00
{
// connect(m_dbManager, &DatabaseManager::connectionStatusChanged,
// [this](const QString& connName, bool connected)
// {
// });
m_curConnection = "";
initView();
}
DBStructureView::~DBStructureView()
{
}
void DBStructureView::setMainWindow(MainWindow* window)
{
m_pMainWindow = window;
}
2025-03-14 16:06:20 +08:00
void DBStructureView::mouseDoubleClickEvent(QMouseEvent* event)
{
//只对鼠标左键的双击事件进行响应
if(event->button() != Qt::LeftButton)
{
//event->accept();
return;
}
QTreeView::mouseDoubleClickEvent(event);
}
void DBStructureView::initView()
{
setHeaderHidden(true);
setContextMenuPolicy(Qt::CustomContextMenu);
setSelectionMode(SingleSelection);
connect(this, &QTreeView::doubleClicked, this, &DBStructureView::itemDoubleClick);
connect(this, &QTreeView::customContextMenuRequested, this ,&DBStructureView::showContextMenu);
}
void DBStructureView::openConnection(const QString& connection)
{
connectToDB(connection);
m_curConnection = connection;
}
2025-03-14 16:06:20 +08:00
void DBStructureView::connectToDB(const QString& connName)
{
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
if(model && m_dbManager)
{
bool bResutl = m_dbManager->connect(connName);
if(bResutl)
{
2025-03-14 16:06:20 +08:00
model->refreshStructure_Connection(connName);
DataSyncManager::instance().registerPublicGroup(connName);
}
2025-03-14 16:06:20 +08:00
/*QModelIndex index = model->getConnNodeIndex(connName);
if(index.isValid())
{
if(!isExpanded(index))
expand(index);
}*/
}
}
void DBStructureView::disconnectToDB(const QString& connName)
{
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
if(model && m_dbManager)
{
m_dbManager->disconnect(connName);
model->refreshStructure_Connection(connName);
QModelIndex index = model->getConnNodeIndex(connName);
if(index.isValid())
collapse(index);
emit disconnect();
2025-03-14 16:06:20 +08:00
}
}
void DBStructureView::removeNode(DBStructureNode* node)
{
if(!node)
{
if(m_pMainWindow)
m_pMainWindow->showMessageDialog(type_warning, QString::fromWCharArray(L"错误"),
QString::fromWCharArray(L"获取节点数据失败"));
return;
}
if(node->type() == ConnectionNode)
{
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
if(model && m_dbManager)
{
m_dbManager->removeDatabase(node->name());
model->removeConnection(node->name());
}
}
else if(node->type() == TableNode)
{
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
if(model)
model->removeDataModel(node);
}
else if(node->type() == GroupNode)
2025-04-17 15:34:43 +08:00
{
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
if(model)
model->removeDataGroup(node);
}
}
2025-03-24 19:55:01 +08:00
void DBStructureView::openAttributeGroup(DBStructureNode* node)
{
DBStructureNode* parent = node->parentNode();
if(parent && parent->type() == TableNode)
{
int modelID = parent->data(Qt::UserRole + NodeDataRole::ID).toInt();
int groupID = node->data(Qt::UserRole + NodeDataRole::ID).toInt();
QString modelName = parent->name();
QString groupName = node->name();
ModelAttributeGroup attributeGroup(modelID, groupID, modelName, groupName);
emit openAttributeInfo(m_curConnection, attributeGroup);
}
}
2025-04-17 15:34:43 +08:00
void DBStructureView::closeAttributeGroup(DBStructureNode* node)
{
if(node->type() == GroupNode)
2025-04-17 15:34:43 +08:00
{
DBStructureNode* parent = node->parentNode();
if(parent && parent->type() == TableNode)
{
int modelID = parent->data(Qt::UserRole + NodeDataRole::ID).toInt();
int groupID = node->data(Qt::UserRole + NodeDataRole::ID).toInt();
QString modelName = parent->name();
QString groupName = node->name();
ModelAttributeGroup attributeGroup(modelID, groupID, modelName, groupName);
emit closeAttributeInfo(attributeGroup);
}
}
else if(node->type() == TableNode)
{
int modelID = node->data(Qt::UserRole + NodeDataRole::ID).toInt();
QString modelName = node->name();
ModelAttributeGroup attributeGroup(modelID, -1, modelName, "");
2025-04-17 15:34:43 +08:00
emit closeAttributeInfo(attributeGroup);
}
}
2025-03-14 16:06:20 +08:00
void DBStructureView::disconnectCurConnection()
{
if(!m_curConnection.isEmpty())
{
disconnectToDB(m_curConnection);
m_curConnection = "";
}
}
const QString DBStructureView::curConnection()
{
return m_curConnection;
}
2025-04-18 18:44:26 +08:00
DBStructureNode* DBStructureView::currentNode()
{
QModelIndex currentInex = currentIndex();
DBStructureNode* node = static_cast<DBStructureNode*>(currentInex.internalPointer());
2025-04-18 18:44:26 +08:00
return node;
}
void DBStructureView::onActionTrigger_removeModel()
{
DBStructureNode* node = currentNode();
if(!node || (node->type() != TableNode && node->type() != GroupNode))
{
if(m_pMainWindow)
m_pMainWindow->showMessageDialog(type_warning, QString::fromWCharArray(L"错误"),
QString::fromWCharArray(L"请先选择一个模型节点"));
return;
}
if(node->type() == TableNode)
{
if(m_pMainWindow)
{
m_pMainWindow->showMessageDialog(type_question, QString::fromWCharArray(L"提示"),
QString::fromWCharArray(L"确认删除模型 '%1' 吗").arg(node->name()));
if(g_msgDlgBtn == btn_No)
return;
}
removeNode(node);
}
else if(node->type() == GroupNode)
{
DBStructureNode* parent = node->parentNode();
if(m_pMainWindow)
{
m_pMainWindow->showMessageDialog(type_question, QString::fromWCharArray(L"提示"),
QString::fromWCharArray(L"确认删除模型 '%1' 吗").arg(parent->name()));
if(g_msgDlgBtn == btn_No)
return;
}
removeNode(parent);
}
}
2025-03-14 16:06:20 +08:00
void DBStructureView::itemDoubleClick(const QModelIndex& index)
{
DBStructureNode* node = static_cast<DBStructureNode*>(index.internalPointer());
if(node->type() == ConnectionNode)
2025-03-14 16:06:20 +08:00
{
//先断掉当前链接
disconnectCurConnection();
//打开新链接
openConnection(node->name());
2025-03-14 16:06:20 +08:00
}
else if(node->type() == GroupNode)
{
2025-03-24 19:55:01 +08:00
openAttributeGroup(node);
}
2025-03-14 16:06:20 +08:00
}
void DBStructureView::showContextMenu(const QPoint& pos)
{
QModelIndex index = indexAt(pos);
if(!index.isValid())
return;
DBStructureNode* node = static_cast<DBStructureNode*>(index.internalPointer());
if(node && node->type() == ConnectionNode)
2025-03-14 16:06:20 +08:00
{
bool isConnected = true;
if(node->status() == Disconnect)
isConnected = false;
QString connName = node->name();
CustomMenu menu;
menu.addAction(QString::fromWCharArray(L"链接"), [this, &connName]{
//先断掉当前链接
disconnectCurConnection();
//打开新链接
openConnection(connName);
2025-03-14 16:06:20 +08:00
//展开链接
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
QModelIndex index = model->getConnNodeIndex(connName);
if(model && index.isValid())
expand(index);
})->setEnabled(!isConnected);
2025-05-19 15:11:54 +08:00
menu.addAction(QString::fromWCharArray(L"断开链接"), [this]{disconnectCurConnection();})->setEnabled(isConnected);
2025-03-14 16:06:20 +08:00
menu.addAction(QString::fromWCharArray(L"刷新"), [this, &connName]{
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
if(model)
model->refreshStructure_Connection(connName);
})->setEnabled(isConnected);
2025-05-19 15:11:54 +08:00
menu.addAction(QString::fromWCharArray(L"移除链接"), [this, &connName, node, isConnected]{
if(m_pMainWindow)
{
m_pMainWindow->showMessageDialog(type_question, QString::fromWCharArray(L"提示"),
2025-05-19 15:11:54 +08:00
QString::fromWCharArray(L"确认移除链接 '%1' 吗").arg(connName));
if(g_msgDlgBtn == btn_No)
return;
}
2025-05-19 15:11:54 +08:00
if(isConnected)
disconnectCurConnection();
removeNode(node);
});
2025-03-14 16:06:20 +08:00
menu.addSeparator();
menu.addAction(QString::fromWCharArray(L"添加模型"), [this]{emit actionTrigger_addModel();})->setEnabled(isConnected);
/*menu.addAction(QString::fromWCharArray(L"导入数据"), []{});
menu.addAction(QString::fromWCharArray(L"导出数据"), []{});*/
2025-03-14 16:06:20 +08:00
QPoint originPoint = this->mapToGlobal(QPoint(0,0));
menu.exec(originPoint + pos);
}
else if(node && node->type() == TableNode)
2025-03-14 16:06:20 +08:00
{
CustomMenu menu;
menu.addAction(QString::fromWCharArray(L"删除"), [this, node]{
if(m_pMainWindow)
{
m_pMainWindow->showMessageDialog(type_question, QString::fromWCharArray(L"提示"),
QString::fromWCharArray(L"确认删除模型 '%1' 吗").arg(node->name()));
if(g_msgDlgBtn == btn_No)
return;
}
//关闭tab(若打开)
closeAttributeGroup(node);
removeNode(node);
});
menu.addAction(QString::fromWCharArray(L"刷新"), []{});
2025-04-18 18:44:26 +08:00
menu.addSeparator();
menu.addAction(QString::fromWCharArray(L"添加属性组"), [this, node]{
emit actionTrigger_addGroup(node->data(Qt::UserRole + NodeDataRole::ID).toInt());
});
2025-04-17 15:34:43 +08:00
//menu.addAction(QString::fromWCharArray(L"打开"), []{});
/*menu.addSeparator();
menu.addAction(QString::fromWCharArray(L"清空数据"), []{});
2025-04-17 15:34:43 +08:00
menu.addAction(QString::fromWCharArray(L"导入"), []{});
menu.addAction(QString::fromWCharArray(L"导出"), []{});*/
QPoint originPoint = this->mapToGlobal(QPoint(0,0));
menu.exec(originPoint + pos);
}
else if(node && node->type() == GroupNode)
{
CustomMenu menu;
2025-04-25 16:44:32 +08:00
bool isPublic = node->data(Qt::UserRole + NodeDataRole::Type).toBool();
2025-04-17 15:34:43 +08:00
menu.addAction(QString::fromWCharArray(L"打开"), [this, node]{openAttributeGroup(node);});
menu.addAction(QString::fromWCharArray(L"删除"), [this, node]{
2025-04-17 15:34:43 +08:00
if(m_pMainWindow)
{
m_pMainWindow->showMessageDialog(type_question, QString::fromWCharArray(L"提示"),
QString::fromWCharArray(L"确认删除属性组 '%1' 吗").arg(node->name()));
if(g_msgDlgBtn == btn_No)
return;
}
//关闭tab(若打开)
closeAttributeGroup(node);
//移除节点
removeNode(node);
2025-04-25 16:44:32 +08:00
})->setEnabled(!isPublic);
menu.addSeparator();
menu.addAction(QString::fromWCharArray(L"清空数据"), [this, node]{});
QPoint originPoint = this->mapToGlobal(QPoint(0,0));
menu.exec(originPoint + pos);
2025-03-14 16:06:20 +08:00
}
}