author | lbessard |
Tue, 10 Jul 2007 19:19:54 +0200 | |
changeset 34 | ad3fa2752106 |
parent 32 | cf9efccff009 |
child 42 | 4a8400732001 |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C): Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
5 | 12 |
#modify it under the terms of the GNU General Public |
0 | 13 |
#License as published by the Free Software Foundation; either |
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#Lesser General Public License for more details. |
|
20 |
# |
|
5 | 21 |
#You should have received a copy of the GNU General Public |
0 | 22 |
#License along with this library; if not, write to the Free Software |
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
from wxPython.wx import * |
|
26 |
import wx |
|
27 |
||
28 |
from GraphicCommons import * |
|
29 |
from plcopen.structures import * |
|
30 |
||
31 |
||
32 |
#------------------------------------------------------------------------------- |
|
33 |
# Function Block Diagram Block |
|
34 |
#------------------------------------------------------------------------------- |
|
35 |
||
36 |
""" |
|
37 |
Class that implements the graphic representation of a function block |
|
38 |
""" |
|
39 |
||
40 |
class FBD_Block(Graphic_Element): |
|
41 |
||
42 |
# Create a new block |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
43 |
def __init__(self, parent, type, name, id = None, extension = 0, inputs = None): |
0 | 44 |
Graphic_Element.__init__(self, parent) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
45 |
self.Type = None |
27 | 46 |
self.Extension = None |
0 | 47 |
self.Name = name |
48 |
self.Id = id |
|
2 | 49 |
self.Inputs = [] |
50 |
self.Outputs = [] |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
51 |
self.SetType(type, extension, inputs) |
0 | 52 |
|
53 |
# Destructor |
|
54 |
def __del__(self): |
|
55 |
self.Inputs = [] |
|
56 |
self.Outputs = [] |
|
57 |
||
58 |
# Delete this block by calling the appropriate method |
|
59 |
def Delete(self): |
|
60 |
self.Parent.DeleteBlock(self) |
|
61 |
||
62 |
# Unconnect all inputs and outputs |
|
63 |
def Clean(self): |
|
64 |
for input in self.Inputs: |
|
2 | 65 |
input.UnConnect(delete = True) |
66 |
for output in self.Outputs: |
|
67 |
output.UnConnect(delete = True) |
|
0 | 68 |
|
69 |
# Refresh the block bounding box |
|
70 |
def RefreshBoundingBox(self): |
|
71 |
dc = wxClientDC(self.Parent) |
|
72 |
# Calculate the size of the name outside the block |
|
73 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
74 |
# Calculate the bounding box size |
|
75 |
bbx_x = self.Pos.x - max(min(1, len(self.Inputs)) * CONNECTOR_SIZE, (text_width - self.Size[0]) / 2) |
|
76 |
bbx_width = self.Size[0] + 1 + (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE |
|
77 |
if self.Name != "": |
|
78 |
bbx_y = self.Pos.y - (text_height + 2) |
|
79 |
bbx_height = self.Size[1] + (text_height + 2) |
|
80 |
else: |
|
81 |
bbx_y = self.Pos.y |
|
82 |
bbx_height = self.Size[1] |
|
83 |
self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
|
84 |
||
85 |
# Refresh the positions of the block connectors |
|
86 |
def RefreshConnectors(self): |
|
87 |
# Calculate the size for the connector lines |
|
88 |
lines = max(len(self.Inputs), len(self.Outputs)) |
|
89 |
linesize = max((self.Size[1] - BLOCK_LINE_SIZE) / lines, BLOCK_LINE_SIZE) |
|
90 |
# Update inputs positions |
|
91 |
position = BLOCK_LINE_SIZE + linesize / 2 |
|
92 |
for input in self.Inputs: |
|
93 |
input.SetPosition(wxPoint(0, position)) |
|
94 |
position += linesize |
|
95 |
# Update outputs positions |
|
96 |
position = BLOCK_LINE_SIZE + linesize / 2 |
|
97 |
for output in self.Outputs: |
|
98 |
output.SetPosition(wxPoint(self.Size[0], position)) |
|
99 |
position += linesize |
|
100 |
self.RefreshConnected() |
|
101 |
||
102 |
# Refresh the positions of wires connected to inputs and outputs |
|
103 |
def RefreshConnected(self, exclude = []): |
|
104 |
for input in self.Inputs: |
|
105 |
input.MoveConnected(exclude) |
|
106 |
for output in self.Outputs: |
|
107 |
output.MoveConnected(exclude) |
|
108 |
||
109 |
# Returns the block connector that starts with the point given if it exists |
|
27 | 110 |
def GetConnector(self, position, name = None): |
111 |
# if a name is given |
|
112 |
if name: |
|
113 |
# Test each input and output connector |
|
114 |
for input in self.Inputs: |
|
115 |
if name == input.GetName(): |
|
116 |
return input |
|
117 |
for output in self.Outputs: |
|
118 |
if name == output.GetName(): |
|
119 |
return output |
|
0 | 120 |
# Test each input connector |
121 |
for input in self.Inputs: |
|
122 |
input_pos = input.GetRelPosition() |
|
123 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
124 |
return input |
|
125 |
# Test each output connector |
|
126 |
for output in self.Outputs: |
|
127 |
output_pos = output.GetRelPosition() |
|
128 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
129 |
return output |
|
130 |
return None |
|
131 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
132 |
def GetInputTypes(self): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
133 |
return tuple([input.GetType() for input in self.Inputs]) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
134 |
|
0 | 135 |
# Returns all the block connectors |
136 |
def GetConnectors(self): |
|
137 |
return {"inputs" : self.Inputs, "outputs" : self.Outputs} |
|
138 |
||
139 |
# Test if point given is on one of the block connectors |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
140 |
def TestConnector(self, pt, exclude = True): |
0 | 141 |
# Test each input connector |
142 |
for input in self.Inputs: |
|
143 |
if input.TestPoint(pt, exclude): |
|
144 |
return input |
|
145 |
# Test each output connector |
|
146 |
for output in self.Outputs: |
|
147 |
if output.TestPoint(pt, exclude): |
|
148 |
return output |
|
149 |
return None |
|
150 |
||
2 | 151 |
# Changes the block type |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
152 |
def SetType(self, type, extension, inputs = None): |
27 | 153 |
if type != self.Type or self.Extension != extension: |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
154 |
self.Type = type |
27 | 155 |
self.Extension = extension |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
156 |
# Find the block definition from type given and create the corresponding |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
157 |
# inputs and outputs |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
158 |
blocktype = GetBlockType(type, inputs) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
159 |
if blocktype: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
160 |
inputs = [input for input in blocktype["inputs"]] |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
161 |
outputs = [output for output in blocktype["outputs"]] |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
162 |
if blocktype["extensible"]: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
163 |
start = int(inputs[-1][0].replace("IN", "")) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
164 |
for i in xrange(self.Extension - len(blocktype["inputs"])): |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
165 |
start += 1 |
27 | 166 |
inputs.append(("IN%d"%start, inputs[-1][1], inputs[-1][2])) |
16 | 167 |
else: |
168 |
raise ValueError, "This block type isn't defined" |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
169 |
self.Clean() |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
170 |
# Extract the inputs properties and create the corresponding connector |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
171 |
self.Inputs = [] |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
172 |
for input_name, input_type, input_modifier in inputs: |
27 | 173 |
connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST, onlyone = True) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
174 |
if input_modifier == "negated": |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
175 |
connector.SetNegated(True) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
176 |
elif input_modifier != "none": |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
177 |
connector.SetEdge(input_modifier) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
178 |
self.Inputs.append(connector) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
179 |
# Extract the outputs properties and create the corresponding connector |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
180 |
self.Outputs = [] |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
181 |
for output_name, output_type, output_modifier in outputs: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
182 |
connector = Connector(self, output_name, output_type, wxPoint(0, 0), EAST) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
183 |
if output_modifier == "negated": |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
184 |
connector.SetNegated(True) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
185 |
elif output_modifier != "none": |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
186 |
connector.SetEdge(output_modifier) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
187 |
self.Outputs.append(connector) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
188 |
self.RefreshConnectors() |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
189 |
self.RefreshBoundingBox() |
2 | 190 |
|
0 | 191 |
# Returns the block type |
192 |
def GetType(self): |
|
193 |
return self.Type |
|
194 |
||
195 |
# Changes the block name |
|
196 |
def SetName(self, name): |
|
197 |
self.Name = name |
|
198 |
||
199 |
# Returs the block name |
|
200 |
def GetName(self): |
|
201 |
return self.Name |
|
202 |
||
2 | 203 |
# Changes the extension name |
204 |
def SetExtension(self, extension): |
|
205 |
self.Extension = extension |
|
206 |
||
207 |
# Returs the extension name |
|
208 |
def GetExtension(self): |
|
209 |
return self.Extension |
|
210 |
||
0 | 211 |
# Returns the block minimum size |
212 |
def GetMinSize(self): |
|
213 |
dc = wxClientDC(self.Parent) |
|
214 |
text_width, text_height = dc.GetTextExtent(self.Type) |
|
215 |
# Calculate the inputs maximum width |
|
216 |
max_input = 0 |
|
217 |
for input in self.Inputs: |
|
218 |
w, h = dc.GetTextExtent(input.GetName()) |
|
219 |
max_input = max(max_input, w) |
|
220 |
# Calculate the outputs maximum width |
|
221 |
max_output = 0 |
|
222 |
for output in self.Outputs: |
|
223 |
w, h = dc.GetTextExtent(output.GetName()) |
|
224 |
max_output = max(max_output, w) |
|
225 |
width = max(text_width + 10, max_input + max_output + 15) |
|
226 |
height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE |
|
227 |
return width, height |
|
228 |
||
229 |
# Changes the negated property of the connector handled |
|
230 |
def SetConnectorNegated(self, negated): |
|
231 |
handle_type, handle = self.Handle |
|
232 |
if handle_type == HANDLE_CONNECTOR: |
|
233 |
handle.SetNegated(negated) |
|
234 |
self.RefreshModel(False) |
|
235 |
||
236 |
# Changes the edge property of the connector handled |
|
237 |
def SetConnectorEdge(self, edge): |
|
238 |
handle_type, handle = self.Handle |
|
239 |
if handle_type == HANDLE_CONNECTOR: |
|
240 |
handle.SetEdge(edge) |
|
241 |
self.RefreshModel(False) |
|
242 |
||
2 | 243 |
# Method called when a LeftDClick event have been generated |
27 | 244 |
def OnLeftDClick(self, event, dc, scaling): |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
245 |
# Edit the block properties |
2 | 246 |
self.Parent.EditBlockContent(self) |
247 |
||
0 | 248 |
# Method called when a RightUp event have been generated |
27 | 249 |
def OnRightUp(self, event, dc, scaling): |
250 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 251 |
# Popup the menu with special items for a block and a connector if one is handled |
252 |
connector = self.TestConnector(pos, False) |
|
253 |
if connector: |
|
254 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
255 |
self.Parent.PopupBlockMenu(connector) |
|
256 |
else: |
|
257 |
self.Parent.PopupBlockMenu() |
|
258 |
||
259 |
# Refreshes the block model |
|
260 |
def RefreshModel(self, move=True): |
|
261 |
self.Parent.RefreshBlockModel(self) |
|
262 |
# If block has moved, refresh the model of wires connected to outputs |
|
263 |
if move: |
|
264 |
for output in self.Outputs: |
|
265 |
output.RefreshWires() |
|
266 |
||
267 |
# Draws block |
|
268 |
def Draw(self, dc): |
|
269 |
dc.SetPen(wxBLACK_PEN) |
|
270 |
dc.SetBrush(wxWHITE_BRUSH) |
|
271 |
# Draw a rectangle with the block size |
|
272 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
273 |
# Draw block name and block type |
|
274 |
namewidth, nameheight = dc.GetTextExtent(self.Name) |
|
275 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, |
|
276 |
self.Pos.y - (nameheight + 2)) |
|
277 |
typewidth, typeheight = dc.GetTextExtent(self.Type) |
|
278 |
dc.DrawText(self.Type, self.Pos.x + (self.Size[0] - typewidth) / 2, |
|
279 |
self.Pos.y + 5) |
|
280 |
# Draw inputs and outputs connectors |
|
281 |
for input in self.Inputs: |
|
282 |
input.Draw(dc) |
|
283 |
for output in self.Outputs: |
|
284 |
output.Draw(dc) |
|
285 |
Graphic_Element.Draw(self, dc) |
|
286 |
||
287 |
||
288 |
#------------------------------------------------------------------------------- |
|
289 |
# Function Block Diagram Variable |
|
290 |
#------------------------------------------------------------------------------- |
|
291 |
||
292 |
""" |
|
293 |
Class that implements the graphic representation of a variable |
|
294 |
""" |
|
295 |
||
296 |
class FBD_Variable(Graphic_Element): |
|
297 |
||
298 |
# Create a new variable |
|
299 |
def __init__(self, parent, type, name, value_type, id = None): |
|
300 |
Graphic_Element.__init__(self, parent) |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
301 |
self.Type = None |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
302 |
self.ValueType = None |
0 | 303 |
self.Name = name |
304 |
self.Id = id |
|
305 |
self.Input = None |
|
306 |
self.Output = None |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
307 |
self.SetType(type, value_type) |
0 | 308 |
|
309 |
# Destructor |
|
310 |
def __del__(self): |
|
311 |
self.Input = None |
|
312 |
self.Output = None |
|
313 |
||
314 |
# Unconnect connector |
|
315 |
def Clean(self): |
|
316 |
if self.Input: |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
317 |
self.Input.UnConnect(delete = True) |
0 | 318 |
if self.Output: |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
319 |
self.Output.UnConnect(delete = True) |
0 | 320 |
|
321 |
# Delete this variable by calling the appropriate method |
|
322 |
def Delete(self): |
|
323 |
self.Parent.DeleteVariable(self) |
|
324 |
||
325 |
# Refresh the variable bounding box |
|
326 |
def RefreshBoundingBox(self): |
|
327 |
dc = wxClientDC(self.Parent) |
|
328 |
if self.Type in (OUTPUT, INOUT): |
|
329 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
330 |
else: |
|
331 |
bbx_x = self.Pos.x |
|
332 |
if self.Type == INOUT: |
|
333 |
bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE |
|
334 |
else: |
|
335 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
336 |
self.BoundingBox = wxRect(bbx_x, self.Pos.y, bbx_width + 1, self.Size[1] + 1) |
|
337 |
||
338 |
# Refresh the position of the variable connector |
|
339 |
def RefreshConnectors(self): |
|
340 |
if self.Input: |
|
341 |
self.Input.SetPosition(wxPoint(0, self.Size[1] / 2)) |
|
342 |
if self.Output: |
|
343 |
self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2)) |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
344 |
self.RefreshConnected() |
0 | 345 |
|
346 |
# Refresh the position of wires connected to connector |
|
347 |
def RefreshConnected(self, exclude = []): |
|
348 |
if self.Input: |
|
349 |
self.Input.MoveConnected(exclude) |
|
350 |
if self.Output: |
|
351 |
self.Output.MoveConnected(exclude) |
|
352 |
||
353 |
# Test if point given is on the variable connector |
|
354 |
def TestConnector(self, pt, exclude=True): |
|
355 |
if self.Input and self.Input.TestPoint(pt, exclude): |
|
356 |
return self.Input |
|
357 |
if self.Output and self.Output.TestPoint(pt, exclude): |
|
358 |
return self.Output |
|
359 |
return None |
|
360 |
||
361 |
# Returns the block connector that starts with the point given if it exists |
|
27 | 362 |
def GetConnector(self, position, name = None): |
363 |
# if a name is given |
|
364 |
if name: |
|
365 |
# Test input and output connector if they exists |
|
366 |
if self.Input and name == self.Input.GetName(): |
|
367 |
return self.Input |
|
368 |
if self.Output and name == self.Output.GetName(): |
|
369 |
return self.Output |
|
0 | 370 |
# Test input connector if it exists |
371 |
if self.Input: |
|
372 |
input_pos = self.Input.GetRelPosition() |
|
373 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
374 |
return self.Input |
|
375 |
# Test output connector if it exists |
|
376 |
if self.Output: |
|
377 |
output_pos = self.Output.GetRelPosition() |
|
378 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
379 |
return self.Output |
|
380 |
return None |
|
381 |
||
382 |
# Returns all the block connectors |
|
383 |
def GetConnectors(self): |
|
384 |
return {"input" : self.Input, "output" : self.Output} |
|
385 |
||
386 |
# Changes the negated property of the variable connector if handled |
|
387 |
def SetConnectorNegated(self, negated): |
|
388 |
handle_type, handle = self.Handle |
|
389 |
if handle_type == HANDLE_CONNECTOR: |
|
390 |
handle.SetNegated(negated) |
|
391 |
self.RefreshModel(False) |
|
392 |
||
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
393 |
# Changes the variable type |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
394 |
def SetType(self, type, value_type): |
32
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
395 |
if type != self.Type: |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
396 |
self.Type = type |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
397 |
self.Clean() |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
398 |
self.Input = None |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
399 |
self.Output = None |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
400 |
# Create an input or output connector according to variable type |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
401 |
if self.Type != INPUT: |
27 | 402 |
self.Input = Connector(self, "", value_type, wxPoint(0, 0), WEST, onlyone = True) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
403 |
if self.Type != OUTPUT: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
404 |
self.Output = Connector(self, "", value_type, wxPoint(0, 0), EAST) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
405 |
self.RefreshConnectors() |
32
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
406 |
elif value_type != self.ValueType: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
407 |
if self.Input: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
408 |
self.Input.SetType(value_type) |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
409 |
if self.Output: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
410 |
self.Output.SetType(value_type) |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
411 |
self.RefreshConnectors() |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
412 |
|
0 | 413 |
# Returns the variable type |
414 |
def GetType(self): |
|
415 |
return self.Type |
|
416 |
||
417 |
# Changes the variable name |
|
418 |
def SetName(self, name): |
|
419 |
self.Name = name |
|
420 |
||
421 |
# Returns the variable name |
|
422 |
def GetName(self): |
|
423 |
return self.Name |
|
424 |
||
425 |
# Returns the variable minimum size |
|
426 |
def GetMinSize(self): |
|
427 |
dc = wxClientDC(self.Parent) |
|
428 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
429 |
return text_width + 10, text_height + 10 |
|
430 |
||
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
431 |
# Method called when a LeftDClick event have been generated |
27 | 432 |
def OnLeftDClick(self, event, dc, scaling): |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
433 |
# Edit the variable properties |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
434 |
self.Parent.EditVariableContent(self) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
435 |
|
0 | 436 |
# Method called when a RightUp event have been generated |
27 | 437 |
def OnRightUp(self, event, dc, scaling): |
438 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 439 |
# Popup the menu with special items for a variable and a connector if it's handled |
27 | 440 |
connector = self.TestConnector(pos, False) |
0 | 441 |
if connector: |
442 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
443 |
self.Parent.PopupVariableMenu(connector) |
|
444 |
else: |
|
445 |
self.Parent.PopupVariableMenu() |
|
446 |
||
447 |
# Refreshes the variable model |
|
448 |
def RefreshModel(self, move=True): |
|
449 |
self.Parent.RefreshVariableModel(self) |
|
450 |
# If variable has moved and variable is not of type OUTPUT, refresh the model |
|
451 |
# of wires connected to output connector |
|
452 |
if move and self.Type != OUTPUT: |
|
453 |
if self.Output: |
|
454 |
self.Output.RefreshWires() |
|
455 |
||
456 |
# Draws variable |
|
457 |
def Draw(self, dc): |
|
458 |
dc.SetPen(wxBLACK_PEN) |
|
459 |
dc.SetBrush(wxWHITE_BRUSH) |
|
460 |
# Draw a rectangle with the variable size |
|
461 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
462 |
# Draw variable name |
|
463 |
namewidth, nameheight = dc.GetTextExtent(self.Name) |
|
464 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, |
|
465 |
self.Pos.y + (self.Size[1] - nameheight) / 2) |
|
466 |
# Draw connectors |
|
467 |
if self.Input: |
|
468 |
self.Input.Draw(dc) |
|
469 |
if self.Output: |
|
470 |
self.Output.Draw(dc) |
|
471 |
Graphic_Element.Draw(self, dc) |
|
472 |
||
473 |
||
474 |
#------------------------------------------------------------------------------- |
|
475 |
# Function Block Diagram Connector |
|
476 |
#------------------------------------------------------------------------------- |
|
477 |
||
478 |
""" |
|
479 |
Class that implements the graphic representation of a connection |
|
480 |
""" |
|
481 |
||
482 |
class FBD_Connector(Graphic_Element): |
|
483 |
||
484 |
# Create a new connection |
|
485 |
def __init__(self, parent, type, name, id = None): |
|
486 |
Graphic_Element.__init__(self, parent) |
|
487 |
self.Type = type |
|
488 |
self.Name = name |
|
489 |
self.Id = id |
|
490 |
self.Pos = wxPoint(0, 0) |
|
491 |
self.Size = wxSize(0, 0) |
|
492 |
# Create an input or output connector according to connection type |
|
493 |
if self.Type == CONNECTOR: |
|
27 | 494 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True) |
0 | 495 |
else: |
496 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST) |
|
497 |
self.RefreshConnectors() |
|
498 |
||
499 |
# Destructor |
|
500 |
def __del__(self): |
|
501 |
self.Connector = None |
|
502 |
||
503 |
# Unconnect connector |
|
504 |
def Clean(self): |
|
505 |
if self.Connector: |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
506 |
self.Connector.UnConnect(delete = True) |
0 | 507 |
|
508 |
# Delete this connection by calling the appropriate method |
|
509 |
def Delete(self): |
|
510 |
self.Parent.DeleteConnection(self) |
|
511 |
||
512 |
# Refresh the connection bounding box |
|
513 |
def RefreshBoundingBox(self): |
|
514 |
dc = wxClientDC(self.Parent) |
|
515 |
if self.Type == CONNECTOR: |
|
516 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
517 |
else: |
|
518 |
bbx_x = self.Pos.x |
|
519 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
520 |
self.BoundingBox = wxRect(bbx_x, self.Pos.y, bbx_width, self.Size[1]) |
|
521 |
||
522 |
# Refresh the position of the connection connector |
|
523 |
def RefreshConnectors(self): |
|
524 |
if self.Type == CONNECTOR: |
|
525 |
self.Connector.SetPosition(wxPoint(0, self.Size[1] / 2)) |
|
526 |
else: |
|
527 |
self.Connector.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2)) |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
528 |
self.RefreshConnected() |
0 | 529 |
|
530 |
# Refresh the position of wires connected to connector |
|
531 |
def RefreshConnected(self, exclude = []): |
|
532 |
if self.Connector: |
|
533 |
self.Connector.MoveConnected(exclude) |
|
534 |
||
535 |
# Test if point given is on the connection connector |
|
536 |
def TestConnector(self, pt, exclude=True): |
|
537 |
if self.Connector and self.Connector.TestPoint(pt, exclude): |
|
538 |
return self.Connector |
|
539 |
return None |
|
540 |
||
541 |
# Returns the connection connector |
|
27 | 542 |
def GetConnector(self, position = None, name = None): |
0 | 543 |
return self.Connector |
544 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
545 |
# Changes the variable type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
546 |
def SetType(self, type): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
547 |
if type != self.Type: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
548 |
self.Type = type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
549 |
self.Clean() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
550 |
# Create an input or output connector according to connection type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
551 |
if self.Type == CONNECTOR: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
552 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
553 |
else: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
554 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
555 |
self.RefreshConnectors() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
556 |
|
0 | 557 |
# Returns the connection type |
558 |
def GetType(self): |
|
559 |
return self.Type |
|
560 |
||
561 |
# Changes the connection name |
|
562 |
def SetName(self, name): |
|
563 |
self.Name = name |
|
564 |
||
565 |
# Returns the connection name |
|
566 |
def GetName(self): |
|
567 |
return self.Name |
|
568 |
||
569 |
# Returns the connection minimum size |
|
570 |
def GetMinSize(self): |
|
571 |
dc = wxClientDC(self.Parent) |
|
572 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
573 |
if text_height % 2 == 1: |
|
574 |
text_height += 1 |
|
575 |
return text_width + text_height + 20, text_height + 10 |
|
576 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
577 |
# Method called when a LeftDClick event have been generated |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
578 |
def OnLeftDClick(self, event, dc, scaling): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
579 |
# Edit the connection properties |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
580 |
self.Parent.EditConnectionContent(self) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
581 |
|
0 | 582 |
# Method called when a RightUp event have been generated |
27 | 583 |
def OnRightUp(self, event, dc, scaling): |
0 | 584 |
# Popup the default menu |
585 |
self.Parent.PopupDefaultMenu() |
|
586 |
||
587 |
# Refreshes the connection model |
|
588 |
def RefreshModel(self, move=True): |
|
589 |
self.Parent.RefreshConnectionModel(self) |
|
590 |
# If connection has moved and connection is of type CONTINUATION, refresh |
|
591 |
# the model of wires connected to connector |
|
592 |
if move and self.Type == CONTINUATION: |
|
593 |
if self.Connector: |
|
594 |
self.Connector.RefreshWires() |
|
595 |
||
596 |
# Draws connection |
|
597 |
def Draw(self, dc): |
|
598 |
dc.SetPen(wxBLACK_PEN) |
|
599 |
dc.SetBrush(wxWHITE_BRUSH) |
|
600 |
# Draw a rectangle with the connection size with arrows in |
|
601 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
602 |
namewidth, nameheight = dc.GetTextExtent(self.Name) |
|
603 |
arrowsize = min(self.Size[1] / 2, (self.Size[0] - namewidth - 10) / 2) |
|
604 |
dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, |
|
605 |
self.Pos.y + self.Size[1] / 2) |
|
606 |
dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, |
|
607 |
self.Pos.x, self.Pos.y + self.Size[1]) |
|
608 |
dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, |
|
609 |
self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2) |
|
610 |
dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, |
|
611 |
self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1]) |
|
612 |
# Draw variable name |
|
613 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, |
|
614 |
self.Pos.y + (self.Size[1] - nameheight) / 2) |
|
615 |
# Draw connector |
|
616 |
if self.Connector: |
|
617 |
self.Connector.Draw(dc) |
|
618 |
Graphic_Element.Draw(self, dc) |