|
1 | 1 | #include "TreeVisualiser.h"
|
| 2 | +#include <QPainter> |
| 3 | +#include <QWheelEvent> |
2 | 4 |
|
3 | 5 | using Tiny::Widgets::TreeVisualiser;
|
4 | 6 |
|
| 7 | +void TreeVisualiser::paintEvent(QPaintEvent *event) { |
| 8 | + Q_UNUSED(event); |
| 9 | + if (!root) return; |
| 10 | + |
| 11 | + QPainter painter(this); |
| 12 | + painter.setRenderHint(QPainter::Antialiasing); |
| 13 | + |
| 14 | + // Clear background |
| 15 | + painter.fillRect(rect(), QColor(240, 240, 240)); |
| 16 | + painter.scale(zoomFactor, zoomFactor); |
| 17 | + |
| 18 | + // Calculate the total width needed for the tree |
| 19 | + int treeWidth = calculateTreeWidth(root); |
| 20 | + |
| 21 | + // Draw the entire tree |
| 22 | + drawTree(&painter, root, width() / 2, 50, treeWidth, root->getLevel()); |
| 23 | +} |
| 24 | + |
5 | 25 | TreeVisualiser::TreeVisualiser(QWidget *parent) : QWidget(parent) {
|
6 |
| - // set the layout |
7 |
| - QVBoxLayout* layout = new QVBoxLayout(this); |
8 |
| - layout->setContentsMargins(0, 0, 0, 0); |
9 |
| - setLayout(layout); |
| 26 | + Node *nroot = new Node(Node::NodeType::Expression, "+"); |
| 27 | + nroot->setLevel(1); |
| 28 | + Node *child1 = new Node(Node::NodeType::Number, "3"); |
| 29 | + child1->setLevel(2); |
| 30 | + Node *child2 = new Node(Node::NodeType::Number, "4"); |
| 31 | + child2->setLevel(1); |
| 32 | + Node *child3 = new Node(Node::NodeType::Number, "5"); |
| 33 | + child3->setLevel(3); |
| 34 | + |
| 35 | + Node *child4 = new Node(Node::NodeType::Number, "A7a"); |
| 36 | + Node *child5 = new Node(Node::NodeType::Addition, "5555555"); |
| 37 | + |
| 38 | + this->root = nroot; |
| 39 | + this->root->addChild(child1); |
| 40 | + this->root->addChild(child2); |
| 41 | + child2->addChild(child3); |
| 42 | + child2->addChild(child4); |
| 43 | + child1->addChild(child5); |
| 44 | +} |
| 45 | + |
| 46 | +void TreeVisualiser::drawTree(QPainter *painter, Node *node, int x, int y, int availableWidth, int level) { |
| 47 | + if (!node) return; |
| 48 | + |
| 49 | + // Draw the current node |
| 50 | + painter->setPen(QPen(Qt::black, 2)); |
| 51 | + painter->setBrush(QColor(70, 130, 180)); // Steel Blue |
| 52 | + |
| 53 | + if (isOval(node->getType())) { |
| 54 | + painter->drawEllipse(x - 20, y - 20, 40, 40); // Node as an oval |
| 55 | + } else { |
| 56 | + painter->drawRect(x - 40, y - 20, 80, 40); // Node as a rectangle |
| 57 | + } |
| 58 | + |
| 59 | + // Draw node value |
| 60 | + painter->setPen(Qt::white); |
| 61 | + QString nodeText; |
| 62 | + if (!hasValue(node->getType())) { |
| 63 | + nodeText = node->getNodeTypeString().toString() + "\n(" + node->getValue() + ")"; |
| 64 | + } else { |
| 65 | + nodeText = node->getValue(); |
| 66 | + } |
| 67 | + painter->drawText(QRect(x - 40, y - 20, 80, 40), |
| 68 | + Qt::AlignCenter | Qt::TextWordWrap, nodeText); |
10 | 69 |
|
11 |
| - // set the style |
12 |
| - setStyleSheet("background-color: #2e2e2e ; color: #c0c0c0; border: 1px solid #2a2a2a;"); |
| 70 | + if (node->getChildren().empty()) return; |
13 | 71 |
|
14 |
| - // add the label |
15 |
| - QLabel* label = new QLabel("Tree Visualiser"); |
16 |
| - label->setAlignment(Qt::AlignCenter); |
17 |
| - label->setStyleSheet("font-size: 20px; color: #c0c0c0;"); |
| 72 | + // Calculate the total width and starting positions for children |
| 73 | + int childCount = node->getChildren().size(); |
| 74 | + std::vector<int> childWidths; |
| 75 | + int totalChildWidth = 0; |
18 | 76 |
|
19 |
| - // add the label to the layout |
20 |
| - layout->addWidget(label); |
| 77 | + // Calculate width for each child's subtree |
| 78 | + for (Node* child : node->getChildren()) { |
| 79 | + int childWidth = calculateTreeWidth(child); |
| 80 | + childWidths.push_back(childWidth); |
| 81 | + totalChildWidth += childWidth; |
| 82 | + } |
| 83 | + |
| 84 | + // Vertical and horizontal spacing between nodes |
| 85 | + int verticalSpacing = 100; |
| 86 | + int horizontalSpacing = 100; |
| 87 | + |
| 88 | + // Starting X position for the first child node |
| 89 | + int startX = x - totalChildWidth / 2; |
| 90 | + |
| 91 | + // Draw children and their subtrees |
| 92 | + for (size_t i = 0; i < node->getChildren().size(); ++i) { |
| 93 | + Node* child = node->getChildren()[i]; |
| 94 | + int childWidth = childWidths[i]; |
| 95 | + |
| 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 |
| 101 | + painter->setPen(QPen(Qt::black, 2)); |
| 102 | + int yLineOffset = (child->getLevel() == node->getLevel())? 0 : 20; |
| 103 | + |
| 104 | + painter->drawLine(x, y + yLineOffset, childX, childY); |
| 105 | + |
| 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; |
| 111 | + } |
21 | 112 | }
|
22 | 113 |
|
23 |
| -void TreeVisualiser::setRoot(Node *root) |
| 114 | +void TreeVisualiser::wheelEvent(QWheelEvent *event) |
24 | 115 | {
|
25 |
| - qDebug() << "Drawing tree"; |
26 |
| - // print the tree |
27 |
| - root->printTree(); |
| 116 | + |
| 117 | + const qreal zoomStep = 0.1; // Amount to zoom in/out |
| 118 | + const qreal minZoom = 0.5; // Minimum zoom level |
| 119 | + const qreal maxZoom = 3.0; // Maximum zoom level |
| 120 | + |
| 121 | + if (event->angleDelta().y() > 0) { |
| 122 | + // Zoom in |
| 123 | + zoomFactor = qMin(maxZoom, zoomFactor + zoomStep); |
| 124 | + } else { |
| 125 | + // Zoom out |
| 126 | + zoomFactor = qMax(minZoom, zoomFactor - zoomStep); |
| 127 | + } |
| 128 | + |
| 129 | + update(); // Redraw the widget |
28 | 130 | }
|
| 131 | + |
0 commit comments