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-26 15:57:08 +08:00
|
|
|
|
#include "messageDialog.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();
|
|
|
|
|
|
|
2025-03-27 14:47:51 +08:00
|
|
|
|
QModelIndex index(int row, int column, const QModelIndex& parent) const override;
|
2025-03-24 18:13:06 +08:00
|
|
|
|
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;
|
2025-03-26 15:57:08 +08:00
|
|
|
|
void previousPage();
|
|
|
|
|
|
void nextPage();
|
|
|
|
|
|
void firstPage();
|
|
|
|
|
|
void lastPage();
|
|
|
|
|
|
|
|
|
|
|
|
//数据操作
|
2025-04-01 16:45:30 +08:00
|
|
|
|
//void setTable(const QString&);
|
2025-03-26 15:57:08 +08:00
|
|
|
|
void refresh();
|
2025-03-27 14:47:51 +08:00
|
|
|
|
void insertRecord(int);
|
|
|
|
|
|
void removeRecord(int);
|
2025-04-01 14:37:41 +08:00
|
|
|
|
void submitChanges(); //提交更改(增、删、改)
|
2025-03-24 18:13:06 +08:00
|
|
|
|
|
|
|
|
|
|
//展示列控制
|
2025-03-27 21:01:25 +08:00
|
|
|
|
//void setVisibleColumns(const QStringList& columns);
|
2025-03-24 18:13:06 +08:00
|
|
|
|
|
2025-03-25 20:53:15 +08:00
|
|
|
|
void triggerSyncSignal();
|
|
|
|
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
|
void syncDataStatus(bool, const PaginationInfo&);
|
2025-03-26 15:57:08 +08:00
|
|
|
|
void showMessage(MessageDialogType,const QString&,const QString&);
|
2025-03-25 20:53:15 +08:00
|
|
|
|
|
2025-03-24 18:13:06 +08:00
|
|
|
|
private:
|
2025-03-27 21:01:25 +08:00
|
|
|
|
struct FieldInfo //字段信息
|
|
|
|
|
|
{
|
|
|
|
|
|
QString originalName; //数据库中的字段命名
|
|
|
|
|
|
QString displayName; //对外展示的名称
|
|
|
|
|
|
QString dataType = "unknown type"; //数据类型
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-03-24 18:13:06 +08:00
|
|
|
|
struct RowData
|
|
|
|
|
|
{
|
2025-03-26 15:57:08 +08:00
|
|
|
|
//QSqlRecord record;
|
|
|
|
|
|
QVector<QVariant> values;
|
2025-03-24 18:13:06 +08:00
|
|
|
|
EditState state = Clean;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-03-31 16:01:49 +08:00
|
|
|
|
struct DataType //数据类型
|
|
|
|
|
|
{
|
|
|
|
|
|
int id;
|
|
|
|
|
|
QString type;
|
|
|
|
|
|
QString db; //为哪个数据库的可用类型
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-03-27 21:01:25 +08:00
|
|
|
|
void iniDisplayField();
|
2025-03-31 16:01:49 +08:00
|
|
|
|
void getDataTypes();
|
2025-03-24 18:13:06 +08:00
|
|
|
|
void loadPageData(); // 加载当前页数据
|
|
|
|
|
|
void updateTotalCount(); // 更新总记录数
|
2025-04-01 14:37:41 +08:00
|
|
|
|
QList<RowData> filterRowsByEditState(EditState);
|
2025-03-24 18:13:06 +08:00
|
|
|
|
|
|
|
|
|
|
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-27 21:01:25 +08:00
|
|
|
|
QList<FieldInfo> m_displayField;
|
2025-03-24 18:13:06 +08:00
|
|
|
|
QList<RowData> m_currentPageData;
|
|
|
|
|
|
QHash<int, RowData> m_modifiedRows; //key:global row number
|
2025-03-31 16:01:49 +08:00
|
|
|
|
QMap<int, DataType> m_dataTypes;
|
2025-03-24 18:13:06 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif //ATTRIBUTETABLEMODEL_H
|