@@ -11,86 +11,89 @@ void TreeVisualiser::paintEvent(QPaintEvent *event) {
11
11
QPainter painter (this );
12
12
painter.setRenderHint (QPainter::Antialiasing);
13
13
14
- // Clear background
15
14
painter.fillRect (rect (), QColor (240 , 240 , 240 ));
16
15
painter.scale (zoomFactor, zoomFactor);
17
16
18
- // Calculate the total width needed for the tree
19
- int treeWidth = calculateTreeWidth (root );
17
+ // Compute positions before drawing
18
+ computePositions ( );
20
19
21
- // Draw the entire tree
22
- drawTree (&painter, root, width () / 2 , 50 , treeWidth, root-> getLevel () );
20
+ // Draw the entire tree now
21
+ drawTree (&painter, root);
23
22
}
24
23
25
24
TreeVisualiser::TreeVisualiser (QWidget *parent) : QWidget(parent) {
26
25
27
26
}
28
27
29
- void TreeVisualiser::drawTree (QPainter *painter, Node *node, int x, int y, int availableWidth, int level) {
28
+ void TreeVisualiser::drawTree (QPainter *painter, Node *node)
29
+ {
30
30
if (!node) return ;
31
31
32
- // Draw the current node
32
+ QPoint pos = positions[node];
33
+
34
+ // Draw this node
33
35
painter->setPen (QPen (Qt::black, 2 ));
34
36
painter->setBrush (QColor (70 , 130 , 180 )); // Steel Blue
35
37
36
38
if (isOval (node->getType ())) {
37
- painter->drawEllipse (x - 20 , y - 20 , 40 , 40 ); // Node as an oval
39
+ painter->drawEllipse (pos. x () - 20 , pos. y () - 20 , 40 , 40 );
38
40
} else {
39
- painter->drawRect (x - 40 , y - 20 , 80 , 40 ); // Node as a rectangle
41
+ painter->drawRect (pos. x () - 40 , pos. y () - 20 , 80 , 40 );
40
42
}
41
43
42
- // Draw node value
43
44
painter->setPen (Qt::white);
44
45
QString nodeText;
45
46
if (!hasValue (node->getType ())) {
46
47
nodeText = node->getNodeTypeString ().toString () + " \n (" + node->getValue () + " )" ;
47
48
} else {
48
49
nodeText = node->getNodeTypeString ().toString ();
49
50
}
50
- painter->drawText (QRect (x - 40 , y - 20 , 80 , 40 ),
51
- Qt::AlignCenter | Qt::TextWordWrap, nodeText);
52
51
53
- if (node->getChildren ().empty ()) return ;
54
-
55
- // Calculate the total width and starting positions for children
56
- int childCount = node->getChildren ().size ();
57
- std::vector<int > childWidths;
58
- int totalChildWidth = 0 ;
52
+ painter->drawText (QRect (pos.x () - 40 , pos.y () - 20 , 80 , 40 ),
53
+ Qt::AlignCenter | Qt::TextWordWrap, nodeText);
59
54
60
- // Calculate width for each child's subtree
55
+ // Draw lines to children
56
+ painter->setPen (QPen (Qt::black, 2 ));
61
57
for (Node* child : node->getChildren ()) {
62
- int childWidth = calculateTreeWidth ( child) ;
63
- childWidths. push_back (childWidth );
64
- totalChildWidth += childWidth;
58
+ QPoint childPos = positions[ child] ;
59
+ painter-> drawLine (pos. x (), pos. y () + 20 , childPos. x (), childPos. y () - 20 );
60
+ drawTree (painter, child); // Recursively draw children
65
61
}
66
62
67
- // Vertical and horizontal spacing between nodes
68
- int verticalSpacing = 100 ;
69
- int horizontalSpacing = 100 ;
63
+ }
70
64
71
- // Starting X position for the first child node
72
- int startX = x - totalChildWidth / 2 ;
65
+ void TreeVisualiser::drawTree (QPainter *painter, Node *node, int x, int y, int availableWidth, int level) {
66
+ if (!node) return ;
73
67
74
- // Draw children and their subtrees
75
- for (size_t i = 0 ; i < node->getChildren ().size (); ++i) {
76
- Node* child = node->getChildren ()[i];
77
- int childWidth = childWidths[i];
68
+ QPoint pos = positions[node];
78
69
79
- // Calculate position for each child
80
- int childX = startX + childWidth / 2 - 20 ;
81
- int childY = y + verticalSpacing;
70
+ // Draw this node
71
+ painter-> setPen ( QPen (Qt::black, 2 )) ;
72
+ painter-> setBrush ( QColor ( 70 , 130 , 180 )); // Steel Blue
82
73
83
- // Draw connection line to child node
84
- painter->setPen (QPen (Qt::black, 2 ));
85
- int yLineOffset = (child->getLevel () == node->getLevel ())? 0 : 20 ;
74
+ if (isOval (node->getType ())) {
75
+ painter->drawEllipse (pos.x () - 20 , pos.y () - 20 , 40 , 40 );
76
+ } else {
77
+ painter->drawRect (pos.x () - 40 , pos.y () - 20 , 80 , 40 );
78
+ }
86
79
87
- painter->drawLine (x, y + 20 , childX, childY);
80
+ painter->setPen (Qt::white);
81
+ QString nodeText;
82
+ if (!hasValue (node->getType ())) {
83
+ nodeText = node->getNodeTypeString ().toString () + " \n (" + node->getValue () + " )" ;
84
+ } else {
85
+ nodeText = node->getNodeTypeString ().toString ();
86
+ }
88
87
89
- // Recursively draw the child node
90
- drawTree (painter, child, childX, childY, childWidth, child-> getLevel () );
88
+ painter-> drawText ( QRect (pos. x () - 40 , pos. y () - 20 , 80 , 40 ),
89
+ Qt::AlignCenter | Qt::TextWordWrap, nodeText );
91
90
92
- // Update the starting position for the next child
93
- startX += childWidth + horizontalSpacing;
91
+ // Draw lines to children
92
+ painter->setPen (QPen (Qt::black, 2 ));
93
+ for (Node* child : node->getChildren ()) {
94
+ QPoint childPos = positions[child];
95
+ painter->drawLine (pos.x (), pos.y () + 20 , childPos.x (), childPos.y () - 20 );
96
+ drawTree (painter, child); // Recursively draw children
94
97
}
95
98
}
96
99
0 commit comments