author | lbessard |
Tue, 10 Jul 2007 11:59:28 +0200 | |
changeset 29 | 3b7e23a323a6 |
parent 28 | fc23e1f415d8 |
child 32 | cf9efccff009 |
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): |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
395 |
if type != self.Type or value_type != self.ValueType: |
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.ValueType = value_type |
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.Clean() |
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.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
|
400 |
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
|
401 |
# 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
|
402 |
if self.Type != INPUT: |
27 | 403 |
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
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
|
0 | 408 |
# Returns the variable type |
409 |
def GetType(self): |
|
410 |
return self.Type |
|
411 |
||
412 |
# Changes the variable name |
|
413 |
def SetName(self, name): |
|
414 |
self.Name = name |
|
415 |
||
416 |
# Returns the variable name |
|
417 |
def GetName(self): |
|
418 |
return self.Name |
|
419 |
||
420 |
# Returns the variable minimum size |
|
421 |
def GetMinSize(self): |
|
422 |
dc = wxClientDC(self.Parent) |
|
423 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
424 |
return text_width + 10, text_height + 10 |
|
425 |
||
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
426 |
# Method called when a LeftDClick event have been generated |
27 | 427 |
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
|
428 |
# 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
|
429 |
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
|
430 |
|
0 | 431 |
# Method called when a RightUp event have been generated |
27 | 432 |
def OnRightUp(self, event, dc, scaling): |
433 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 434 |
# Popup the menu with special items for a variable and a connector if it's handled |
27 | 435 |
connector = self.TestConnector(pos, False) |
0 | 436 |
if connector: |
437 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
438 |
self.Parent.PopupVariableMenu(connector) |
|
439 |
else: |
|
440 |
self.Parent.PopupVariableMenu() |
|
441 |
||
442 |
# Refreshes the variable model |
|
443 |
def RefreshModel(self, move=True): |
|
444 |
self.Parent.RefreshVariableModel(self) |
|
445 |
# If variable has moved and variable is not of type OUTPUT, refresh the model |
|
446 |
# of wires connected to output connector |
|
447 |
if move and self.Type != OUTPUT: |
|
448 |
if self.Output: |
|
449 |
self.Output.RefreshWires() |
|
450 |
||
451 |
# Draws variable |
|
452 |
def Draw(self, dc): |
|
453 |
dc.SetPen(wxBLACK_PEN) |
|
454 |
dc.SetBrush(wxWHITE_BRUSH) |
|
455 |
# Draw a rectangle with the variable size |
|
456 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
457 |
# Draw variable name |
|
458 |
namewidth, nameheight = dc.GetTextExtent(self.Name) |
|
459 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, |
|
460 |
self.Pos.y + (self.Size[1] - nameheight) / 2) |
|
461 |
# Draw connectors |
|
462 |
if self.Input: |
|
463 |
self.Input.Draw(dc) |
|
464 |
if self.Output: |
|
465 |
self.Output.Draw(dc) |
|
466 |
Graphic_Element.Draw(self, dc) |
|
467 |
||
468 |
||
469 |
#------------------------------------------------------------------------------- |
|
470 |
# Function Block Diagram Connector |
|
471 |
#------------------------------------------------------------------------------- |
|
472 |
||
473 |
""" |
|
474 |
Class that implements the graphic representation of a connection |
|
475 |
""" |
|
476 |
||
477 |
class FBD_Connector(Graphic_Element): |
|
478 |
||
479 |
# Create a new connection |
|
480 |
def __init__(self, parent, type, name, id = None): |
|
481 |
Graphic_Element.__init__(self, parent) |
|
482 |
self.Type = type |
|
483 |
self.Name = name |
|
484 |
self.Id = id |
|
485 |
self.Pos = wxPoint(0, 0) |
|
486 |
self.Size = wxSize(0, 0) |
|
487 |
# Create an input or output connector according to connection type |
|
488 |
if self.Type == CONNECTOR: |
|
27 | 489 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True) |
0 | 490 |
else: |
491 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST) |
|
492 |
self.RefreshConnectors() |
|
493 |
||
494 |
# Destructor |
|
495 |
def __del__(self): |
|
496 |
self.Connector = None |
|
497 |
||
498 |
# Unconnect connector |
|
499 |
def Clean(self): |
|
500 |
if self.Connector: |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
501 |
self.Connector.UnConnect(delete = True) |
0 | 502 |
|
503 |
# Delete this connection by calling the appropriate method |
|
504 |
def Delete(self): |
|
505 |
self.Parent.DeleteConnection(self) |
|
506 |
||
507 |
# Refresh the connection bounding box |
|
508 |
def RefreshBoundingBox(self): |
|
509 |
dc = wxClientDC(self.Parent) |
|
510 |
if self.Type == CONNECTOR: |
|
511 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
512 |
else: |
|
513 |
bbx_x = self.Pos.x |
|
514 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
515 |
self.BoundingBox = wxRect(bbx_x, self.Pos.y, bbx_width, self.Size[1]) |
|
516 |
||
517 |
# Refresh the position of the connection connector |
|
518 |
def RefreshConnectors(self): |
|
519 |
if self.Type == CONNECTOR: |
|
520 |
self.Connector.SetPosition(wxPoint(0, self.Size[1] / 2)) |
|
521 |
else: |
|
522 |
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
|
523 |
self.RefreshConnected() |
0 | 524 |
|
525 |
# Refresh the position of wires connected to connector |
|
526 |
def RefreshConnected(self, exclude = []): |
|
527 |
if self.Connector: |
|
528 |
self.Connector.MoveConnected(exclude) |
|
529 |
||
530 |
# Test if point given is on the connection connector |
|
531 |
def TestConnector(self, pt, exclude=True): |
|
532 |
if self.Connector and self.Connector.TestPoint(pt, exclude): |
|
533 |
return self.Connector |
|
534 |
return None |
|
535 |
||
536 |
# Returns the connection connector |
|
27 | 537 |
def GetConnector(self, position = None, name = None): |
0 | 538 |
return self.Connector |
539 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
540 |
# Changes the variable type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
541 |
def SetType(self, type): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
542 |
if type != self.Type: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
543 |
self.Type = type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
544 |
self.Clean() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
545 |
# Create an input or output connector according to connection type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
546 |
if self.Type == CONNECTOR: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
547 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
548 |
else: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
549 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
550 |
self.RefreshConnectors() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
551 |
|
0 | 552 |
# Returns the connection type |
553 |
def GetType(self): |
|
554 |
return self.Type |
|
555 |
||
556 |
# Changes the connection name |
|
557 |
def SetName(self, name): |
|
558 |
self.Name = name |
|
559 |
||
560 |
# Returns the connection name |
|
561 |
def GetName(self): |
|
562 |
return self.Name |
|
563 |
||
564 |
# Returns the connection minimum size |
|
565 |
def GetMinSize(self): |
|
566 |
dc = wxClientDC(self.Parent) |
|
567 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
568 |
if text_height % 2 == 1: |
|
569 |
text_height += 1 |
|
570 |
return text_width + text_height + 20, text_height + 10 |
|
571 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
572 |
# Method called when a LeftDClick event have been generated |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
573 |
def OnLeftDClick(self, event, dc, scaling): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
574 |
# Edit the connection properties |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
575 |
self.Parent.EditConnectionContent(self) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
576 |
|
0 | 577 |
# Method called when a RightUp event have been generated |
27 | 578 |
def OnRightUp(self, event, dc, scaling): |
0 | 579 |
# Popup the default menu |
580 |
self.Parent.PopupDefaultMenu() |
|
581 |
||
582 |
# Refreshes the connection model |
|
583 |
def RefreshModel(self, move=True): |
|
584 |
self.Parent.RefreshConnectionModel(self) |
|
585 |
# If connection has moved and connection is of type CONTINUATION, refresh |
|
586 |
# the model of wires connected to connector |
|
587 |
if move and self.Type == CONTINUATION: |
|
588 |
if self.Connector: |
|
589 |
self.Connector.RefreshWires() |
|
590 |
||
591 |
# Draws connection |
|
592 |
def Draw(self, dc): |
|
593 |
dc.SetPen(wxBLACK_PEN) |
|
594 |
dc.SetBrush(wxWHITE_BRUSH) |
|
595 |
# Draw a rectangle with the connection size with arrows in |
|
596 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
597 |
namewidth, nameheight = dc.GetTextExtent(self.Name) |
|
598 |
arrowsize = min(self.Size[1] / 2, (self.Size[0] - namewidth - 10) / 2) |
|
599 |
dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, |
|
600 |
self.Pos.y + self.Size[1] / 2) |
|
601 |
dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, |
|
602 |
self.Pos.x, self.Pos.y + self.Size[1]) |
|
603 |
dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, |
|
604 |
self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2) |
|
605 |
dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, |
|
606 |
self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1]) |
|
607 |
# Draw variable name |
|
608 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, |
|
609 |
self.Pos.y + (self.Size[1] - nameheight) / 2) |
|
610 |
# Draw connector |
|
611 |
if self.Connector: |
|
612 |
self.Connector.Draw(dc) |
|
613 |
Graphic_Element.Draw(self, dc) |