48 lines
1.6 KiB
C++
48 lines
1.6 KiB
C++
#pragma once
|
||
#include <QCoreApplication>
|
||
#include <QAbstractItemModel>
|
||
#include "LayerTree.h"
|
||
|
||
/**
|
||
* LayerTreeModel:Qt 适配层(不再管理树)
|
||
* - 1 列:名称(带图标)+ checkbox
|
||
* - 勾选可见性(可选级联勾选)
|
||
*/
|
||
class LayerTreeModel : public QAbstractItemModel
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
explicit LayerTreeModel(LayerTree* tree,
|
||
QObject* parent = nullptr,
|
||
bool cascadeCheck = true);
|
||
~LayerTreeModel() override = default;
|
||
|
||
// QAbstractItemModel 必须接口
|
||
QModelIndex index(int row, int column,
|
||
const QModelIndex& parent = QModelIndex()) const override;
|
||
QModelIndex parent(const QModelIndex& child) const override;
|
||
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
||
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
||
|
||
QVariant data(const QModelIndex& index, int role) const override;
|
||
bool setData(const QModelIndex& index, const QVariant& value, int role) override;
|
||
Qt::ItemFlags flags(const QModelIndex& index) const override;
|
||
|
||
// 对外 API:构建树(内部会正确调用 begin/endInsertRows)
|
||
LayerTreeNode* root() const;
|
||
|
||
LayerTreeNode* addGroup(LayerTreeNode* parent, const QString& name, const QIcon& icon = QIcon());
|
||
LayerTreeNode* addLayer(LayerTreeNode* parent, const QString& name, const QIcon& icon = QIcon());
|
||
|
||
void setCascadeCheckEnabled(bool enabled);
|
||
bool cascadeCheckEnabled() const;
|
||
|
||
private:
|
||
LayerTree* m_tree = nullptr; // not owned
|
||
bool m_cascadeCheck = true;
|
||
|
||
private:
|
||
LayerTreeNode* nodeFromIndex(const QModelIndex& index) const;
|
||
QModelIndex indexFromNode(LayerTreeNode* n) const;
|
||
};
|