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