24 from qtcanvas
import *
25 from GraphViewer
import GraphViewer
29 from pygraphviz
import graphviz
as gv
37 object.customEvent(event)
49 root=self.node.getRootNode()
50 rootItem=Item.adapt(root)
51 CONNECTOR.Connect(rootItem,
"selected",self.
selectItem,())
53 CONNECTOR.Connect(self.item.datalinks,
"add",self.
addLink,())
65 lnode=self.node.edGetDirectDescendants()
71 for k,n
in citems.items():
73 pitems[p.port.ptr()]=p
75 pitems[p.port.ptr()]=p
77 for pout,pin
in self.node.getSetOfInternalLinks():
78 if pout.getNode().getFather() != self.
node and pin.getNode().getFather() != self.
node:
80 po=pitems.get(pout.ptr())
81 pi=pitems.get(pin.ptr())
86 itemup=citems[n.ptr()]
87 for ndown
in n.getOutNodes():
88 itemdown=citems[ndown.ptr()]
94 print "graph.addLink",link
96 nodeS=self.
citems[link.pout.getNode().ptr()]
97 nodeE=self.
citems[link.pin.getNode().ptr()]
99 for p
in nodeS.outports:
100 if p.port == link.pout:
103 for p
in nodeE.inports:
104 if p.port == link.pin:
115 self.
citems[item.node.ptr()]=node
121 self.editor.selectItem(item)
124 """Compute graph layout with graphviz package"""
125 G=pygraphviz.AGraph(strict=
False,directed=
True)
126 G.graph_attr[
"rankdir"]=rankdir
127 G.graph_attr[
"dpi"]=
"72"
130 for k,n
in self.citems.items():
135 for pout,pin
in self.node.getSetOfInternalLinks():
136 if pout.getNode().ptr()
not in self.
citems :
138 if pin.getNode().ptr()
not in self.
citems:
140 G.add_edge(pout.getNode().ptr(),pin.getNode().ptr())
142 for k,n
in self.citems.items():
143 for ndown
in n.node.getOutNodes():
144 G.add_edge(n.node.ptr(),ndown.ptr())
151 n.attr[
'height']=str(h)
152 n.attr[
'width']=str(w)
153 n.attr[
'fixedsize']=
"true"
154 n.attr[
'shape']=
"box"
161 graph_attr=dict(
attrs(G))
162 bbox=graph_attr[
"bb"]
163 x1,y1,x2,y2=eval(bbox)
164 h=self.canvas.height()
165 w=self.canvas.width()
169 self.canvas.resize(w2,h2)
186 items=self.citems.values()
188 for port
in node.outports:
189 if not hasattr(port,
"links"):
191 for link
in port.links():
196 items=self.citems.values()
199 for port
in node.outports:
200 if not hasattr(port,
"links"):
202 for link
in port.links():
210 x0,y0=port.x()+5,port.y()
211 while g.get((x0,y0)).blocked:
213 x1,y1=link.toPort.x()-5,link.toPort.y()
214 while g.get((x1,y1)).blocked:
216 path=g.findPath((x0,y0),(x1,y1))
219 if port.y() == link.toPort.y():
224 path=[(x,port.y()),(x,link.toPort.y())]
230 path=[(x1,port.y()),(x1,link.toPort.y())]
233 if port.y() == link.toPort.y():
239 path=[(x,port.y()),(x,link.toPort.y())]
256 path[-1]=x0,link.toPort.y()
262 link.splitLine(line,x,y)
268 ah=gv.agnxtattr(g.handle,t,ah)
269 value=gv.agxget(g.handle,ah)
270 yield gv.agattrname(ah),value
272 def h(x,y,destx,desty):
273 return abs(destx-x)+abs(desty-y)
278 d= abs(x1-x)+abs(y1-y)
279 if node.parent !=
None:
280 x0,y0=node.parent.coord
281 if (x1-x)*(y0-y)-(y1-y)*(x0-x) != 0:
331 for w
in xrange(len(xs)-1):
334 for h
in xrange(len(ys)-1):
336 col.append(
node((x,y),(w,h)))
337 self.cols.append(col)
344 l1,l2=bisect.bisect_left(ys,y-ymargin),bisect.bisect_left(ys,y+h+ymargin)
345 i1,i2=bisect.bisect_left(xs,x-xmargin),bisect.bisect_left(xs,x+w+xmargin)
346 for c
in self.
cols[i1:i2]:
357 n.total= n.path_cost=0
362 col= bisect.bisect_left(self.
xs,x)-1
363 if col < 0
or col >= len(self.
cols):
366 row=bisect.bisect_left(self.
ys,y)-1
367 if row < 0
or row >= len(col):
383 if (x1-x)*(y0-y)-(y1-y)*(x0-x) == 0:
385 path.append(node.coord)
392 steps=((0, +1), (+1, 0), (0, -1), (-1, 0 ))
396 if c < 0
or c >=len(self.
cols):
399 if r <0
or r >= len(co):
408 """Find shortest path from fromLoc to toLoc"""
411 fromNode=self.
get(fromLoc)
412 self.open.append((fromNode.total,fromNode))
413 toNode=self.
get(toLoc)
415 print "toNode is blocked"
417 destx,desty=toNode.coord
420 t,node=self.open.pop(0)
430 path_cost=node.path_cost+
distance(node,new_node)
431 total=path_cost+
h(x,y,destx,desty)
434 if total < new_node.total:
435 self.open.remove((new_node.total,new_node))
436 new_node.path_cost=path_cost
439 bisect.insort(self.
open,(new_node.total,new_node))
442 new_node.path_cost=path_cost
445 bisect.insort(self.
open,(new_node.total,new_node))