author | lbessard |
Tue, 19 Jun 2007 08:43:41 +0200 | |
changeset 21 | e619d7bea692 |
parent 16 | 20dcc0dce64b |
child 27 | dae55dd9ee14 |
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 |
|
43 |
def __init__(self, parent, type, name, id = None, extension = 0, inputs = [], outputs = []): |
|
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 |
0 | 46 |
self.Name = name |
47 |
self.Id = id |
|
2 | 48 |
self.Extension = extension |
49 |
self.Inputs = [] |
|
50 |
self.Outputs = [] |
|
51 |
self.SetType(type) |
|
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 |
|
110 |
def GetConnector(self, position): |
|
111 |
# Test each input connector |
|
112 |
for input in self.Inputs: |
|
113 |
input_pos = input.GetRelPosition() |
|
114 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
115 |
return input |
|
116 |
# Test each output connector |
|
117 |
for output in self.Outputs: |
|
118 |
output_pos = output.GetRelPosition() |
|
119 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
120 |
return output |
|
121 |
return None |
|
122 |
||
123 |
# Returns all the block connectors |
|
124 |
def GetConnectors(self): |
|
125 |
return {"inputs" : self.Inputs, "outputs" : self.Outputs} |
|
126 |
||
127 |
# 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
|
128 |
def TestConnector(self, pt, exclude = True): |
0 | 129 |
# Test each input connector |
130 |
for input in self.Inputs: |
|
131 |
if input.TestPoint(pt, exclude): |
|
132 |
return input |
|
133 |
# Test each output connector |
|
134 |
for output in self.Outputs: |
|
135 |
if output.TestPoint(pt, exclude): |
|
136 |
return output |
|
137 |
return None |
|
138 |
||
2 | 139 |
# Changes the block type |
140 |
def SetType(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
|
141 |
if type != self.Type: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
142 |
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
|
143 |
# 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
|
144 |
# inputs and outputs |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
145 |
blocktype = GetBlockType(type) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
146 |
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
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
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
|
151 |
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
|
152 |
start += 1 |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
153 |
inputs.append(("IN%d"%start, inputs[-1][1], input[-1][2])) |
16 | 154 |
else: |
155 |
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
|
156 |
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
|
157 |
# 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
|
158 |
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
|
159 |
for input_name, input_type, input_modifier in inputs: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
160 |
connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
161 |
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
|
162 |
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
|
163 |
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
|
164 |
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
|
165 |
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
|
166 |
# 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
|
167 |
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
|
168 |
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
|
169 |
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
|
170 |
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
|
171 |
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
|
172 |
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
|
173 |
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
|
174 |
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
|
175 |
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
|
176 |
self.RefreshBoundingBox() |
2 | 177 |
|
0 | 178 |
# Returns the block type |
179 |
def GetType(self): |
|
180 |
return self.Type |
|
181 |
||
182 |
# Changes the block name |
|
183 |
def SetName(self, name): |
|
184 |
self.Name = name |
|
185 |
||
186 |
# Returs the block name |
|
187 |
def GetName(self): |
|
188 |
return self.Name |
|
189 |
||
2 | 190 |
# Changes the extension name |
191 |
def SetExtension(self, extension): |
|
192 |
self.Extension = extension |
|
193 |
||
194 |
# Returs the extension name |
|
195 |
def GetExtension(self): |
|
196 |
return self.Extension |
|
197 |
||
0 | 198 |
# Returns the block minimum size |
199 |
def GetMinSize(self): |
|
200 |
dc = wxClientDC(self.Parent) |
|
201 |
text_width, text_height = dc.GetTextExtent(self.Type) |
|
202 |
# Calculate the inputs maximum width |
|
203 |
max_input = 0 |
|
204 |
for input in self.Inputs: |
|
205 |
w, h = dc.GetTextExtent(input.GetName()) |
|
206 |
max_input = max(max_input, w) |
|
207 |
# Calculate the outputs maximum width |
|
208 |
max_output = 0 |
|
209 |
for output in self.Outputs: |
|
210 |
w, h = dc.GetTextExtent(output.GetName()) |
|
211 |
max_output = max(max_output, w) |
|
212 |
width = max(text_width + 10, max_input + max_output + 15) |
|
213 |
height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE |
|
214 |
return width, height |
|
215 |
||
216 |
# Changes the negated property of the connector handled |
|
217 |
def SetConnectorNegated(self, negated): |
|
218 |
handle_type, handle = self.Handle |
|
219 |
if handle_type == HANDLE_CONNECTOR: |
|
220 |
handle.SetNegated(negated) |
|
221 |
self.RefreshModel(False) |
|
222 |
||
223 |
# Changes the edge property of the connector handled |
|
224 |
def SetConnectorEdge(self, edge): |
|
225 |
handle_type, handle = self.Handle |
|
226 |
if handle_type == HANDLE_CONNECTOR: |
|
227 |
handle.SetEdge(edge) |
|
228 |
self.RefreshModel(False) |
|
229 |
||
2 | 230 |
# Method called when a LeftDClick event have been generated |
231 |
def OnLeftDClick(self, event, 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
|
232 |
# Edit the block properties |
2 | 233 |
self.Parent.EditBlockContent(self) |
234 |
||
0 | 235 |
# Method called when a RightUp event have been generated |
236 |
def OnRightUp(self, event, scaling): |
|
237 |
pos = GetScaledEventPosition(event, scaling) |
|
238 |
# Popup the menu with special items for a block and a connector if one is handled |
|
239 |
connector = self.TestConnector(pos, False) |
|
240 |
if connector: |
|
241 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
242 |
self.Parent.PopupBlockMenu(connector) |
|
243 |
else: |
|
244 |
self.Parent.PopupBlockMenu() |
|
245 |
||
246 |
# Refreshes the block model |
|
247 |
def RefreshModel(self, move=True): |
|
248 |
self.Parent.RefreshBlockModel(self) |
|
249 |
# If block has moved, refresh the model of wires connected to outputs |
|
250 |
if move: |
|
251 |
for output in self.Outputs: |
|
252 |
output.RefreshWires() |
|
253 |
||
254 |
# Draws block |
|
255 |
def Draw(self, dc): |
|
256 |
dc.SetPen(wxBLACK_PEN) |
|
257 |
dc.SetBrush(wxWHITE_BRUSH) |
|
258 |
# Draw a rectangle with the block size |
|
259 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
260 |
# Draw block name and block type |
|
261 |
namewidth, nameheight = dc.GetTextExtent(self.Name) |
|
262 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, |
|
263 |
self.Pos.y - (nameheight + 2)) |
|
264 |
typewidth, typeheight = dc.GetTextExtent(self.Type) |
|
265 |
dc.DrawText(self.Type, self.Pos.x + (self.Size[0] - typewidth) / 2, |
|
266 |
self.Pos.y + 5) |
|
267 |
# Draw inputs and outputs connectors |
|
268 |
for input in self.Inputs: |
|
269 |
input.Draw(dc) |
|
270 |
for output in self.Outputs: |
|
271 |
output.Draw(dc) |
|
272 |
Graphic_Element.Draw(self, dc) |
|
273 |
||
274 |
||
275 |
#------------------------------------------------------------------------------- |
|
276 |
# Function Block Diagram Variable |
|
277 |
#------------------------------------------------------------------------------- |
|
278 |
||
279 |
""" |
|
280 |
Class that implements the graphic representation of a variable |
|
281 |
""" |
|
282 |
||
283 |
class FBD_Variable(Graphic_Element): |
|
284 |
||
285 |
# Create a new variable |
|
286 |
def __init__(self, parent, type, name, value_type, id = None): |
|
287 |
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
|
288 |
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
|
289 |
self.ValueType = None |
0 | 290 |
self.Name = name |
291 |
self.Id = id |
|
292 |
self.Input = None |
|
293 |
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
|
294 |
self.SetType(type, value_type) |
0 | 295 |
|
296 |
# Destructor |
|
297 |
def __del__(self): |
|
298 |
self.Input = None |
|
299 |
self.Output = None |
|
300 |
||
301 |
# Unconnect connector |
|
302 |
def Clean(self): |
|
303 |
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
|
304 |
self.Input.UnConnect(delete = True) |
0 | 305 |
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
|
306 |
self.Output.UnConnect(delete = True) |
0 | 307 |
|
308 |
# Delete this variable by calling the appropriate method |
|
309 |
def Delete(self): |
|
310 |
self.Parent.DeleteVariable(self) |
|
311 |
||
312 |
# Refresh the variable bounding box |
|
313 |
def RefreshBoundingBox(self): |
|
314 |
dc = wxClientDC(self.Parent) |
|
315 |
if self.Type in (OUTPUT, INOUT): |
|
316 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
317 |
else: |
|
318 |
bbx_x = self.Pos.x |
|
319 |
if self.Type == INOUT: |
|
320 |
bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE |
|
321 |
else: |
|
322 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
323 |
self.BoundingBox = wxRect(bbx_x, self.Pos.y, bbx_width + 1, self.Size[1] + 1) |
|
324 |
||
325 |
# Refresh the position of the variable connector |
|
326 |
def RefreshConnectors(self): |
|
327 |
if self.Input: |
|
328 |
self.Input.SetPosition(wxPoint(0, self.Size[1] / 2)) |
|
329 |
if self.Output: |
|
330 |
self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2)) |
|
331 |
self.RefreshConnected(self) |
|
332 |
||
333 |
# Refresh the position of wires connected to connector |
|
334 |
def RefreshConnected(self, exclude = []): |
|
335 |
if self.Input: |
|
336 |
self.Input.MoveConnected(exclude) |
|
337 |
if self.Output: |
|
338 |
self.Output.MoveConnected(exclude) |
|
339 |
||
340 |
# Test if point given is on the variable connector |
|
341 |
def TestConnector(self, pt, exclude=True): |
|
342 |
if self.Input and self.Input.TestPoint(pt, exclude): |
|
343 |
return self.Input |
|
344 |
if self.Output and self.Output.TestPoint(pt, exclude): |
|
345 |
return self.Output |
|
346 |
return None |
|
347 |
||
348 |
# Returns the block connector that starts with the point given if it exists |
|
349 |
def GetConnector(self, position): |
|
350 |
# Test input connector if it exists |
|
351 |
if self.Input: |
|
352 |
input_pos = self.Input.GetRelPosition() |
|
353 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
354 |
return self.Input |
|
355 |
# Test output connector if it exists |
|
356 |
if self.Output: |
|
357 |
output_pos = self.Output.GetRelPosition() |
|
358 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
359 |
return self.Output |
|
360 |
return None |
|
361 |
||
362 |
# Returns all the block connectors |
|
363 |
def GetConnectors(self): |
|
364 |
return {"input" : self.Input, "output" : self.Output} |
|
365 |
||
366 |
# Changes the negated property of the variable connector if handled |
|
367 |
def SetConnectorNegated(self, negated): |
|
368 |
handle_type, handle = self.Handle |
|
369 |
if handle_type == HANDLE_CONNECTOR: |
|
370 |
handle.SetNegated(negated) |
|
371 |
self.RefreshModel(False) |
|
372 |
||
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
373 |
# 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
|
374 |
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
|
375 |
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
|
376 |
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
|
377 |
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
|
378 |
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
|
379 |
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
|
380 |
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
|
381 |
# 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
|
382 |
if self.Type != INPUT: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
383 |
self.Input = Connector(self, "", value_type, wxPoint(0, 0), WEST) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
384 |
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
|
385 |
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
|
386 |
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
|
387 |
|
0 | 388 |
# Returns the variable type |
389 |
def GetType(self): |
|
390 |
return self.Type |
|
391 |
||
392 |
# Changes the variable name |
|
393 |
def SetName(self, name): |
|
394 |
self.Name = name |
|
395 |
||
396 |
# Returns the variable name |
|
397 |
def GetName(self): |
|
398 |
return self.Name |
|
399 |
||
400 |
# Returns the variable minimum size |
|
401 |
def GetMinSize(self): |
|
402 |
dc = wxClientDC(self.Parent) |
|
403 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
404 |
return text_width + 10, text_height + 10 |
|
405 |
||
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
406 |
# Method called when a LeftDClick event have been generated |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
407 |
def OnLeftDClick(self, event, scaling): |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
408 |
# 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
|
409 |
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
|
410 |
|
0 | 411 |
# Method called when a RightUp event have been generated |
412 |
def OnRightUp(self, event, scaling): |
|
413 |
pos = GetScaledEventPosition(event, scaling) |
|
414 |
# Popup the menu with special items for a variable and a connector if it's handled |
|
415 |
connector = self.TestConnectors(pos, False) |
|
416 |
if connector: |
|
417 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
418 |
self.Parent.PopupVariableMenu(connector) |
|
419 |
else: |
|
420 |
self.Parent.PopupVariableMenu() |
|
421 |
||
422 |
# Refreshes the variable model |
|
423 |
def RefreshModel(self, move=True): |
|
424 |
self.Parent.RefreshVariableModel(self) |
|
425 |
# If variable has moved and variable is not of type OUTPUT, refresh the model |
|
426 |
# of wires connected to output connector |
|
427 |
if move and self.Type != OUTPUT: |
|
428 |
if self.Output: |
|
429 |
self.Output.RefreshWires() |
|
430 |
||
431 |
# Draws variable |
|
432 |
def Draw(self, dc): |
|
433 |
dc.SetPen(wxBLACK_PEN) |
|
434 |
dc.SetBrush(wxWHITE_BRUSH) |
|
435 |
# Draw a rectangle with the variable size |
|
436 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
437 |
# Draw variable name |
|
438 |
namewidth, nameheight = dc.GetTextExtent(self.Name) |
|
439 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, |
|
440 |
self.Pos.y + (self.Size[1] - nameheight) / 2) |
|
441 |
# Draw connectors |
|
442 |
if self.Input: |
|
443 |
self.Input.Draw(dc) |
|
444 |
if self.Output: |
|
445 |
self.Output.Draw(dc) |
|
446 |
Graphic_Element.Draw(self, dc) |
|
447 |
||
448 |
||
449 |
#------------------------------------------------------------------------------- |
|
450 |
# Function Block Diagram Connector |
|
451 |
#------------------------------------------------------------------------------- |
|
452 |
||
453 |
""" |
|
454 |
Class that implements the graphic representation of a connection |
|
455 |
""" |
|
456 |
||
457 |
class FBD_Connector(Graphic_Element): |
|
458 |
||
459 |
# Create a new connection |
|
460 |
def __init__(self, parent, type, name, id = None): |
|
461 |
Graphic_Element.__init__(self, parent) |
|
462 |
self.Type = type |
|
463 |
self.Name = name |
|
464 |
self.Id = id |
|
465 |
self.Pos = wxPoint(0, 0) |
|
466 |
self.Size = wxSize(0, 0) |
|
467 |
# Create an input or output connector according to connection type |
|
468 |
if self.Type == CONNECTOR: |
|
469 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST) |
|
470 |
else: |
|
471 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST) |
|
472 |
self.RefreshConnectors() |
|
473 |
||
474 |
# Destructor |
|
475 |
def __del__(self): |
|
476 |
self.Connector = None |
|
477 |
||
478 |
# Unconnect connector |
|
479 |
def Clean(self): |
|
480 |
if self.Connector: |
|
481 |
self.Connector.UnConnect() |
|
482 |
||
483 |
# Delete this connection by calling the appropriate method |
|
484 |
def Delete(self): |
|
485 |
self.Parent.DeleteConnection(self) |
|
486 |
||
487 |
# Refresh the connection bounding box |
|
488 |
def RefreshBoundingBox(self): |
|
489 |
dc = wxClientDC(self.Parent) |
|
490 |
if self.Type == CONNECTOR: |
|
491 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
492 |
else: |
|
493 |
bbx_x = self.Pos.x |
|
494 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
495 |
self.BoundingBox = wxRect(bbx_x, self.Pos.y, bbx_width, self.Size[1]) |
|
496 |
||
497 |
# Refresh the position of the connection connector |
|
498 |
def RefreshConnectors(self): |
|
499 |
if self.Type == CONNECTOR: |
|
500 |
self.Connector.SetPosition(wxPoint(0, self.Size[1] / 2)) |
|
501 |
else: |
|
502 |
self.Connector.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2)) |
|
503 |
self.RefreshConnected(self) |
|
504 |
||
505 |
# Refresh the position of wires connected to connector |
|
506 |
def RefreshConnected(self, exclude = []): |
|
507 |
if self.Connector: |
|
508 |
self.Connector.MoveConnected(exclude) |
|
509 |
||
510 |
# Test if point given is on the connection connector |
|
511 |
def TestConnector(self, pt, exclude=True): |
|
512 |
if self.Connector and self.Connector.TestPoint(pt, exclude): |
|
513 |
return self.Connector |
|
514 |
return None |
|
515 |
||
516 |
# Returns the connection connector |
|
517 |
def GetConnector(self, position = None): |
|
518 |
return self.Connector |
|
519 |
||
520 |
# Returns the connection type |
|
521 |
def GetType(self): |
|
522 |
return self.Type |
|
523 |
||
524 |
# Changes the connection name |
|
525 |
def SetName(self, name): |
|
526 |
self.Name = name |
|
527 |
||
528 |
# Returns the connection name |
|
529 |
def GetName(self): |
|
530 |
return self.Name |
|
531 |
||
532 |
# Returns the connection minimum size |
|
533 |
def GetMinSize(self): |
|
534 |
dc = wxClientDC(self.Parent) |
|
535 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
536 |
if text_height % 2 == 1: |
|
537 |
text_height += 1 |
|
538 |
return text_width + text_height + 20, text_height + 10 |
|
539 |
||
540 |
# Method called when a RightUp event have been generated |
|
541 |
def OnRightUp(self, event, scaling): |
|
542 |
# Popup the default menu |
|
543 |
self.Parent.PopupDefaultMenu() |
|
544 |
||
545 |
# Refreshes the connection model |
|
546 |
def RefreshModel(self, move=True): |
|
547 |
self.Parent.RefreshConnectionModel(self) |
|
548 |
# If connection has moved and connection is of type CONTINUATION, refresh |
|
549 |
# the model of wires connected to connector |
|
550 |
if move and self.Type == CONTINUATION: |
|
551 |
if self.Connector: |
|
552 |
self.Connector.RefreshWires() |
|
553 |
||
554 |
# Draws connection |
|
555 |
def Draw(self, dc): |
|
556 |
dc.SetPen(wxBLACK_PEN) |
|
557 |
dc.SetBrush(wxWHITE_BRUSH) |
|
558 |
# Draw a rectangle with the connection size with arrows in |
|
559 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
560 |
namewidth, nameheight = dc.GetTextExtent(self.Name) |
|
561 |
arrowsize = min(self.Size[1] / 2, (self.Size[0] - namewidth - 10) / 2) |
|
562 |
dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, |
|
563 |
self.Pos.y + self.Size[1] / 2) |
|
564 |
dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, |
|
565 |
self.Pos.x, self.Pos.y + self.Size[1]) |
|
566 |
dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, |
|
567 |
self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2) |
|
568 |
dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, |
|
569 |
self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1]) |
|
570 |
# Draw variable name |
|
571 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, |
|
572 |
self.Pos.y + (self.Size[1] - nameheight) / 2) |
|
573 |
# Draw connector |
|
574 |
if self.Connector: |
|
575 |
self.Connector.Draw(dc) |
|
576 |
Graphic_Element.Draw(self, dc) |