123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- #include "CMind.h"
- #include <QDebug>
- 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<CNodeData> CMind::nodeList() const
- {
- return m_nodeList;
- }
- void CMind::setNodeList(QList<CNodeData> 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<int> CMind::numberList() const
- {
- QList<int> 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<CNodeData> CMind::nodesInLevel(int i) const
- {
- QList<CNodeData> l;
- for (CNodeData n : m_nodeList) {
- if (levelOfNode(n) == i) {
- l.append(n);
- }
- }
- return l;
- }
- QList<CNodeData> CMind::subNodes(CNodeData n) const
- {
- QList<CNodeData> 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<CNodeData> 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<CNodeData> CMind::leavesOfNode(CNodeData n) const
- {
- QList<CNodeData> list;
- QList<CNodeData> subList = subNodes(n);
- if (subList.count() > 0) {
- for (CNodeData sub : subList) {
- list.append(leavesOfNode(sub));
- }
- } else {
- list.append(n);
- }
- return list;
- }
- QList<CNodeData> CMind::leaves() const
- {
- return leavesOfNode(root());
- }
- void CMind::getSeqNodes(QList<QList<SeqNode>> &seqNodes) const
- {
- for (int i = 1; i < this->levels(); ++i) {
- seqNodes << QList<SeqNode>();
- }
- QMap<QString, QStringList> all;
- for (int i = 1; i < this->levels(); i++) {
- QList<CNodeData> nodes = this->nodesInLevel(i);
- for (const CNodeData &node : nodes) {
- QList<CNodeData> 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;
- }
|