diff --git a/include/attributeSelector.h b/include/attributeSelector.h index 6c6ef60..9a2d3ec 100644 --- a/include/attributeSelector.h +++ b/include/attributeSelector.h @@ -2,6 +2,7 @@ #define ATTRIBUTESELECTOR_H #include "global.h" +#include "messageDialog.h" #include QT_BEGIN_NAMESPACE @@ -23,11 +24,17 @@ public: void setMainWindow(MainWindow*); +protected: + virtual void showEvent(QShowEvent*); + private slots: void onBtnClicked_refreshData(); void onSyncDataStatus(bool, const PaginationInfo&); + void onShowMessage(MessageDialogType,const QString&,const QString&); private: + void iniData(); + Ui::AttributeSelector *ui; MainWindow* m_pMainWindow; AttributeView* m_attributeView; diff --git a/include/attributeTableModel.h b/include/attributeTableModel.h index e345d49..f4c0846 100644 --- a/include/attributeTableModel.h +++ b/include/attributeTableModel.h @@ -56,6 +56,7 @@ public: Qt::ItemFlags flags(const QModelIndex &index) const override; //ModelAttributeGroup getModelAttributeGroup() {return m_modelAttributeGroup;} + void setModelAttributeGroup(const ModelAttributeGroup&); bool attributeTypeExistsInCurrentGroup(int, const QString&); //除数据库外,还有从内存存储的数据(当前页m_currentPageData)中进行查找判断,因为可能有编辑完但未提交至数据空的信息 void updateRowThroughExisitingAttribute(int, int); diff --git a/include/attributeView.h b/include/attributeView.h index 0a6a1da..335fc47 100644 --- a/include/attributeView.h +++ b/include/attributeView.h @@ -25,6 +25,7 @@ public: AttributeTableDelegate* delegate() const { return m_attributeTableDelegate; } void active(); + void setEditable(bool); void syncChangeRecord(); signals: diff --git a/source/attributeSelector.cpp b/source/attributeSelector.cpp index 4afdb82..ec7f90f 100644 --- a/source/attributeSelector.cpp +++ b/source/attributeSelector.cpp @@ -2,6 +2,7 @@ #include "ui_attributeSelector.h" #include "mainwindow.h" #include "attributeView.h" +#include "sqlQueryExecutor.h" AttributeSelector::AttributeSelector(const QString& connection, QWidget *parent) : QWidget(parent) @@ -17,7 +18,10 @@ AttributeSelector::AttributeSelector(const QString& connection, QWidget *parent) ModelAttributeGroup attributeGroup(-1, -1, "" ,""); m_attributeView = new AttributeView(attributeGroup, ui->attributeViewContainer, connection); + m_attributeView->setEditable(false);//不可编辑 ui->layoutTableView->addWidget(m_attributeView); + connect(m_attributeView->model(), &AttributeTableModel::showMessage, this, &AttributeSelector::onShowMessage); + connect(m_attributeView->model(), &AttributeTableModel::syncDataStatus, this, &AttributeSelector::onSyncDataStatus); connect(ui->btnRefresh, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_refreshData); } @@ -27,6 +31,28 @@ AttributeSelector::~AttributeSelector() delete ui; } +void AttributeSelector::iniData() +{ + ui->lineEdit_attributeType->setText(""); + + ui->comboBox_model->clear(); + ui->comboBox_model->addItem(QString::fromWCharArray(L"所有模型"), -1); + const QVector models = SqlQueryExecutor::instance().getModels(m_connection); + for(const Model& model : models) + ui->comboBox_model->addItem(model.name, model.id); + + ui->comboBox_group->clear(); + ui->comboBox_group->addItem(QString::fromWCharArray(L"所有属性组"), -1); + const QVector groups = SqlQueryExecutor::instance().getAttributeGroup(m_connection); + for(const AttributeGroup& group : groups) + ui->comboBox_group->addItem(group.name, group.id); +} + +void AttributeSelector::showEvent(QShowEvent* e) +{ + +} + void AttributeSelector::setMainWindow(MainWindow* window) { m_pMainWindow = window; @@ -34,16 +60,25 @@ void AttributeSelector::setMainWindow(MainWindow* window) void AttributeSelector::onBtnClicked_refreshData() { - + AttributeTableModel* model = m_attributeView->model(); + if(model) + model->forceRefresh(); } void AttributeSelector::onSyncDataStatus(bool hasModifiedData, const PaginationInfo& paginationInfo) { - ui->btnSave->setEnabled(!hasModifiedData); - ui->btnCancle->setEnabled(!hasModifiedData); + // ui->btnSave->setEnabled(!hasModifiedData); + // ui->btnCancle->setEnabled(!hasModifiedData); QString recordInfo = QString::fromWCharArray(L"共 %1 条记录").arg(paginationInfo.totalEntries); ui->recordInfo->setText(recordInfo); ui->lineEdit->setText(QString::number(paginationInfo.currentPage)); ui->lineEdit->setEnabled(true); } + +void AttributeSelector::onShowMessage(MessageDialogType type,const QString& strTitle,const QString& strContent) +{ + if(m_pMainWindow) + m_pMainWindow->showMessageDialog(type, strTitle, strContent); +} + diff --git a/source/attributeTableModel.cpp b/source/attributeTableModel.cpp index 61d506c..740bc51 100644 --- a/source/attributeTableModel.cpp +++ b/source/attributeTableModel.cpp @@ -181,17 +181,20 @@ int AttributeTableModel::columnCount(const QModelIndex &) const Qt::ItemFlags AttributeTableModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QAbstractTableModel::flags(index); - if(m_modelAttributeGroup.modelID < 0) //表示当前加载数据对象是所有属性,前端UI是属性选择器,选择器为非可编辑状态 - return flags; - else - { - QModelIndex numberIndex = createIndex(index.row(), 0); - QString text = numberIndex.data(Qt::DisplayRole).toString(); + QModelIndex numberIndex = createIndex(index.row(), 0); + QString text = numberIndex.data(Qt::DisplayRole).toString(); - if (index.column() != 0 && !text.contains("-")) //行号列和处于删除状态的item不能编辑 - flags = flags | Qt::ItemIsEditable; - return flags; - } + if (index.column() != 0 && !text.contains("-")) //行号列和处于删除状态的item不能编辑 + flags = flags | Qt::ItemIsEditable; + return flags; +} + +void AttributeTableModel::setModelAttributeGroup(const ModelAttributeGroup& modelAttributeGroup) +{ + m_modelAttributeGroup = modelAttributeGroup; + //重新刷新 + m_paginationInfo.currentPage = 1; + forceRefresh(); } bool AttributeTableModel::attributeTypeExistsInCurrentGroup(int id, const QString& type) @@ -335,7 +338,7 @@ void AttributeTableModel::loadPageData() for(int i = 0; i < m_displayField.count(); i++) columns << m_displayField.at(i).originalName; - if(m_modelAttributeGroup.modelID < 0) //表示当前加载数据对象是所有属性 + if(m_modelAttributeGroup.modelID == -1) //表示当前加载数据对象是所有属性 { columns.append("id"); //将id放到最后,和按照具体模型、具体属性组读取数据方式时逻辑统一,具体见下面分支逻辑 QString strSQL = QString("SELECT %1 FROM basic.attribute LIMIT %2 OFFSET %3") @@ -367,8 +370,8 @@ void AttributeTableModel::loadPageData() .arg(m_modelAttributeGroup.groupID) .arg(m_paginationInfo.entriesPerPage) .arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage); - else - strSQL = QString("SELECT attribute_id FROM basic.model_attribute WHERE model_type_id = %1 AND attribute_group_id = %2 LIMIT %3 OFFSET %4") + else //利用短路机制,当attribute_group_id!=-1时,括号整体为false,需要进行内部条件判断,=-1时,整体为true,忽略整体,也就忽略了attribute_group_id的条件判断 + strSQL = QString("SELECT attribute_id FROM basic.model_attribute WHERE model_type_id = %1 AND (attribute_group_id = %2 OR %2 = -1) LIMIT %3 OFFSET %4") .arg(m_modelAttributeGroup.modelID) .arg(m_modelAttributeGroup.groupID) .arg(m_paginationInfo.entriesPerPage) @@ -474,7 +477,7 @@ void AttributeTableModel::updateTotalCount() return; }*/ - if(m_modelAttributeGroup.modelID < 0) + if(m_modelAttributeGroup.modelID == -1) m_paginationInfo.totalEntries = SqlQueryExecutor::instance().getAllAttributeCount(m_connection); else m_paginationInfo.totalEntries = SqlQueryExecutor::instance().getAttributeCount(m_connection, m_modelAttributeGroup.modelID, m_modelAttributeGroup.groupID); diff --git a/source/attributeView.cpp b/source/attributeView.cpp index f9529bf..c5460c3 100644 --- a/source/attributeView.cpp +++ b/source/attributeView.cpp @@ -76,6 +76,14 @@ void AttributeView::active() m_attributeTableModel->triggerSyncSignal(); } +void AttributeView::setEditable(bool editable) +{ + if(editable) + m_tableView->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed); + else + m_tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); +} + void AttributeView::syncChangeRecord() { if(m_attributeGroup.isPublic) diff --git a/source/dbBrowser.cpp b/source/dbBrowser.cpp index 1b04f53..6b213ce 100644 --- a/source/dbBrowser.cpp +++ b/source/dbBrowser.cpp @@ -97,12 +97,28 @@ void DatabaseBrowser::addTab_attribute(const QString& connection, ModelAttribute void DatabaseBrowser::closeTab_attribute(ModelAttributeGroup& attributeGroup) { - QString tabText = attributeGroup.strModelName + "/" + attributeGroup.strGroupName; - int index = tabIndex(tabText); - if(index == -1) - return; + if(attributeGroup.groupID == -1) //关闭该模型下所有打开的tab + { + //int tabCount = ui->tabWidget->count(); + for(int i = 0; i < ui->tabWidget->count(); i++) + { + if(ui->tabWidget->tabText(i).contains(attributeGroup.strModelName + "/")) + { + onTabCloseRequested(i); + i--; + } + } - onTabCloseRequested(index); + } + else //关闭具体的tab + { + QString tabText = attributeGroup.strModelName + "/" + attributeGroup.strGroupName; + int index = tabIndex(tabText); + if(index == -1) + return; + + onTabCloseRequested(index); + } } void DatabaseBrowser::closeAllTab_attribute() diff --git a/source/dbStructureView.cpp b/source/dbStructureView.cpp index 320fc73..74f1f4a 100644 --- a/source/dbStructureView.cpp +++ b/source/dbStructureView.cpp @@ -144,14 +144,24 @@ void DBStructureView::openAttributeGroup(DBStructureNode* node) void DBStructureView::closeAttributeGroup(DBStructureNode* node) { - DBStructureNode* parent = node->parentNode(); - if(parent && parent->type() == TableNode) + if(node->type() == GroupNode) { - 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); + 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, ""); emit closeAttributeInfo(attributeGroup); } } @@ -290,6 +300,8 @@ void DBStructureView::showContextMenu(const QPoint& pos) if(g_msgDlgBtn == btn_No) return; } + //关闭tab(若打开) + closeAttributeGroup(node); removeNode(node); }); menu.addAction(QString::fromWCharArray(L"刷新"), []{}); diff --git a/source/sqlQueryExecutor.cpp b/source/sqlQueryExecutor.cpp index fc016fe..9c3b6cc 100644 --- a/source/sqlQueryExecutor.cpp +++ b/source/sqlQueryExecutor.cpp @@ -564,7 +564,8 @@ bool SqlQueryExecutor::removeAttributeGroup(const QString& connectionName, int m int SqlQueryExecutor::getAttributeCount(const QString& connectionName, int modelID, int groupID) { int count = 0; - QString strSQL = QString("SELECT COUNT(*) FROM basic.model_attribute WHERE model_type_id = %1 AND attribute_group_id = %2").arg(modelID).arg(groupID); + //利用短路机制,当attribute_group_id!=-1时,括号整体为false,需要进行内部条件判断,=-1时,整体为true,忽略整体,也就忽略了attribute_group_id的条件判断 + QString strSQL = QString("SELECT COUNT(*) FROM basic.model_attribute WHERE model_type_id = %1 AND (attribute_group_id = %2 OR %2 = -1)").arg(modelID).arg(groupID); try { QSqlQuery query = executeSQL(connectionName, strSQL);