LDViewer.py
changeset 27 dae55dd9ee14
parent 9 b29105e29081
child 28 fc23e1f415d8
equal deleted inserted replaced
26:36d378bd852e 27:dae55dd9ee14
    28 
    28 
    29 from plcopen.structures import *
    29 from plcopen.structures import *
    30 from graphics.GraphicCommons import *
    30 from graphics.GraphicCommons import *
    31 from graphics.FBD_Objects import *
    31 from graphics.FBD_Objects import *
    32 from Viewer import *
    32 from Viewer import *
       
    33 from Dialogs import *
    33 
    34 
    34 def ExtractNextBlocks(block, block_list):
    35 def ExtractNextBlocks(block, block_list):
    35     current_list = [block]
    36     current_list = [block]
    36     while len(current_list) > 0:
    37     while len(current_list) > 0:
    37         next_list = []
    38         next_list = []
    51                     if not isinstance(next, LD_PowerRail) and next not in block_list:
    52                     if not isinstance(next, LD_PowerRail) and next not in block_list:
    52                         block_list.append(next)
    53                         block_list.append(next)
    53                         next_list.append(next)
    54                         next_list.append(next)
    54         current_list = next_list
    55         current_list = next_list
    55     
    56     
    56 def CalcBranchSize(elements, stop):
    57 def CalcBranchSize(elements, stops):
    57     branch_size = 0
    58     branch_size = 0
    58     stop_list = [stop]
    59     stop_list = stops
    59     ExtractNextBlocks(stop, stop_list)
    60     for stop in stops:
       
    61         ExtractNextBlocks(stop, stop_list)
    60     element_tree = {}
    62     element_tree = {}
    61     for element in elements:
    63     for element in elements:
    62         if element not in element_tree:
    64         if element not in element_tree:
    63             element_tree[element] = {"parents":["start"], "children":[], "weight":None}
    65             element_tree[element] = {"parents":["start"], "children":[], "weight":None}
    64             GenerateTree(element, element_tree, stop_list)
    66             GenerateTree(element, element_tree, stop_list)
    65         elif element_tree[element]:
    67         elif element_tree[element]:
    66             element_tree[element]["parents"].append("start")
    68             element_tree[element]["parents"].append("start")
       
    69     remove_stops = {"start":[], "stop":[]}
    67     for element, values in element_tree.items():
    70     for element, values in element_tree.items():
    68         if values and values["children"] == ["stop"]:
    71         if "stop" in values["children"]:
       
    72             removed = []
       
    73             for child in values["children"]:
       
    74                 if child != "stop":
       
    75 ##                    if child in elements:
       
    76 ##                        RemoveElement(child, element_tree)
       
    77 ##                        removed.append(child)
       
    78                     if "start" in element_tree[child]["parents"]:
       
    79                         if element not in remove_stops["stop"]:
       
    80                             remove_stops["stop"].append(element)
       
    81                         if child not in remove_stops["start"]:
       
    82                             remove_stops["start"].append(child)
       
    83             for child in removed:
       
    84                 values["children"].remove(child)
       
    85     for element in remove_stops["start"]:
       
    86         element_tree[element]["parents"].remove("start")
       
    87     for element in remove_stops["stop"]:
       
    88         element_tree[element]["children"].remove("stop")
       
    89     for element, values in element_tree.items():
       
    90         if values and "stop" in values["children"]:
    69             CalcWeight(element, element_tree)
    91             CalcWeight(element, element_tree)
    70             if values["weight"]:
    92             if values["weight"]:
    71                 branch_size += values["weight"]
    93                 branch_size += values["weight"]
    72             else:
    94             else:
    73                 return 1
    95                 return 1
       
    96     #print branch_size
    74     return branch_size
    97     return branch_size
    75 
    98 
    76 def RemoveElement(remove, element_tree):
    99 def RemoveElement(remove, element_tree):
    77     if remove in element_tree and element_tree[remove]:
   100     if remove in element_tree and element_tree[remove]:
    78         for child in element_tree[remove]["children"]:
   101         for child in element_tree[remove]["children"]:
    79             if child != "stop":
   102             if child != "stop":
    80                 RemoveElement(child, element_tree)
   103                 RemoveElement(child, element_tree)
    81         element_tree[remove] = None
   104         element_tree.pop(remove)
       
   105 ##        element_tree[remove] = None
    82 
   106 
    83 def GenerateTree(element, element_tree, stop_list):
   107 def GenerateTree(element, element_tree, stop_list):
    84     if element in element_tree:
   108     if element in element_tree:
    85         connectors = element.GetConnectors()
   109         connectors = element.GetConnectors()
    86         input_connectors = []
   110         input_connectors = []
    93                 input_connectors = [connectors["input"]]
   117                 input_connectors = [connectors["input"]]
    94         for connector in input_connectors:
   118         for connector in input_connectors:
    95             for wire, handle in connector.GetWires():
   119             for wire, handle in connector.GetWires():
    96                 next = wire.EndConnected.GetParentBlock()
   120                 next = wire.EndConnected.GetParentBlock()
    97                 if isinstance(next, LD_PowerRail) and next.GetType() == LEFTRAIL or next in stop_list:
   121                 if isinstance(next, LD_PowerRail) and next.GetType() == LEFTRAIL or next in stop_list:
    98                     for remove in element_tree[element]["children"]:
   122 ##                    for remove in element_tree[element]["children"]:
    99                         RemoveElement(remove, element_tree)
   123 ##                        RemoveElement(remove, element_tree)
   100                     element_tree[element]["children"] = ["stop"]
   124 ##                    element_tree[element]["children"] = ["stop"]
   101                 elif element_tree[element]["children"] == ["stop"]:
   125                     element_tree[element]["children"].append("stop")
   102                     element_tree[next] = None
   126 ##                elif element_tree[element]["children"] == ["stop"]:
       
   127 ##                    element_tree[next] = None
   103                 elif next not in element_tree or element_tree[next]:
   128                 elif next not in element_tree or element_tree[next]:
   104                     element_tree[element]["children"].append(next)
   129                     element_tree[element]["children"].append(next)
   105                     if next in element_tree:
   130                     if next in element_tree:
   106                         element_tree[next]["parents"].append(element)
   131                         element_tree[next]["parents"].append(element)
   107                     else:
   132                     else:
   225             if rung.IsElementIn(element):
   250             if rung.IsElementIn(element):
   226                 return i
   251                 return i
   227         return None
   252         return None
   228 
   253 
   229     def FindElement(self, pos):
   254     def FindElement(self, pos):
       
   255         if self.GetDrawingMode() == FREEDRAWING_MODE:
       
   256             return Viewer.FindElement(self, pos)
       
   257         
   230         elements = []
   258         elements = []
   231         for element in self.Elements:
   259         for element in self.Elements:
   232             if element.HitTest(pos) or element.TestHandle(pos) != (0, 0):
   260             if element.HitTest(pos) or element.TestHandle(pos) != (0, 0):
   233                 elements.append(element)
   261                 elements.append(element)
   234         if len(elements) == 1:
   262         if len(elements) == 1:
   241                 group.SelectElement(element)
   269                 group.SelectElement(element)
   242             return group
   270             return group
   243         return None
   271         return None
   244 
   272 
   245     def SearchElements(self, bbox):
   273     def SearchElements(self, bbox):
       
   274         if self.GetDrawingMode() == FREEDRAWING_MODE:
       
   275             return Viewer.SearchElements(self, bbox)
       
   276         
   246         elements = []
   277         elements = []
   247         for element in self.Blocks:
   278         for element in self.Blocks:
   248             element_bbox = element.GetBoundingBox()
   279             element_bbox = element.GetBoundingBox()
   249             if element_bbox.x >= bbox.x and element_bbox.y >= bbox.y and element_bbox.x + element_bbox.width <= bbox.x + bbox.width and element_bbox.y + element_bbox.height <= bbox.y + bbox.height:
   280             if element_bbox.x >= bbox.x and element_bbox.y >= bbox.y and element_bbox.x + element_bbox.width <= bbox.x + bbox.width and element_bbox.y + element_bbox.height <= bbox.y + bbox.height:
   250                 elements.append(element)
   281                 elements.append(element)
   253 #-------------------------------------------------------------------------------
   284 #-------------------------------------------------------------------------------
   254 #                          Mouse event functions
   285 #                          Mouse event functions
   255 #-------------------------------------------------------------------------------
   286 #-------------------------------------------------------------------------------
   256 
   287 
   257     def OnViewerLeftDown(self, event):
   288     def OnViewerLeftDown(self, event):
   258         if self.Mode == MODE_SELECTION:
   289         if self.GetDrawingMode() == FREEDRAWING_MODE:
   259             pos = event.GetPosition()
   290             Viewer.OnViewerLeftDown(self, event)
       
   291         elif self.Mode == MODE_SELECTION:
       
   292             dc = self.GetLogicalDC()
       
   293             pos = event.GetLogicalPosition(dc)
   260             element = self.FindElement(pos)
   294             element = self.FindElement(pos)
   261             if self.SelectedElement:
   295             if self.SelectedElement:
   262                 if self.SelectedElement in self.Elements:
   296                 if self.SelectedElement in self.Elements:
   263                     if self.SelectedElement != element:
   297                     if self.SelectedElement != element:
   264                         if self.SelectedElement in self.Wires:
   298                         if self.SelectedElement in self.Wires:
   281                     self.SelectedElement.SetSelected(False)
   315                     self.SelectedElement.SetSelected(False)
   282                     self.SelectedElement = None
   316                     self.SelectedElement = None
   283                 self.Refresh()
   317                 self.Refresh()
   284             if element:
   318             if element:
   285                 self.SelectedElement = element
   319                 self.SelectedElement = element
   286                 self.SelectedElement.OnLeftDown(event, self.Scaling)
   320                 self.SelectedElement.OnLeftDown(event, dc, self.Scaling)
   287                 self.Refresh()
   321                 self.Refresh()
   288             else:
   322             else:
   289                 self.rubberBand.Reset()
   323                 self.rubberBand.Reset()
   290                 self.rubberBand.OnLeftDown(event, self.Scaling)
   324                 self.rubberBand.OnLeftDown(event, dc, self.Scaling)
   291         event.Skip()
   325         event.Skip()
   292 
   326 
   293     def OnViewerLeftUp(self, event):
   327     def OnViewerLeftUp(self, event):
   294         if self.rubberBand.IsShown():
   328         if self.GetDrawingMode() == FREEDRAWING_MODE:
       
   329             Viewer.OnViewerLeftUp(self, event)
       
   330         elif self.rubberBand.IsShown():
   295             if self.Mode == MODE_SELECTION:
   331             if self.Mode == MODE_SELECTION:
   296                 elements = self.SearchElements(self.rubberBand.GetCurrentExtent())
   332                 elements = self.SearchElements(self.rubberBand.GetCurrentExtent())
   297                 self.rubberBand.OnLeftUp(event, self.Scaling)
   333                 self.rubberBand.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)
   298                 if len(elements) > 0:
   334                 if len(elements) > 0:
   299                     self.SelectedElement = Graphic_Group(self)
   335                     self.SelectedElement = Graphic_Group(self)
   300                     self.SelectedElement.SetElements(elements)
   336                     self.SelectedElement.SetElements(elements)
   301                     self.SelectedElement.SetSelected(True)
   337                     self.SelectedElement.SetSelected(True)
   302                     self.Refresh()
   338                     self.Refresh()
   303         elif self.Mode == MODE_SELECTION and self.SelectedElement:
   339         elif self.Mode == MODE_SELECTION and self.SelectedElement:
       
   340             dc = self.GetLogicalDC() 
   304             if self.SelectedElement in self.Elements:
   341             if self.SelectedElement in self.Elements:
   305                 if self.SelectedElement in self.Wires:
   342                 if self.SelectedElement in self.Wires:
   306                     result = self.SelectedElement.TestSegment(event.GetPosition(), True)
   343                     result = self.SelectedElement.TestSegment(event.GetLogicalPosition(dc), True)
   307                     if result and result[1] in [EAST, WEST]:
   344                     if result and result[1] in [EAST, WEST]:
   308                         self.SelectedElement.SetSelectedSegment(result[0])
   345                         self.SelectedElement.SetSelectedSegment(result[0])
   309                 else:
   346                 else:
   310                     self.SelectedElement.OnLeftUp(event, self.Scaling)
   347                     self.SelectedElement.OnLeftUp(event, dc, self.Scaling)
   311             else:
   348             else:
   312                 for element in self.SelectedElement.GetElements():
   349                 for element in self.SelectedElement.GetElements():
   313                     if element in self.Wires:
   350                     if element in self.Wires:
   314                         result = element.TestSegment(event.GetPosition(), True)
   351                         result = element.TestSegment(event.GetLogicalPosition(dc), True)
   315                         if result and result[1] in [EAST, WEST]:
   352                         if result and result[1] in [EAST, WEST]:
   316                             element.SetSelectedSegment(result[0])
   353                             element.SetSelectedSegment(result[0])
   317                     else:
   354                     else:
   318                         element.OnLeftUp(event, self.Scaling)
   355                         element.OnLeftUp(event, dc, self.Scaling)
   319             wxCallAfter(self.SetCursor, wxNullCursor)
   356             wxCallAfter(self.SetCursor, wxNullCursor)
   320             self.ReleaseMouse()
   357             self.ReleaseMouse()
   321             self.Refresh()
   358             self.Refresh()
   322         event.Skip()
   359         event.Skip()
   323 
   360 
   324     def OnViewerRightUp(self, event):
   361     def OnViewerRightUp(self, event):
   325         pos = event.GetPosition()
   362         if self.GetDrawingMode() == FREEDRAWING_MODE:
   326         element = self.FindElement(pos)
   363             Viewer.OnViewerRightUp(self, event)
   327         if element:
   364         else:
   328             if self.SelectedElement and self.SelectedElement != element:
   365             dc = self.GetLogicalDC()
   329                 self.SelectedElement.SetSelected(False)
   366             pos = event.GetLogicalPosition(dc)
   330             self.SelectedElement = element
   367             element = self.FindElement(pos)
   331             if self.SelectedElement in self.Wires:
   368             if element:
   332                 self.SelectedElement.SetSelectedSegment(0)
   369                 if self.SelectedElement and self.SelectedElement != element:
   333             else:
   370                     self.SelectedElement.SetSelected(False)
   334                 self.SelectedElement.SetSelected(True)
   371                 self.SelectedElement = element
   335                 self.SelectedElement.OnRightUp(event, self.Scaling)
   372                 if self.SelectedElement in self.Wires:
   336             wxCallAfter(self.SetCursor, wxNullCursor)
   373                     self.SelectedElement.SetSelectedSegment(0)
   337             self.ReleaseMouse()
   374                 else:
       
   375                     self.SelectedElement.SetSelected(True)
       
   376                     self.SelectedElement.OnRightUp(event, dc, self.Scaling)
       
   377                 wxCallAfter(self.SetCursor, wxNullCursor)
       
   378                 self.ReleaseMouse()
       
   379                 self.Refresh()
       
   380         event.Skip()
       
   381 
       
   382     def OnViewerLeftDClick(self, event):
       
   383         if self.GetDrawingMode() == FREEDRAWING_MODE:
       
   384             Viewer.OnViewerLeftDClick(self, event)
       
   385         elif self.Mode == MODE_SELECTION and self.SelectedElement:
       
   386             self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
   338             self.Refresh()
   387             self.Refresh()
   339         event.Skip()
   388         event.Skip()
   340 
   389 
   341     def OnViewerLeftDClick(self, event):
   390     def OnViewerMotion(self, event):
   342         if self.Mode == MODE_SELECTION and self.SelectedElement:
   391         if self.GetDrawingMode() == FREEDRAWING_MODE:
   343             self.SelectedElement.OnLeftDClick(event, self.Scaling)
   392             Viewer.OnViewerMotion(self, event)
       
   393         event.Skip()
       
   394 
       
   395 #-------------------------------------------------------------------------------
       
   396 #                          Keyboard event functions
       
   397 #-------------------------------------------------------------------------------
       
   398 
       
   399     def OnChar(self, event):
       
   400         if self.GetDrawingMode() == FREEDRAWING_MODE:
       
   401             Viewer.OnChar(self, event)
       
   402         else:
       
   403             keycode = event.GetKeyCode()
       
   404             if keycode == WXK_DELETE and self.SelectedElement:
       
   405                 if self.SelectedElement in self.Blocks:
       
   406                     self.SelectedElement.Delete()
       
   407                 elif self.SelectedElement in self.Wires:
       
   408                     self.DeleteWire(self.SelectedElement)
       
   409                 elif self.SelectedElement not in self.Elements:
       
   410                     all_wires = True
       
   411                     for element in self.SelectedElement.GetElements():
       
   412                         all_wires &= element in self.Wires
       
   413                     if all_wires:
       
   414                         self.DeleteWire(self.SelectedElement)
       
   415                     else:
       
   416                         self.SelectedElement.Delete()
   344             self.Refresh()
   417             self.Refresh()
   345         event.Skip()
       
   346 
       
   347     def OnViewerMotion(self, event):
       
   348         if self.rubberBand.IsShown():
       
   349             self.rubberBand.OnMotion(event, self.Scaling)
       
   350         event.Skip()
       
   351 
       
   352 #-------------------------------------------------------------------------------
       
   353 #                          Keyboard event functions
       
   354 #-------------------------------------------------------------------------------
       
   355 
       
   356     def OnChar(self, event):
       
   357         keycode = event.GetKeyCode()
       
   358         if keycode == WXK_DELETE and self.SelectedElement:
       
   359             if self.SelectedElement in self.Blocks:
       
   360                 self.SelectedElement.Delete()
       
   361             elif self.SelectedElement in self.Wires:
       
   362                 self.DeleteWire(self.SelectedElement)
       
   363             elif self.SelectedElement not in self.Elements:
       
   364                 all_wires = True
       
   365                 for element in self.SelectedElement.GetElements():
       
   366                     all_wires &= element in self.Wires
       
   367                 if all_wires:
       
   368                     self.DeleteWire(self.SelectedElement)
       
   369                 else:
       
   370                     self.SelectedElement.Delete()
       
   371         self.Refresh()
       
   372         event.Skip()
   418         event.Skip()
   373 
   419 
   374 #-------------------------------------------------------------------------------
   420 #-------------------------------------------------------------------------------
   375 #                          Adding element functions
   421 #                          Adding element functions
   376 #-------------------------------------------------------------------------------
   422 #-------------------------------------------------------------------------------
   667                 for element in left_elements:
   713                 for element in left_elements:
   668                     left_powerrail &= isinstance(element.GetParentBlock(), LD_PowerRail)
   714                     left_powerrail &= isinstance(element.GetParentBlock(), LD_PowerRail)
   669                 for element in right_elements:
   715                 for element in right_elements:
   670                     right_powerrail &= isinstance(element.GetParentBlock(), LD_PowerRail)
   716                     right_powerrail &= isinstance(element.GetParentBlock(), LD_PowerRail)
   671                 if not left_powerrail or not right_powerrail:
   717                 if not left_powerrail or not right_powerrail:
       
   718                     wires = []
   672                     if left_powerrail:
   719                     if left_powerrail:
   673                         powerrail = left_elements[0].GetParentBlock()
   720                         powerrail = left_elements[0].GetParentBlock()
   674                         index = 0
   721                         index = 0
   675                         for left_element in left_elements: 
   722                         for left_element in left_elements: 
   676                             index = max(index, powerrail.GetConnectorIndex(left_element))
   723                             index = max(index, powerrail.GetConnectorIndex(left_element))
   677                         if powerrail.IsNullConnector(index + 1):
   724                         if powerrail.IsNullConnector(index + 1):
   678                             powerrail.DeleteConnector(index + 1)
   725                             powerrail.DeleteConnector(index + 1)
   679                         powerrail.InsertConnector(index + 1)
   726                         powerrail.InsertConnector(index + 1)
   680                         powerrail.RefreshModel()
   727                         powerrail.RefreshModel()
   681                         connectors = powerrail.GetConnectors()
   728                         connectors = powerrail.GetConnectors()
       
   729                         right_elements.reverse()
   682                         for i, right_element in enumerate(right_elements):
   730                         for i, right_element in enumerate(right_elements):
   683                             new_wire = Wire(self)
   731                             new_wire = Wire(self)
       
   732                             wires.append(new_wire)
   684                             right_element.InsertConnect(right_index[i] + 1, (new_wire, 0), False)
   733                             right_element.InsertConnect(right_index[i] + 1, (new_wire, 0), False)
   685                             connectors[index + 1].Connect((new_wire, -1), False)
   734                             connectors[index + 1].Connect((new_wire, -1), False)
   686                             new_wire.ConnectStartPoint(None, right_element)
   735                             new_wire.ConnectStartPoint(None, right_element)
   687                             new_wire.ConnectEndPoint(None, connectors[index + 1])
   736                             new_wire.ConnectEndPoint(None, connectors[index + 1])
   688                             self.Wires.append(new_wire)
       
   689                             self.Elements.append(new_wire)
       
   690                             rung.SelectElement(new_wire)
       
   691                         right_elements.reverse()
   737                         right_elements.reverse()
   692                     elif right_powerrail:
   738                     elif right_powerrail:
   693                         dialog = LDElementDialog(self.Parent, "coil")
   739                         dialog = LDElementDialog(self.Parent, "coil")
   694                         varlist = []
   740                         varlist = []
   695                         vars = self.Controler.GetCurrentElementEditingInterfaceVars()
   741                         vars = self.Controler.GetCurrentElementEditingInterfaceVars()
   729                             coil_connectors["output"].Connect((wire, -1), False)
   775                             coil_connectors["output"].Connect((wire, -1), False)
   730                             wire.ConnectStartPoint(None, connectors[index + 1])
   776                             wire.ConnectStartPoint(None, connectors[index + 1])
   731                             wire.ConnectEndPoint(None, coil_connectors["output"])
   777                             wire.ConnectEndPoint(None, coil_connectors["output"])
   732                             self.Wires.append(wire)
   778                             self.Wires.append(wire)
   733                             self.Elements.append(wire)
   779                             self.Elements.append(wire)
   734                             rung.SelectElement(wire)                                
   780                             rung.SelectElement(wire)
       
   781                             left_elements.reverse()
   735                             for i, left_element in enumerate(left_elements):
   782                             for i, left_element in enumerate(left_elements):
   736                                 # Create Wire between LeftPowerRail and Coil
   783                                 # Create Wire between LeftPowerRail and Coil
   737                                 new_wire = Wire(self)
   784                                 new_wire = Wire(self)
       
   785                                 wires.append(new_wire)
   738                                 coil_connectors["input"].Connect((new_wire, 0), False)
   786                                 coil_connectors["input"].Connect((new_wire, 0), False)
   739                                 left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False)
   787                                 left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False)
   740                                 new_wire.ConnectStartPoint(None, coil_connectors["input"])
   788                                 new_wire.ConnectStartPoint(None, coil_connectors["input"])
   741                                 new_wire.ConnectEndPoint(None, left_element)
   789                                 new_wire.ConnectEndPoint(None, left_element)
   742                                 self.Wires.append(new_wire)
       
   743                                 self.Elements.append(new_wire)
       
   744                                 rung.SelectElement(new_wire)
       
   745                             left_elements.reverse()
       
   746                             self.RefreshPosition(coil)
   790                             self.RefreshPosition(coil)
   747                     else:
   791                     else:
   748                         left_elements.reverse()
   792                         left_elements.reverse()
   749                         right_elements.reverse()
   793                         right_elements.reverse()
   750                         wires = []
       
   751                         for i, left_element in enumerate(left_elements):
   794                         for i, left_element in enumerate(left_elements):
   752                             for j, right_element in enumerate(right_elements):
   795                             for j, right_element in enumerate(right_elements):
   753                                 exist = False
   796                                 exist = False
   754                                 for wire, handle in right_element.GetWires():
   797                                 for wire, handle in right_element.GetWires():
   755                                     exist |= wire.EndConnected == left_element
   798                                     exist |= wire.EndConnected == left_element
   758                                     wires.append(new_wire)
   801                                     wires.append(new_wire)
   759                                     right_element.InsertConnect(right_index[j] + 1, (new_wire, 0), False)
   802                                     right_element.InsertConnect(right_index[j] + 1, (new_wire, 0), False)
   760                                     left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False)
   803                                     left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False)
   761                                     new_wire.ConnectStartPoint(None, right_element)
   804                                     new_wire.ConnectStartPoint(None, right_element)
   762                                     new_wire.ConnectEndPoint(None, left_element)
   805                                     new_wire.ConnectEndPoint(None, left_element)
   763                         wires.reverse()
   806                     wires.reverse()
   764                         for wire in wires:
   807                     for wire in wires:
   765                             self.Wires.append(wire)
   808                         self.Wires.append(wire)
   766                             self.Elements.append(wire)
   809                         self.Elements.append(wire)
   767                             rung.SelectElement(wire)
   810                         rung.SelectElement(wire)
   768                         right_elements.reverse()
   811                     right_elements.reverse()
   769                 for block in blocks:
   812                 for block in blocks:
   770                     self.RefreshPosition(block)
   813                     self.RefreshPosition(block)
   771                 for right_element in right_elements:
   814                 for right_element in right_elements:
   772                     self.RefreshPosition(right_element.GetParentBlock())
   815                     self.RefreshPosition(right_element.GetParentBlock())
   773                 self.SelectedElement.RefreshBoundingBox()
   816                 self.SelectedElement.RefreshBoundingBox()
   782         else:
   825         else:
   783             message = wxMessageDialog(self, "You must select the block or group of blocks around which a branch should be added!", "Error", wxOK|wxICON_ERROR)
   826             message = wxMessageDialog(self, "You must select the block or group of blocks around which a branch should be added!", "Error", wxOK|wxICON_ERROR)
   784             message.ShowModal()
   827             message.ShowModal()
   785             message.Destroy()
   828             message.Destroy()
   786 
   829 
   787 
       
   788     def AddBlock(self):
   830     def AddBlock(self):
   789         message = wxMessageDialog(self, "This option isn't available yet!", "Warning", wxOK|wxICON_EXCLAMATION)
   831         message = wxMessageDialog(self, "This option isn't available yet!", "Warning", wxOK|wxICON_EXCLAMATION)
   790         message.ShowModal()
   832         message.ShowModal()
   791         message.Destroy()
   833         message.Destroy()
   792 
   834 
   793 #-------------------------------------------------------------------------------
   835 #-------------------------------------------------------------------------------
   794 #                          Delete element functions
   836 #                          Delete element functions
   795 #-------------------------------------------------------------------------------
   837 #-------------------------------------------------------------------------------
   796 
   838 
   797     def DeleteContact(self, contact):
   839     def DeleteContact(self, contact):
   798         rungindex = self.FindRung(contact)
   840         if self.GetDrawingMode() == FREEDRAWING_MODE:
   799         rung = self.Rungs[rungindex]
   841             Viewer.DeleteContact(self, contact)
   800         old_bbox = rung.GetBoundingBox()
   842         else:
   801         connectors = contact.GetConnectors()
   843             rungindex = self.FindRung(contact)
   802         input_wires = [wire for wire, handle in connectors["input"].GetWires()]
   844             rung = self.Rungs[rungindex]
   803         output_wires = [wire for wire, handle in connectors["output"].GetWires()]
   845             old_bbox = rung.GetBoundingBox()
   804         left_elements = [(wire.EndConnected, wire.EndConnected.GetWireIndex(wire)) for wire in input_wires]
   846             connectors = contact.GetConnectors()
   805         right_elements = [(wire.StartConnected, wire.StartConnected.GetWireIndex(wire)) for wire in output_wires]
   847             input_wires = [wire for wire, handle in connectors["input"].GetWires()]
   806         for wire in input_wires:
   848             output_wires = [wire for wire, handle in connectors["output"].GetWires()]
   807             wire.Clean()
   849             left_elements = [(wire.EndConnected, wire.EndConnected.GetWireIndex(wire)) for wire in input_wires]
   808             rung.SelectElement(wire)
   850             right_elements = [(wire.StartConnected, wire.StartConnected.GetWireIndex(wire)) for wire in output_wires]
   809             self.Wires.remove(wire)
   851             for wire in input_wires:
   810             self.Elements.remove(wire)
   852                 wire.Clean()
   811         for wire in output_wires:
   853                 rung.SelectElement(wire)
   812             wire.Clean()
   854                 self.Wires.remove(wire)
   813             rung.SelectElement(wire)
   855                 self.Elements.remove(wire)
   814             self.Wires.remove(wire)
   856             for wire in output_wires:
   815             self.Elements.remove(wire)
   857                 wire.Clean()
   816         rung.SelectElement(contact)
   858                 rung.SelectElement(wire)
   817         contact.Clean()
   859                 self.Wires.remove(wire)
   818         left_elements.reverse()
   860                 self.Elements.remove(wire)
   819         right_elements.reverse()
   861             rung.SelectElement(contact)
   820         powerrail = len(left_elements) == 1 and isinstance(left_elements[0][0].GetParentBlock(), LD_PowerRail)
   862             contact.Clean()
   821         for left_element, left_index in left_elements:
   863             left_elements.reverse()
       
   864             right_elements.reverse()
       
   865             powerrail = len(left_elements) == 1 and isinstance(left_elements[0][0].GetParentBlock(), LD_PowerRail)
       
   866             for left_element, left_index in left_elements:
       
   867                 for right_element, right_index in right_elements:
       
   868                     wire_removed = []
       
   869                     for wire, handle in right_element.GetWires():
       
   870                         if wire.EndConnected == left_element:
       
   871                             wire_removed.append(wire)
       
   872                         elif isinstance(wire.EndConnected.GetParentBlock(), LD_PowerRail) and powerrail:
       
   873                             left_powerrail = wire.EndConnected.GetParentBlock()
       
   874                             index = left_powerrail.GetConnectorIndex(wire.EndConnected)
       
   875                             left_powerrail.DeleteConnector(index)
       
   876                             wire_removed.append(wire)
       
   877                     for wire in wire_removed:
       
   878                         wire.Clean()
       
   879                         self.Wires.remove(wire)
       
   880                         self.Elements.remove(wire)
       
   881                         rung.SelectElement(wire)
       
   882             wires = []
       
   883             for left_element, left_index in left_elements:
       
   884                 for right_element, right_index in right_elements:
       
   885                     wire = Wire(self)
       
   886                     wires.append(wire)
       
   887                     right_element.InsertConnect(right_index, (wire, 0), False)
       
   888                     left_element.InsertConnect(left_index, (wire, -1), False)
       
   889                     wire.ConnectStartPoint(None, right_element)
       
   890                     wire.ConnectEndPoint(None, left_element)
       
   891             wires.reverse()
       
   892             for wire in wires:
       
   893                 self.Wires.append(wire)
       
   894                 self.Elements.append(wire)
       
   895                 rung.SelectElement(wire)
       
   896             right_elements.reverse()
   822             for right_element, right_index in right_elements:
   897             for right_element, right_index in right_elements:
   823                 wire_removed = []
   898                 self.RefreshPosition(right_element.GetParentBlock())
   824                 for wire, handle in right_element.GetWires():
   899             self.Blocks.remove(contact)
   825                     if wire.EndConnected == left_element:
   900             self.Elements.remove(contact)
   826                         wire_removed.append(wire)
   901             self.Controler.RemoveCurrentElementEditingInstance(contact.GetId())
   827                     elif isinstance(wire.EndConnected.GetParentBlock(), LD_PowerRail) and powerrail:
   902             rung.RefreshBoundingBox()
   828                         left_powerrail = wire.EndConnected.GetParentBlock()
   903             new_bbox = rung.GetBoundingBox()
   829                         index = left_powerrail.GetConnectorIndex(wire.EndConnected)
   904             self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1)
   830                         left_powerrail.DeleteConnector(index)
   905             self.SelectedElement = None
   831                         wire_removed.append(wire)
       
   832                 for wire in wire_removed:
       
   833                     wire.Clean()
       
   834                     self.Wires.remove(wire)
       
   835                     self.Elements.remove(wire)
       
   836                     rung.SelectElement(wire)
       
   837         wires = []
       
   838         for left_element, left_index in left_elements:
       
   839             for right_element, right_index in right_elements:
       
   840                 wire = Wire(self)
       
   841                 wires.append(wire)
       
   842                 right_element.InsertConnect(right_index, (wire, 0), False)
       
   843                 left_element.InsertConnect(left_index, (wire, -1), False)
       
   844                 wire.ConnectStartPoint(None, right_element)
       
   845                 wire.ConnectEndPoint(None, left_element)
       
   846         wires.reverse()
       
   847         for wire in wires:
       
   848             self.Wires.append(wire)
       
   849             self.Elements.append(wire)
       
   850             rung.SelectElement(wire)
       
   851         right_elements.reverse()
       
   852         for right_element, right_index in right_elements:
       
   853             self.RefreshPosition(right_element.GetParentBlock())
       
   854         self.Blocks.remove(contact)
       
   855         self.Elements.remove(contact)
       
   856         self.Controler.RemoveCurrentElementEditingInstance(contact.GetId())
       
   857         rung.RefreshBoundingBox()
       
   858         new_bbox = rung.GetBoundingBox()
       
   859         self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1)
       
   860         self.SelectedElement = None
       
   861 
   906 
   862     def RecursiveDeletion(self, element, rung):
   907     def RecursiveDeletion(self, element, rung):
   863         connectors = element.GetConnectors()
   908         connectors = element.GetConnectors()
   864         input_wires = [wire for wire, handle in connectors["input"].GetWires()]
   909         input_wires = [wire for wire, handle in connectors["input"].GetWires()]
   865         left_elements = [wire.EndConnected for wire in input_wires]
   910         left_elements = [wire.EndConnected for wire in input_wires]
   879                 self.RecursiveDeletion(block, rung)
   924                 self.RecursiveDeletion(block, rung)
   880             else:
   925             else:
   881                 self.RefreshPosition(block)
   926                 self.RefreshPosition(block)
   882 
   927 
   883     def DeleteCoil(self, coil):
   928     def DeleteCoil(self, coil):
   884         rungindex = self.FindRung(coil)
   929         if self.GetDrawingMode() == FREEDRAWING_MODE:
   885         rung = self.Rungs[rungindex]
   930             Viewer.DeleteContact(self, coil)
   886         old_bbox = rung.GetBoundingBox()
       
   887         nbcoils = 0
       
   888         for element in rung.GetElements():
       
   889             if isinstance(element, LD_Coil):
       
   890                 nbcoils += 1
       
   891         if nbcoils > 1:
       
   892             connectors = coil.GetConnectors()
       
   893             output_wires = [wire for wire, handle in connectors["output"].GetWires()]
       
   894             right_elements = [wire.StartConnected for wire in output_wires]
       
   895             for wire in output_wires:
       
   896                 wire.Clean()
       
   897                 self.Wires.remove(wire)
       
   898                 self.Elements.remove(wire)
       
   899                 rung.SelectElement(wire)
       
   900             for right_element in right_elements:
       
   901                 right_block = right_element.GetParentBlock()
       
   902                 if isinstance(right_block, LD_PowerRail):
       
   903                     if len(right_element.GetWires()) == 0:
       
   904                         index = right_block.GetConnectorIndex(right_element)
       
   905                         right_block.DeleteConnector(index)
       
   906                         powerrail_connectors = right_block.GetConnectors()
       
   907                         for connector in powerrail_connectors:
       
   908                             for wire, handle in connector.GetWires():
       
   909                                 block = wire.EndConnected.GetParentBlock()
       
   910                                 endpoint = wire.EndConnected.GetPosition(False)
       
   911                                 startpoint = connector.GetPosition(False)
       
   912                                 block.Move(0, startpoint.y - endpoint.y)
       
   913                                 self.RefreshPosition(block)
       
   914             self.RecursiveDeletion(coil, rung)
       
   915         else:
   931         else:
   916             for element in rung.GetElements():
   932             rungindex = self.FindRung(coil)
   917                 if element in self.Wires:
       
   918                     element.Clean()
       
   919                     self.Wires.remove(element)
       
   920                     self.Elements.remove(element)
       
   921             for element in rung.GetElements():
       
   922                 if element in self.Blocks:
       
   923                     self.Controler.RemoveCurrentElementEditingInstance(element.GetId())
       
   924                     self.Blocks.remove(element)
       
   925                     self.Elements.remove(element)
       
   926             self.Controler.RemoveCurrentElementEditingInstance(self.Comments[rungindex].GetId())
       
   927             self.Elements.remove(self.Comments[rungindex])
       
   928             self.Comments.pop(rungindex)
       
   929             self.Rungs.pop(rungindex)
       
   930             if rungindex < len(self.Rungs):
       
   931                 next_bbox = self.Rungs[rungindex].GetBoundingBox()
       
   932                 self.RefreshRungs(old_bbox.y - next_bbox.y, rungindex)
       
   933         self.SelectedElement = None
       
   934 
       
   935     def DeleteWire(self, wire):
       
   936         wires = []
       
   937         left_elements = []
       
   938         right_elements = []
       
   939         if wire in self.Wires:
       
   940             wires = [wire]
       
   941         elif wire not in self.Elements:
       
   942             for element in wire.GetElements():
       
   943                 if element in self.Wires:
       
   944                     wires.append(element)
       
   945                 else:
       
   946                     wires = []
       
   947                     break
       
   948         if len(wires) > 0:
       
   949             rungindex = self.FindRung(wires[0])
       
   950             rung = self.Rungs[rungindex]
   933             rung = self.Rungs[rungindex]
   951             old_bbox = rung.GetBoundingBox()
   934             old_bbox = rung.GetBoundingBox()
   952             for wire in wires:
   935             nbcoils = 0
   953                 connections = wire.GetSelectedSegmentConnections()
   936             for element in rung.GetElements():
   954                 left_block = wire.EndConnected.GetParentBlock()
   937                 if isinstance(element, LD_Coil):
   955                 if wire.EndConnected not in left_elements:
   938                     nbcoils += 1
   956                     left_elements.append(wire.EndConnected)
   939             if nbcoils > 1:
   957                 if wire.StartConnected not in right_elements:
   940                 connectors = coil.GetConnectors()
   958                     right_elements.append(wire.StartConnected)
   941                 output_wires = [wire for wire, handle in connectors["output"].GetWires()]
   959                 if connections == (False, False) or connections == (False, True) and isinstance(left_block, LD_PowerRail):
   942                 right_elements = [wire.StartConnected for wire in output_wires]
       
   943                 for wire in output_wires:
   960                     wire.Clean()
   944                     wire.Clean()
   961                     self.Wires.remove(wire)
   945                     self.Wires.remove(wire)
   962                     self.Elements.remove(wire)
   946                     self.Elements.remove(wire)
   963                     rung.SelectElement(wire)
   947                     rung.SelectElement(wire)
   964             for left_element in left_elements:
   948                 for right_element in right_elements:
   965                 left_block = left_element.GetParentBlock()
   949                     right_block = right_element.GetParentBlock()
   966                 if isinstance(left_block, LD_PowerRail):
   950                     if isinstance(right_block, LD_PowerRail):
   967                     if len(left_element.GetWires()) == 0:
   951                         if len(right_element.GetWires()) == 0:
   968                         index = left_block.GetConnectorIndex(left_element)
   952                             index = right_block.GetConnectorIndex(right_element)
   969                         left_block.DeleteConnector(index)
   953                             right_block.DeleteConnector(index)
   970                 else:
   954                             powerrail_connectors = right_block.GetConnectors()
   971                     connectors = left_block.GetConnectors()
   955                             for connector in powerrail_connectors:
   972                     output_connectors = []
   956                                 for wire, handle in connector.GetWires():
   973                     if "outputs" in connectors:
   957                                     block = wire.EndConnected.GetParentBlock()
   974                         output_connectors = connectors["outputs"]
   958                                     endpoint = wire.EndConnected.GetPosition(False)
   975                     if "output" in connectors:
   959                                     startpoint = connector.GetPosition(False)
   976                         output_connectors = [connectors["output"]]
   960                                     block.Move(0, startpoint.y - endpoint.y)
   977                     for connector in output_connectors:
   961                                     self.RefreshPosition(block)
   978                         for wire, handle in connector.GetWires():
   962                 self.RecursiveDeletion(coil, rung)
   979                             self.RefreshPosition(wire.StartConnected.GetParentBlock())
   963             else:
   980             for right_element in right_elements:
   964                 for element in rung.GetElements():
   981                 self.RefreshPosition(right_element.GetParentBlock())
   965                     if element in self.Wires:
   982             rung.RefreshBoundingBox()
   966                         element.Clean()
   983             new_bbox = rung.GetBoundingBox()
   967                         self.Wires.remove(element)
   984             self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1)
   968                         self.Elements.remove(element)
       
   969                 for element in rung.GetElements():
       
   970                     if element in self.Blocks:
       
   971                         self.Controler.RemoveCurrentElementEditingInstance(element.GetId())
       
   972                         self.Blocks.remove(element)
       
   973                         self.Elements.remove(element)
       
   974                 self.Controler.RemoveCurrentElementEditingInstance(self.Comments[rungindex].GetId())
       
   975                 self.Elements.remove(self.Comments[rungindex])
       
   976                 self.Comments.pop(rungindex)
       
   977                 self.Rungs.pop(rungindex)
       
   978                 if rungindex < len(self.Rungs):
       
   979                     next_bbox = self.Rungs[rungindex].GetBoundingBox()
       
   980                     self.RefreshRungs(old_bbox.y - next_bbox.y, rungindex)
   985             self.SelectedElement = None
   981             self.SelectedElement = None
       
   982 
       
   983     def DeleteWire(self, wire):
       
   984         if self.GetDrawingMode() == FREEDRAWING_MODE:
       
   985             Viewer.DeleteWire(self, wire)
       
   986         else:
       
   987             wires = []
       
   988             left_elements = []
       
   989             right_elements = []
       
   990             if wire in self.Wires:
       
   991                 wires = [wire]
       
   992             elif wire not in self.Elements:
       
   993                 for element in wire.GetElements():
       
   994                     if element in self.Wires:
       
   995                         wires.append(element)
       
   996                     else:
       
   997                         wires = []
       
   998                         break
       
   999             if len(wires) > 0:
       
  1000                 rungindex = self.FindRung(wires[0])
       
  1001                 rung = self.Rungs[rungindex]
       
  1002                 old_bbox = rung.GetBoundingBox()
       
  1003                 for wire in wires:
       
  1004                     connections = wire.GetSelectedSegmentConnections()
       
  1005                     left_block = wire.EndConnected.GetParentBlock()
       
  1006                     if wire.EndConnected not in left_elements:
       
  1007                         left_elements.append(wire.EndConnected)
       
  1008                     if wire.StartConnected not in right_elements:
       
  1009                         right_elements.append(wire.StartConnected)
       
  1010                     if connections == (False, False) or connections == (False, True) and isinstance(left_block, LD_PowerRail):
       
  1011                         wire.Clean()
       
  1012                         self.Wires.remove(wire)
       
  1013                         self.Elements.remove(wire)
       
  1014                         rung.SelectElement(wire)
       
  1015                 for left_element in left_elements:
       
  1016                     left_block = left_element.GetParentBlock()
       
  1017                     if isinstance(left_block, LD_PowerRail):
       
  1018                         if len(left_element.GetWires()) == 0:
       
  1019                             index = left_block.GetConnectorIndex(left_element)
       
  1020                             left_block.DeleteConnector(index)
       
  1021                     else:
       
  1022                         connectors = left_block.GetConnectors()
       
  1023                         output_connectors = []
       
  1024                         if "outputs" in connectors:
       
  1025                             output_connectors = connectors["outputs"]
       
  1026                         if "output" in connectors:
       
  1027                             output_connectors = [connectors["output"]]
       
  1028                         for connector in output_connectors:
       
  1029                             for wire, handle in connector.GetWires():
       
  1030                                 self.RefreshPosition(wire.StartConnected.GetParentBlock())
       
  1031                 for right_element in right_elements:
       
  1032                     self.RefreshPosition(right_element.GetParentBlock())
       
  1033                 rung.RefreshBoundingBox()
       
  1034                 new_bbox = rung.GetBoundingBox()
       
  1035                 self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1)
       
  1036                 self.SelectedElement = None
   986 
  1037 
   987 #-------------------------------------------------------------------------------
  1038 #-------------------------------------------------------------------------------
   988 #                        Refresh element position functions
  1039 #                        Refresh element position functions
   989 #-------------------------------------------------------------------------------
  1040 #-------------------------------------------------------------------------------
   990 
  1041 
  1028         if False in onlyone:
  1079         if False in onlyone:
  1029             interval += LD_WIRE_SIZE
  1080             interval += LD_WIRE_SIZE
  1030         movex = minx + interval - position[0]
  1081         movex = minx + interval - position[0]
  1031         element.Move(movex, 0)
  1082         element.Move(movex, 0)
  1032         position = element.GetPosition()
  1083         position = element.GetPosition()
       
  1084         blocks = []
       
  1085         for i, connector in enumerate(input_connectors):
       
  1086             for j, (wire, handle) in enumerate(connector.GetWires()):
       
  1087                 blocks.append(wire.EndConnected.GetParentBlock())
  1033         for i, connector in enumerate(input_connectors):
  1088         for i, connector in enumerate(input_connectors):
  1034             startpoint = connector.GetPosition(False)
  1089             startpoint = connector.GetPosition(False)
  1035             previous_blocks = []
  1090             previous_blocks = []
  1036             block_list = []
  1091             block_list = []
  1037             start_offset = 0
  1092             start_offset = 0
  1048                 if j == 0:
  1103                 if j == 0:
  1049                     if not onlyone[i] and wire.EndConnected.GetWireIndex(wire) > 0:
  1104                     if not onlyone[i] and wire.EndConnected.GetWireIndex(wire) > 0:
  1050                         start_offset = endpoint.y - startpoint.y
  1105                         start_offset = endpoint.y - startpoint.y
  1051                     offset = start_offset
  1106                     offset = start_offset
  1052                 else:
  1107                 else:
  1053                     offset = start_offset + LD_LINE_SIZE * CalcBranchSize(previous_blocks, block)
  1108                     offset = start_offset + LD_LINE_SIZE * CalcBranchSize(previous_blocks, blocks)
  1054                 if block in block_list:
  1109                 if block in block_list:
  1055                     wires = wire.EndConnected.GetWires()
  1110                     wires = wire.EndConnected.GetWires()
  1056                     endmiddlepoint = wires[0][0].StartConnected.GetPosition(False)[0] - LD_WIRE_SIZE
  1111                     endmiddlepoint = wires[0][0].StartConnected.GetPosition(False)[0] - LD_WIRE_SIZE
  1057                     points = [startpoint, wxPoint(middlepoint, startpoint.y),
  1112                     points = [startpoint, wxPoint(middlepoint, startpoint.y),
  1058                               wxPoint(middlepoint, startpoint.y + offset),
  1113                               wxPoint(middlepoint, startpoint.y + offset),
  1085                                   wxPoint(middlepoint, endpoint.y), endpoint]
  1140                                   wxPoint(middlepoint, endpoint.y), endpoint]
  1086                     else:
  1141                     else:
  1087                         points = [startpoint, endpoint]
  1142                         points = [startpoint, endpoint]
  1088                 wire.SetPoints(points)
  1143                 wire.SetPoints(points)
  1089                 previous_blocks.append(block)
  1144                 previous_blocks.append(block)
       
  1145                 blocks.remove(block)
  1090                 ExtractNextBlocks(block, block_list)
  1146                 ExtractNextBlocks(block, block_list)
  1091         element.RefreshModel(False)
  1147         element.RefreshModel(False)
  1092         if recursive:
  1148         if recursive:
  1093             for connector in output_connectors:
  1149             for connector in output_connectors:
  1094                 for wire, handle in connector.GetWires():
  1150                 for wire, handle in connector.GetWires():
  1145             coil.SetType(values["type"])
  1201             coil.SetType(values["type"])
  1146             coil.RefreshModel(False)
  1202             coil.RefreshModel(False)
  1147             self.Refresh()
  1203             self.Refresh()
  1148         dialog.Destroy()
  1204         dialog.Destroy()
  1149 
  1205 
  1150 #-------------------------------------------------------------------------------
       
  1151 #                        Edit Ladder Element Properties Dialog
       
  1152 #-------------------------------------------------------------------------------
       
  1153 
       
  1154 [wxID_LDELEMENTDIALOG, wxID_LDELEMENTDIALOGMAINPANEL, 
       
  1155  wxID_LDELEMENTDIALOGNAME, wxID_LDELEMENTDIALOGRADIOBUTTON1, 
       
  1156  wxID_LDELEMENTDIALOGRADIOBUTTON2, wxID_LDELEMENTDIALOGRADIOBUTTON3,
       
  1157  wxID_LDELEMENTDIALOGRADIOBUTTON4, wxID_LDELEMENTDIALOGPREVIEW,
       
  1158  wxID_LDELEMENTDIALOGSTATICTEXT1, wxID_LDELEMENTDIALOGSTATICTEXT2, 
       
  1159  wxID_LDELEMENTDIALOGSTATICTEXT3, 
       
  1160 ] = [wx.NewId() for _init_ctrls in range(11)]
       
  1161 
       
  1162 class LDElementDialog(wx.Dialog):
       
  1163     def _init_coll_flexGridSizer1_Items(self, parent):
       
  1164         # generated method, don't edit
       
  1165 
       
  1166         parent.AddWindow(self.MainPanel, 0, border=0, flag=0)
       
  1167 
       
  1168     def _init_sizers(self):
       
  1169         # generated method, don't edit
       
  1170         self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
       
  1171 
       
  1172         self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
       
  1173 
       
  1174         self.SetSizer(self.flexGridSizer1)
       
  1175 
       
  1176     def _init_ctrls(self, prnt, title, labels):
       
  1177         # generated method, don't edit
       
  1178         wx.Dialog.__init__(self, id=wxID_LDELEMENTDIALOG,
       
  1179               name='VariablePropertiesDialog', parent=prnt, pos=wx.Point(376, 223),
       
  1180               size=wx.Size(350, 260), style=wx.DEFAULT_DIALOG_STYLE,
       
  1181               title=title)
       
  1182         self.SetClientSize(wx.Size(350, 260))
       
  1183 
       
  1184         self.MainPanel = wx.Panel(id=wxID_LDELEMENTDIALOGMAINPANEL,
       
  1185               name='MainPanel', parent=self, pos=wx.Point(0, 0),
       
  1186               size=wx.Size(340, 200), style=wx.TAB_TRAVERSAL)
       
  1187         self.MainPanel.SetAutoLayout(True)
       
  1188 
       
  1189         self.staticText1 = wx.StaticText(id=wxID_LDELEMENTDIALOGSTATICTEXT1,
       
  1190               label='Modifier:', name='staticText1', parent=self.MainPanel,
       
  1191               pos=wx.Point(24, 24), size=wx.Size(70, 17), style=0)
       
  1192 
       
  1193         self.staticText2 = wx.StaticText(id=wxID_LDELEMENTDIALOGSTATICTEXT2,
       
  1194               label='Name:', name='staticText2', parent=self.MainPanel,
       
  1195               pos=wx.Point(24, 150), size=wx.Size(70, 17), style=0)
       
  1196 
       
  1197         self.staticText3 = wx.StaticText(id=wxID_LDELEMENTDIALOGSTATICTEXT3,
       
  1198               label='Preview:', name='staticText3', parent=self.MainPanel,
       
  1199               pos=wx.Point(174, 24), size=wx.Size(100, 17), style=0)
       
  1200 
       
  1201         self.radioButton1 = wx.RadioButton(id=wxID_LDELEMENTDIALOGRADIOBUTTON1,
       
  1202               label=labels[0], name='radioButton1', parent=self.MainPanel,
       
  1203               pos=wx.Point(24, 48), size=wx.Size(114, 24), style=0)
       
  1204         EVT_RADIOBUTTON(self, wxID_LDELEMENTDIALOGRADIOBUTTON1, self.OnTypeChanged)
       
  1205         self.radioButton1.SetValue(True)
       
  1206 
       
  1207         self.radioButton2 = wx.RadioButton(id=wxID_LDELEMENTDIALOGRADIOBUTTON2,
       
  1208               label=labels[1], name='radioButton2', parent=self.MainPanel, 
       
  1209               pos=wx.Point(24, 72), size=wx.Size(128, 24), style=0)
       
  1210         EVT_RADIOBUTTON(self, wxID_LDELEMENTDIALOGRADIOBUTTON2, self.OnTypeChanged)
       
  1211 
       
  1212         self.radioButton3 = wx.RadioButton(id=wxID_LDELEMENTDIALOGRADIOBUTTON3,
       
  1213               label=labels[2], name='radioButton3', parent=self.MainPanel,
       
  1214               pos=wx.Point(24, 96), size=wx.Size(114, 24), style=0)
       
  1215         EVT_RADIOBUTTON(self, wxID_LDELEMENTDIALOGRADIOBUTTON3, self.OnTypeChanged)
       
  1216 
       
  1217         self.radioButton4 = wx.RadioButton(id=wxID_LDELEMENTDIALOGRADIOBUTTON4,
       
  1218               label=labels[3], name='radioButton4', parent=self.MainPanel, 
       
  1219               pos=wx.Point(24, 120), size=wx.Size(128, 24), style=0)
       
  1220         EVT_RADIOBUTTON(self, wxID_LDELEMENTDIALOGRADIOBUTTON4, self.OnTypeChanged)
       
  1221 
       
  1222         self.Name = wx.Choice(id=wxID_LDELEMENTDIALOGNAME,
       
  1223               name='Name', parent=self.MainPanel, pos=wx.Point(24, 174),
       
  1224               size=wx.Size(145, 24), style=0)
       
  1225         EVT_CHOICE(self, wxID_LDELEMENTDIALOGNAME, self.OnNameChanged)
       
  1226 
       
  1227         self.Preview = wx.Panel(id=wxID_LDELEMENTDIALOGPREVIEW,
       
  1228               name='Preview', parent=self.MainPanel, pos=wx.Point(174, 48),
       
  1229               size=wx.Size(150, 150), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
       
  1230         self.Preview.SetBackgroundColour(wxColour(255,255,255))
       
  1231 
       
  1232         self._init_sizers()
       
  1233 
       
  1234     def __init__(self, parent, type):
       
  1235         self.Type = type
       
  1236         if type == "contact":
       
  1237             self._init_ctrls(parent, "Edit Contact Values", ['Normal','Negate','Rising Edge','Falling Edge'])
       
  1238             self.Element = LD_Contact(self.Preview, CONTACT_NORMAL, "")
       
  1239         elif type == "coil":
       
  1240             self._init_ctrls(parent, "Edit Coil Values", ['Normal','Negate','Set','Reset'])
       
  1241             self.Element = LD_Coil(self.Preview, COIL_NORMAL, "")
       
  1242         self.Element.SetPosition((150 - LD_ELEMENT_SIZE[0]) / 2, (150 - LD_ELEMENT_SIZE[1]) / 2)
       
  1243 
       
  1244         self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
       
  1245         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
       
  1246 
       
  1247         EVT_PAINT(self, self.OnPaint)
       
  1248 
       
  1249     def SetVariables(self, vars):
       
  1250         self.Name.Clear()
       
  1251         for name in vars:
       
  1252             self.Name.Append(name)
       
  1253         self.Name.Enable(self.Name.GetCount() > 0)
       
  1254 
       
  1255     def SetValues(self, values):
       
  1256         for name, value in values.items():
       
  1257             if name == "name":
       
  1258                 self.Element.SetName(value)
       
  1259                 self.Name.SetStringSelection(value)
       
  1260             elif name == "type":
       
  1261                 self.Element.SetType(value)
       
  1262                 if self.Type == "contact":
       
  1263                     if value == CONTACT_NORMAL:
       
  1264                         self.radioButton1.SetValue(True)
       
  1265                     elif value == CONTACT_REVERSE:
       
  1266                         self.radioButton2.SetValue(True)
       
  1267                     elif value == CONTACT_RISING:
       
  1268                         self.radioButton3.SetValue(True)
       
  1269                     elif value == CONTACT_FALLING:
       
  1270                         self.radioButton4.SetValue(True)
       
  1271                 elif self.Type == "coil":
       
  1272                     if value == COIL_NORMAL:
       
  1273                         self.radioButton1.SetValue(True)
       
  1274                     elif value == COIL_REVERSE:
       
  1275                         self.radioButton2.SetValue(True)
       
  1276                     elif value == COIL_SET:
       
  1277                         self.radioButton3.SetValue(True)
       
  1278                     elif value == COIL_RESET:
       
  1279                         self.radioButton4.SetValue(True)
       
  1280 
       
  1281     def GetValues(self):
       
  1282         values = {}
       
  1283         values["name"] = self.Element.GetName()
       
  1284         values["type"] = self.Element.GetType()
       
  1285         return values
       
  1286 
       
  1287     def OnTypeChanged(self, event):
       
  1288         if self.Type == "contact":
       
  1289             if self.radioButton1.GetValue():
       
  1290                 self.Element.SetType(CONTACT_NORMAL)
       
  1291             elif self.radioButton2.GetValue():
       
  1292                 self.Element.SetType(CONTACT_REVERSE)
       
  1293             elif self.radioButton3.GetValue():
       
  1294                 self.Element.SetType(CONTACT_RISING)
       
  1295             elif self.radioButton4.GetValue():
       
  1296                 self.Element.SetType(CONTACT_FALLING)
       
  1297         elif self.Type == "coil":
       
  1298             if self.radioButton1.GetValue():
       
  1299                 self.Element.SetType(COIL_NORMAL)
       
  1300             elif self.radioButton2.GetValue():
       
  1301                 self.Element.SetType(COIL_REVERSE)
       
  1302             elif self.radioButton3.GetValue():
       
  1303                 self.Element.SetType(COIL_SET)
       
  1304             elif self.radioButton4.GetValue():
       
  1305                 self.Element.SetType(COIL_RESET)
       
  1306         self.RefreshPreview()
       
  1307         event.Skip()
       
  1308 
       
  1309     def OnNameChanged(self, event):
       
  1310         self.Element.SetName(self.Name.GetStringSelection())
       
  1311         self.RefreshPreview()
       
  1312         event.Skip()
       
  1313 
       
  1314     def RefreshPreview(self):
       
  1315         dc = wxClientDC(self.Preview)
       
  1316         dc.Clear()
       
  1317         self.Element.Draw(dc)
       
  1318 
       
  1319     def OnPaint(self, event):
       
  1320         self.RefreshPreview()
       
  1321         event.Skip()