1 |
jpye |
1777 |
import pygtk |
2 |
|
|
pygtk.require('2.0') |
3 |
|
|
|
4 |
|
|
import math |
5 |
|
|
import gtk |
6 |
|
|
import cairo |
7 |
|
|
|
8 |
jpye |
1775 |
from gaphas.item import Item |
9 |
jpye |
1783 |
from gaphas.state import observed, reversible_property, disable_dispatching |
10 |
jpye |
1775 |
from gaphas.tool import HandleTool |
11 |
|
|
|
12 |
jpye |
1942 |
from gaphas import GtkView, View |
13 |
jpye |
1945 |
|
14 |
|
|
from gaphas.solver import Variable, solvable, WEAK, NORMAL, STRONG, VERY_STRONG |
15 |
jpye |
1781 |
from gaphas.constraint import LineConstraint, LessThanConstraint, EqualsConstraint, Constraint, _update, BalanceConstraint |
16 |
jpye |
1777 |
from gaphas.canvas import CanvasProjection |
17 |
|
|
|
18 |
|
|
#------------------------------------------------------------------------------ |
19 |
|
|
|
20 |
jpye |
1775 |
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 |
jpye |
1945 |
A subclass of Item called 'BlockItem' will be given the ability to store |
26 |
jpye |
1775 |
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 |
jpye |
1945 |
BlockItem's local coordinate system. |
30 |
jpye |
1775 |
|
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 |
jpye |
1945 |
- block: BlockItem to which Port belongs |
41 |
jpye |
1788 |
- 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 |
jpye |
1775 |
|
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 |
jpye |
1788 |
- _connected_to: Handle to which port is connected, if any |
49 |
jpye |
1775 |
""" |
50 |
|
|
|
51 |
jpye |
1783 |
_x = solvable() |
52 |
|
|
_y = solvable() |
53 |
jpye |
1779 |
|
54 |
jpye |
1783 |
def __init__(self, block, strength = STRONG): |
55 |
jpye |
1775 |
self.block = block |
56 |
jpye |
1783 |
self._x.strength = strength |
57 |
|
|
self._y.strength = strength |
58 |
jpye |
1788 |
self._connected_to = None |
59 |
jpye |
1775 |
|
60 |
|
|
@observed |
61 |
jpye |
1783 |
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 |
jpye |
1788 |
def _set_connected_to(self, connected_to): |
76 |
|
|
self._connected_to = connected_to |
77 |
jpye |
1775 |
|
78 |
jpye |
1788 |
connected_to = reversible_property(lambda s: s._connected_to, |
79 |
|
|
_set_connected_to) |
80 |
jpye |
1775 |
|
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 |
jpye |
1778 |
@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 |
jpye |
1785 |
Ensure that point B is always kept on top of point A |
114 |
jpye |
1778 |
|
115 |
|
|
Attributes: |
116 |
jpye |
1785 |
_A: first point, defined by (x,y) |
117 |
|
|
_B: second point, defined by (x,y) |
118 |
jpye |
1778 |
""" |
119 |
|
|
|
120 |
jpye |
1785 |
def __init__(self, A, B): |
121 |
jpye |
1945 |
#print "A =",A |
122 |
|
|
#print "A[0] =",A[0].variable() |
123 |
|
|
#print "A[0].strength =",A[0].variable().strength |
124 |
jpye |
1779 |
|
125 |
jpye |
1945 |
#print "B =",B |
126 |
|
|
#print "B[0] =",B[0].variable() |
127 |
|
|
#print "B[0].strength =",B[0].variable().strength |
128 |
jpye |
1779 |
|
129 |
jpye |
1778 |
# 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 |
jpye |
1785 |
super(PointConstraint, self).__init__(A[0],A[1],B[0],B[1]) |
135 |
|
|
self._A = A |
136 |
|
|
self._B = B |
137 |
jpye |
1778 |
|
138 |
jpye |
1785 |
def solve_for(self, var=None): |
139 |
jpye |
1945 |
#print "Solving PointConstraint",self,"for var",var |
140 |
jpye |
1778 |
|
141 |
jpye |
1785 |
_update(self._B[0], self._A[0].value) |
142 |
|
|
_update(self._B[1], self._A[1].value) |
143 |
|
|
|
144 |
jpye |
1775 |
# vim: sw=4:et:ai |