完成模型删除功能,支持窗口主菜单和模型节点右键菜单触发
This commit is contained in:
parent
0aca9bd518
commit
a39f8f2faf
|
|
@ -6,6 +6,7 @@
|
|||
#include "dbStructureNode.h"
|
||||
#include "global.h"
|
||||
|
||||
class MainWindow;
|
||||
class DBStructureModel : public QAbstractItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
@ -14,6 +15,8 @@ public:
|
|||
explicit DBStructureModel(QObject* parent = nullptr);
|
||||
~DBStructureModel();
|
||||
|
||||
void setMainWindow(MainWindow*);
|
||||
|
||||
//QAbstractItemModel接口实现
|
||||
QModelIndex index(int row, int column, const QModelIndex& parent) const override;
|
||||
QModelIndex parent(const QModelIndex& index) const override;
|
||||
|
|
@ -26,6 +29,7 @@ public:
|
|||
void addConnection(const QString& name, const QString& dbType);
|
||||
QModelIndex getConnNodeIndex(const QString& name);
|
||||
void addDataModel(const QString& connection, Model& model);
|
||||
void removeDataModel(DBStructureNode*);
|
||||
|
||||
signals:
|
||||
void errorOccurred(const QString& strConnectionName);
|
||||
|
|
@ -37,6 +41,7 @@ private:
|
|||
DBStructureNode* getNode(const QModelIndex& index) const;
|
||||
DBStructureNode* getConnectionNode(const QString& name) const;
|
||||
|
||||
MainWindow* m_pMainWindow;
|
||||
DBStructureNode* m_rootNode;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ public:
|
|||
|
||||
//节点操作
|
||||
void appendChild(DBStructureNode* child);
|
||||
void removeChild(DBStructureNode* child);
|
||||
DBStructureNode* child(int row);
|
||||
int childCount() const;
|
||||
int row() const;
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@
|
|||
|
||||
#include <QTreeView>
|
||||
|
||||
class MainWindow;
|
||||
class DatabaseManager;
|
||||
class DBStructureNode;
|
||||
class DBStructureView : public QTreeView
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
@ -12,19 +14,25 @@ public:
|
|||
explicit DBStructureView(DatabaseManager* dbManager, QWidget* parent = nullptr);
|
||||
~DBStructureView();
|
||||
|
||||
void setMainWindow(MainWindow*);
|
||||
|
||||
void disconnectCurConnection();
|
||||
const QString curConnection();
|
||||
|
||||
void onActionTrigger_removeModel();
|
||||
|
||||
protected:
|
||||
void mouseDoubleClickEvent(QMouseEvent* event) override;
|
||||
|
||||
private:
|
||||
MainWindow* m_pMainWindow;
|
||||
DatabaseManager* m_dbManager;
|
||||
QString m_curConnection; //用来记录当前链接,只能存在一个链接
|
||||
|
||||
void initView();
|
||||
void connectToDB(const QString&);
|
||||
void disconnectToDB(const QString&);
|
||||
void removeNode(DBStructureNode*);
|
||||
|
||||
signals:
|
||||
void actionTrigger_addModel();
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
#include "dbStructureModel.h"
|
||||
#include "mainwindow.h"
|
||||
#include "sqlQueryExecutor.h"
|
||||
#include "logger.h"
|
||||
// #include <QSqlDatabase>
|
||||
// #include <QSqlQuery>
|
||||
|
||||
DBStructureModel::DBStructureModel(QObject* parent)
|
||||
: QAbstractItemModel(parent), m_rootNode(new DBStructureNode(RootNode, "Root"))
|
||||
: QAbstractItemModel(parent)
|
||||
, m_rootNode(new DBStructureNode(RootNode, "Root"))
|
||||
, m_pMainWindow(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -15,6 +18,11 @@ DBStructureModel::~DBStructureModel()
|
|||
delete m_rootNode;
|
||||
}
|
||||
|
||||
void DBStructureModel::setMainWindow(MainWindow* window)
|
||||
{
|
||||
m_pMainWindow = window;
|
||||
}
|
||||
|
||||
DBStructureNode* DBStructureModel::getNode(const QModelIndex& index) const
|
||||
{
|
||||
if(index.isValid())
|
||||
|
|
@ -156,6 +164,39 @@ void DBStructureModel::addDataModel(const QString& connection, Model& model)
|
|||
endInsertRows();
|
||||
}
|
||||
|
||||
void DBStructureModel::removeDataModel(DBStructureNode* modelNode)
|
||||
{
|
||||
//可以不对node在进行一系列的检查,因为该函数调取之前已有相关逻辑
|
||||
int modelID = modelNode->data(Qt::UserRole + NodeDataRole::ID).toInt();
|
||||
//先从数据库中删除
|
||||
DBStructureNode* connNode = modelNode->parentNode();
|
||||
if(!(connNode && connNode->type() == ConnectionNode))
|
||||
{
|
||||
if(m_pMainWindow)
|
||||
{
|
||||
QString error = QString::fromWCharArray(L"删除失败,未找到所在链接节点信息");
|
||||
m_pMainWindow->showMessageDialog(type_information, QString::fromWCharArray(L"失败"), error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
bool result = SqlQueryExecutor::instance().removeMode(connNode->name(), modelID);
|
||||
if(!result)
|
||||
{
|
||||
if(m_pMainWindow)
|
||||
{
|
||||
QString error = QString::fromWCharArray(L"删除失败,详情可见日志文件");
|
||||
m_pMainWindow->showMessageDialog(type_information, QString::fromWCharArray(L"失败"), error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else //从列表中删除
|
||||
{
|
||||
beginRemoveRows(index(connNode->row(), 0, QModelIndex()), modelNode->row(), modelNode->row());
|
||||
connNode->removeChild(modelNode);
|
||||
endRemoveRows();
|
||||
}
|
||||
}
|
||||
|
||||
void DBStructureModel::refreshStructure_Connection(const QString& connection)
|
||||
{
|
||||
DBStructureNode* connNode = getConnectionNode(connection);
|
||||
|
|
@ -202,6 +243,7 @@ void DBStructureModel::refreshStructure_Connection(const QString& connection)
|
|||
for(const Model& model : models)
|
||||
{
|
||||
DBStructureNode* modelNode = new DBStructureNode(TableNode, model.name, connNode);
|
||||
modelNode->setData(Qt::UserRole + NodeDataRole::ID, model.id);
|
||||
for(int groupID : model.groups)
|
||||
{
|
||||
QString groupName = SqlQueryExecutor::instance().getArributeGropuName(connection, groupID);
|
||||
|
|
|
|||
|
|
@ -57,6 +57,15 @@ void DBStructureNode::appendChild(DBStructureNode* child)
|
|||
}
|
||||
}
|
||||
|
||||
void DBStructureNode::removeChild(DBStructureNode* child)
|
||||
{
|
||||
if(m_children.removeOne(child))
|
||||
{
|
||||
delete child;
|
||||
child = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
DBStructureNode* DBStructureNode::child(int row)
|
||||
{
|
||||
if(row < 0 || row >= m_children.count())
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "dbStructureView.h"
|
||||
#include "dbStructureNode.h"
|
||||
#include "dbStructureModel.h"
|
||||
#include "mainwindow.h"
|
||||
#include "dbManager.h"
|
||||
#include "customMenu.h"
|
||||
#include <QMenu>
|
||||
|
|
@ -8,7 +9,7 @@
|
|||
#include <QMouseEvent>
|
||||
|
||||
DBStructureView::DBStructureView(DatabaseManager* dbManager, QWidget* parent)
|
||||
: QTreeView(parent), m_dbManager(dbManager)
|
||||
: QTreeView(parent), m_dbManager(dbManager), m_pMainWindow(nullptr)
|
||||
{
|
||||
// connect(m_dbManager, &DatabaseManager::connectionStatusChanged,
|
||||
// [this](const QString& connName, bool connected)
|
||||
|
|
@ -25,6 +26,11 @@ DBStructureView::~DBStructureView()
|
|||
|
||||
}
|
||||
|
||||
void DBStructureView::setMainWindow(MainWindow* window)
|
||||
{
|
||||
m_pMainWindow = window;
|
||||
}
|
||||
|
||||
void DBStructureView::mouseDoubleClickEvent(QMouseEvent* event)
|
||||
{
|
||||
//只对鼠标左键的双击事件进行响应
|
||||
|
|
@ -77,6 +83,24 @@ void DBStructureView::disconnectToDB(const QString& connName)
|
|||
}
|
||||
}
|
||||
|
||||
void DBStructureView::removeNode(DBStructureNode* node)
|
||||
{
|
||||
if(m_pMainWindow)
|
||||
{
|
||||
m_pMainWindow->showMessageDialog(type_question, QString::fromWCharArray(L"提示"),
|
||||
QString::fromWCharArray(L"确认删除当前节点吗"));
|
||||
if(g_msgDlgBtn == btn_No)
|
||||
return;
|
||||
}
|
||||
|
||||
if(node && node->type() == TableNode)
|
||||
{
|
||||
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
|
||||
if(model)
|
||||
model->removeDataModel(node);
|
||||
}
|
||||
}
|
||||
|
||||
void DBStructureView::disconnectCurConnection()
|
||||
{
|
||||
if(!m_curConnection.isEmpty())
|
||||
|
|
@ -91,6 +115,26 @@ const QString DBStructureView::curConnection()
|
|||
return m_curConnection;
|
||||
}
|
||||
|
||||
void DBStructureView::onActionTrigger_removeModel()
|
||||
{
|
||||
QModelIndex currentInex = currentIndex();
|
||||
DBStructureNode* node = static_cast<DBStructureNode*>(currentInex.internalPointer());
|
||||
if(node && node->type() == TableNode)
|
||||
{
|
||||
removeNode(node);
|
||||
}
|
||||
else if(node && node->type() == GroupNode)
|
||||
{
|
||||
DBStructureNode* parent = node->parentNode();
|
||||
removeNode(parent);
|
||||
}
|
||||
else if(m_pMainWindow)
|
||||
{
|
||||
m_pMainWindow->showMessageDialog(type_warning, QString::fromWCharArray(L"错误"),
|
||||
QString::fromWCharArray(L"请先选择一个模型节点"));
|
||||
}
|
||||
}
|
||||
|
||||
void DBStructureView::itemDoubleClick(const QModelIndex& index)
|
||||
{
|
||||
DBStructureNode* node = static_cast<DBStructureNode*>(index.internalPointer());
|
||||
|
|
@ -117,7 +161,7 @@ void DBStructureView::showContextMenu(const QPoint& pos)
|
|||
return;
|
||||
|
||||
DBStructureNode* node = static_cast<DBStructureNode*>(index.internalPointer());
|
||||
if(node->type() == ConnectionNode)
|
||||
if(node && node->type() == ConnectionNode)
|
||||
{
|
||||
bool isConnected = true;
|
||||
if(node->status() == Disconnect)
|
||||
|
|
@ -144,9 +188,13 @@ void DBStructureView::showContextMenu(const QPoint& pos)
|
|||
QPoint originPoint = this->mapToGlobal(QPoint(0,0));
|
||||
menu.exec(originPoint + pos);
|
||||
}
|
||||
else if(node->type() == TableNode)
|
||||
else if(node && node->type() == TableNode)
|
||||
{
|
||||
CustomMenu menu;
|
||||
menu.addAction(QString::fromWCharArray(L"删除"), [this]{});
|
||||
menu.addAction(QString::fromWCharArray(L"删除"), [this,node]{
|
||||
removeNode(node);
|
||||
});
|
||||
QPoint originPoint = this->mapToGlobal(QPoint(0,0));
|
||||
menu.exec(originPoint + pos);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,9 +63,11 @@ void MainWindow::initialize()
|
|||
connect(m_dbManager, &DatabaseManager::connectionStatusChanged, this, &MainWindow::onSIG_connectionStatusChanged);
|
||||
|
||||
m_pDBStrutureView = new DBStructureView(m_dbManager, this);
|
||||
m_pDBStrutureView->setMainWindow(this);
|
||||
connect(m_pDBStrutureView, &DBStructureView::actionTrigger_addModel, this, &MainWindow::onActionTrigger_addModel);
|
||||
ui->layoutDBStructure->addWidget(m_pDBStrutureView);
|
||||
m_pDBStrutureModel = new DBStructureModel(this);
|
||||
m_pDBStrutureModel->setMainWindow(this);
|
||||
m_pDBStrutureView->setModel(m_pDBStrutureModel);
|
||||
|
||||
QScreen* screen = this->screen();
|
||||
|
|
@ -160,7 +162,8 @@ void MainWindow::onActionTrigger_addModel()
|
|||
}
|
||||
void MainWindow::onActionTrigger_removeModel()
|
||||
{
|
||||
|
||||
if(m_pDBStrutureView)
|
||||
m_pDBStrutureView->onActionTrigger_removeModel();
|
||||
}
|
||||
|
||||
void MainWindow::onSIG_addConnection(DatabaseConfig& config)
|
||||
|
|
|
|||
|
|
@ -250,10 +250,12 @@ const QVector<AttributeGroup> SqlQueryExecutor::getAttributeGroup(const QString&
|
|||
const QString SqlQueryExecutor::getArributeGropuName(const QString& strConnectionName, int groupID)
|
||||
{
|
||||
QString name;
|
||||
QString strSQL = "SELECT group_name FROM basic.attribute_group WHERE id = " + QString::number(groupID);
|
||||
QString strSQL = "SELECT group_name FROM basic.attribute_group WHERE id = :id";
|
||||
QVariantHash params;
|
||||
params.insert(":id", groupID);
|
||||
try
|
||||
{
|
||||
QSqlQuery query = executeSQL(strConnectionName, strSQL);
|
||||
QSqlQuery query = executeSQL(strConnectionName, strSQL, params);
|
||||
if(query.next())
|
||||
name = query.value(0).toString();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue