-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphwidget.cpp
More file actions
226 lines (190 loc) · 6.97 KB
/
graphwidget.cpp
File metadata and controls
226 lines (190 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "graphwidget.h"
#include <QPen>
#include <QBrush>
#include <QFont>
#include <QFontMetrics>
#include <QWheelEvent>
#include <QGraphicsDropShadowEffect>
#include <QQueue>
#include <algorithm>
GraphWidget::GraphWidget(QWidget *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene(this);
scene->setBackgroundBrush(QColor(245, 245, 245));
setScene(scene);
setRenderHint(QPainter::Antialiasing);
setDragMode(QGraphicsView::ScrollHandDrag);
setInteractive(true);
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
setAlignment(Qt::AlignCenter);
}
void GraphWidget::loadGraph(const GraphData &graph) {
scene->clear();
scene->setBackgroundBrush(QColor(245, 245, 245));
nodePositions.clear();
nodeItems.clear();
textItems.clear();
if (graph.nodes.isEmpty()) {
return;
}
positionNodes(graph);
drawGraph(graph);
scene->setSceneRect(scene->itemsBoundingRect());
fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
}
int GraphWidget::calculateDepth(const GraphData &graph, const QString &nodeId, QSet<QString> &visited) {
if (visited.contains(nodeId)) return 0;
visited.insert(nodeId);
const GraphNode &node = graph.nodes[nodeId];
int maxDepth = 0;
for (const QString &conn : node.connections) {
if (!visited.contains(conn)) {
maxDepth = qMax(maxDepth, calculateDepth(graph, conn, visited));
}
}
return maxDepth + 1;
}
void GraphWidget::positionNodes(const GraphData &graph) {
QQueue<QString> queue;
QMap<QString, int> levels;
QMap<QString, int> horizontalOrder;
QSet<QString> visited;
// Инициализация с корневых узлов
for (const QString &rootId : graph.rootNodes) {
queue.enqueue(rootId);
levels[rootId] = 0;
visited.insert(rootId);
horizontalOrder[rootId] = horizontalOrder.size();
}
if (queue.isEmpty() && !graph.nodes.isEmpty()) {
QString firstId = graph.nodes.firstKey();
queue.enqueue(firstId);
levels[firstId] = 0;
visited.insert(firstId);
horizontalOrder[firstId] = 0;
}
// BFS с учётом порядка обхода
while (!queue.isEmpty()) {
QString currentId = queue.dequeue();
int currentLevel = levels[currentId];
const GraphNode &node = graph.nodes[currentId];
for (const QString &conn : node.connections) {
if (!visited.contains(conn)) {
visited.insert(conn);
levels[conn] = currentLevel + 1;
horizontalOrder[conn] = horizontalOrder.size();
queue.enqueue(conn);
}
}
}
// Обрабатываем изолированные узлы
for (const QString &nodeId : graph.nodes.keys()) {
if (!visited.contains(nodeId)) {
levels[nodeId] = 0;
visited.insert(nodeId);
horizontalOrder[nodeId] = horizontalOrder.size();
}
}
// Группируем по уровням
QMap<int, QList<QString>> levelNodes;
for (const QString &nodeId : levels.keys()) {
levelNodes[levels[nodeId]].append(nodeId);
}
// Сортируем узлы на каждом уровне по порядку обхода
for (int level : levelNodes.keys()) {
std::sort(levelNodes[level].begin(), levelNodes[level].end(),
[&horizontalOrder](const QString &a, const QString &b) {
return horizontalOrder[a] < horizontalOrder[b];
});
}
// Позиционируем с увеличенными отступами
qreal verticalSpacing = 180;
qreal horizontalSpacing = 250;
for (int level : levelNodes.keys()) {
const QList<QString> &nodes = levelNodes[level];
qreal levelWidth = nodes.size() * horizontalSpacing;
qreal startX = -levelWidth / 2 + horizontalSpacing / 2;
for (int i = 0; i < nodes.size(); ++i) {
QString nodeId = nodes[i];
qreal x = startX + i * horizontalSpacing;
qreal y = level * verticalSpacing;
nodePositions[nodeId] = QPointF(x, y);
}
}
}
QColor GraphWidget::getNodeColor(int level) {
QList<QColor> colors = {
QColor(100, 149, 237),
QColor(60, 179, 113),
QColor(255, 165, 0),
QColor(221, 160, 221),
QColor(144, 238, 144),
QColor(176, 196, 222)
};
return colors[qMin(level, colors.size() - 1)];
}
void GraphWidget::drawGraph(const GraphData &graph) {
QSet<QString> drawnEdges;
for (const QString &nodeId : graph.nodes.keys()) {
const GraphNode &node = graph.nodes[nodeId];
for (const QString &conn : node.connections) {
QString edgeKey = nodeId < conn ? nodeId + "-" + conn : conn + "-" + nodeId;
if (drawnEdges.contains(edgeKey)) continue;
drawnEdges.insert(edgeKey);
if (nodePositions.contains(conn)) {
QGraphicsLineItem *line = scene->addLine(
nodePositions[nodeId].x(),
nodePositions[nodeId].y(),
nodePositions[conn].x(),
nodePositions[conn].y(),
QPen(QColor(70, 70, 70), 2, Qt::SolidLine)
);
line->setZValue(0);
}
}
}
for (const QString &nodeId : graph.nodes.keys()) {
const GraphNode &node = graph.nodes[nodeId];
QPointF pos = nodePositions[nodeId];
QFont font("Arial", 9, QFont::Bold);
QFontMetrics metrics(font);
int textWidth = metrics.horizontalAdvance(node.name);
int textHeight = metrics.height();
qreal rectWidth = qMax(120.0, static_cast<qreal>(textWidth) + 40);
qreal rectHeight = 50;
QSet<QString> visited;
int level = calculateDepth(graph, nodeId, visited);
level = qMin(level, 5);
QGraphicsRectItem *rect = scene->addRect(
pos.x() - rectWidth / 2, pos.y() - rectHeight / 2,
rectWidth, rectHeight,
QPen(Qt::black, 2),
QBrush(getNodeColor(level))
);
rect->setZValue(1);
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
shadow->setBlurRadius(10);
shadow->setOffset(3, 3);
shadow->setColor(QColor(0, 0, 0, 100));
rect->setGraphicsEffect(shadow);
QGraphicsTextItem *textItem = scene->addText(node.name);
textItem->setFont(font);
textItem->setDefaultTextColor(Qt::white);
textItem->setPos(pos.x() - textWidth / 2, pos.y() - textHeight / 2);
textItem->setZValue(2);
nodeItems[nodeId] = rect;
textItems[nodeId] = textItem;
}
}
void GraphWidget::wheelEvent(QWheelEvent *event) {
if (event->angleDelta().y() > 0) {
scale(1.1, 1.1);
} else {
scale(0.9, 0.9);
}
event->accept();
}
void GraphWidget::fitGraph() {
fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
}