@@ -19,44 +19,48 @@ void TreeVisualiser::paintEvent(QPaintEvent *event) {
19
19
int treeWidth = calculateTreeWidth (root);
20
20
21
21
// Draw the entire tree
22
- drawTree (&painter, root, width () / 2 , 50 , treeWidth, 1 );
22
+ drawTree (&painter, root, width () / 2 , 50 , treeWidth, root-> getLevel () );
23
23
}
24
24
25
25
TreeVisualiser::TreeVisualiser (QWidget *parent) : QWidget(parent) {
26
26
Node *nroot = new Node (Node::NodeType::Expression, " +" );
27
- // nroot->setLevel(1);
27
+ nroot->setLevel (1 );
28
28
Node *child1 = new Node (Node::NodeType::Number, " 3" );
29
- // child1->setLevel(1 );
29
+ child1->setLevel (2 );
30
30
Node *child2 = new Node (Node::NodeType::Number, " 4" );
31
- // child2->setLevel(2 );
31
+ child2->setLevel (1 );
32
32
Node *child3 = new Node (Node::NodeType::Number, " 5" );
33
- // child3->setLevel(3);
33
+ child3->setLevel (3 );
34
34
35
+ Node *child4 = new Node (Node::NodeType::Number, " A7a" );
36
+ Node *child5 = new Node (Node::NodeType::Addition, " 5555555" );
35
37
36
38
this ->root = nroot;
37
39
this ->root ->addChild (child1);
38
40
this ->root ->addChild (child2);
39
41
child2->addChild (child3);
42
+ child2->addChild (child4);
43
+ child1->addChild (child5);
40
44
}
41
45
42
- void TreeVisualiser::drawTree (QPainter *painter, Node *node, int x, int y, int availableWidth, int currentLevel ) {
46
+ void TreeVisualiser::drawTree (QPainter *painter, Node *node, int x, int y, int availableWidth, int level ) {
43
47
if (!node) return ;
44
48
45
49
// Draw the current node
46
50
painter->setPen (QPen (Qt::black, 2 ));
47
51
painter->setBrush (QColor (70 , 130 , 180 )); // Steel Blue
48
52
49
53
if (isOval (node->getType ())) {
50
- painter->drawEllipse (x - 20 , y - 20 , 40 , 40 );
54
+ painter->drawEllipse (x - 20 , y - 20 , 40 , 40 ); // Node as an oval
51
55
} else {
52
- painter->drawRect (x - 40 , y - 20 , 80 , 40 );
56
+ painter->drawRect (x - 40 , y - 20 , 80 , 40 ); // Node as a rectangle
53
57
}
54
58
55
59
// Draw node value
56
60
painter->setPen (Qt::white);
57
61
QString nodeText;
58
62
if (!hasValue (node->getType ())) {
59
- nodeText = node->getNodeTypeString ().toString () + " \n (" + node->getValue () + " )" ;
63
+ nodeText = node->getNodeTypeString ().toString () + " \n (" + node->getValue () + " )" ;
60
64
} else {
61
65
nodeText = node->getValue ();
62
66
}
@@ -65,10 +69,10 @@ void TreeVisualiser::drawTree(QPainter *painter, Node *node, int x, int y, int a
65
69
66
70
if (node->getChildren ().empty ()) return ;
67
71
68
- // Calculate spacing for children
72
+ // Calculate the total width and starting positions for children
69
73
int childCount = node->getChildren ().size ();
70
- int totalChildWidth = 0 ;
71
74
std::vector<int > childWidths;
75
+ int totalChildWidth = 0 ;
72
76
73
77
// Calculate width for each child's subtree
74
78
for (Node* child : node->getChildren ()) {
@@ -77,25 +81,33 @@ void TreeVisualiser::drawTree(QPainter *painter, Node *node, int x, int y, int a
77
81
totalChildWidth += childWidth;
78
82
}
79
83
80
- // Determine vertical spacing based on level
81
- int verticalSpacing = (currentLevel == 1 ) ? 0 : 100 ;
84
+ // Vertical and horizontal spacing between nodes
85
+ int verticalSpacing = 100 ;
86
+ int horizontalSpacing = 100 ;
82
87
83
- // Starting x position for first child
88
+ // Starting X position for the first child node
84
89
int startX = x - totalChildWidth / 2 ;
85
90
86
91
// Draw children and their subtrees
87
92
for (size_t i = 0 ; i < node->getChildren ().size (); ++i) {
88
93
Node* child = node->getChildren ()[i];
89
94
int childWidth = childWidths[i];
90
95
91
- // Draw connection line
96
+ // Calculate position for each child
97
+ int childX = startX + childWidth / 2 ;
98
+ int childY = (child->getLevel () == node->getLevel ())? y : y + verticalSpacing;
99
+
100
+ // Draw connection line to child node
92
101
painter->setPen (QPen (Qt::black, 2 ));
93
- painter-> drawLine (x, y + 20 , startX + childWidth/ 2 , y + verticalSpacing) ;
102
+ int yLineOffset = (child-> getLevel () == node-> getLevel ())? 0 : 20 ;
94
103
95
- // Recursive call with updated level
96
- drawTree (painter, child, startX + childWidth/2 , y + verticalSpacing, childWidth, 1 );
104
+ painter->drawLine (x, y + yLineOffset, childX, childY);
97
105
98
- startX += childWidth;
106
+ // Recursively draw the child node
107
+ drawTree (painter, child, childX, childY, childWidth, child->getLevel ());
108
+
109
+ // Update the starting position for the next child
110
+ startX += childWidth + horizontalSpacing;
99
111
}
100
112
}
101
113
@@ -116,4 +128,3 @@ void TreeVisualiser::wheelEvent(QWheelEvent *event)
116
128
update (); // Redraw the widget
117
129
}
118
130
119
-
0 commit comments