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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1945 - (show annotations) (download) (as text)
Thu Nov 6 08:54:02 2008 UTC (13 years, 7 months ago) by jpye
File MIME type: text/x-python
File size: 4638 byte(s)
Refactor some of this into separate files.
Support for renaming of blocks.
Added proper BlockInstance class to hold block names.
Automatic sequential default numbering of new blocks.
1 import pygtk
2 pygtk.require('2.0')
3
4 import math
5 import gtk
6 import cairo
7
8 from gaphas.item import Item
9 from gaphas.state import observed, reversible_property, disable_dispatching
10 from gaphas.tool import HandleTool
11
12 from gaphas import GtkView, View
13
14 from gaphas.solver import Variable, solvable, WEAK, NORMAL, STRONG, VERY_STRONG
15 from gaphas.constraint import LineConstraint, LessThanConstraint, EqualsConstraint, Constraint, _update, BalanceConstraint
16 from gaphas.canvas import CanvasProjection
17
18 #------------------------------------------------------------------------------
19
20 class Port(object):
21 """
22 Ports are special places onto which Handles can be connected, specifically
23 when users are drawing 'connector' lines between modelling 'blocks'.
24
25 A subclass of Item called 'BlockItem' will be given the ability to store
26 an array of these Ports, in such a way that gluing methods will be able to
27 select appropriate ports for a given connector line. The Ports also give
28 data about where these available connected are located graphically, in the
29 BlockItem's local coordinate system.
30
31 It is not intended that the number or location of ports would be editable
32 once a port is instantiated. Only the connection of a port to a handle
33 is editable by the user.
34
35 It is intended that subclasses to the Port class would be created to provide
36 finer control over whether or not a certain handle can be permitted to
37 connect to any given port.
38
39 Attributes:
40 - block: BlockItem to which Port belongs
41 - connected_to: Handle to which port is connected, if any
42 - x: port x-location in item's local coordinate system
43 - y: port y-location in item's local coordinate system
44
45 Private:
46 - _x: port x-location in item's local coordinate system
47 - _y: port y-location in item's local coordinate system
48 - _connected_to: Handle to which port is connected, if any
49 """
50
51 _x = solvable()
52 _y = solvable()
53
54 def __init__(self, block, strength = STRONG):
55 self.block = block
56 self._x.strength = strength
57 self._y.strength = strength
58 self._connected_to = None
59
60 @observed
61 def _set_x(self, x):
62 self._x = x
63
64 x = reversible_property(lambda s: s._x, _set_x, bind={'x': lambda self: float(self.x) })
65 disable_dispatching(_set_x)
66
67 @observed
68 def _set_y(self, y):
69 self._y = y
70
71 y = reversible_property(lambda s: s._y, _set_y, bind={'y': lambda self: float(self.y) })
72 disable_dispatching(_set_y)
73
74 @observed
75 def _set_connected_to(self, connected_to):
76 self._connected_to = connected_to
77
78 connected_to = reversible_property(lambda s: s._connected_to,
79 _set_connected_to)
80
81 def draw(self, context):
82 """
83 Render the item to a canvas view.
84 Context contains the following attributes:
85
86 - cairo: the Cairo Context use this one to draw
87 - view: the view that is to be rendered to
88 - selected, focused, hovered, dropzone: view state of items (True/False)
89 - draw_all: a request to draw everything, for bounding box calculation
90 """
91 pass
92
93 def point(self, x, y):
94 """
95 Get the distance from a point (``x``, ``y``) to the item.
96 ``x`` and ``y`` are in item coordinates.
97
98 Defined here because a port is just a 'point' at this stage.
99 """
100 return math.sqrt((x-self.x)**2 + (y-self.y)**2)
101
102 @observed
103 def _set_pos(self, pos):
104 """
105 Set handle position (Item coordinates).
106 """
107 self.x, self.y = pos
108
109 pos = property(lambda s: (s.x, s.y), _set_pos)
110
111 class PointConstraint(Constraint):
112 """
113 Ensure that point B is always kept on top of point A
114
115 Attributes:
116 _A: first point, defined by (x,y)
117 _B: second point, defined by (x,y)
118 """
119
120 def __init__(self, A, B):
121 #print "A =",A
122 #print "A[0] =",A[0].variable()
123 #print "A[0].strength =",A[0].variable().strength
124
125 #print "B =",B
126 #print "B[0] =",B[0].variable()
127 #print "B[0].strength =",B[0].variable().strength
128
129 # assert isinstance(p1[0],Variable)
130 # assert isinstance(p1[1],Variable)
131 # assert isinstance(p2[0],Variable)
132 # assert isinstance(p2[1],Variable)
133
134 super(PointConstraint, self).__init__(A[0],A[1],B[0],B[1])
135 self._A = A
136 self._B = B
137
138 def solve_for(self, var=None):
139 #print "Solving PointConstraint",self,"for var",var
140
141 _update(self._B[0], self._A[0].value)
142 _update(self._B[1], self._A[1].value)
143
144 # vim: sw=4:et:ai

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