File indexing completed on 2023-11-26 07:22:30
0001 /* 0002 SPDX-FileCopyrightText: 2003-2006 Cies Breijs <cies AT kde DOT nl> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "treenode.h" 0008 0009 #include <QDebug> 0010 0011 0012 TreeNode::~TreeNode() 0013 { 0014 if (childList != nullptr) { 0015 // this should work here: 0016 qDeleteAll(*childList); 0017 childList->clear(); 0018 0019 // but this is nicer (and probably less optimal): 0020 // while (!list.isEmpty()) delete list.takeFirst(); 0021 } 0022 delete _value; 0023 delete _token; 0024 } 0025 0026 0027 void TreeNode::init(TreeNode* parent, Token* token) 0028 { 0029 setParent(parent); 0030 setToken(token); 0031 childList = nullptr; 0032 currentChildIndex = -1; 0033 _value = nullptr; 0034 } 0035 0036 0037 void TreeNode::appendChild(TreeNode* newChild) 0038 { 0039 if (childList == nullptr) childList = new ChildList(); 0040 newChild->setParent(this); 0041 childList->append(newChild); 0042 // // // QString out = QString("TreeNode::appendChild(): \"%5\" [%6] @ (%1,%2)-(%3,%4) to parent '%7'") 0043 // // // .arg(newChild->token()->startRow()) 0044 // // // .arg(newChild->token()->startCol()) 0045 // // // .arg(newChild->token()->endRow()) 0046 // // // .arg(newChild->token()->endCol()) 0047 // // // .arg(newChild->token()->look()) 0048 // // // .arg(newChild->token()->type()) 0049 // // // .arg(_token->look()); 0050 // //qDebug() << "TreeNode::appendChild():" << newChild->token()->look() << " on line" << newChild->token()->startRow() << "to parent" << _token->look(); 0051 } 0052 0053 0054 0055 TreeNode* TreeNode::child(int i) 0056 { 0057 if (childList == nullptr) return nullptr; 0058 if (0 <= i && i < childList->size()) return childList->at(i); 0059 return nullptr; 0060 } 0061 0062 0063 TreeNode* TreeNode::firstChild() 0064 { 0065 if (childList == nullptr || childList->isEmpty()) return nullptr; 0066 currentChildIndex = 0; 0067 return childList->first(); 0068 } 0069 0070 TreeNode* TreeNode::nextChild() 0071 { 0072 if (childList == nullptr) return nullptr; 0073 currentChildIndex++; 0074 return child(currentChildIndex); 0075 } 0076 0077 0078 int TreeNode::findChildIndex(TreeNode* child) 0079 { 0080 return childList->indexOf(child); 0081 } 0082 0083 0084 TreeNode* TreeNode::nextSibling() 0085 { 0086 if (_parent == nullptr) return nullptr; 0087 return _parent->child(_parent->findChildIndex(this)+1); 0088 } 0089 0090 0091 QString TreeNode::toString() 0092 { 0093 QString str = QLatin1String(""); 0094 showTree(str); 0095 return str; 0096 } 0097 0098 // recursively walk through tree and show node names with indentation 0099 void TreeNode::showTree(QString& str, int indent) 0100 { 0101 if (childList == nullptr) return; 0102 indent++; 0103 TreeNode* node; 0104 for (int i = 0; i < childList->size(); i++) { 0105 node = childList->at(i); 0106 node->show(str, indent); 0107 node->showTree(str, indent); 0108 } 0109 } 0110 0111 void TreeNode::show(QString& str, int indent) const 0112 { 0113 QString indentString = QLatin1String(""); 0114 for (int i = 0; i < indent; i++) indentString += QLatin1String("> "); 0115 str += indentString + _token->look() + QStringLiteral(" @ (%1, %2)-(%3, %4)\n") 0116 .arg(_token->startRow()) 0117 .arg(_token->startCol()) 0118 .arg(_token->endRow()) 0119 .arg(_token->endCol()); 0120 } 0121