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