Version: 8.3.0
gui.graph.Graph Class Reference

Public Member Functions

def __init__
 
def createGraph
 
def addLink
 
def addItem
 
def selectItem
 
def layout
 
def clearLinks
 
def orthoLinks
 

Public Attributes

 parent
 
 item
 
 node
 
 canvas
 
 editor
 
 citems
 

Detailed Description

Definition at line 40 of file graph.py.

Constructor & Destructor Documentation

def gui.graph.Graph.__init__ (   self,
  item,
  parent 
)

Definition at line 41 of file graph.py.

41 
42  def __init__(self,item,parent):
43  self.parent=parent
44  self.item=item
45  self.node=item.node
46  #initial canvas size : 1000x1000
47  self.canvas=MyCanvas(1000,1000)
48  self.editor=GraphViewer(self.canvas,parent,"example",0)
49  self.createGraph()
50  root=self.node.getRootNode()
51  rootItem=Item.adapt(root)
52  CONNECTOR.Connect(rootItem,"selected",self.selectItem,())
53  CONNECTOR.Connect(self.item,"add",self.addItem,())
54  CONNECTOR.Connect(self.item.datalinks,"add",self.addLink,())

Member Function Documentation

def gui.graph.Graph.addItem (   self,
  item 
)

Definition at line 112 of file graph.py.

References gui.graph.Graph.canvas, gui.CItems.LinkItem.canvas, and gui.graph.Graph.citems.

113  def addItem(self,item):
114  #print "graph.addItem",item
115  node=CItems.Cell(item.node,self.canvas)
116  self.citems[item.node.ptr()]=node
117  node.show()
118  self.canvas.update()
def gui.graph.Graph.addLink (   self,
  link 
)

Definition at line 93 of file graph.py.

References gui.graph.Graph.canvas, gui.CItems.LinkItem.canvas, and gui.graph.Graph.citems.

93 
94  def addLink(self,link):
95  print "graph.addLink",link
96  #CItem for outport
97  nodeS=self.citems[link.pout.getNode().ptr()]
98  nodeE=self.citems[link.pin.getNode().ptr()]
99  po=pi=None
100  for p in nodeS.outports:
101  if p.port == link.pout:
102  po=p
103  break
104  for p in nodeE.inports:
105  if p.port == link.pin:
106  pi=p
107  break
108 
109  if pi and po:
110  l=CItems.LinkItem(po,pi,self.canvas)
111  self.canvas.update()
def gui.graph.Graph.clearLinks (   self)

Definition at line 185 of file graph.py.

186  def clearLinks(self):
187  items=self.citems.values()
188  for node in items:
189  for port in node.outports:
190  if not hasattr(port,"links"):
191  continue
192  for link in port.links():
193  link.clearPoints()
194  self.canvas.update()
def gui.graph.Graph.createGraph (   self)

Definition at line 55 of file graph.py.

55 
56  def createGraph(self):
57  #citems dict helps finding items in canvas from swig proxy
58  #To find an item node make : citems[node.ptr()]
59  citems={}
60  self.citems=citems
61  #pitems dict helps finding items in canvas from swig proxy
62  #To find an item port make : pitems[port.ptr()]
63  pitems={}
64 
65  y=0
66  lnode=self.node.edGetDirectDescendants()
67  for n in lnode:
68  c=CItems.Cell(n,self.canvas)
69  citems[n.ptr()]=c
70  c.show()
71 
72  for k,n in citems.items():
73  for p in n.inports:
74  pitems[p.port.ptr()]=p
75  for p in n.outports:
76  pitems[p.port.ptr()]=p
77 
78  for pout,pin in self.node.getSetOfInternalLinks():
79  if pout.getNode().getFather() != self.node and pin.getNode().getFather() != self.node:
80  continue
81  po=pitems.get(pout.ptr())
82  pi=pitems.get(pin.ptr())
83  if pi and po:
84  CItems.LinkItem(po,pi,self.canvas)
85 
86  for n in lnode:
87  itemup=citems[n.ptr()]
88  for ndown in n.getOutNodes():
89  itemdown=citems[ndown.ptr()]
90  CItems.ControlLinkItem(itemup.outgate,itemdown.ingate,self.canvas)
91 
92  self.layout("LR")
def gui.graph.Graph.layout (   self,
  rankdir 
)
Compute graph layout with graphviz package

Definition at line 123 of file graph.py.

References gui.graph.attrs(), and gui.graph.Graph.citems.

124  def layout(self,rankdir):
125  """Compute graph layout with graphviz package"""
126  G=pygraphviz.AGraph(strict=False,directed=True)
127  G.graph_attr["rankdir"]=rankdir
128  G.graph_attr["dpi"]="72"
129  dpi=72.
130  aspect=dpi/72
131  for k,n in self.citems.items():
132  #k is node address (YACS)
133  #n is item in canvas
134  G.add_node(k)
135 
136  for pout,pin in self.node.getSetOfInternalLinks():
137  if pout.getNode().ptr() not in self.citems :
138  continue
139  if pin.getNode().ptr() not in self.citems:
140  continue
141  G.add_edge(pout.getNode().ptr(),pin.getNode().ptr())
142 
143  for k,n in self.citems.items():
144  for ndown in n.node.getOutNodes():
145  G.add_edge(n.node.ptr(),ndown.ptr())
146 
147  #By default graphviz uses 96.0 pixel per inch (dpi=96.0)
148  for n in G.nodes():
149  item=self.citems[int(n)]
150  h=item.height()/dpi #height in inch
151  w=item.width()/dpi #width in inch
152  n.attr['height']=str(h)
153  n.attr['width']=str(w)
154  n.attr['fixedsize']="true"
155  n.attr['shape']="box"
156  #n.attr['label']=item.node.getName()
157 
158  G.layout(prog='dot') # use dot
159  #G.write("layout.dot")
160  #G.draw("layout.png")
161 
162  graph_attr=dict(attrs(G))
163  bbox=graph_attr["bb"]
164  x1,y1,x2,y2=eval(bbox)
165  h=self.canvas.height()
166  w=self.canvas.width()
167  h2=max(h,y2-y1+100)
168  w2=max(w,x2-x1+100)
169  if h2 > h or w2 > w:
170  self.canvas.resize(w2,h2)
171 
172  for n in G:
173  pos=n.attr['pos'] #position is given in points (72 points par inch, so 1 point = dpi/72=1.34)
174  x,y=eval(pos)
175  x=aspect*x
176  y=aspect*y
177  item=self.citems[int(n)]
178  x0=item.x()
179  y0=item.y()
180  x=x-x0
181  y=y-y0
182  item.moveBy(x,y)
183 
184  self.canvas.update()
def gui.graph.Graph.orthoLinks (   self)

Definition at line 195 of file graph.py.

196  def orthoLinks(self):
197  items=self.citems.values()
198  g=grid(items)
199  for node in items:
200  for port in node.outports:
201  if not hasattr(port,"links"):
202  continue
203  for link in port.links():
204  #clear all intermediate points of the link
205  link.clearPoints()
206  #if isinstance(link,CItems.ControlLinkItem):
207  # print port.port.getNode().getName() +"->"+link.toPort.port.getNode().getName()
208  #else:
209  # print port.port.getNode().getName() +":"+port.port.getName()+"->"+link.toPort.port.getNode().getName()+":"+link.toPort.port.getName()
210  #print (port.x(),port.y()),(link.toPort.x(),link.toPort.y())
211  x0,y0=port.x()+5,port.y()
212  while g.get((x0,y0)).blocked:
213  x0=x0+1
214  x1,y1=link.toPort.x()-5,link.toPort.y()
215  while g.get((x1,y1)).blocked:
216  x1=x1-1
217  path=g.findPath((x0,y0),(x1,y1))
218  #print path
219  if len(path)==1:
220  if port.y() == link.toPort.y():
221  #near ports face to face
222  continue
223  else:
224  x,y=path[0]
225  path=[(x,port.y()),(x,link.toPort.y())]
226  elif len(path)==2:
227  x1,y1=path[0]
228  x2,y2=path[1]
229  if x1 == x2:
230  #vertical line
231  path=[(x1,port.y()),(x1,link.toPort.y())]
232  else:
233  #horizontal line
234  if port.y() == link.toPort.y():
235  #near ports face to face
236  continue
237  else:
238  #transform it into a vertical line
239  x=(x1+x2)/2
240  path=[(x,port.y()),(x,link.toPort.y())]
241 
242  #adjust the first point to the same y as port
243  x0,y0=path[0]
244  x1,y1=path[1]
245  if y0==y1:
246  #remove first point and adjust second one
247  del path[0]
248  x0=x1
249  path[0]=x0,port.y()
250  #adjust the last point to the same y as link.toPort
251  x0,y0=path[-1]
252  x1,y1=path[-2]
253  if y0==y1:
254  #remove last point and adjust new last one
255  del path[-1]
256  x0=x1
257  path[-1]=x0,link.toPort.y()
258  #print path
259 
260  #add intermediate points
261  for x,y in path:
262  line=link.lines[-1]
263  link.splitLine(line,x,y)
264  self.canvas.update()
def gui.graph.Graph.selectItem (   self,
  item 
)

Definition at line 119 of file graph.py.

Referenced by gui.Tree.Tree.additem().

120  def selectItem(self,item):
121  #print "graph.selectItem",item
122  self.editor.selectItem(item)

Member Data Documentation

gui.graph.Graph.canvas

Definition at line 46 of file graph.py.

Referenced by gui.graph.Graph.addItem(), gui.graph.Graph.addLink(), gui.GraphViewer.GraphViewer.clear(), gui.GraphViewer.GraphViewer.contentsMouseDoubleClickEvent(), gui.GraphViewer.GraphViewer.contentsMouseMoveEvent(), gui.GraphViewer.GraphViewer.contentsMousePressEvent(), gui.GraphViewer.InPortItem.link(), gui.CItems.InControlItem.link(), gui.GraphViewer.OutPortItem.link(), gui.CItems.OutControlItem.link(), gui.CItems.InPortItem.link(), gui.CItems.OutPortItem.link(), gui.CItems.Cell.pynotify(), and gui.GraphViewer.GraphViewer.selectItem().

gui.graph.Graph.citems

Definition at line 59 of file graph.py.

Referenced by gui.graph.Graph.addItem(), gui.graph.Graph.addLink(), and gui.graph.Graph.layout().

gui.graph.Graph.editor

Definition at line 47 of file graph.py.

gui.graph.Graph.item

Definition at line 43 of file graph.py.

gui.graph.Graph.node

Definition at line 44 of file graph.py.

gui.graph.Graph.parent

Definition at line 42 of file graph.py.


The documentation for this class was generated from the following file: