Skip to content

Commit 47577dd

Browse files
authored
Update I want to die.cpp
1 parent 9250dfe commit 47577dd

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

parser_gui/Widgets/src/TreeVisualiser.cpp

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,48 @@ void TreeVisualiser::paintEvent(QPaintEvent *event) {
1919
int treeWidth = calculateTreeWidth(root);
2020

2121
// Draw the entire tree
22-
drawTree(&painter, root, width() / 2, 50, treeWidth, 1);
22+
drawTree(&painter, root, width() / 2, 50, treeWidth, root->getLevel());
2323
}
2424

2525
TreeVisualiser::TreeVisualiser(QWidget *parent) : QWidget(parent) {
2626
Node *nroot = new Node(Node::NodeType::Expression, "+");
27-
// nroot->setLevel(1);
27+
nroot->setLevel(1);
2828
Node *child1 = new Node(Node::NodeType::Number, "3");
29-
// child1->setLevel(1);
29+
child1->setLevel(2);
3030
Node *child2 = new Node(Node::NodeType::Number, "4");
31-
// child2->setLevel(2);
31+
child2->setLevel(1);
3232
Node *child3 = new Node(Node::NodeType::Number, "5");
33-
// child3->setLevel(3);
33+
child3->setLevel(3);
3434

35+
Node *child4 = new Node(Node::NodeType::Number, "A7a");
36+
Node *child5 = new Node(Node::NodeType::Addition, "5555555");
3537

3638
this->root = nroot;
3739
this->root->addChild(child1);
3840
this->root->addChild(child2);
3941
child2->addChild(child3);
42+
child2->addChild(child4);
43+
child1->addChild(child5);
4044
}
4145

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) {
4347
if (!node) return;
4448

4549
// Draw the current node
4650
painter->setPen(QPen(Qt::black, 2));
4751
painter->setBrush(QColor(70, 130, 180)); // Steel Blue
4852

4953
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
5155
} else {
52-
painter->drawRect(x - 40, y - 20, 80, 40);
56+
painter->drawRect(x - 40, y - 20, 80, 40); // Node as a rectangle
5357
}
5458

5559
// Draw node value
5660
painter->setPen(Qt::white);
5761
QString nodeText;
5862
if (!hasValue(node->getType())) {
59-
nodeText = node->getNodeTypeString().toString() + "\n (" + node->getValue() + ")";
63+
nodeText = node->getNodeTypeString().toString() + "\n(" + node->getValue() + ")";
6064
} else {
6165
nodeText = node->getValue();
6266
}
@@ -65,10 +69,10 @@ void TreeVisualiser::drawTree(QPainter *painter, Node *node, int x, int y, int a
6569

6670
if (node->getChildren().empty()) return;
6771

68-
// Calculate spacing for children
72+
// Calculate the total width and starting positions for children
6973
int childCount = node->getChildren().size();
70-
int totalChildWidth = 0;
7174
std::vector<int> childWidths;
75+
int totalChildWidth = 0;
7276

7377
// Calculate width for each child's subtree
7478
for (Node* child : node->getChildren()) {
@@ -77,25 +81,33 @@ void TreeVisualiser::drawTree(QPainter *painter, Node *node, int x, int y, int a
7781
totalChildWidth += childWidth;
7882
}
7983

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;
8287

83-
// Starting x position for first child
88+
// Starting X position for the first child node
8489
int startX = x - totalChildWidth / 2;
8590

8691
// Draw children and their subtrees
8792
for (size_t i = 0; i < node->getChildren().size(); ++i) {
8893
Node* child = node->getChildren()[i];
8994
int childWidth = childWidths[i];
9095

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
92101
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;
94103

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);
97105

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;
99111
}
100112
}
101113

@@ -116,4 +128,3 @@ void TreeVisualiser::wheelEvent(QWheelEvent *event)
116128
update(); // Redraw the widget
117129
}
118130

119-

0 commit comments

Comments
 (0)