92 lines
2.5 KiB
C++
92 lines
2.5 KiB
C++
#pragma once
|
||
|
||
#include <QObject>
|
||
#include <QVector>
|
||
#include <QIcon>
|
||
#include <QString>
|
||
|
||
/**
|
||
* LayerTreeNode:节点基类(抽象)
|
||
* - 仅包含通用属性:名称/图标/可见性/父子关系
|
||
* - Group / Layer 节点通过继承实现
|
||
* - 提供插入/删除节点的信号通知
|
||
*
|
||
* 说明:
|
||
* - 这里同时维护"树父指针"(m_parentNode)与 QObject parent(可选)
|
||
* - children 由节点自己持有并负责释放(析构时 delete children)
|
||
*/
|
||
class LayerTreeNode : public QObject
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
enum class Type { Group, Layer };
|
||
|
||
explicit LayerTreeNode(const QString& name,
|
||
QObject* parent = nullptr);
|
||
~LayerTreeNode() override;
|
||
|
||
LayerTreeNode(const LayerTreeNode&) = delete;
|
||
LayerTreeNode& operator=(const LayerTreeNode&) = delete;
|
||
|
||
virtual Type type() const = 0;
|
||
|
||
// ---- properties ----
|
||
QString name() const;
|
||
void setName(const QString& name);
|
||
|
||
QIcon icon() const;
|
||
void setIcon(const QIcon& icon);
|
||
|
||
Qt::CheckState visible() const;
|
||
void setVisible(Qt::CheckState s);
|
||
|
||
// ---- tree relations ----
|
||
LayerTreeNode* parentNode() const;
|
||
int rowInParent() const;
|
||
|
||
int childCount() const;
|
||
LayerTreeNode* childAt(int row) const;
|
||
const QVector<LayerTreeNode*>& children() const;
|
||
|
||
// ---- structure mutation (used by LayerTree / Model) ----
|
||
void appendChild(LayerTreeNode* child);
|
||
void insertChild(int row, LayerTreeNode* child);
|
||
|
||
// 基于 QgsLayerTreeNode::removeChildrenPrivate 改进
|
||
// from: 起始索引, count: 移除数量, destroy: true 则 delete 被移除节点
|
||
void removeChild(int from, int count, bool destroy = true);
|
||
|
||
// ---- static type helpers ----
|
||
static inline bool isLayer(LayerTreeNode* node)
|
||
{
|
||
return node && node->type() == LayerTreeNode::Type::Layer;
|
||
}
|
||
|
||
static inline bool isGroup(LayerTreeNode* node)
|
||
{
|
||
return node && node->type() == LayerTreeNode::Type::Group;
|
||
}
|
||
|
||
signals:
|
||
// 在插入子节点之前/之后发出
|
||
void willAddChildren(LayerTreeNode* node, int indexFrom, int indexTo);
|
||
void addedChildren(LayerTreeNode* node, int indexFrom, int indexTo);
|
||
|
||
// 在移除子节点之前/之后发出
|
||
void willRemoveChildren(LayerTreeNode* node, int indexFrom, int indexTo);
|
||
void removedChildren(LayerTreeNode* node, int indexFrom, int indexTo);
|
||
|
||
void nameChanged(LayerTreeNode* node, const QString& name);
|
||
|
||
protected:
|
||
void setParentNode(LayerTreeNode* p);
|
||
|
||
private:
|
||
QString m_name;
|
||
QIcon m_icon;
|
||
Qt::CheckState m_visible = Qt::Checked;
|
||
|
||
LayerTreeNode* m_parentNode = nullptr;
|
||
QVector<LayerTreeNode*> m_children;
|
||
};
|