134 lines
4.6 KiB
C++
134 lines
4.6 KiB
C++
#ifndef ATTRIBUTETABLEMODEL_H
|
||
#define ATTRIBUTETABLEMODEL_H
|
||
|
||
/**
|
||
* @brief 用来处理attribute数据的Model类
|
||
*
|
||
* 基本功能包括:
|
||
* 1、可以自定义显示数据表中的哪些列
|
||
* 2、可以分页显示并自定义每页展示数量
|
||
* 3、编辑的数据可以突出展示(如加粗、改色)
|
||
* 4、最前方加入一个用于展示行号的列(行号从1开始)
|
||
* 5、点击行号列可以实现选中标识(通过icon)
|
||
* 6、哪一行被编辑,改行的行号指示可以加*进行标识
|
||
*
|
||
*/
|
||
|
||
#include <QAbstractTableModel>
|
||
#include <QSqlRecord>
|
||
#include "global.h"
|
||
#include "messageDialog.h"
|
||
|
||
class AttributeTableModel : public QAbstractTableModel
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
enum EditStateFlag
|
||
{
|
||
Clean = 0, // 二进制: 0b0000
|
||
Modified = 1 << 0, // 二进制: 0b0001
|
||
New = 1 << 1, // 二进制: 0b0010
|
||
Deleted = 1 << 2 // 二进制: 0b0100
|
||
};
|
||
//创建 QFlags 包装类型
|
||
Q_DECLARE_FLAGS(EditState, EditStateFlag)
|
||
|
||
struct DataType //数据类型
|
||
{
|
||
int id;
|
||
QString type;
|
||
QString db; //为哪个数据库的可用类型
|
||
};
|
||
|
||
explicit AttributeTableModel(const ModelAttributeGroup& modelAttributeGroup
|
||
, QObject* parent = nullptr
|
||
, const QString& connection = ""
|
||
, const QString& tableName = "basic.attribute");
|
||
~AttributeTableModel();
|
||
|
||
QModelIndex index(int row, int column, const QModelIndex& parent) const override;
|
||
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;
|
||
|
||
//ModelAttributeGroup getModelAttributeGroup() {return m_modelAttributeGroup;}
|
||
void setModelAttributeGroup(const ModelAttributeGroup&);
|
||
bool attributeTypeExistsInCurrentGroup(int, const QString&); //除数据库外,还有从内存存储的数据(当前页m_currentPageData)中进行查找判断,因为可能有编辑完但未提交至数据空的信息
|
||
void updateRowThroughExisitingAttribute(int, int);
|
||
|
||
//分页控制
|
||
void setPageSize(int);
|
||
int pageSize() const;
|
||
bool setCurrentPage(int);
|
||
int currentPage() const;
|
||
int totalPages() const;
|
||
void previousPage();
|
||
void nextPage();
|
||
void firstPage();
|
||
void lastPage();
|
||
|
||
//数据操作
|
||
//void setTable(const QString&);
|
||
void refresh();
|
||
void forceRefresh(); //强制刷新(不会出现询问提示,数据同步时会用到)
|
||
void insertRecord(int);
|
||
void removeRecord(int);
|
||
void submitChanges(); //提交更改(增、删、改)
|
||
void cancleChanges(); //取消修改
|
||
|
||
//展示列控制
|
||
//void setVisibleColumns(const QStringList& columns);
|
||
|
||
//others
|
||
QMap<int, DataType> getDataTypes() {return m_dataTypes;}
|
||
void triggerSyncSignal();
|
||
bool dataHasbeenModified() {return !m_modifiedRows.isEmpty();};
|
||
|
||
signals:
|
||
void syncDataStatus(bool, const PaginationInfo&);
|
||
void showMessage(MessageDialogType,const QString&,const QString&);
|
||
|
||
private:
|
||
struct FieldInfo //字段信息
|
||
{
|
||
QString originalName; //数据库中的字段命名
|
||
QString displayName; //对外展示的名称
|
||
QString dataType = "unknown type"; //数据类型
|
||
};
|
||
|
||
struct RowData
|
||
{
|
||
//QSqlRecord record;
|
||
QVector<QVariant> values;
|
||
QHash<int, bool> cellModified; //记录单元格是否被修改
|
||
EditState state = EditState(Clean);
|
||
};
|
||
|
||
void iniDisplayField();
|
||
void getDataTypesFromDB();
|
||
void loadPageData(); // 加载当前页数据
|
||
void updateTotalCount(); // 更新总记录数
|
||
QList<RowData> filterRowsByEditState(EditStateFlag);
|
||
int getDataTypeID(const QString&);
|
||
|
||
QString m_connection;
|
||
QString m_tableName;
|
||
ModelAttributeGroup m_modelAttributeGroup;
|
||
|
||
PaginationInfo m_paginationInfo;
|
||
QList<FieldInfo> m_displayField;
|
||
QList<RowData> m_currentPageData;
|
||
QHash<int, RowData> m_modifiedRows; //key:global row number
|
||
QMap<int, DataType> m_dataTypes;
|
||
QHash<QString, int> m_reversalDataTypes; //dataType的反转,用来通过名称快速查找对应id
|
||
};
|
||
|
||
//生成运算符重载
|
||
Q_DECLARE_OPERATORS_FOR_FLAGS(AttributeTableModel::EditState)
|
||
|
||
#endif //ATTRIBUTETABLEMODEL_H
|