GraphView: Move initial node positioning logic to its own class

Right now we're just placing it in a grid, but we could also place them
in a circle as is done by Obsidian.
This commit is contained in:
Vishesh Handa
2020-09-14 17:57:38 +02:00
parent f7adeafe7d
commit 719d6842c3

View File

@ -39,10 +39,11 @@ class Graph extends ChangeNotifier {
Map<String, Set<int>> _neighbours = {}; Map<String, Set<int>> _neighbours = {};
Map<String, int> _nodeIndexes; Map<String, int> _nodeIndexes;
var x = 1050.0; GraphNodeLayout initLayouter;
var y = 1050.0;
Graph.fromFolder(NotesFolder folder) { Graph.fromFolder(NotesFolder folder) {
initLayouter = GraphNodeLayout(maxHeight: 2000, maxWidth: 2000);
print("Building graph .... "); print("Building graph .... ");
_addFolder(folder).then((_) { _addFolder(folder).then((_) {
print("Done Building graph"); print("Done Building graph");
@ -88,16 +89,8 @@ class Graph extends ChangeNotifier {
var i = nodes.indexWhere((n) => n.note.filePath == note.filePath); var i = nodes.indexWhere((n) => n.note.filePath == note.filePath);
if (i == -1) { if (i == -1) {
var node = Node(note); var node = Node(note);
node.x = x; initLayouter.positionNode(node);
node.y = y;
if (x >= 500) {
x = 50;
y += 50;
}
x += 50;
//print('${node.label} -> ${node.x} ${node.y}');
nodes.add(node); nodes.add(node);
return node; return node;
} }
@ -202,6 +195,38 @@ class Graph extends ChangeNotifier {
} }
} }
class GraphNodeLayout {
final double maxWidth;
final double maxHeight;
double x = 0.0;
double y = 0.0;
double startX = 500.0;
double startY = 500.0;
double gap = 70;
double nodeSize = 50;
GraphNodeLayout({@required this.maxWidth, @required this.maxHeight}) {
x = startX;
y = startY;
}
void positionNode(Node node) {
node.x = x;
node.y = y;
print('INIT ${node.label} -> ${node.x} ${node.y}');
x += gap;
if (x + nodeSize >= maxWidth) {
x = startX;
y += gap;
}
}
}
// //
// Basic Force Directed Layout // Basic Force Directed Layout
// //