CNode.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "CNode.h"
  2. CNode::CNode(QObject *parent) : QObject(parent) { }
  3. const CNode *CNode::pNode() const
  4. {
  5. return dynamic_cast<CNode *>(QObject::parent());
  6. }
  7. const CNode *CNode::rNode() const
  8. {
  9. if (pNode() == nullptr) {
  10. return this;
  11. }
  12. return pNode()->rNode();
  13. }
  14. QList<CNode *> CNode::cNodes() const
  15. {
  16. QList<CNode *> l;
  17. for (QObject *o : children()) {
  18. CNode *n = dynamic_cast<CNode *>(o);
  19. if (n != nullptr) {
  20. l.append(n);
  21. }
  22. }
  23. return l;
  24. }
  25. int CNode::height() const
  26. {
  27. int h = 1;
  28. for (CNode *n : cNodes()) {
  29. h = std::max(h, n->height() + 1);
  30. }
  31. return h;
  32. }
  33. int CNode::depth() const
  34. {
  35. int d = 1;
  36. const CNode *n = this;
  37. while (n->pNode() != nullptr) {
  38. d++;
  39. n = n->pNode();
  40. }
  41. return d;
  42. }
  43. int CNode::leaves() const
  44. {
  45. int d = 1;
  46. if (cNodes().count() > 0) {
  47. d = 0;
  48. for (CNode *n : cNodes()) {
  49. d += n->leaves();
  50. }
  51. }
  52. return d;
  53. }
  54. int CNode::sizeOfLevel(int lev) const
  55. {
  56. if (lev < 1) {
  57. return 0;
  58. }
  59. if (lev == 1) {
  60. return 1;
  61. }
  62. int size = 0;
  63. for (CNode *n : cNodes()) {
  64. size += n->sizeOfLevel(lev - 1);
  65. }
  66. return size;
  67. }