2025-03-24 18:13:06 +08:00
|
|
|
|
#ifndef ATTRIBUTETABLEMODEL_H
|
|
|
|
|
|
#define ATTRIBUTETABLEMODEL_H
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 用来处理attribute数据的Model类
|
|
|
|
|
|
*
|
|
|
|
|
|
* 基本功能包括:
|
|
|
|
|
|
* 1、可以自定义显示数据表中的哪些列
|
|
|
|
|
|
* 2、可以分页显示并自定义每页展示数量
|
|
|
|
|
|
* 3、编辑的数据可以突出展示(如加粗、改色)
|
|
|
|
|
|
* 4、最前方加入一个用于展示行号的列(行号从1开始)
|
|
|
|
|
|
* 5、点击行号列可以实现选中标识(通过icon)
|
|
|
|
|
|
* 6、哪一行被编辑,改行的行号指示可以加*进行标识
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <QAbstractTableModel>
|
|
|
|
|
|
#include <QSqlRecord>
|
2025-03-25 17:58:48 +08:00
|
|
|
|
#include "global.h"
|
2025-03-24 18:13:06 +08:00
|
|
|
|
|
|
|
|
|
|
class AttributeTableModel : public QAbstractTableModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
enum EditState
|
|
|
|
|
|
{
|
|
|
|
|
|
Clean = 0,
|
|
|
|
|
|
Modified,
|
|
|
|
|
|
New,
|
|
|
|
|
|
Deleted
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-03-25 17:58:48 +08:00
|
|
|
|
explicit AttributeTableModel(const ModelAttributeGroup& modelAttributeGroup
|
|
|
|
|
|
, QObject* parent = nullptr
|
2025-03-24 18:13:06 +08:00
|
|
|
|
, const QString& connection = ""
|
|
|
|
|
|
, const QString& tableName = "basic.attribute");
|
|
|
|
|
|
~AttributeTableModel();
|
|
|
|
|
|
|
|
|
|
|
|
QVariant data(const QModelIndex& index, int role) const override;
|
|
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
|
|
|
|
|
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
|
|
|
|
|
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
|
|
|
|
|
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
|
|
|
|
|
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
|
|
|
|
|
|
|
|
|
|
|
//分页控制
|
|
|
|
|
|
void setPageSize(int);
|
|
|
|
|
|
int pageSize() const;
|
|
|
|
|
|
void setCurrentPage(int);
|
|
|
|
|
|
int currentPage() const;
|
|
|
|
|
|
int totalPages() const;
|
|
|
|
|
|
|
|
|
|
|
|
//展示列控制
|
|
|
|
|
|
void setVisibleColumns(const QStringList& columns);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
struct RowData
|
|
|
|
|
|
{
|
|
|
|
|
|
QSqlRecord record;
|
|
|
|
|
|
EditState state = Clean;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void loadPageData(); // 加载当前页数据
|
|
|
|
|
|
void updateTotalCount(); // 更新总记录数
|
|
|
|
|
|
|
|
|
|
|
|
QString m_connection;
|
|
|
|
|
|
QString m_tableName;
|
2025-03-25 17:58:48 +08:00
|
|
|
|
ModelAttributeGroup m_modelAttributeGroup;
|
2025-03-24 18:13:06 +08:00
|
|
|
|
|
2025-03-25 17:58:48 +08:00
|
|
|
|
PaginationInfo m_paginationInfo;
|
2025-03-24 18:13:06 +08:00
|
|
|
|
QStringList m_visibleColumns;
|
|
|
|
|
|
QList<RowData> m_currentPageData;
|
|
|
|
|
|
QHash<int, RowData> m_modifiedRows; //key:global row number
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif //ATTRIBUTETABLEMODEL_H
|