2025-03-24 18:13:06 +08:00
|
|
|
|
#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)
|
2025-03-24 18:13:06 +08:00
|
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
|
|
, m_connection(connection)
|
|
|
|
|
|
, m_tableName(tableName)
|
2025-03-25 17:58:48 +08:00
|
|
|
|
, m_modelAttributeGroup(modelAttributeGroup)
|
2025-03-24 18:13:06 +08:00
|
|
|
|
{
|
2025-03-25 17:58:48 +08:00
|
|
|
|
m_paginationInfo.entriesPerPage = 100;
|
|
|
|
|
|
m_paginationInfo.currentPage = 1;
|
|
|
|
|
|
m_paginationInfo.totalEntries = 0;
|
2025-03-24 18:13:06 +08:00
|
|
|
|
|
|
|
|
|
|
m_visibleColumns << "attribute" << "attribute_name" << "data_type_id" << "length_precision" << "default_value";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AttributeTableModel::~AttributeTableModel()
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2025-03-24 18:13:06 +08:00
|
|
|
|
|
|
|
|
|
|
if(index.column() == 0) //第一列显示行号
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (role)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
|
|
{
|
|
|
|
|
|
QString prefix;
|
|
|
|
|
|
if(m_modifiedRows.contains(globalRow))
|
|
|
|
|
|
{
|
|
|
|
|
|
switch(m_modifiedRows[globalRow].state)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Modified:
|
|
|
|
|
|
prefix = "*";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case New:
|
|
|
|
|
|
prefix = "+";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Deleted:
|
|
|
|
|
|
prefix = "-";
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return prefix + QString::number(globalRow + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
int row = index.row();
|
2025-03-25 17:58:48 +08:00
|
|
|
|
int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row;
|
2025-03-24 18:13:06 +08:00
|
|
|
|
int dataCol = index.column() - 1; //第一列显示了行号
|
|
|
|
|
|
//记录修改
|
|
|
|
|
|
RowData modifiedRow = m_currentPageData[row];
|
|
|
|
|
|
modifiedRow.record.setValue(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
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_visibleColumns.value(section);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return QAbstractTableModel::headerData(section, orientation, role);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int AttributeTableModel::rowCount(const QModelIndex& parent) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_currentPageData.count();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int AttributeTableModel::columnCount(const QModelIndex &) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_visibleColumns.isEmpty() ? 0 : m_visibleColumns.count() + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Qt::ItemFlags AttributeTableModel::flags(const QModelIndex &index) const
|
|
|
|
|
|
{
|
|
|
|
|
|
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
|
|
|
|
|
|
if (index.column() != 0) //行号列不能编辑
|
|
|
|
|
|
flags = flags | Qt::ItemIsEditable;
|
|
|
|
|
|
return flags;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AttributeTableModel::loadPageData()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_tableName.isEmpty())
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_ERROR("DB", QString("Attribute table name is empty, load data failed"));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
|
|
|
|
QString strSQL = QString("SELECT %1 FROM %2 LIMIT %3 OFFSET %4")
|
|
|
|
|
|
.arg(m_visibleColumns.join(", "))
|
|
|
|
|
|
.arg(m_tableName)
|
2025-03-25 17:58:48 +08:00
|
|
|
|
.arg(m_paginationInfo.entriesPerPage)
|
|
|
|
|
|
.arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage);
|
2025-03-24 18:13:06 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL);
|
|
|
|
|
|
while(query.next())
|
|
|
|
|
|
{
|
|
|
|
|
|
RowData data;
|
|
|
|
|
|
data.record = query.record();
|
|
|
|
|
|
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));
|
2025-03-24 18:13:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AttributeTableModel::updateTotalCount()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_tableName.isEmpty())
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_ERROR("SQL", QString::fromWCharArray(L"属性表名称为空,获取属性数量失败"));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-25 17:58:48 +08:00
|
|
|
|
m_paginationInfo.totalEntries = SqlQueryExecutor::instance().getAttributeCount(m_connection, m_tableName);
|
2025-03-24 18:13:06 +08:00
|
|
|
|
}
|