summaryrefslogtreecommitdiffstats
path: root/quanta/components/framewizard/treenode.cpp
blob: ecf42bf852455b97881e4f88ef2a79ebaaacf9f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/***************************************************************************
                          treenode.cpp  -  description
                             -------------------
    begin                : lun mar 17 2003
    copyright            : (C) 2003 by gulmini luciano
    email                : gulmini.luciano@student.unife.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "treenode.h"

static const int SIZE = 101;

treeNode::treeNode(const TQString &l, const TQString &pl) : m_label(l), m_parentLabel(pl), m_splitType(NONE){
  m_tqchildrenList.setAutoDelete(true);
  m_atts = new areaAttribute;
}

treeNode::~treeNode(){
  delete m_atts;
}

void treeNode::addChildNode(const TQString &l) {
  m_tqchildrenList.append( new treeNode(l,m_label) );
}

void treeNode::removeChildNode(const TQString &l,bool autoDelete) {
  m_tqchildrenList.setAutoDelete(autoDelete);
  m_tqchildrenList.remove(findChild(l));
}

treeNode* treeNode::findChild(const TQString &l){
  TQPtrListIterator<treeNode> it( m_tqchildrenList );
  treeNode *node;
  while ( (node = it.current()) != 0 ) {
    ++it;
    if(node->label() == l) return node;
  }
  return 0L;
}

int tree::nodeId = 0;

tree::tree(){
  m_root = new treeNode(TQString::number(nodeId,10));
  m_nodeList.resize(SIZE);
}

tree::~tree(){
  delete m_root;
}

void tree::refreshGeometries(treeNode *n){
  int dim = -6;// so we won't add exceeding pixels

  if(n->hasChildren()){
    n->firstChild();
    while(n->currentChild()){
      refreshGeometries(n->currentChild());
      n->nextChild();
    }

    TQPtrList<treeNode> list = n->tqchildrenList();
    TQPtrListIterator<treeNode> it( list );
    treeNode *node= it.current();
    TQRect newGeometry = n->atts()->tqgeometry();
    if(n->splitType()==VERTICAL){
      newGeometry.setHeight(node->atts()->tqgeometry().height());
      while ( (node = it.current()) != 0 ) {
        ++it;
        dim += node->atts()->tqgeometry().width();
        dim += 6;
      }
      newGeometry.setWidth(dim);
    }
    else
    if(n->splitType()==HORIZONTAL){
      newGeometry.setWidth(node->atts()->tqgeometry().width());
      while ( (node = it.current()) != 0 ) {
        ++it;
        dim += node->atts()->tqgeometry().height();
        dim += 6;
      }
      newGeometry.setHeight(dim);
    }

    n->atts()->setGeometry( newGeometry );
  }
}

treeNode* tree::findNode(const TQString &l){
  if(l==m_root->label()) return m_root;
  return m_nodeList.tqfind(l);
}

TQString tree::addChildNode(const TQString &l){
  treeNode *node;
  if( (node = findNode(l)) != 0) {
    ++nodeId;
    treeNode *newNode = new treeNode(TQString::number(nodeId,10),node->label());
    newNode->atts()->setAttribute( "src",node->atts()->src() );
    node->addChildNode(newNode);
    m_nodeList.insert(TQString::number(nodeId,10),newNode);
  }
  return TQString::number(nodeId,10);
}

bool tree::insertChildNode(const TQString &l){
  TQString tqparent = findNode(l)->parentLabel();
  int pos=findNode( tqparent )->childPosition( findNode(l) );
  ++nodeId;
  treeNode *newNode = new treeNode(TQString::number(nodeId,10),tqparent);
  newNode->atts()->setAttribute( "src",findNode(l)->atts()->src() );
  m_nodeList.insert(TQString::number(nodeId,10),newNode);
  return findNode( tqparent )->insertChild(pos,newNode);
}

void tree::reset(){
  nodeId = 1;
  m_root->removeChildren();
  m_root->atts()->resetAttributes();
  m_nodeList.clear();
}

void tree::removeChildNode(const TQString &pl,const TQString &l,bool autoDelete){
  findNode(pl)->removeChildNode(l,autoDelete);
}