/[ascend]/trunk/pygtk/canvas/port.py
ViewVC logotype

Diff of /trunk/pygtk/canvas/port.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1784 by jpye, Sat May 31 08:34:00 2008 UTC revision 1785 by jpye, Sun Jun 1 03:41:26 2008 UTC
# Line 125  class Port(object): Line 125  class Port(object):
125    
126  class PointConstraint(Constraint):  class PointConstraint(Constraint):
127      """      """
128      Ensure two points are kept together      Ensure that point B is always kept on top of point A
129    
130      Attributes:      Attributes:
131      _p1: first point, defined by (x,y)      _A: first point, defined by (x,y)
132      _p2: second point, defined by (x,y)      _B: second point, defined by (x,y)
133      """      """
134    
135      def __init__(self, p1, p2):      def __init__(self, A, B):
136          print "p1 =",p1          print "A =",A
137          print "p1[0] =",p1[0].variable()          print "A[0] =",A[0].variable()
138          print "p1[0].strength =",p1[0].variable().strength          print "A[0].strength =",A[0].variable().strength
139    
140          print "p2 =",p2          print "B =",B
141          print "p2[0] =",p2[0].variable()          print "B[0] =",B[0].variable()
142          print "p2[0].strength =",p2[0].variable().strength          print "B[0].strength =",B[0].variable().strength
143    
144  #        assert isinstance(p1[0],Variable)  #        assert isinstance(p1[0],Variable)
145  #        assert isinstance(p1[1],Variable)  #        assert isinstance(p1[1],Variable)
146  #        assert isinstance(p2[0],Variable)  #        assert isinstance(p2[0],Variable)
147  #        assert isinstance(p2[1],Variable)  #        assert isinstance(p2[1],Variable)
148    
149          super(PointConstraint, self).__init__(p1[0],p1[1],p2[0],p2[1])          super(PointConstraint, self).__init__(A[0],A[1],B[0],B[1])
150          self.p1 = p1          self._A = A
151          self.p2 = p2          self._B = B
152    
153      def solve_for(self, var):      def solve_for(self, var=None):
154          print "Solving PointConstraint..."          print "Solving PointConstraint",self,"for var",var
155          match = {0:2,1:3,2:0,3:1}  
156          for k in match:          _update(self._B[0], self._A[0].value)
157              if var is self._variables[k]:          _update(self._B[1], self._A[1].value)
                 print "Updating variable %d to equal value of variable %d" % (k,match[k])  
                 _update(self._variables[match[k]], self._variables[k].value)  
                 return  
         raise AssertionError("wrong variable in solve_for")  
158    
159  class Block(Element):  class Block(Element):
160      """      """
# Line 284  class PortConnectingHandleTool(HandleToo Line 280  class PortConnectingHandleTool(HandleToo
280          glue_distance = 10          glue_distance = 10
281          glue_port = None          glue_port = None
282          glue_point = None          glue_point = None
283          print "Gluing..."            #print "Gluing..."  
284          for i in view.canvas.get_all_items():          for i in view.canvas.get_all_items():
285              if not hasattr(i,'ports'):              if not hasattr(i,'ports'):
286                  continue                  continue
287              if not i is item:              if not i is item:
288                  print "Trying glue to",i                  #print "Trying glue to",i
289                  v2i = view.get_matrix_v2i(i).transform_point                  v2i = view.get_matrix_v2i(i).transform_point
290                  ix, iy = v2i(wx, wy)                  ix, iy = v2i(wx, wy)
291                  distance, port = i.glue(item, handle, ix, iy)                  distance, port = i.glue(item, handle, ix, iy)
# Line 305  class PortConnectingHandleTool(HandleToo Line 301  class PortConnectingHandleTool(HandleToo
301          if glue_point:          if glue_point:
302              v2i = view.get_matrix_v2i(item).transform_point              v2i = view.get_matrix_v2i(item).transform_point
303              handle.x, handle.y = v2i(*glue_point)              handle.x, handle.y = v2i(*glue_point)
304              print "Found glue point ",handle.x,handle.y              #print "Found glue point ",handle.x,handle.y
305          return glue_port          return glue_port
306    
307      def connect(self, view, item, handle, wx, wy):      def connect(self, view, item, handle, wx, wy):
308          """          """
309          Connect a handle to another item.          Connect a handle to a port. 'item' is the line to which the handle
310            belongs; wx and wy are the location of the cursor, so we run the 'glue'
311            routine to find the desired gluing point, then make the connection to
312            the object which 'glue' returns, which will be a Port object (in the
313            context of this tool).
314    
315          In this "method" the following assumptios are made:          In this "method" the following assumptios are made:
316           1. The only item that accepts handle connections are the Box instances           1. Only ``Port``s of ``Block``s will accept connections from handles.
317           2. The only items with connectable handles are ``Line``s           2. The only items with connectable handles are ``Line``s
318                    
319          """          """
# Line 359  class PortConnectingHandleTool(HandleToo Line 359  class PortConnectingHandleTool(HandleToo
359                  print "glue_port =",glue_port                  print "glue_port =",glue_port
360                  print "glue_port.pos = ",glue_port.pos                  print "glue_port.pos = ",glue_port.pos
361                  print "glue_port.block =",glue_port.block                  print "glue_port.block =",glue_port.block
                 p2 = CanvasProjection(glue_port.pos, glue_port.block)      
                 print "connect: p2 =",p2  
                 print "connect: p2[0] =",p2[0].variable()  
                 print "connect: p2[0].strength =",p2[0].variable().strength  
362    
363                  handle.connection_data = PointConstraint(                  handle.connection_data = PointConstraint(
364                      p1=CanvasProjection(handle.pos,item)                      B=CanvasProjection(handle.pos,item)
365                      ,p2=p2                      ,A=CanvasProjection(glue_port.pos, glue_port.block)
366                  )                  )
367                  view.canvas.solver.add_constraint(handle.connection_data)                  view.canvas.solver.add_constraint(handle.connection_data)
368                    #glue_port.block._constraints.append(handle.connection_data)
369    
370                  handle.connected_to = glue_port                  handle.connected_to = glue_port
371                  handle.disconnect = handle_disconnect                  handle.disconnect = handle_disconnect
372                  glue_port.connectable = False                  glue_port.connectable = False

Legend:
Removed from v.1784  
changed lines
  Added in v.1785

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22