Browse code

Parse subgraphs correctly.

Jose.R.Fonseca authored on 08/04/2009 23:30:27
Showing 1 changed files

  • xdot.py index 3f885e8..95d2330 100755
... ...
@@ -397,11 +397,12 @@ class Edge(Element):
397 397
 
398 398
 class Graph(Shape):
399 399
 
400
-    def __init__(self, width=1, height=1, nodes=(), edges=()):
400
+    def __init__(self, width=1, height=1, shapes=(), nodes=(), edges=()):
401 401
         Shape.__init__(self)
402 402
 
403 403
         self.width = width
404 404
         self.height = height
405
+        self.shapes = shapes
405 406
         self.nodes = nodes
406 407
         self.edges = edges
407 408
 
... ...
@@ -416,6 +417,8 @@ class Graph(Shape):
416 417
         cr.set_line_cap(cairo.LINE_CAP_BUTT)
417 418
         cr.set_line_join(cairo.LINE_JOIN_MITER)
418 419
 
420
+        for shape in self.shapes:
421
+            shape.draw(cr)
419 422
         for edge in self.edges:
420 423
             edge.draw(cr, highlight=(edge in highlight_items))
421 424
         for node in self.nodes:
... ...
@@ -1046,27 +1049,37 @@ class XDotParser(DotParser):
1046 1049
         
1047 1050
         self.nodes = []
1048 1051
         self.edges = []
1052
+        self.shapes = []
1049 1053
         self.node_by_name = {}
1054
+        self.top_graph = True
1050 1055
 
1051 1056
     def handle_graph(self, attrs):
1052
-        try:
1053
-            bb = attrs['bb']
1054
-        except KeyError:
1055
-            return
1057
+        if self.top_graph:
1058
+            try:
1059
+                bb = attrs['bb']
1060
+            except KeyError:
1061
+                return
1056 1062
 
1057
-        if not bb:
1058
-            return
1063
+            if not bb:
1064
+                return
1065
+
1066
+            xmin, ymin, xmax, ymax = map(int, bb.split(","))
1059 1067
 
1060
-        xmin, ymin, xmax, ymax = map(int, bb.split(","))
1068
+            self.xoffset = -xmin
1069
+            self.yoffset = -ymax
1070
+            self.xscale = 1.0
1071
+            self.yscale = -1.0
1072
+            # FIXME: scale from points to pixels
1061 1073
 
1062
-        self.xoffset = -xmin
1063
-        self.yoffset = -ymax
1064
-        self.xscale = 1.0
1065
-        self.yscale = -1.0
1066
-        # FIXME: scale from points to pixels
1074
+            self.width = xmax - xmin
1075
+            self.height = ymax - ymin
1067 1076
 
1068
-        self.width = xmax - xmin
1069
-        self.height = ymax - ymin
1077
+            self.top_graph = False
1078
+        
1079
+        for attr in ("_draw_", "_ldraw_", "_hdraw_", "_tdraw_", "_hldraw_", "_tldraw_"):
1080
+            if attr in attrs:
1081
+                parser = XDotAttrParser(self, attrs[attr])
1082
+                self.shapes.extend(parser.parse())
1070 1083
 
1071 1084
     def handle_node(self, id, attrs):
1072 1085
         try:
... ...
@@ -1108,7 +1121,7 @@ class XDotParser(DotParser):
1108 1121
     def parse(self):
1109 1122
         DotParser.parse(self)
1110 1123
 
1111
-        return Graph(self.width, self.height, self.nodes, self.edges)
1124
+        return Graph(self.width, self.height, self.shapes, self.nodes, self.edges)
1112 1125
 
1113 1126
     def parse_node_pos(self, pos):
1114 1127
         x, y = pos.split(",")