PowerModeler/source/attributeTableModel.cpp

468 lines
14 KiB
C++
Raw Normal View History

#include "attributeTableModel.h"
#include "logger.h"
#include "sqlQueryExecutor.h"
2025-03-25 17:58:48 +08:00
AttributeTableModel::AttributeTableModel(const ModelAttributeGroup& modelAttributeGroup, QObject* parent, const QString& connection, const QString& tableName)
: QAbstractTableModel(parent)
, m_connection(connection)
, m_tableName(tableName)
2025-03-25 17:58:48 +08:00
, m_modelAttributeGroup(modelAttributeGroup)
{
2025-03-25 17:58:48 +08:00
m_paginationInfo.entriesPerPage = 100;
m_paginationInfo.currentPage = 1;
m_paginationInfo.totalEntries = 0;
getDataTypes();
2025-03-27 21:01:25 +08:00
iniDisplayField();
2025-04-01 16:45:30 +08:00
refresh();
}
AttributeTableModel::~AttributeTableModel()
{}
QModelIndex AttributeTableModel::index(int row, int column, const QModelIndex& parent) const
{
if(!hasIndex(row, column, parent))
return QModelIndex();
int dataCol = column - 1;
2025-03-27 21:01:25 +08:00
if(dataCol >= m_displayField.count())
return QModelIndex();
return createIndex(row, column);
}
QVariant AttributeTableModel::data(const QModelIndex& index, int role) const
{
if(!index.isValid())
return QVariant();
int row = index.row();
int col = index.column();
2025-03-25 17:58:48 +08:00
int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row;
if(index.column() == 0) //第一列显示行号
{
switch (role)
{
case Qt::DisplayRole:
{
QString mark;
if(m_modifiedRows.contains(globalRow))
{
switch(m_modifiedRows[globalRow].state)
{
case Modified:
mark = "*";
break;
case New:
mark = "+";
break;
case Deleted:
mark = "-";
break;
default:
break;
}
}
return QString::number(globalRow + 1) + mark;
}
case Qt::DecorationRole:
return QVariant();
case Qt::TextAlignmentRole:
return Qt::AlignCenter; //行号居中展示
default:
return QVariant();
}
}
else
{
int dataCol = col - 1;
const RowData& rowData = m_currentPageData[row];
if (role == Qt::DisplayRole || role == Qt::EditRole)
//return rowData.record.value(dataCol);
return rowData.values.value(dataCol);
else if (role == Qt::UserRole + 100) //以100来标识数据是否被编辑,在相关代理Delegate类实现中同步进行处理文字加粗等效果
return (rowData.state != Clean);
}
return QVariant();
}
bool AttributeTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole || index.column() == 0)
return false;
2025-03-27 21:01:25 +08:00
// QModelIndex numberIndex = createIndex(index.row(), 0);
// QString text = numberIndex.data(Qt::DisplayRole).toString();
// qDebug() << text;
// if(text.contains("-")) //删除状态不可编辑
// return false;
int row = index.row();
2025-03-25 17:58:48 +08:00
int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row;
int dataCol = index.column() - 1; //第一列显示了行号
//记录修改
RowData modifiedRow = m_currentPageData[row];
//modifiedRow.record.setValue(dataCol, value);
modifiedRow.values[dataCol] = value;
modifiedRow.state = (modifiedRow.state == New) ? New : Modified;
m_modifiedRows[globalRow] = modifiedRow;
m_currentPageData[row] = modifiedRow;
emit dataChanged(index, index, {role, Qt::UserRole + 100});
return true;
}
QVariant AttributeTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
if(section == 0)
return role == Qt::DisplayRole ? "No." : QVariant();
else
{
QString headerText = m_displayField.value(section - 1).displayName;
if(!m_displayField.value(section - 1).dataType.isEmpty())
headerText = headerText + "\n" + m_displayField.value(section - 1).dataType;
return headerText;
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}
int AttributeTableModel::rowCount(const QModelIndex& parent) const
{
return m_currentPageData.count();
}
int AttributeTableModel::columnCount(const QModelIndex &) const
{
2025-03-27 21:01:25 +08:00
return m_displayField.isEmpty() ? 0 : m_displayField.count() + 1;
}
Qt::ItemFlags AttributeTableModel::flags(const QModelIndex &index) const
{
2025-03-27 21:01:25 +08:00
QModelIndex numberIndex = createIndex(index.row(), 0);
QString text = numberIndex.data(Qt::DisplayRole).toString();
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
if (index.column() != 0 && !text.contains("-")) //行号列和处于删除状态的item不能编辑
flags = flags | Qt::ItemIsEditable;
return flags;
}
2025-03-27 21:01:25 +08:00
void AttributeTableModel::iniDisplayField()
{
FieldInfo field1;
field1.originalName = "attribute";
field1.displayName = QString::fromWCharArray(L"属性类别");
//field1.dataType = "character varying(128)";
2025-03-27 21:01:25 +08:00
m_displayField.append(field1);
FieldInfo field2;
field2.originalName = "attribute_name";
field2.displayName = QString::fromWCharArray(L"属性名称");
//field2.dataType = "character varying(64)";
2025-03-27 21:01:25 +08:00
m_displayField.append(field2);
FieldInfo field3;
field3.originalName = "data_type_id";
field3.displayName = QString::fromWCharArray(L"数据类型");
//field3.dataType = "bigint";
2025-03-27 21:01:25 +08:00
m_displayField.append(field3);
FieldInfo field4;
field4.originalName = "length_precision";
field4.displayName = QString::fromWCharArray(L"数据长度");
//field4.dataType = "integer";
2025-03-27 21:01:25 +08:00
m_displayField.append(field4);
FieldInfo field5;
field5.originalName = "default_value";
field5.displayName = QString::fromWCharArray(L"默认值");
//field5.dataType = "character varying(64)";
2025-03-27 21:01:25 +08:00
m_displayField.append(field5);
QHash<QString, QString> fieldType = SqlQueryExecutor::instance().getFiledType(m_connection, "attribute", "basic");
for(int i = 0; i < m_displayField.count(); i++)
{
QString strFiled = m_displayField.at(i).originalName;
m_displayField[i].dataType = fieldType.value(strFiled);
}
}
void AttributeTableModel::getDataTypes()
{
QString strSQL = "SELECT * FROM basic.data_type ORDER BY id ASC";
try
{
QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL);
while(query.next())
{
DataType dataType;
dataType.id = query.value(0).toInt();
dataType.type = query.value(1).toString();
dataType.db = query.value(2).toString();
m_dataTypes.insert(dataType.id, dataType);
}
}
catch (const DatabaseException& e)
{
LOG_ERROR("SQL", QString::fromWCharArray(L"获取数据类型失败"));
}
2025-03-27 21:01:25 +08:00
}
void AttributeTableModel::loadPageData()
{
m_currentPageData.clear();
2025-04-01 16:45:30 +08:00
/*if (m_tableName.isEmpty())
{
LOG_ERROR("DB", QString("Attribute table name is empty, load data failed"));
return;
2025-04-01 16:45:30 +08:00
}*/
beginResetModel();
2025-04-01 16:45:30 +08:00
QString strSQL = QString("SELECT attribute_id FROM basic.model_attribute WHERE model_type_id = %1 AND attribute_group_id = %2 LIMIT %3 OFFSET %4")
.arg(m_modelAttributeGroup.modelID)
.arg(m_modelAttributeGroup.groupID)
2025-03-25 17:58:48 +08:00
.arg(m_paginationInfo.entriesPerPage)
.arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage);
try
{
2025-04-01 16:45:30 +08:00
QStringList columns;
for(int i = 0; i < m_displayField.count(); i++)
columns << m_displayField.at(i).originalName;
QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL);
while(query.next())
{
2025-04-01 16:45:30 +08:00
Attribute attribute;
attribute.id = query.value(0).toInt();
if(SqlQueryExecutor::instance().getAtrributeInfo(m_connection, columns.join(", "), attribute))
{
2025-04-01 16:45:30 +08:00
RowData data;
data.values.append(attribute.type);
data.values.append(attribute.name);
data.values.append(attribute.dataTypeID);
data.values.append(attribute.dataLength);
data.values.append(attribute.defaultValue);
m_currentPageData.append(data);
}
}
}
catch (const DatabaseException& e)
{
2025-03-25 17:58:48 +08:00
LOG_ERROR("SQL", QString::fromWCharArray(L"获取属性信息失败。modelID:%1, groupID:%2").arg(m_modelAttributeGroup.modelID, m_modelAttributeGroup.groupID));
}
endResetModel();
}
void AttributeTableModel::setPageSize(int size)
{
if(m_paginationInfo.entriesPerPage != size && size > 0)
{
m_paginationInfo.entriesPerPage = size;
refresh();
}
}
int AttributeTableModel::pageSize() const
{
return m_paginationInfo.entriesPerPage;
}
void AttributeTableModel::setCurrentPage(int page)
{
if(m_paginationInfo.currentPage != page && page > 0 && page <= totalPages())
{
m_paginationInfo.currentPage = page;
refresh();
}
}
int AttributeTableModel::currentPage() const
{
return m_paginationInfo.currentPage;
}
int AttributeTableModel::totalPages() const
{
return qCeil(static_cast<qreal>(m_paginationInfo.totalEntries) / m_paginationInfo.entriesPerPage);
}
void AttributeTableModel::previousPage()
{
if(m_paginationInfo.entriesPerPage == 1)
return;
setCurrentPage(m_paginationInfo.entriesPerPage--);
}
void AttributeTableModel::nextPage()
{
if(m_paginationInfo.entriesPerPage == totalPages())
return;
setCurrentPage(m_paginationInfo.entriesPerPage++);
}
void AttributeTableModel::firstPage()
{
if(m_paginationInfo.entriesPerPage == 1)
return;
setCurrentPage(1);
}
void AttributeTableModel::lastPage()
{
if(m_paginationInfo.entriesPerPage == totalPages())
return;
setCurrentPage(totalPages());
}
void AttributeTableModel::updateTotalCount()
{
2025-04-01 16:45:30 +08:00
/*if (m_tableName.isEmpty())
{
LOG_ERROR("SQL", QString::fromWCharArray(L"属性表名称为空,获取属性数量失败"));
return;
2025-04-01 16:45:30 +08:00
}*/
2025-04-01 16:45:30 +08:00
m_paginationInfo.totalEntries = SqlQueryExecutor::instance().getAttributeCount(m_connection, m_modelAttributeGroup.modelID, m_modelAttributeGroup.groupID);
}
QList<AttributeTableModel::RowData> AttributeTableModel::filterRowsByEditState(EditState state)
{
QList<RowData> result;
for(auto it = m_modifiedRows.begin(); it != m_modifiedRows.end(); it++)
{
if(it.value().state == state)
result.append(it.value());
}
return result;
}
2025-04-01 16:45:30 +08:00
/*void AttributeTableModel::setTable(const QString& tableName)
{
if(m_tableName == tableName)
return;
m_tableName = tableName;
m_modifiedRows.clear();
refresh();
2025-04-01 16:45:30 +08:00
}*/
void AttributeTableModel::refresh()
{
if(!m_modifiedRows.isEmpty())
{
emit showMessage(type_question, QString::fromWCharArray(L"提示"),
QString::fromWCharArray(L"当前有编辑或修改数据未提交,是否进行提交?"));
if(g_msgDlgBtn == btn_Yes)
{
}
else
m_modifiedRows.clear();
}
loadPageData();
updateTotalCount();
emit syncDataStatus(m_modifiedRows.isEmpty(), m_paginationInfo);
}
void AttributeTableModel::insertRecord(int row)
{
if(row < 0 || row > rowCount())
return;
RowData newRow;
newRow.state = New;
2025-03-27 21:01:25 +08:00
newRow.values.resize(m_displayField.count());
int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row;
m_modifiedRows[globalRow] = newRow;
beginInsertRows(QModelIndex(), row, row);
m_currentPageData.insert(row, newRow);
endInsertRows();
}
void AttributeTableModel::removeRecord(int row)
{
if(row < 0 || row > rowCount())
return;
emit showMessage(type_question, QString::fromWCharArray(L"提示"),
QString::fromWCharArray(L"确认要删除该记录吗?"));
if(g_msgDlgBtn == btn_No)
return;
int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row;
if(m_currentPageData.at(row).state == New) //新添加未提交的记录,直接删除
{
2025-03-27 21:01:25 +08:00
beginRemoveRows(QModelIndex(), row, row);
m_modifiedRows.remove(globalRow);
2025-03-27 21:01:25 +08:00
m_currentPageData.removeAt(row);
endRemoveRows();
}
else //整行画红线进行标记
{
m_currentPageData[row].state = Deleted;
m_modifiedRows[globalRow] = m_currentPageData[row];
2025-03-27 21:01:25 +08:00
m_modifiedRows[globalRow].state = Deleted;
QModelIndex topLeft = createIndex(row, 0);
QModelIndex bottomRight = createIndex(row, columnCount() - 1);
emit dataChanged(topLeft, bottomRight, {Qt::DisplayRole});
}
}
void AttributeTableModel::submitChanges()
{
QList<RowData> insertRows = filterRowsByEditState(New);
// QList<RowData> updateRows = filterRowsByEditState(Modified);
// QList<RowData> deleteRows = filterRowsByEditState(Deleted);
bool insertResult = false;
if(!insertRows.isEmpty())
{
QList<Attribute> insertAttributes;
for(const RowData& rowData : insertRows)
{
QString type = rowData.values.value(0).toString();
QString name = rowData.values.value(1).toString();
if(type.isEmpty() || name.isEmpty())
continue;
int dataTypeID = rowData.values.value(2).toInt();
int dataLength = -1;
if(rowData.values.value(3).isValid())
dataLength = rowData.values.value(3).toInt();
QString defaultValue = rowData.values.value(4).toString();
insertAttributes.emplace_back(-1, name, type, dataTypeID, dataLength, defaultValue);
}
insertResult = SqlQueryExecutor::instance().batchInserAttributis(m_connection, m_modelAttributeGroup.modelID, m_modelAttributeGroup.groupID, insertAttributes);
}
if( (!insertRows.isEmpty() && insertResult) )
{
m_modifiedRows.clear();
refresh();
}
else
emit showMessage(type_warning, QString::fromWCharArray(L"错误"), QString::fromWCharArray(L"数据提交失败,详情可查看日志文件"));
}
void AttributeTableModel::triggerSyncSignal()
{
emit syncDataStatus(m_modifiedRows.isEmpty(), m_paginationInfo);
}