修改链接bug,增加移除链接功能

This commit is contained in:
duanshengchao 2025-04-16 16:37:32 +08:00
parent b325b5f2e1
commit 246df65400
11 changed files with 196 additions and 35 deletions

View File

@ -16,6 +16,7 @@ public:
~DatabaseManager();
bool addDatabase(const DatabaseConfig& config);
void removeDatabase(const QString& strConnectionName);
bool connect(const QString& strConnectionName);
void disconnect(const QString& strConnectionName);
QStringList conncetions();//获取所有链接名称

View File

@ -27,6 +27,7 @@ public:
//业务功能接口
void addConnection(const QString& name, const QString& dbType);
void removeConnection(const QString& name);
QModelIndex getConnNodeIndex(const QString& name);
void addDataModel(const QString& connection, Model& model);
void removeDataModel(DBStructureNode*);

View File

@ -4,6 +4,61 @@
#include <QWidget>
#include <QSet>
template <typename T>
class UniqueList
{
private:
QList<T> m_list;
QSet<T> m_set;
public:
const QList<T>& getList() const { return m_list; }
bool isEmpty() { return m_list.isEmpty(); }
int size() const { return m_list.size(); }
void appendUnique(const T& value)
{
if(m_set.contains(value))
return;
m_set.insert(value);
m_list.append(value);
}
void remove(const T& value)
{
if(m_set.remove(value))
{
m_list.removeOne(value);
}
}
T& first()
{
if(m_list.isEmpty())
throw std::out_of_range("UniqueList is empty");
return m_list.first();
}
T& last()
{
if(m_list.isEmpty())
throw std::out_of_range("UniqueList is empty");
return m_list.last();
}
T& at(int index)
{
if (index < 0 || index >= m_list.size())
throw std::out_of_range("Index out of range");
return m_list[index];
}
T& operator[](int index) { return at(index); }
};
class MaskManager : public QObject
{
Q_OBJECT
@ -24,7 +79,7 @@ private:
static MaskManager* m_instance;
QWidget* m_mainWindow;
QWidget* m_maskLayer;
QSet<QWidget*> m_activeRequests;
UniqueList<QWidget*> m_activeRequests;
};
#endif //MASKMANAGER_H

View File

@ -25,7 +25,7 @@ ConnectionDialog::ConnectionDialog(QWidget *parent)
}
else
{
setModal(true);
//setModal(true);
}
initialize();
@ -39,7 +39,14 @@ ConnectionDialog::~ConnectionDialog()
void ConnectionDialog::showEvent(QShowEvent* e)
{
if(ui->connectionList->rowCount() > 0)
{
ui->connectionList->setCurrentCell(0, 0);
//刷新信息
QTableWidgetItem* item = ui->connectionList->item(0, 0);
QString connID = item->data(Qt::UserRole + itemID).toString();
loadConnInfo(connID);
m_curConnListRow = 0;
}
QDialog::showEvent(e);
}
@ -241,7 +248,10 @@ void ConnectionDialog::onTableCellClicked_connList(int row, int column)
m_pMainWindow->showMessageDialog(type_question, QString::fromWCharArray(L"提示"),
QString::fromWCharArray(L"当前正在新建连接的编辑中,确定放弃吗"));
if(g_msgDlgBtn == btn_No)
{
ui->connectionList->setCurrentCell(m_curConnListRow, 0);
return;
}
}
m_isNewStatus = false;
removeConnListItem(m_curConnListRow);

View File

@ -54,6 +54,13 @@ bool DatabaseManager::addDatabase(const DatabaseConfig& config)
m_configs[config.strConnectionName] = config;
return true;
}
void DatabaseManager::removeDatabase(const QString& strConnectionName)
{
//先断开链接
disconnect(strConnectionName);
//从内存中删除
m_configs.remove(strConnectionName);
}
bool DatabaseManager::connect(const QString& strConnectionName)
{

View File

@ -123,6 +123,17 @@ void DBStructureModel::addConnection(const QString& name, const QString& dbType)
endInsertRows(); //该语句之后会触发rowsInserted(const QModelIndex &parent, int first, int last)信号,通知视图刷新对应行
}
void DBStructureModel::removeConnection(const QString& name)
{
DBStructureNode* connNode = getConnectionNode(name);
if(!connNode)
return;
beginRemoveRows(QModelIndex(), connNode->row(), connNode->row());
m_rootNode->removeChild(connNode);
endRemoveRows();
}
QModelIndex DBStructureModel::getConnNodeIndex(const QString& name)
{
DBStructureNode* connNode = getConnectionNode(name);

View File

@ -91,20 +91,31 @@ void DBStructureView::disconnectToDB(const QString& connName)
void DBStructureView::removeNode(DBStructureNode* node)
{
if(m_pMainWindow)
if(!node)
{
m_pMainWindow->showMessageDialog(type_question, QString::fromWCharArray(L"提示"),
QString::fromWCharArray(L"确认删除当前节点吗"));
if(g_msgDlgBtn == btn_No)
return;
if(m_pMainWindow)
m_pMainWindow->showMessageDialog(type_warning, QString::fromWCharArray(L"错误"),
QString::fromWCharArray(L"获取节点数据失败"));
return;
}
if(node && node->type() == TableNode)
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)
{}
}
void DBStructureView::openAttributeGroup(DBStructureNode* node)
@ -139,20 +150,38 @@ void DBStructureView::onActionTrigger_removeModel()
{
QModelIndex currentInex = currentIndex();
DBStructureNode* node = static_cast<DBStructureNode*>(currentInex.internalPointer());
if(node && node->type() == TableNode)
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 && node->type() == GroupNode)
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);
}
else if(m_pMainWindow)
{
m_pMainWindow->showMessageDialog(type_warning, QString::fromWCharArray(L"错误"),
QString::fromWCharArray(L"请先选择一个模型节点"));
}
}
void DBStructureView::itemDoubleClick(const QModelIndex& index)
@ -203,6 +232,16 @@ void DBStructureView::showContextMenu(const QPoint& pos)
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"导入数据"), []{});
@ -213,7 +252,14 @@ void DBStructureView::showContextMenu(const QPoint& pos)
else if(node && node->type() == TableNode)
{
CustomMenu menu;
menu.addAction(QString::fromWCharArray(L"删除"), [this,node]{
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.addSeparator();
@ -229,7 +275,7 @@ void DBStructureView::showContextMenu(const QPoint& pos)
else if(node && node->type() == GroupNode)
{
CustomMenu menu;
menu.addAction(QString::fromWCharArray(L"删除"), [this,node]{
menu.addAction(QString::fromWCharArray(L"删除"), [this, node]{
removeNode(node);
});
menu.addSeparator();

View File

@ -135,10 +135,10 @@ QString Logger::formatLogMessage(LogLevel level, const QString& context, const Q
{
static const char* levelStrings[] = {"FATAL", "ERROR", "WARNING", "INFO", "DEBUG"};
return QString("[%1] [%2] [%3] %4")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
.arg(levelStrings[level])
.arg(context)
.arg(message);
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")
,levelStrings[level]
,context
,message);
}
void Logger::log(LogLevel level, const QString& context, const QString& message)

View File

@ -103,21 +103,21 @@ void MainWindow::showMessageDialog(MessageDialogType type,const QString& strTitl
{
m_pMessageDialog = new MessageDialog(this);
m_pMessageDialog->installEventFilter(this);
//对话框必须是close才会发送finished信号hide不可以
connect(m_pMessageDialog, &MessageDialog::finished, this, [=](int result){MaskManager::instance()->hideMask(m_pMessageDialog);});
}
m_pMessageDialog->setMessage(type, strTitle, strContent);
int nX = this->geometry().x() + (this->width() - m_pMessageDialog->width()) * 0.5;
int nY = this->geometry().y() + (this->height() - m_pMessageDialog->height()) * 0.5;
m_pMessageDialog->move(nX, nY);
//m_pMessageDialog->raise();
MaskManager::instance()->showMask(m_pMessageDialog);
// if(type == type_question)
// m_pMessageDialog->exec();
// else
// m_pMessageDialog->show();
MaskManager::instance()->showMask(m_pMessageDialog);
m_pMessageDialog->exec();
MaskManager::instance()->hideMask(m_pMessageDialog);
}
void MainWindow::hideMessageDialog()
{
@ -150,6 +150,7 @@ void MainWindow::onActionTrigger_connect()
m_pConnectionDialog->setMainWindow(this);
m_pConnectionDialog->installEventFilter(this);
connect(m_pConnectionDialog, &ConnectionDialog::addConnection, this, &MainWindow::onSIG_addConnection);
connect(m_pConnectionDialog, &ConnectionDialog::finished, this, [=]{ MaskManager::instance()->hideMask(m_pConnectionDialog);});
}
int nX = this->geometry().x() + (this->width() - m_pConnectionDialog->width()) * 0.5;
@ -160,8 +161,7 @@ void MainWindow::onActionTrigger_connect()
// m_pConnectionDialog->move(centerPos);
MaskManager::instance()->showMask(m_pConnectionDialog);
m_pConnectionDialog->exec();
MaskManager::instance()->hideMask(m_pConnectionDialog);
m_pConnectionDialog->show();
}
void MainWindow::onActionTrigger_disconnect()
{

View File

@ -1,5 +1,7 @@
#include "maskManager.h"
#include <QEvent>
#include <QApplication>
#include <QDialog>
MaskManager* MaskManager::m_instance = nullptr;
@ -21,6 +23,7 @@ MaskManager::MaskManager(QWidget* mainWindow)
{
m_maskLayer = new QWidget(m_mainWindow);
//m_maskLayer->setAttribute(Qt::WA_TransparentForMouseEvents, true);
m_maskLayer->setObjectName("maskLayer");
m_maskLayer->setStyleSheet("background:rgba(112,128,144,180);");
m_maskLayer->hide();
@ -42,10 +45,30 @@ void MaskManager::showMask(QWidget* requester)
if(m_activeRequests.isEmpty())
{
m_maskLayer->setGeometry(m_mainWindow->rect());
//m_maskLayer->raise(); //显示在最上层,后续打开的同级子窗口依然在其之上
m_maskLayer->show();
}
m_activeRequests.insert(requester);
else
{
QWidget* widget = m_activeRequests.last();
if(widget->parentWidget() == m_mainWindow && widget->isVisible() && !widget->isModal())
{
// qDebug() << "Child Dialog Order:";
// foreach (QObject* child, m_mainWindow->children())
// {
// if(qobject_cast<QDialog*>(child) || qobject_cast<QWidget*>(child))
// qDebug() << child->objectName();
// }
widget->stackUnder(m_maskLayer);
//widget->lower();
//强制刷新
//m_mainWindow->update();
//QApplication::processEvents();
}
}
m_activeRequests.appendUnique(requester);
//qDebug() << "append requester: " << requester->objectName() << "requester count: " << m_activeRequests.size();
}
void MaskManager::hideMask(QWidget* requester)
@ -53,4 +76,11 @@ void MaskManager::hideMask(QWidget* requester)
m_activeRequests.remove(requester);
if(m_activeRequests.isEmpty())
m_maskLayer->hide();
else
{
QWidget* widget = m_activeRequests.last();
widget->raise();
}
//qDebug() << "remove requester: " << requester->objectName() << "requester count: " << m_activeRequests.size();
}

View File

@ -60,21 +60,21 @@ void MessageDialog::setMessage(MessageDialogType type,const QString& strTitle,co
void MessageDialog::onBtnClicked_confirm()
{
hide();
emit sgl_hide();
close();
//emit sgl_hide();
}
void MessageDialog::onBtnClicked_yes()
{
g_msgDlgBtn = btn_Yes;
hide();
emit sgl_hide();
close();
//emit sgl_hide();
}
void MessageDialog::onBtnClicked_no()
{
g_msgDlgBtn = btn_No;
hide();
emit sgl_hide();
close();
//emit sgl_hide();
}