81 lines
2.3 KiB
C
81 lines
2.3 KiB
C
|
|
#ifndef ATTRIBUTETABLEMODEL_H
|
|||
|
|
#define ATTRIBUTETABLEMODEL_H
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 用来处理attribute数据的Model类
|
|||
|
|
*
|
|||
|
|
* 基本功能包括:
|
|||
|
|
* 1、可以自定义显示数据表中的哪些列
|
|||
|
|
* 2、可以分页显示并自定义每页展示数量
|
|||
|
|
* 3、编辑的数据可以突出展示(如加粗、改色)
|
|||
|
|
* 4、最前方加入一个用于展示行号的列(行号从1开始)
|
|||
|
|
* 5、点击行号列可以实现选中标识(通过icon)
|
|||
|
|
* 6、哪一行被编辑,改行的行号指示可以加*进行标识
|
|||
|
|
*
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
#include <QAbstractTableModel>
|
|||
|
|
#include <QSqlRecord>
|
|||
|
|
|
|||
|
|
class AttributeTableModel : public QAbstractTableModel
|
|||
|
|
{
|
|||
|
|
Q_OBJECT
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
enum EditState
|
|||
|
|
{
|
|||
|
|
Clean = 0,
|
|||
|
|
Modified,
|
|||
|
|
New,
|
|||
|
|
Deleted
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
explicit AttributeTableModel(QObject* parent = nullptr
|
|||
|
|
, const QString& connection = ""
|
|||
|
|
, const QString& modelID = ""
|
|||
|
|
, const QString& groupID = ""
|
|||
|
|
, 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_modelID;
|
|||
|
|
QString m_groupID;
|
|||
|
|
QString m_tableName;
|
|||
|
|
|
|||
|
|
int m_pageSize;
|
|||
|
|
int m_currentPage;
|
|||
|
|
int m_totalCount;
|
|||
|
|
QStringList m_visibleColumns;
|
|||
|
|
QList<RowData> m_currentPageData;
|
|||
|
|
QHash<int, RowData> m_modifiedRows; //key:global row number
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
#endif //ATTRIBUTETABLEMODEL_H
|