diff --git a/include/attributeTableModel.h b/include/attributeTableModel.h index 6bb86b2..a54caa0 100644 --- a/include/attributeTableModel.h +++ b/include/attributeTableModel.h @@ -35,6 +35,14 @@ public: //创建 QFlags 包装类型 Q_DECLARE_FLAGS(EditState, EditStateFlag) + struct RowData + { + //QSqlRecord record; + QVector values; + QHash cellModified; //记录单元格是否被修改 + EditState state = EditState(Clean); + }; + struct DataType //数据类型 { int id; @@ -78,10 +86,12 @@ public: void refresh(); void forceRefresh(); //强制刷新(不会出现询问提示,数据同步时会用到) void insertRecord(int); + void addRecords(QList); void removeRecord(); bool removeRecord(int); void submitChanges(); //提交更改(增、删、改) void cancleChanges(); //取消修改 + QList getSelectedRowData(); //展示列控制 //void setVisibleColumns(const QStringList& columns); @@ -104,14 +114,6 @@ private: QString dataType = "unknown type"; //数据类型 }; - struct RowData - { - //QSqlRecord record; - QVector values; - QHash cellModified; //记录单元格是否被修改 - EditState state = EditState(Clean); - }; - void iniDisplayField(); void getDataTypesFromDB(); void loadPageData(); // 加载当前页数据 diff --git a/include/mainwindow.h b/include/mainwindow.h index 4ad2532..f383cac 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -65,6 +65,7 @@ private slots: void onSIG_connectionStatusChanged(const QString& strConnectionName, bool bConnected); void onSIG_addModel(Model&); void onSIG_addGroups(int, QVector); + //void onSIG_addAttributes(); void onSIG_openAttributeInfo(const QString&, ModelAttributeGroup&); void onSIG_closeAttributeInfo(ModelAttributeGroup&); void onSIG_closeAllAttributeInfo(); diff --git a/include/sqlQueryExecutor.h b/include/sqlQueryExecutor.h index b28d775..7d53e6a 100644 --- a/include/sqlQueryExecutor.h +++ b/include/sqlQueryExecutor.h @@ -42,7 +42,7 @@ public: int getAttributeCount(const QString&, int, int); int getAllAttributeCount(const QString&); bool getAttributeInfo(const QString&, const QString&, Attribute&); - int attributeTypeExistsInDB(const QString&, const QString&); + int getAttributeID(const QString&, const QString&); bool attributeTypeUseByModelGroup(const QString&, int, int, int); bool batchInsertAttributes(const QString&, int, int, QList); bool batchDeleteAttributes(const QString&, int, int, QList); diff --git a/source/attributeTableDelegate.cpp b/source/attributeTableDelegate.cpp index 3c7c284..8097091 100644 --- a/source/attributeTableDelegate.cpp +++ b/source/attributeTableDelegate.cpp @@ -187,7 +187,7 @@ QWidget* AttributeTableDelegate::createEditor(QWidget *parent, const QStyleOptio textEditor->setRegularExpression("[A-Za-z0-9_]"); connect(textEditor, &TextEditWidget::confirm, this, [=]{ /*QString strText = textEditor->editText(); - int id = SqlQueryExecutor::instance().attributeTypeExistsInDB(m_connection, strText); + int id = SqlQueryExecutor::instance().getAttributeID(m_connection, strText); if(id != -1) textEditor->setErrorInfo(QString::fromWCharArray(L"该类型已存在")); else @@ -313,7 +313,7 @@ void AttributeTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *m if(attributeTableModel && textEditor) { QString strText = textEditor->editText(); - int id = SqlQueryExecutor::instance().attributeTypeExistsInDB(m_connection, strText); + int id = SqlQueryExecutor::instance().getAttributeID(m_connection, strText); //1.是否以已在同组中(要同时从内存和数据库中都进行查找,避免遗漏已创建但未提交的数据) bool existsInCurrentGropu = attributeTableModel->attributeTypeExistsInCurrentGroup(id, strText); if(existsInCurrentGropu) diff --git a/source/attributeTableModel.cpp b/source/attributeTableModel.cpp index c25c700..8a8ded3 100644 --- a/source/attributeTableModel.cpp +++ b/source/attributeTableModel.cpp @@ -355,7 +355,7 @@ void AttributeTableModel::loadPageData() if(m_modelAttributeGroup.modelID == -1) //表示当前加载数据对象是所有属性 { columns.append("id"); //将id放到最后,和按照具体模型、具体属性组读取数据方式时逻辑统一,具体见下面分支逻辑 - QString strSQL = QString("SELECT %1 FROM basic.attribute LIMIT %2 OFFSET %3") + QString strSQL = QString("SELECT %1 FROM basic.attribute ORDER BY id ASC LIMIT %2 OFFSET %3") .arg(columns.join(", ")) .arg(m_paginationInfo.entriesPerPage) .arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage); @@ -384,12 +384,12 @@ void AttributeTableModel::loadPageData() QString strSQL = ""; bool isPublicGroup = SqlQueryExecutor::instance().isPublicAttributeGroup(m_connection, m_modelAttributeGroup.groupID); if(isPublicGroup) - strSQL = QString("SELECT attribute_id FROM basic.model_attribute_public WHERE attribute_group_id = %1 LIMIT %2 OFFSET %3") + strSQL = QString("SELECT attribute_id FROM basic.model_attribute_public WHERE attribute_group_id = %1 ORDER BY id ASC LIMIT %2 OFFSET %3") .arg(m_modelAttributeGroup.groupID) .arg(m_paginationInfo.entriesPerPage) .arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage); //利用短路机制,当attribute_group_id!=-1时,括号整体为false,需要进行内部条件判断,=-1时,整体为true,忽略整体,也就忽略了attribute_group_id的条件判断 - else 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") + else strSQL = QString("SELECT attribute_id FROM basic.model_attribute WHERE model_type_id = %1 AND (attribute_group_id = %2 OR %2 = -1) ORDER BY id ASC LIMIT %3 OFFSET %4") .arg(m_modelAttributeGroup.modelID) .arg(m_modelAttributeGroup.groupID) .arg(m_paginationInfo.entriesPerPage) @@ -568,6 +568,40 @@ void AttributeTableModel::insertRecord(int row) endInsertRows(); } +void AttributeTableModel::addRecords(QList records) +{ + QList validRecords; + //首先过滤掉已经存在记录 + for(const RowData& record : records) + { + int id = record.values.last().toInt(); //目前该函数的数据源都来‘属性选择器’(都是已创建好的属性) + QString type = record.values.value(0).toString(); + bool existed = attributeTypeExistsInCurrentGroup(id, type); + if(!existed) + validRecords.append(record); + } + if(validRecords.count() == 0) + return; + + beginInsertRows(QModelIndex(), rowCount(), rowCount() + records.count() - 1); + + int row = m_currentPageData.count(); + for(const RowData& record : records) + { + RowData newRow; + newRow.state = New; + newRow.values = record.values; + + int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row; + m_modifiedRows[globalRow] = newRow; + m_currentPageData.insert(row, newRow); + + row++; + } + + endInsertRows(); +} + void AttributeTableModel::removeRecord() { //以第一列也就是编号列是否选中为判断依据,找出所有被选中的行 @@ -712,6 +746,18 @@ void AttributeTableModel::cancleChanges() emit syncDataStatus(dataHasbeenModified(), m_paginationInfo); } +QList AttributeTableModel::getSelectedRowData() +{ + QList result; + for(int row = 0; row < rowCount(); row++) + { + QModelIndex numberIndex = createIndex(row, 0); + if(m_selectionModel && m_selectionModel->isSelected(numberIndex)) + result.append(m_currentPageData.at(row)); + } + return result; +} + void AttributeTableModel::triggerSyncSignal() { emit syncDataStatus(dataHasbeenModified(), m_paginationInfo); diff --git a/source/dbStructureModel.cpp b/source/dbStructureModel.cpp index 40d9c9c..7324385 100644 --- a/source/dbStructureModel.cpp +++ b/source/dbStructureModel.cpp @@ -244,7 +244,7 @@ void DBStructureModel::addDataGroup(const QString& connection, int modelID, QVec QModelIndex connIndex = index(connNode->row(), 0, QModelIndex()); QModelIndex modelIndex = index(modelNode->row(), 0, connIndex); - beginInsertRows(modelIndex, modelNode->childCount(), groups.count()); + beginInsertRows(modelIndex, modelNode->childCount(), modelNode->childCount() + groups.count() - 1); for(int groupID : groups) { diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index ebe2f01..409e59c 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -337,7 +337,7 @@ void MainWindow::onSIG_openAttributeSelector() m_pAttributeSelector = new AttributeSelector(getCurConnection(), this); m_pAttributeSelector->setMainWindow(this); m_pAttributeSelector->installEventFilter(this); - connect(m_pAttributeSelector, &AttributeSelector::finished, this, [=]{ MaskManager::instance()->hideMask(m_pAttributeSelector);}); + connect(m_pAttributeSelector, &AttributeSelector::finished, this, [=]{ MaskManager::instance()->hideMask(m_pAttributeSelector); }); } int nX = (this->width() - m_pAttributeSelector->width()) * 0.5; diff --git a/source/sqlQueryExecutor.cpp b/source/sqlQueryExecutor.cpp index bdb84cf..f374145 100644 --- a/source/sqlQueryExecutor.cpp +++ b/source/sqlQueryExecutor.cpp @@ -233,7 +233,7 @@ int SqlQueryExecutor::getModelCount(const QString& connectionName) QVector SqlQueryExecutor::getModelGroups(const QString& connectionName, int modelID) { QVector groups; - QString strSQL = "SELECT attribute_group_id FROM basic.model_group WHERE model_type_id = " + QString::number(modelID); + QString strSQL = "SELECT attribute_group_id FROM basic.model_group WHERE model_type_id = " + QString::number(modelID) + " ORDER BY id ASC"; try { QSqlQuery query = executeSQL(connectionName, strSQL); @@ -642,7 +642,7 @@ bool SqlQueryExecutor::getAttributeInfo(const QString& connectionName, const QSt return true; } -int SqlQueryExecutor::attributeTypeExistsInDB(const QString& connectionName, const QString& type) +int SqlQueryExecutor::getAttributeID(const QString& connectionName, const QString& type) { int id = -1; QString strSQL = "SELECT id FROM basic.attribute WHERE attribute = \'" + type + "\'"; @@ -661,11 +661,23 @@ int SqlQueryExecutor::attributeTypeExistsInDB(const QString& connectionName, con bool SqlQueryExecutor::attributeTypeUseByModelGroup(const QString& connectionName, int attributeID, int modelID, int groupID) { - QString strSQL = "SELECT id FROM basic.model_attribute WHERE model_type_id = :modelID AND attribute_group_id = :groupID AND attribute_id = :attributeID"; + QString strSQL; QVariantHash params; - params.insert(":modelID", modelID); - params.insert(":groupID", groupID); - params.insert(":attributeID", attributeID); + bool isPublicGroup = isPublicAttributeGroup(connectionName, groupID); + if(isPublicGroup) + { + strSQL = "SELECT id FROM basic.model_attribute_public WHERE attribute_group_id = :groupID AND attribute_id = :attributeID"; + params.insert(":groupID", groupID); + params.insert(":attributeID", attributeID); + } + else + { + strSQL = "SELECT id FROM basic.model_attribute WHERE model_type_id = :modelID AND attribute_group_id = :groupID AND attribute_id = :attributeID"; + params.insert(":modelID", modelID); + params.insert(":groupID", groupID); + params.insert(":attributeID", attributeID); + } + try { QSqlQuery query = executeSQL(connectionName, strSQL, params);