Files
HPPA/HPPA/LayerTreeNode.cpp
tangchao0503 631216dc66 初步实现单独的图层管理器。
注意:没有和mapcavas通讯。
2026-02-05 15:32:34 +08:00

111 lines
2.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "LayerTreeNode.h"
#include <QtGlobal>
LayerTreeNode::LayerTreeNode(const QString& name, QObject* parent)
: QObject(parent), m_name(name)
{
}
LayerTreeNode::~LayerTreeNode()
{
qDeleteAll(m_children);
m_children.clear();
}
QString LayerTreeNode::name() const
{
return m_name;
}
void LayerTreeNode::setName(const QString& name)
{
m_name = name;
}
QIcon LayerTreeNode::icon() const
{
return m_icon;
}
void LayerTreeNode::setIcon(const QIcon& icon)
{
m_icon = icon;
}
Qt::CheckState LayerTreeNode::visible() const
{
return m_visible;
}
void LayerTreeNode::setVisible(Qt::CheckState s)
{
m_visible = s;
}
LayerTreeNode* LayerTreeNode::parentNode() const
{
return m_parentNode;
}
void LayerTreeNode::setParentNode(LayerTreeNode* p)
{
m_parentNode = p;
// 让 QObject 的 parent 也跟随(便于 Qt 对象层次管理,且不会影响我们手动 delete children
if (p) this->setParent(p);
else this->setParent(nullptr);
}
int LayerTreeNode::rowInParent() const
{
if (!m_parentNode) return 0;
const auto& siblings = m_parentNode->m_children;
for (int i = 0; i < siblings.size(); ++i)
{
if (siblings[i] == this) return i;
}
return 0;
}
int LayerTreeNode::childCount() const
{
return m_children.size();
}
LayerTreeNode* LayerTreeNode::childAt(int row) const
{
if (row < 0 || row >= m_children.size()) return nullptr;
return m_children[row];
}
const QVector<LayerTreeNode*>& LayerTreeNode::children() const
{
return m_children;
}
void LayerTreeNode::appendChild(LayerTreeNode* child)
{
insertChild(m_children.size(), child);
}
void LayerTreeNode::insertChild(int row, LayerTreeNode* child)
{
if (!child) return;
if (row < 0 || row > m_children.size())
row = m_children.size();
child->setParentNode(this);
m_children.insert(row, child);
}
LayerTreeNode* LayerTreeNode::takeChild(int row)
{
if (row < 0 || row >= m_children.size()) return nullptr;
LayerTreeNode* taken = m_children.takeAt(row);
if (taken) taken->setParentNode(nullptr);
return taken;
}