#ifndef DATALISTMODEL_H
#define DATALISTMODEL_H

#include <QAbstractListModel>
#include <QVector>
#include <QVariantMap>

#include <QMutex>
#include <QMutexLocker>

class DataListModel : public QAbstractListModel
{
    Q_OBJECT
public:
    explicit DataListModel(QObject *parent = nullptr);

    // 基本模型接口
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;

    // 类似 QML ListModel 的接口
    Q_INVOKABLE void append(const QVariantMap &entry);
    Q_INVOKABLE void insert(int index, const QVariantMap &entry);
    Q_INVOKABLE void remove(int index);
    Q_INVOKABLE void clear();
    Q_INVOKABLE void setProperty(int index, const QString &property, const QVariant &value);
    Q_INVOKABLE QVariantMap get(int index) const;
    Q_INVOKABLE int count() const;

    // 动态属性支持
    Q_INVOKABLE void addRole(const QString &roleName);

private:
    QVector<QVariantMap> m_data;
    QHash<int, QByteArray> m_roles;
    int m_nextRole;
};

#endif // DATALISTMODEL_H
