#include "CMind.h" #include CNodeData::CNodeData(int projId, int indexType, int number, int pNumber, int id) : id(id), projectId(projId), indexType(indexType), number(number), pNumber(pNumber) { } bool CNodeData::isValid() const { return projectId >= 0 && indexType > 0 && number >= 0; } bool CNodeData::isNull() const { return id == -1 && projectId == -1 && indexType == 0 && number == -1 && pNumber == -1; } bool CNodeData::isCached() const { return id >= 0; } bool CNodeData::hasParent() const { return pNumber >= 0; } bool CNodeData::isSameMind(CNodeData n) const { return projectId == n.projectId && indexType == n.indexType; } CMind::CMind(QObject *parent) : QObject(parent) { } QList CMind::nodeList() const { return m_nodeList; } void CMind::setNodeList(QList list) { m_nodeList.clear(); for (CNodeData n : list) { addNode(n); } } bool CMind::isNodeValid(CNodeData n) const { CNodeData root = node(minNumber()); if (root.isValid()) { bool v = n.isValid() && n.isSameMind(root); return v; } return n.isValid(); } CNodeData CMind::node(int number) const { int i = numberList().indexOf(number); if (i < 0 || i >= m_nodeList.count()) { return CNodeData(); } return m_nodeList[i]; } bool CMind::containsNode(int number) const { return numberList().contains(number); } bool CMind::canAddNode(CNodeData n) { // 节点无效, 不能添加 if (isNodeValid(n) == false) { return false; } // 指明了父节点, 但父节点无效, 不能添加 if (n.hasParent() && containsNode(n.pNumber) == false) { return false; } // 节点编号重复, 不能添加 if (numberList().contains(n.number)) { return false; } return true; } void CMind::addNode(CNodeData n) { m_nodeList.append(n); } void CMind::removeNode(int number) { // 删除节点 int i = numberList().indexOf(number); if (i >= 0 && i < m_nodeList.count()) { emit sigRemoveNode(m_nodeList[i].id); m_nodeList.removeAt(i); } // 删除子节点 for (int i = m_nodeList.count() - 1; i >= 0; i--) { CNodeData n = m_nodeList[i]; if (hasAncestor(n.number, number)) { m_nodeList.removeAt(i); emit sigRemoveNode(n.id); } } } void CMind::updateNode(CNodeData n) { for (int i = 0; i < m_nodeList.count(); i++) { if (n.id == m_nodeList[i].id) { m_nodeList.replace(i, n); break; } } } CNodeData CMind::root() const { return node(minNumber()); } bool CMind::hasAncestor(int childNumber, int ancestorNumber) const { CNodeData c = node(childNumber); if (c.isValid()) { if (c.hasParent() && c.pNumber == ancestorNumber) { return true; } return hasAncestor(c.pNumber, ancestorNumber); } return false; } bool CMind::isInvalidated(int number) const { CNodeData c = node(number); if (c.isEffective) { return true; } if (c.hasParent()) { return isInvalidated(c.pNumber); } return false; } QList CMind::numberList() const { QList l; for (int i = 0; i < m_nodeList.count(); i++) { l.append(m_nodeList[i].number); } return l; } int CMind::maxNumber() const { int n = -1; if (m_nodeList.count() > 0) { n = m_nodeList.first().number; } for (int i = 0; i < m_nodeList.count(); i++) { n = std::max(n, m_nodeList[i].number); } return n; } int CMind::minNumber() const { int n = -1; if (m_nodeList.count() > 0) { n = m_nodeList.first().number; } for (int i = 0; i < m_nodeList.count(); i++) { n = std::min(n, m_nodeList[i].number); } return n; } void CMind::clear() { m_nodeList.clear(); } int CMind::levelOfNode(CNodeData n) const { int l = 1; if (n.hasParent()) { CNodeData p = node(n.pNumber); l = levelOfNode(p) + 1; } return l; } int CMind::levels() const { int l = 0; for (CNodeData n : m_nodeList) { l = std::max(l, levelOfNode(n)); } return l; } QList CMind::nodesInLevel(int i) const { QList l; for (CNodeData n : m_nodeList) { if (levelOfNode(n) == i) { l.append(n); } } return l; } QList CMind::subNodes(CNodeData n) const { QList l; for (CNodeData t : m_nodeList) { if (t.pNumber == n.number) { l.append(t); } } return l; } int CMind::leavesCountOfNode(CNodeData n) const { int d = 0; QList list = subNodes(n); if (list.count() > 0) { for (CNodeData sub : list) { d += leavesCountOfNode(sub); } } else { d++; } return d; } int CMind::leavesCount() const { return leavesCountOfNode(root()); } QList CMind::leavesOfNode(CNodeData n) const { QList list; QList subList = subNodes(n); if (subList.count() > 0) { for (CNodeData sub : subList) { list.append(leavesOfNode(sub)); } } else { list.append(n); } return list; } QList CMind::leaves() const { return leavesOfNode(root()); } void CMind::getSeqNodes(QList> &seqNodes) const { for (int i = 1; i < this->levels(); ++i) { seqNodes << QList(); } QMap all; for (int i = 1; i < this->levels(); i++) { QList nodes = this->nodesInLevel(i); for (const CNodeData &node : nodes) { QList subNodes = this->subNodes(node); SeqNode snode; snode.name = node.name.trimmed(); for (const CNodeData &sub : subNodes) { snode.childs.append(sub.name.trimmed()); } seqNodes[i - 1].append(snode); } } } const CNode *CMind::nodeObject() const { CNode *node = nullptr; return node; }