40 class FBD_Block(Graphic_Element): |
40 class FBD_Block(Graphic_Element): |
41 |
41 |
42 # Create a new block |
42 # Create a new block |
43 def __init__(self, parent, type, name, id = None, extension = 0, inputs = [], outputs = []): |
43 def __init__(self, parent, type, name, id = None, extension = 0, inputs = [], outputs = []): |
44 Graphic_Element.__init__(self, parent) |
44 Graphic_Element.__init__(self, parent) |
|
45 self.Type = None |
45 self.Name = name |
46 self.Name = name |
46 self.Id = id |
47 self.Id = id |
47 self.Extension = extension |
48 self.Extension = extension |
48 self.Inputs = [] |
49 self.Inputs = [] |
49 self.Outputs = [] |
50 self.Outputs = [] |
122 # Returns all the block connectors |
123 # Returns all the block connectors |
123 def GetConnectors(self): |
124 def GetConnectors(self): |
124 return {"inputs" : self.Inputs, "outputs" : self.Outputs} |
125 return {"inputs" : self.Inputs, "outputs" : self.Outputs} |
125 |
126 |
126 # Test if point given is on one of the block connectors |
127 # Test if point given is on one of the block connectors |
127 def TestConnector(self, pt, exclude=True): |
128 def TestConnector(self, pt, exclude = True): |
128 # Test each input connector |
129 # Test each input connector |
129 for input in self.Inputs: |
130 for input in self.Inputs: |
130 if input.TestPoint(pt, exclude): |
131 if input.TestPoint(pt, exclude): |
131 return input |
132 return input |
132 # Test each output connector |
133 # Test each output connector |
135 return output |
136 return output |
136 return None |
137 return None |
137 |
138 |
138 # Changes the block type |
139 # Changes the block type |
139 def SetType(self, type): |
140 def SetType(self, type): |
140 self.Type = type |
141 if type != self.Type: |
141 # Find the block definition from type given and create the corresponding |
142 self.Type = type |
142 # inputs and outputs |
143 # Find the block definition from type given and create the corresponding |
143 blocktype = GetBlockType(type) |
144 # inputs and outputs |
144 if blocktype: |
145 blocktype = GetBlockType(type) |
145 inputs = [input for input in blocktype["inputs"]] |
146 if blocktype: |
146 outputs = [output for output in blocktype["outputs"]] |
147 inputs = [input for input in blocktype["inputs"]] |
147 if blocktype["extensible"]: |
148 outputs = [output for output in blocktype["outputs"]] |
148 start = int(inputs[-1][0].replace("IN", "")) |
149 if blocktype["extensible"]: |
149 for i in xrange(self.Extension - len(blocktype["inputs"])): |
150 start = int(inputs[-1][0].replace("IN", "")) |
150 start += 1 |
151 for i in xrange(self.Extension - len(blocktype["inputs"])): |
151 inputs.append(("IN%d"%start, inputs[-1][1], input[-1][2])) |
152 start += 1 |
152 self.SetConnectors(inputs, outputs) |
153 inputs.append(("IN%d"%start, inputs[-1][1], input[-1][2])) |
|
154 self.Clean() |
|
155 # Extract the inputs properties and create the corresponding connector |
|
156 self.Inputs = [] |
|
157 for input_name, input_type, input_modifier in inputs: |
|
158 connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST) |
|
159 if input_modifier == "negated": |
|
160 connector.SetNegated(True) |
|
161 elif input_modifier != "none": |
|
162 connector.SetEdge(input_modifier) |
|
163 self.Inputs.append(connector) |
|
164 # Extract the outputs properties and create the corresponding connector |
|
165 self.Outputs = [] |
|
166 for output_name, output_type, output_modifier in outputs: |
|
167 connector = Connector(self, output_name, output_type, wxPoint(0, 0), EAST) |
|
168 if output_modifier == "negated": |
|
169 connector.SetNegated(True) |
|
170 elif output_modifier != "none": |
|
171 connector.SetEdge(output_modifier) |
|
172 self.Outputs.append(connector) |
|
173 self.RefreshConnectors() |
|
174 self.RefreshBoundingBox() |
153 |
175 |
154 # Returns the block type |
176 # Returns the block type |
155 def GetType(self): |
177 def GetType(self): |
156 return self.Type |
178 return self.Type |
157 |
179 |
187 max_output = max(max_output, w) |
209 max_output = max(max_output, w) |
188 width = max(text_width + 10, max_input + max_output + 15) |
210 width = max(text_width + 10, max_input + max_output + 15) |
189 height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE |
211 height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE |
190 return width, height |
212 return width, height |
191 |
213 |
192 # Changes the block connectors |
|
193 def SetConnectors(self, inputs, outputs): |
|
194 self.Clean() |
|
195 # Extract the inputs properties and create the corresponding connector |
|
196 self.Inputs = [] |
|
197 for input_name, input_type, input_modifier in inputs: |
|
198 connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST) |
|
199 if input_modifier == "negated": |
|
200 connector.SetNegated(True) |
|
201 elif input_modifier != "none": |
|
202 connector.SetEdge(input_modifier) |
|
203 self.Inputs.append(connector) |
|
204 # Extract the outputs properties and create the corresponding connector |
|
205 self.Outputs = [] |
|
206 for output_name, output_type, output_modifier in outputs: |
|
207 connector = Connector(self, output_name, output_type, wxPoint(0, 0), EAST) |
|
208 if output_modifier == "negated": |
|
209 connector.SetNegated(True) |
|
210 elif output_modifier != "none": |
|
211 connector.SetEdge(output_modifier) |
|
212 self.Outputs.append(connector) |
|
213 self.RefreshConnectors() |
|
214 self.RefreshBoundingBox() |
|
215 |
|
216 # Changes the negated property of the connector handled |
214 # Changes the negated property of the connector handled |
217 def SetConnectorNegated(self, negated): |
215 def SetConnectorNegated(self, negated): |
218 handle_type, handle = self.Handle |
216 handle_type, handle = self.Handle |
219 if handle_type == HANDLE_CONNECTOR: |
217 if handle_type == HANDLE_CONNECTOR: |
220 handle.SetNegated(negated) |
218 handle.SetNegated(negated) |
227 handle.SetEdge(edge) |
225 handle.SetEdge(edge) |
228 self.RefreshModel(False) |
226 self.RefreshModel(False) |
229 |
227 |
230 # Method called when a LeftDClick event have been generated |
228 # Method called when a LeftDClick event have been generated |
231 def OnLeftDClick(self, event, scaling): |
229 def OnLeftDClick(self, event, scaling): |
232 # Edit the step properties |
230 # Edit the block properties |
233 self.Parent.EditBlockContent(self) |
231 self.Parent.EditBlockContent(self) |
234 |
232 |
235 # Method called when a RightUp event have been generated |
233 # Method called when a RightUp event have been generated |
236 def OnRightUp(self, event, scaling): |
234 def OnRightUp(self, event, scaling): |
237 pos = GetScaledEventPosition(event, scaling) |
235 pos = GetScaledEventPosition(event, scaling) |
283 class FBD_Variable(Graphic_Element): |
281 class FBD_Variable(Graphic_Element): |
284 |
282 |
285 # Create a new variable |
283 # Create a new variable |
286 def __init__(self, parent, type, name, value_type, id = None): |
284 def __init__(self, parent, type, name, value_type, id = None): |
287 Graphic_Element.__init__(self, parent) |
285 Graphic_Element.__init__(self, parent) |
288 self.Type = type |
286 self.Type = None |
|
287 self.ValueType = None |
289 self.Name = name |
288 self.Name = name |
290 self.Id = id |
289 self.Id = id |
291 self.Input = None |
290 self.Input = None |
292 self.Output = None |
291 self.Output = None |
293 # Create an input or output connector according to variable type |
292 self.SetType(type, value_type) |
294 if self.Type != INPUT: |
|
295 self.Input = Connector(self, "", value_type, wxPoint(0, 0), WEST) |
|
296 if self.Type != OUTPUT: |
|
297 self.Output = Connector(self, "", value_type, wxPoint(0, 0), EAST) |
|
298 self.RefreshConnectors() |
|
299 |
293 |
300 # Destructor |
294 # Destructor |
301 def __del__(self): |
295 def __del__(self): |
302 self.Input = None |
296 self.Input = None |
303 self.Output = None |
297 self.Output = None |
304 |
298 |
305 # Unconnect connector |
299 # Unconnect connector |
306 def Clean(self): |
300 def Clean(self): |
307 if self.Input: |
301 if self.Input: |
308 self.Input.UnConnect() |
302 self.Input.UnConnect(delete = True) |
309 if self.Output: |
303 if self.Output: |
310 self.Output.UnConnect() |
304 self.Output.UnConnect(delete = True) |
311 |
305 |
312 # Delete this variable by calling the appropriate method |
306 # Delete this variable by calling the appropriate method |
313 def Delete(self): |
307 def Delete(self): |
314 self.Parent.DeleteVariable(self) |
308 self.Parent.DeleteVariable(self) |
315 |
309 |
372 handle_type, handle = self.Handle |
366 handle_type, handle = self.Handle |
373 if handle_type == HANDLE_CONNECTOR: |
367 if handle_type == HANDLE_CONNECTOR: |
374 handle.SetNegated(negated) |
368 handle.SetNegated(negated) |
375 self.RefreshModel(False) |
369 self.RefreshModel(False) |
376 |
370 |
|
371 # Changes the variable type |
|
372 def SetType(self, type, value_type): |
|
373 if type != self.Type or value_type != self.ValueType: |
|
374 self.Type = type |
|
375 self.ValueType = value_type |
|
376 self.Clean() |
|
377 self.Input = None |
|
378 self.Output = None |
|
379 # Create an input or output connector according to variable type |
|
380 if self.Type != INPUT: |
|
381 self.Input = Connector(self, "", value_type, wxPoint(0, 0), WEST) |
|
382 if self.Type != OUTPUT: |
|
383 self.Output = Connector(self, "", value_type, wxPoint(0, 0), EAST) |
|
384 self.RefreshConnectors() |
|
385 |
377 # Returns the variable type |
386 # Returns the variable type |
378 def GetType(self): |
387 def GetType(self): |
379 return self.Type |
388 return self.Type |
380 |
389 |
381 # Changes the variable name |
390 # Changes the variable name |
389 # Returns the variable minimum size |
398 # Returns the variable minimum size |
390 def GetMinSize(self): |
399 def GetMinSize(self): |
391 dc = wxClientDC(self.Parent) |
400 dc = wxClientDC(self.Parent) |
392 text_width, text_height = dc.GetTextExtent(self.Name) |
401 text_width, text_height = dc.GetTextExtent(self.Name) |
393 return text_width + 10, text_height + 10 |
402 return text_width + 10, text_height + 10 |
|
403 |
|
404 # Method called when a LeftDClick event have been generated |
|
405 def OnLeftDClick(self, event, scaling): |
|
406 # Edit the variable properties |
|
407 self.Parent.EditVariableContent(self) |
394 |
408 |
395 # Method called when a RightUp event have been generated |
409 # Method called when a RightUp event have been generated |
396 def OnRightUp(self, event, scaling): |
410 def OnRightUp(self, event, scaling): |
397 pos = GetScaledEventPosition(event, scaling) |
411 pos = GetScaledEventPosition(event, scaling) |
398 # Popup the menu with special items for a variable and a connector if it's handled |
412 # Popup the menu with special items for a variable and a connector if it's handled |