Skip to content

copy xdot from https://github.com/jbohren/xdot,#14

Merged
k-okada merged 4 commits intoros-visualization:indigo-develfrom
k-okada:indigo-devel
Jun 19, 2017
Merged

copy xdot from https://github.com/jbohren/xdot,#14
k-okada merged 4 commits intoros-visualization:indigo-develfrom
k-okada:indigo-devel

Conversation

@k-okada
Copy link
Contributor

@k-okada k-okada commented May 3, 2017

I first try to use system xdot, but found there is a significant difference between jbohren/xdot and ubuntu intalled xdot

23,24d22
< __version__ = "0.4"
< 
194a193,216
> class ImageShape(Shape):
> 
>     def __init__(self, pen, x0, y0, w, h, path):
>         Shape.__init__(self)
>         self.pen = pen.copy()
>         self.x0 = x0
>         self.y0 = y0
>         self.w = w
>         self.h = h
>         self.path = path
> 
>     def draw(self, cr, highlight=False):
>         cr2 = gtk.gdk.CairoContext(cr)
>         pixbuf = gtk.gdk.pixbuf_new_from_file(self.path)
>         sx = float(self.w)/float(pixbuf.get_width())
>         sy = float(self.h)/float(pixbuf.get_height())
>         cr.save()
>         cr.translate(self.x0, self.y0 - self.h)
>         cr.scale(sx, sy)
>         cr2.set_source_pixbuf(pixbuf, 0, 0)
>         cr2.paint()
>         cr.restore()
> 
> 
320c342
<     def __init__(self, item, x, y, highlight=None, url=None):
---
>     def __init__(self, item, x, y, highlight=None):
327d348
<         self.url = url
383c404
<     def __init__(self, src, dst, points, shapes, url):
---
>     def __init__(self, src, dst, points, shapes):
388d408
<         self.url = url
394c414
<             return Jump(self, self.dst.x, self.dst.y, highlight=set([self, self.dst]),url=self.url)
---
>             return Jump(self, self.dst.x, self.dst.y, highlight=set([self, self.dst]))
396c416
<             return Jump(self, self.src.x, self.src.y, highlight=set([self, self.src]),url=self.url)
---
>             return Jump(self, self.src.x, self.src.y, highlight=set([self, self.src]))
402c422
<     def __init__(self, width=1, height=1, shapes=(), nodes=(), edges=(), subgraph_shapes={}):
---
>     def __init__(self, width=1, height=1, shapes=(), nodes=(), edges=()):
410d429
<         self.subgraph_shapes = subgraph_shapes
448a468,475
> BOLD = 1
> ITALIC = 2
> UNDERLINE = 4
> SUPERSCRIPT = 8
> SUBSCRIPT = 16
> STRIKE_THROUGH = 32
> 
> 
457c484
<         self.buf = self.unescape(buf)
---
>         self.buf = buf
466,470d492
<     def unescape(self, buf):
<         buf = buf.replace('\\"', '"')
<         buf = buf.replace('\\n', '\n')
<         return buf
< 
479,480c501,502
<     def read_number(self):
<         return int(float(self.read_code()))
---
>     def read_int(self):
>         return int(self.read_code())
486,487c508,509
<         x = self.read_number()
<         y = self.read_number()
---
>         x = self.read_float()
>         y = self.read_float()
491c513
<         num = self.read_number()
---
>         num = self.read_int()
500c522
<         n = self.read_number()
---
>         n = self.read_int()
526a549,551
>         elif c1 == "[":
>             sys.stderr.write('warning: color gradients not supported yet\n')
>             return None
556c581
<         sys.stderr.write("unknown color '%s'\n" % c)
---
>         sys.stderr.write("warning: unknown color '%s'\n" % c)
579c604
<                 elif style in ("solid", "dashed"):
---
>                 elif style in ("solid", "dashed", "dotted"):
587,588c612,613
<                 j = s.read_number()
<                 w = s.read_number()
---
>                 j = s.read_int()
>                 w = s.read_float()
590a616,618
>             elif op == "t":
>                 f = s.read_int()
>                 self.handle_font_characteristics(f)
593,594c621,622
<                 w = s.read_number()
<                 h = s.read_number()
---
>                 w = s.read_float()
>                 h = s.read_float()
598,599c626,627
<                 w = s.read_number()
<                 h = s.read_number()
---
>                 w = s.read_float()
>                 h = s.read_float()
615a644,649
>             elif op == "I":
>                 x0, y0 = s.read_point()
>                 w = s.read_float()
>                 h = s.read_float()
>                 path = s.read_text()
>                 self.handle_image(x0, y0, w, h, path)
617,618c651,652
<                 sys.stderr.write("unknown xdot opcode '%s'\n" % op)
<                 break
---
>                 sys.stderr.write("error: unknown xdot opcode '%s'\n" % op)
>                 sys.exit(1)
638a673,674
>         elif style == "dotted":
>             self.pen.dash = (2, 4)       # 2pt on, 4pt off
643a680,684
>     def handle_font_characteristics(self, flags):
>         # TODO
>         if flags != 0:
>             sys.stderr.write("warning: font characteristics not supported yet\n" % op)
> 
652a694,696
>     def handle_image(self, x0, y0, w, h, path):
>         self.shapes.append(ImageShape(self.pen, x0, y0, w, h, path))
> 
928,931c972,976
<             text = text.replace('\\r', '\r')
<             text = text.replace('\\n', '\n')
<             text = text.replace('\\t', '\t')
<             text = text.replace('\\', '')
---
>             # quotes
>             text = text.replace('\\"', '"')
> 
>             # layout engines recognize other escape codes (many non-standard)
>             # but we don't translate them here
965d1009
<         shapes_before = set(self.shapes)
976,977d1019
<         new_shapes = set(self.shapes) - shapes_before
<         self.subgraph_shapes[id] = [s for s in new_shapes if not any([s in ss for ss in self.subgraph_shapes.values()])]
1077d1118
<         self.subgraph_shapes = {}
1097,1098c1138,1139
<             self.width = xmax - xmin
<             self.height = ymax - ymin
---
>             self.width  = max(xmax - xmin, 1)
>             self.height = max(ymax - ymin, 1)
1114,1115c1155,1156
<         w = float(attrs['width'])*72
<         h = float(attrs['height'])*72
---
>         w = float(attrs.get('width', 0))*72
>         h = float(attrs.get('height', 0))*72
1139d1179
<         url = attrs.get('URL', None)
1143c1183
<             self.edges.append(Edge(src, dst, points, shapes, url))
---
>             self.edges.append(Edge(src, dst, points, shapes))
1148,1153c1188
<         """
<         for k,shapes in self.subgraph_shapes.iteritems():
<           self.shapes += shapes
<         """
< 
<         return Graph(self.width, self.height, self.shapes, self.nodes, self.edges, self.subgraph_shapes)
---
>         return Graph(self.width, self.height, self.shapes, self.nodes, self.edges)
1414a1450,1452
>         self.last_mtime = None
> 
>         gobject.timeout_add(1000, self.update)
1427,1429c1465,1467
<     def set_dotcode(self, dotcode, filename='<stdin>'):
<         if isinstance(dotcode, unicode):
<             dotcode = dotcode.encode('utf8')
---
>     def run_filter(self, dotcode):
>         if not self.filter:
>             return dotcode
1438a1477
>         sys.stderr.write(error)
1440d1478
<             print "UNABLE TO SHELL TO DOT", error
1446a1485,1493
>             return None
>         return xdotcode
> 
>     def set_dotcode(self, dotcode, filename=None):
>         self.openfilename = None
>         if isinstance(dotcode, unicode):
>             dotcode = dotcode.encode('utf8')
>         xdotcode = self.run_filter(dotcode)
>         if xdotcode is None:
1458a1506,1509
>             if filename is None:
>                 self.last_mtime = None
>             else:
>                 self.last_mtime = os.stat(filename).st_mtime
1466c1517
<         self.zoom_image(self.zoom_ratio, center=False)
---
>         self.zoom_image(self.zoom_ratio, center=True)
1476a1528,1535
>     def update(self):
>         if self.openfilename is not None:
>             current_mtime = os.stat(self.openfilename).st_mtime
>             if current_mtime != self.last_mtime:
>                 self.last_mtime = current_mtime
>                 self.reload()
>         return True
> 
1516a1576,1579
>         # Constrain zoom ratio to a sane range to prevent numeric instability.
>         zoom_ratio = min(zoom_ratio, 1E4)
>         zoom_ratio = max(zoom_ratio, 1E-6)
> 
1591c1654,1657
<         if event.keyval == gtk.keysyms.Page_Up:
---
>         if event.keyval in (gtk.keysyms.Page_Up,
>                             gtk.keysyms.plus,
>                             gtk.keysyms.equal,
>                             gtk.keysyms.KP_Add):
1595c1661,1663
<         if event.keyval == gtk.keysyms.Page_Down:
---
>         if event.keyval in (gtk.keysyms.Page_Down,
>                             gtk.keysyms.minus,
>                             gtk.keysyms.KP_Subtract):
1608a1677,1679
>         if event.keyval == gtk.keysyms.p:
>             self.on_print()
>             return True
1610a1682,1712
>     print_settings = None
>     def on_print(self, action=None):
>         print_op = gtk.PrintOperation()
> 
>         if self.print_settings != None:
>             print_op.set_print_settings(self.print_settings)
> 
>         print_op.connect("begin_print", self.begin_print)
>         print_op.connect("draw_page", self.draw_page)
> 
>         res = print_op.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG, self.parent.parent)
> 
>         if res == gtk.PRINT_OPERATION_RESULT_APPLY:
>             print_settings = print_op.get_print_settings()
> 
>     def begin_print(self, operation, context):
>         operation.set_n_pages(1)
>         #operation.set_support_selection(True)
>         #operation.set_has_selection(True)
>         return True
> 
>     def draw_page(self, operation, context, page_nr):
>         cr = context.get_cairo_context()
> 
>         rect = self.get_allocation()
>         cr.translate(0.5*rect.width, 0.5*rect.height)
>         cr.scale(self.zoom_ratio, self.zoom_ratio)
>         cr.translate(-self.x, -self.y)
> 
>         self.graph.draw(cr, highlight_items=self.highlight)
> 
1711a1814
>             <toolitem action="Print"/>
1720a1824,1825
>     base_title = 'Dot Viewer'
> 
1728c1833
<         window.set_title('Dot Viewer')
---
>         window.set_title(self.base_title)
1749a1855
>             ('Print', gtk.STOCK_PRINT, None, None, "Prints the currently visible part of the graph", self.widget.on_print),
1772,1783d1877
<     def update(self, filename):
<         import os
<         if not hasattr(self, "last_mtime"):
<             self.last_mtime = None
< 
<         current_mtime = os.stat(filename).st_mtime
<         if current_mtime != self.last_mtime:
<             self.last_mtime = current_mtime
<             self.open_file(filename,True)
< 
<         return True
< 
1787c1881
<     def set_dotcode(self, dotcode, filename='<stdin>',refresh=False):
---
>     def set_dotcode(self, dotcode, filename=None):
1789,1791c1883,1884
<             self.set_title(os.path.basename(filename) + ' - Dot Viewer')
<             if not refresh:
<                 self.widget.zoom_to_fit()
---
>             self.update_title(filename)
>             self.widget.zoom_to_fit()
1793c1886
<     def set_xdotcode(self, xdotcode, filename='<stdin>'):
---
>     def set_xdotcode(self, xdotcode, filename=None):
1795c1888
<             self.set_title(os.path.basename(filename) + ' - Dot Viewer')
---
>             self.update_title(filename)
1796a1890,1895
>         
>     def update_title(self, filename=None):
>         if filename is None:
>             self.set_title(self.base_title)
>         else:
>             self.set_title(os.path.basename(filename) + ' - ' + self.base_title)
1798c1897
<     def open_file(self, filename, refresh=False):
---
>     def open_file(self, filename):
1801c1900
<             self.set_dotcode(fp.read(), filename ,refresh)
---
>             self.set_dotcode(fp.read(), filename)
1807c1906
<             dlg.set_title('Dot Viewer')
---
>             dlg.set_title(self.base_title)
1842,1843c1941
<         usage='\n\t%prog [file]',
<         version='%%prog %s' % __version__)
---
>         usage='\n\t%prog [file]')
1848a1947,1950
>     parser.add_option(
>         '-n', '--no-filter',
>         action='store_const', const=None, dest='filter',
>         help='assume input is already filtered into xdot format (use e.g. dot -Txdot)')
1857c1959,1962
<     if len(args) >= 1:
---
>     if len(args) == 0:
>         if not sys.stdin.isatty():
>             win.set_dotcode(sys.stdin.read())
>     else:
1862d1966
<             gobject.timeout_add(1000, win.update, args[0])

@k-okada k-okada merged commit 17a83b5 into ros-visualization:indigo-devel Jun 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant