#include "dbStructureView.h" #include "dbStructureNode.h" #include "dbStructureModel.h" #include "mainwindow.h" #include "dbManager.h" #include "customMenu.h" #include #include #include DBStructureView::DBStructureView(DatabaseManager* dbManager, QWidget* parent) : QTreeView(parent), m_dbManager(dbManager), m_pMainWindow(nullptr) { // connect(m_dbManager, &DatabaseManager::connectionStatusChanged, // [this](const QString& connName, bool connected) // { // }); m_curConnection = ""; initView(); } DBStructureView::~DBStructureView() { } void DBStructureView::setMainWindow(MainWindow* window) { m_pMainWindow = window; } 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; } void DBStructureView::connectToDB(const QString& connName) { DBStructureModel* model = dynamic_cast(this->model()); if(model && m_dbManager) { bool bResutl = m_dbManager->connect(connName); if(bResutl) model->refreshStructure_Connection(connName); /*QModelIndex index = model->getConnNodeIndex(connName); if(index.isValid()) { if(!isExpanded(index)) expand(index); }*/ } } void DBStructureView::disconnectToDB(const QString& connName) { DBStructureModel* model = dynamic_cast(this->model()); if(model && m_dbManager) { m_dbManager->disconnect(connName); model->refreshStructure_Connection(connName); QModelIndex index = model->getConnNodeIndex(connName); if(index.isValid()) collapse(index); } } 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(this->model()); if(model && m_dbManager) { m_dbManager->removeDatabase(node->name()); model->removeConnection(node->name()); } } else if(node->type() == TableNode) { DBStructureModel* model = dynamic_cast(this->model()); if(model) model->removeDataModel(node); } else if(node->type() == GroupNode) { DBStructureModel* model = dynamic_cast(this->model()); if(model) model->removeDataGroup(node); } } 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); } } void DBStructureView::closeAttributeGroup(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 closeAttributeInfo(attributeGroup); } } void DBStructureView::disconnectCurConnection() { if(!m_curConnection.isEmpty()) { disconnectToDB(m_curConnection); m_curConnection = ""; } } const QString DBStructureView::curConnection() { return m_curConnection; } DBStructureNode* DBStructureView::currentNode() { QModelIndex currentInex = currentIndex(); DBStructureNode* node = static_cast(currentInex.internalPointer()); 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); } } void DBStructureView::itemDoubleClick(const QModelIndex& index) { DBStructureNode* node = static_cast(index.internalPointer()); if(node->type() == ConnectionNode) { //先断掉当前链接 disconnectCurConnection(); //打开新链接 openConnection(node->name()); } else if(node->type() == GroupNode) { openAttributeGroup(node); } } void DBStructureView::showContextMenu(const QPoint& pos) { QModelIndex index = indexAt(pos); if(!index.isValid()) return; DBStructureNode* node = static_cast(index.internalPointer()); if(node && node->type() == ConnectionNode) { 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); //展开链接 DBStructureModel* model = dynamic_cast(this->model()); QModelIndex index = model->getConnNodeIndex(connName); if(model && index.isValid()) expand(index); })->setEnabled(!isConnected); menu.addAction(QString::fromWCharArray(L"断开链接"), [this, &connName]{disconnectCurConnection();})->setEnabled(isConnected); menu.addAction(QString::fromWCharArray(L"刷新"), [this, &connName]{ DBStructureModel* model = dynamic_cast(this->model()); if(model) model->refreshStructure_Connection(connName); })->setEnabled(isConnected); menu.addAction(QString::fromWCharArray(L"移除链接"), [this, &connName, node]{ if(m_pMainWindow) { m_pMainWindow->showMessageDialog(type_question, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"确认删除链接 '%1' 吗").arg(connName)); if(g_msgDlgBtn == btn_No) return; } removeNode(node); }); menu.addSeparator(); menu.addAction(QString::fromWCharArray(L"添加模型"), [this]{emit actionTrigger_addModel();})->setEnabled(isConnected); /*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() == TableNode) { 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; } removeNode(node); }); menu.addAction(QString::fromWCharArray(L"刷新"), []{}); menu.addSeparator(); menu.addAction(QString::fromWCharArray(L"添加属性组"), [this, node]{ emit actionTrigger_addGroup(node->data(Qt::UserRole + NodeDataRole::ID).toInt()); }); //menu.addAction(QString::fromWCharArray(L"打开"), []{}); /*menu.addSeparator(); menu.addAction(QString::fromWCharArray(L"清空数据"), []{}); 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; menu.addAction(QString::fromWCharArray(L"打开"), [this, node]{openAttributeGroup(node);}); menu.addAction(QString::fromWCharArray(L"删除"), [this, node]{ bool isPublic = node->data(Qt::UserRole + NodeDataRole::Type).toBool(); if(isPublic) { if(m_pMainWindow) { m_pMainWindow->showMessageDialog(type_warning, QString::fromWCharArray(L"错误"), QString::fromWCharArray(L"该属性组属于必备组别,不可删除")); } return; } 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.addSeparator(); menu.addAction(QString::fromWCharArray(L"清空数据"), []{}); QPoint originPoint = this->mapToGlobal(QPoint(0,0)); menu.exec(originPoint + pos); } }