-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDiagramView.cpp
More file actions
154 lines (131 loc) · 5.07 KB
/
Copy pathDiagramView.cpp
File metadata and controls
154 lines (131 loc) · 5.07 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
#include "DiagramView.h"
#include <QMouseEvent>
#include <QPen>
#include <QPointF>
#include <QSet>
#include "TableNode.h"
DiagramView::DiagramView(QWidget* parent)
: QGraphicsView(parent)
{
setScene(&m_scene);
setRenderHint(QPainter::Antialiasing);
setStyleSheet("background-color: #17171A; border: none;");
m_scene.setBackgroundBrush(QColor("#17171A"));
}
void DiagramView::setFocusTable(const Schema& schema, const QString& focusTable)
{
m_scene.clear();
const Table* table = schema.findTable(focusTable);
if (!table) {
return;
}
// Collect unique neighbour table names from the focus table's
// relations - this is the "spanning tree" step: only direct
// relations are shown, not the whole schema.
QVector<QString> neighbours;
QSet<QString> seen;
for (const auto& fk : schema.relationsFor(focusTable)) {
const QString other = (fk.fromTable == focusTable) ? fk.toTable : fk.fromTable;
if (other != focusTable && !seen.contains(other)) {
seen.insert(other);
neighbours.push_back(other);
}
}
const QPointF center(160, 120);
// Cross layout: up to 4 neighbours placed top / left / right / bottom.
const QVector<QPointF> neighbourSlots = {
QPointF(160, 30), // top
QPointF(50, 120), // left
QPointF(270, 120), // right
QPointF(160, 210), // bottom
};
auto placeNode = [this](const QString& name, const QPointF& centerPos, bool isFocus) {
auto* node = new TableNode(name, isFocus);
node->setPos(centerPos.x() - TableNode::kWidth / 2.0,
centerPos.y() - TableNode::kHeight / 2.0);
m_scene.addItem(node);
return centerPos;
};
// Draw connector lines first so nodes render on top of them.
QPen linePen(QColor("#5F5E5A"), 1.2);
for (int i = 0; i < neighbours.size() && i < neighbourSlots.size(); ++i) {
m_scene.addLine(center.x(), center.y(), neighbourSlots[i].x(), neighbourSlots[i].y(), linePen);
}
placeNode(focusTable, center, true);
for (int i = 0; i < neighbours.size() && i < neighbourSlots.size(); ++i) {
placeNode(neighbours[i], neighbourSlots[i], false);
}
m_scene.setSceneRect(0, 0, 330, 240);
}
void DiagramView::clear()
{
m_scene.clear();
m_scene.setSceneRect(0, 0, 330, 240);
}
void DiagramView::setTablesFromNames(const Schema& schema, const QString& focusTable,
const QVector<QString>& relatedTables)
{
m_scene.clear();
if (!schema.findTable(focusTable)) {
return;
}
// Keep only real, distinct tables other than the focus itself.
QVector<QString> neighbours;
QSet<QString> seen;
for (const auto& name : relatedTables) {
if (name == focusTable || seen.contains(name) || !schema.findTable(name)) {
continue;
}
seen.insert(name);
neighbours.push_back(name);
}
// True if a real foreign key connects focusTable and other.
auto hasKnownRelation = [&schema](const QString& lhs, const QString& rhs) {
for (const auto& fk : schema.relationsFor(lhs)) {
if ((fk.fromTable == lhs && fk.toTable == rhs)
|| (fk.toTable == lhs && fk.fromTable == rhs)) {
return true;
}
}
return false;
};
const QPointF center(160, 120);
// Cross layout: up to 4 neighbours placed top / left / right / bottom.
const QVector<QPointF> neighbourSlots = {
QPointF(160, 30), // top
QPointF(50, 120), // left
QPointF(270, 120), // right
QPointF(160, 210), // bottom
};
auto placeNode = [this](const QString& name, const QPointF& centerPos, bool isFocus) {
auto* node = new TableNode(name, isFocus);
node->setPos(centerPos.x() - TableNode::kWidth / 2.0,
centerPos.y() - TableNode::kHeight / 2.0);
m_scene.addItem(node);
};
// Solid accent line for a real foreign key (as typed in JOIN ... ON),
// dashed gray line for a table just named in the SQL without one -
// e.g. a cross join or a relation the schema doesn't declare.
QPen relatedPen(QColor("#1D9E75"), 1.4);
QPen unrelatedPen(QColor("#5F5E5A"), 1.0, Qt::DashLine);
for (int i = 0; i < neighbours.size() && i < neighbourSlots.size(); ++i) {
const QPen& pen = hasKnownRelation(focusTable, neighbours[i]) ? relatedPen : unrelatedPen;
m_scene.addLine(center.x(), center.y(), neighbourSlots[i].x(), neighbourSlots[i].y(), pen);
}
placeNode(focusTable, center, true);
for (int i = 0; i < neighbours.size() && i < neighbourSlots.size(); ++i) {
placeNode(neighbours[i], neighbourSlots[i], false);
}
m_scene.setSceneRect(0, 0, 330, 240);
}
void DiagramView::mousePressEvent(QMouseEvent* event)
{
QGraphicsItem* item = itemAt(event->pos());
if (item) {
item = item->topLevelItem();
if (auto* node = dynamic_cast<TableNode*>(item)) {
emit tableClicked(node->tableName());
}
}
QGraphicsView::mousePressEvent(event);
}