|
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, 1); |
| 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(1); |
| 30 | + Node *child2 = new Node(Node::NodeType::Number, "4"); |
| 31 | + // child2->setLevel(2); |
| 32 | + Node *child3 = new Node(Node::NodeType::Number, "5"); |
| 33 | + // child3->setLevel(3); |
| 34 | + |
| 35 | + |
| 36 | + this->root = nroot; |
| 37 | + this->root->addChild(child1); |
| 38 | + this->root->addChild(child2); |
| 39 | + child2->addChild(child3); |
| 40 | +} |
| 41 | + |
| 42 | +void TreeVisualiser::drawTree(QPainter *painter, Node *node, int x, int y, int availableWidth, int currentLevel) { |
| 43 | + if (!node) return; |
| 44 | + |
| 45 | + // Draw the current node |
| 46 | + painter->setPen(QPen(Qt::black, 2)); |
| 47 | + painter->setBrush(QColor(70, 130, 180)); // Steel Blue |
| 48 | + |
| 49 | + if (isOval(node->getType())) { |
| 50 | + painter->drawEllipse(x - 20, y - 20, 40, 40); |
| 51 | + } else { |
| 52 | + painter->drawRect(x - 40, y - 20, 80, 40); |
| 53 | + } |
10 | 54 |
|
11 |
| - // set the style |
12 |
| - setStyleSheet("background-color: #2e2e2e ; color: #c0c0c0; border: 1px solid #2a2a2a;"); |
| 55 | + // Draw node value |
| 56 | + painter->setPen(Qt::white); |
| 57 | + QString nodeText; |
| 58 | + if (!hasValue(node->getType())) { |
| 59 | + nodeText = node->getNodeTypeString().toString() + "\n (" + node->getValue() + ")"; |
| 60 | + } else { |
| 61 | + nodeText = node->getValue(); |
| 62 | + } |
| 63 | + painter->drawText(QRect(x - 40, y - 20, 80, 40), |
| 64 | + Qt::AlignCenter | Qt::TextWordWrap, nodeText); |
13 | 65 |
|
14 |
| - // add the label |
15 |
| - QLabel* label = new QLabel("Tree Visualiser"); |
16 |
| - label->setAlignment(Qt::AlignCenter); |
17 |
| - label->setStyleSheet("font-size: 20px; color: #c0c0c0;"); |
| 66 | + if (node->getChildren().empty()) return; |
18 | 67 |
|
19 |
| - // add the label to the layout |
20 |
| - layout->addWidget(label); |
| 68 | + // Calculate spacing for children |
| 69 | + int childCount = node->getChildren().size(); |
| 70 | + int totalChildWidth = 0; |
| 71 | + std::vector<int> childWidths; |
| 72 | + |
| 73 | + // Calculate width for each child's subtree |
| 74 | + for (Node* child : node->getChildren()) { |
| 75 | + int childWidth = calculateTreeWidth(child); |
| 76 | + childWidths.push_back(childWidth); |
| 77 | + totalChildWidth += childWidth; |
| 78 | + } |
| 79 | + |
| 80 | + // Determine vertical spacing based on level |
| 81 | + int verticalSpacing = (currentLevel == 1) ? 0 : 100; |
| 82 | + |
| 83 | + // Starting x position for first child |
| 84 | + int startX = x - totalChildWidth / 2; |
| 85 | + |
| 86 | + // Draw children and their subtrees |
| 87 | + for (size_t i = 0; i < node->getChildren().size(); ++i) { |
| 88 | + Node* child = node->getChildren()[i]; |
| 89 | + int childWidth = childWidths[i]; |
| 90 | + |
| 91 | + // Draw connection line |
| 92 | + painter->setPen(QPen(Qt::black, 2)); |
| 93 | + painter->drawLine(x, y + 20, startX + childWidth/2, y + verticalSpacing); |
| 94 | + |
| 95 | + // Recursive call with updated level |
| 96 | + drawTree(painter, child, startX + childWidth/2, y + verticalSpacing, childWidth, 1); |
| 97 | + |
| 98 | + startX += childWidth; |
| 99 | + } |
21 | 100 | }
|
22 | 101 |
|
23 |
| -void TreeVisualiser::setRoot(Node *root) |
| 102 | +void TreeVisualiser::wheelEvent(QWheelEvent *event) |
24 | 103 | {
|
25 |
| - qDebug() << "Drawing tree"; |
| 104 | + const qreal zoomStep = 0.1; // Amount to zoom in/out |
| 105 | + const qreal minZoom = 0.5; // Minimum zoom level |
| 106 | + const qreal maxZoom = 3.0; // Maximum zoom level |
| 107 | + |
| 108 | + if (event->angleDelta().y() > 0) { |
| 109 | + // Zoom in |
| 110 | + zoomFactor = qMin(maxZoom, zoomFactor + zoomStep); |
| 111 | + } else { |
| 112 | + // Zoom out |
| 113 | + zoomFactor = qMax(minZoom, zoomFactor - zoomStep); |
| 114 | + } |
| 115 | + |
| 116 | + update(); // Redraw the widget |
26 | 117 | }
|
| 118 | + |
| 119 | + |
0 commit comments