LDViewer.py
changeset 42 4a8400732001
parent 28 fc23e1f415d8
child 45 42637f721b5b
equal deleted inserted replaced
41:0688db995ddf 42:4a8400732001
   171 class LD_Viewer(Viewer):
   171 class LD_Viewer(Viewer):
   172 
   172 
   173     def __init__(self, parent, window, controler):
   173     def __init__(self, parent, window, controler):
   174         Viewer.__init__(self, parent, window, controler)
   174         Viewer.__init__(self, parent, window, controler)
   175         self.Rungs = []
   175         self.Rungs = []
   176         self.Comments = []
   176         self.RungComments = []
   177 
   177 
   178 #-------------------------------------------------------------------------------
   178 #-------------------------------------------------------------------------------
   179 #                          Refresh functions
   179 #                          Refresh functions
   180 #-------------------------------------------------------------------------------
   180 #-------------------------------------------------------------------------------
   181 
   181 
   182     def RefreshView(self):
   182     def RefreshView(self):
   183         Viewer.RefreshView(self)
   183         Viewer.RefreshView(self)
   184         for i, rung in enumerate(self.Rungs):
   184         for i, rung in enumerate(self.Rungs):
   185             bbox = rung.GetBoundingBox()
   185             bbox = rung.GetBoundingBox()
   186             if i < len(self.Comments):
   186             if i < len(self.RungComments):
   187                 pos = self.Comments[i].GetPosition()
   187                 pos = self.RungComments[i].GetPosition()
   188                 if pos[1] > bbox.y:
   188                 if pos[1] > bbox.y:
   189                     self.Comment.insert(i, None)
   189                     self.RungComments.insert(i, None)
   190             else:
   190             else:
   191                 self.Comment.insert(i, None)
   191                 self.RungComments.insert(i, None)
   192     
   192     
   193     def loadInstance(self, instance, ids):
   193     def loadInstance(self, instance, ids):
   194         Viewer.loadInstance(self, instance, ids)
   194         Viewer.loadInstance(self, instance, ids)
   195         if instance["type"] == "leftPowerRail":
   195         if instance["type"] == "leftPowerRail":
   196             element = self.FindElementById(instance["id"])
   196             element = self.FindElementById(instance["id"])
   230         elif instance["type"] == "comment":
   230         elif instance["type"] == "comment":
   231             element = self.FindElementById(instance["id"])
   231             element = self.FindElementById(instance["id"])
   232             pos = element.GetPosition()
   232             pos = element.GetPosition()
   233             i = 0
   233             i = 0
   234             inserted = False
   234             inserted = False
   235             while i < len(self.Comments) and not inserted: 
   235             while i < len(self.RungComments) and not inserted: 
   236                 ipos = self.Comments[i].GetPosition()
   236                 ipos = self.RungComments[i].GetPosition()
   237                 if pos[1] < ipos[1]:
   237                 if pos[1] < ipos[1]:
   238                     self.Comments.insert(i, element)
   238                     self.RungComments.insert(i, element)
   239                     inserted = True
   239                     inserted = True
   240                 i += 1
   240                 i += 1
   241             if not inserted:
   241             if not inserted:
   242                 self.Comments.append(element)
   242                 self.RungComments.append(element)
   243 
   243 
   244 #-------------------------------------------------------------------------------
   244 #-------------------------------------------------------------------------------
   245 #                          Search Element functions
   245 #                          Search Element functions
   246 #-------------------------------------------------------------------------------
   246 #-------------------------------------------------------------------------------
   247 
   247 
   254     def FindElement(self, pos):
   254     def FindElement(self, pos):
   255         if self.GetDrawingMode() == FREEDRAWING_MODE:
   255         if self.GetDrawingMode() == FREEDRAWING_MODE:
   256             return Viewer.FindElement(self, pos)
   256             return Viewer.FindElement(self, pos)
   257         
   257         
   258         elements = []
   258         elements = []
   259         for element in self.Elements:
   259         for element in self.GetElements(sort_wires=True):
   260             if element.HitTest(pos) or element.TestHandle(pos) != (0, 0):
   260             if element.HitTest(pos) or element.TestHandle(pos) != (0, 0):
   261                 elements.append(element)
   261                 elements.append(element)
   262         if len(elements) == 1:
   262         if len(elements) == 1:
   263             return elements[0]
   263             return elements[0]
   264         elif len(elements) > 1:
   264         elif len(elements) > 1:
   265             group = Graphic_Group(self)
   265             group = Graphic_Group(self)
   266             for element in elements:
   266             for element in elements:
   267                 if element in self.Blocks:
   267                 if self.IsBlock(element):
   268                     return element
   268                     return element
   269                 group.SelectElement(element)
   269                 group.SelectElement(element)
   270             return group
   270             return group
   271         return None
   271         return None
   272 
   272 
   274         if self.GetDrawingMode() == FREEDRAWING_MODE:
   274         if self.GetDrawingMode() == FREEDRAWING_MODE:
   275             return Viewer.SearchElements(self, bbox)
   275             return Viewer.SearchElements(self, bbox)
   276         
   276         
   277         elements = []
   277         elements = []
   278         for element in self.Blocks:
   278         for element in self.Blocks:
   279             element_bbox = element.GetBoundingBox()
   279             if element.IsInSelection(bbox):
   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:
       
   281                 elements.append(element)
   280                 elements.append(element)
   282         return elements
   281         return elements
   283 
   282 
   284 #-------------------------------------------------------------------------------
   283 #-------------------------------------------------------------------------------
   285 #                          Mouse event functions
   284 #                          Mouse event functions
   291         elif self.Mode == MODE_SELECTION:
   290         elif self.Mode == MODE_SELECTION:
   292             dc = self.GetLogicalDC()
   291             dc = self.GetLogicalDC()
   293             pos = event.GetLogicalPosition(dc)
   292             pos = event.GetLogicalPosition(dc)
   294             element = self.FindElement(pos)
   293             element = self.FindElement(pos)
   295             if self.SelectedElement:
   294             if self.SelectedElement:
   296                 if self.SelectedElement in self.Elements:
   295                 if not isinstance(self.SelectedElement, Graphic_Group):
   297                     if self.SelectedElement != element:
   296                     if self.SelectedElement != element:
   298                         if self.SelectedElement in self.Wires:
   297                         if self.IsWire(self.SelectedElement):
   299                             self.SelectedElement.SetSelectedSegment(None)
   298                             self.SelectedElement.SetSelectedSegment(None)
   300                         else:
   299                         else:
   301                             self.SelectedElement.SetSelected(False)
   300                             self.SelectedElement.SetSelected(False)
   302                     else:
   301                     else:
   303                         self.SelectedElement = None
   302                         self.SelectedElement = None
   304                 elif element and element not in self.Elements:
   303                 elif element and isinstance(element, Graphic_Group):
   305                     if self.SelectedElement.GetElements() != element.GetElements():
   304                     if self.SelectedElement.GetElements() != element.GetElements():
   306                         for elt in self.SelectedElement.GetElements():
   305                         for elt in self.SelectedElement.GetElements():
   307                             if elt in self.Wires:
   306                             if self.IsWire(elt):
   308                                 elt.SetSelectedSegment(None)
   307                                 elt.SetSelectedSegment(None)
   309                         self.SelectedElement.SetSelected(False)
   308                         self.SelectedElement.SetSelected(False)
   310                         self.SelectedElement = None
   309                         self.SelectedElement = None
   311                 else:
   310                 else:
   312                     for elt in self.SelectedElement.GetElements():
   311                     for elt in self.SelectedElement.GetElements():
   313                         if elt in self.Wires:
   312                         if self.IsWire(elt):
   314                             elt.SetSelectedSegment(None)
   313                             elt.SetSelectedSegment(None)
   315                     self.SelectedElement.SetSelected(False)
   314                     self.SelectedElement.SetSelected(False)
   316                     self.SelectedElement = None
   315                     self.SelectedElement = None
   317                 self.Refresh()
   316                 self.Refresh()
   318             if element:
   317             if element:
   336                     self.SelectedElement.SetElements(elements)
   335                     self.SelectedElement.SetElements(elements)
   337                     self.SelectedElement.SetSelected(True)
   336                     self.SelectedElement.SetSelected(True)
   338                     self.Refresh()
   337                     self.Refresh()
   339         elif self.Mode == MODE_SELECTION and self.SelectedElement:
   338         elif self.Mode == MODE_SELECTION and self.SelectedElement:
   340             dc = self.GetLogicalDC() 
   339             dc = self.GetLogicalDC() 
   341             if self.SelectedElement in self.Elements:
   340             if not isinstance(self.SelectedElement, Graphic_Group):
   342                 if self.SelectedElement in self.Wires:
   341                 if self.IsWire(self.SelectedElement):
   343                     result = self.SelectedElement.TestSegment(event.GetLogicalPosition(dc), True)
   342                     result = self.SelectedElement.TestSegment(event.GetLogicalPosition(dc), True)
   344                     if result and result[1] in [EAST, WEST]:
   343                     if result and result[1] in [EAST, WEST]:
   345                         self.SelectedElement.SetSelectedSegment(result[0])
   344                         self.SelectedElement.SetSelectedSegment(result[0])
   346                 else:
   345                 else:
   347                     self.SelectedElement.OnLeftUp(event, dc, self.Scaling)
   346                     self.SelectedElement.OnLeftUp(event, dc, self.Scaling)
   348             else:
   347             else:
   349                 for element in self.SelectedElement.GetElements():
   348                 for element in self.SelectedElement.GetElements():
   350                     if element in self.Wires:
   349                     if self.IsWire(element):
   351                         result = element.TestSegment(event.GetLogicalPosition(dc), True)
   350                         result = element.TestSegment(event.GetLogicalPosition(dc), True)
   352                         if result and result[1] in [EAST, WEST]:
   351                         if result and result[1] in [EAST, WEST]:
   353                             element.SetSelectedSegment(result[0])
   352                             element.SetSelectedSegment(result[0])
   354                     else:
   353                     else:
   355                         element.OnLeftUp(event, dc, self.Scaling)
   354                         element.OnLeftUp(event, dc, self.Scaling)
   367             element = self.FindElement(pos)
   366             element = self.FindElement(pos)
   368             if element:
   367             if element:
   369                 if self.SelectedElement and self.SelectedElement != element:
   368                 if self.SelectedElement and self.SelectedElement != element:
   370                     self.SelectedElement.SetSelected(False)
   369                     self.SelectedElement.SetSelected(False)
   371                 self.SelectedElement = element
   370                 self.SelectedElement = element
   372                 if self.SelectedElement in self.Wires:
   371                 if self.IsWire(self.SelectedElement):
   373                     self.SelectedElement.SetSelectedSegment(0)
   372                     self.SelectedElement.SetSelectedSegment(0)
   374                 else:
   373                 else:
   375                     self.SelectedElement.SetSelected(True)
   374                     self.SelectedElement.SetSelected(True)
   376                     self.SelectedElement.OnRightUp(event, dc, self.Scaling)
   375                     self.SelectedElement.OnRightUp(event, dc, self.Scaling)
   377                 wxCallAfter(self.SetCursor, wxNullCursor)
   376                 wxCallAfter(self.SetCursor, wxNullCursor)
   388         event.Skip()
   387         event.Skip()
   389 
   388 
   390     def OnViewerMotion(self, event):
   389     def OnViewerMotion(self, event):
   391         if self.GetDrawingMode() == FREEDRAWING_MODE:
   390         if self.GetDrawingMode() == FREEDRAWING_MODE:
   392             Viewer.OnViewerMotion(self, event)
   391             Viewer.OnViewerMotion(self, event)
       
   392         elif self.rubberBand.IsShown():
       
   393             self.rubberBand.OnMotion(event, self.GetLogicalDC(), self.Scaling)
   393         event.Skip()
   394         event.Skip()
   394 
   395 
   395 #-------------------------------------------------------------------------------
   396 #-------------------------------------------------------------------------------
   396 #                          Keyboard event functions
   397 #                          Keyboard event functions
   397 #-------------------------------------------------------------------------------
   398 #-------------------------------------------------------------------------------
   400         if self.GetDrawingMode() == FREEDRAWING_MODE:
   401         if self.GetDrawingMode() == FREEDRAWING_MODE:
   401             Viewer.OnChar(self, event)
   402             Viewer.OnChar(self, event)
   402         else:
   403         else:
   403             keycode = event.GetKeyCode()
   404             keycode = event.GetKeyCode()
   404             if keycode == WXK_DELETE and self.SelectedElement:
   405             if keycode == WXK_DELETE and self.SelectedElement:
   405                 if self.SelectedElement in self.Blocks:
   406                 if self.IsBlock(self.SelectedElement):
   406                     self.SelectedElement.Delete()
   407                     self.SelectedElement.Delete()
   407                 elif self.SelectedElement in self.Wires:
   408                 elif self.IsWire(self.SelectedElement):
   408                     self.DeleteWire(self.SelectedElement)
   409                     self.DeleteWire(self.SelectedElement)
   409                 elif self.SelectedElement not in self.Elements:
   410                 elif isinstance(self.SelectedElement, Graphic_Group):
   410                     all_wires = True
   411                     all_wires = True
   411                     for element in self.SelectedElement.GetElements():
   412                     for element in self.SelectedElement.GetElements():
   412                         all_wires &= element in self.Wires
   413                         all_wires &= self.IsWire(element)
   413                     if all_wires:
   414                     if all_wires:
   414                         self.DeleteWire(self.SelectedElement)
   415                         self.DeleteWire(self.SelectedElement)
   415                     else:
   416                     else:
   416                         self.SelectedElement.Delete()
   417                         self.SelectedElement.Delete()
   417             self.Refresh()
   418             self.Refresh()
   419 
   420 
   420 #-------------------------------------------------------------------------------
   421 #-------------------------------------------------------------------------------
   421 #                          Adding element functions
   422 #                          Adding element functions
   422 #-------------------------------------------------------------------------------
   423 #-------------------------------------------------------------------------------
   423 
   424 
   424     def AddRung(self):
   425     def AddLadderRung(self):
   425         dialog = LDElementDialog(self.Parent, "coil")
   426         dialog = LDElementDialog(self.Parent, "coil")
   426         varlist = []
   427         varlist = []
   427         vars = self.Controler.GetCurrentElementEditingInterfaceVars()
   428         vars = self.Controler.GetCurrentElementEditingInterfaceVars()
   428         if vars:
   429         if vars:
   429             for var in vars:
   430             for var in vars:
   445             # Create comment
   446             # Create comment
   446             id = self.GetNewId()
   447             id = self.GetNewId()
   447             comment = Comment(self, "Commentaire", id)
   448             comment = Comment(self, "Commentaire", id)
   448             comment.SetPosition(startx, starty)
   449             comment.SetPosition(startx, starty)
   449             comment.SetSize(LD_COMMENT_DEFAULTSIZE[0], LD_COMMENT_DEFAULTSIZE[1])
   450             comment.SetSize(LD_COMMENT_DEFAULTSIZE[0], LD_COMMENT_DEFAULTSIZE[1])
   450             self.Elements.append(comment)
   451             self.AddComment(comment)
   451             self.Comments.append(comment)
   452             self.RungComments.append(comment)
   452             self.Controler.AddCurrentElementEditingComment(id)
   453             self.Controler.AddCurrentElementEditingComment(id)
   453             self.RefreshCommentModel(comment)
   454             self.RefreshCommentModel(comment)
   454             starty += LD_COMMENT_DEFAULTSIZE[1] + LD_OFFSET[1]
   455             starty += LD_COMMENT_DEFAULTSIZE[1] + LD_OFFSET[1]
   455             # Create LeftPowerRail
   456             # Create LeftPowerRail
   456             id = self.GetNewId()
   457             id = self.GetNewId()
   457             leftpowerrail = LD_PowerRail(self, LEFTRAIL, id)
   458             leftpowerrail = LD_PowerRail(self, LEFTRAIL, id)
   458             leftpowerrail.SetPosition(startx, starty)
   459             leftpowerrail.SetPosition(startx, starty)
   459             self.Elements.append(leftpowerrail)
   460             self.AddBlock(leftpowerrail)
   460             self.Blocks.append(leftpowerrail)
       
   461             rung.SelectElement(leftpowerrail)
   461             rung.SelectElement(leftpowerrail)
   462             self.Controler.AddCurrentElementEditingPowerRail(id, LEFTRAIL)
   462             self.Controler.AddCurrentElementEditingPowerRail(id, LEFTRAIL)
   463             self.RefreshPowerRailModel(leftpowerrail)
   463             self.RefreshPowerRailModel(leftpowerrail)
   464             # Create Coil
   464             # Create Coil
   465             id = self.GetNewId()
   465             id = self.GetNewId()
   466             coil = LD_Coil(self, values["type"], values["name"], id)
   466             coil = LD_Coil(self, values["type"], values["name"], id)
   467             coil.SetPosition(startx, starty + (LD_LINE_SIZE - LD_ELEMENT_SIZE[1]) / 2)
   467             coil.SetPosition(startx, starty + (LD_LINE_SIZE - LD_ELEMENT_SIZE[1]) / 2)
   468             self.Elements.append(coil)
   468             self.AddBlock(coil)
   469             self.Blocks.append(coil)
       
   470             rung.SelectElement(coil)
   469             rung.SelectElement(coil)
   471             self.Controler.AddCurrentElementEditingCoil(id)
   470             self.Controler.AddCurrentElementEditingCoil(id)
   472             # Create Wire between LeftPowerRail and Coil
   471             # Create Wire between LeftPowerRail and Coil
   473             wire = Wire(self)
   472             wire = Wire(self)
   474             start_connector = coil.GetConnectors()["input"]
   473             start_connector = coil.GetConnectors()["input"]
   475             end_connector = leftpowerrail.GetConnectors()[0]
   474             end_connector = leftpowerrail.GetConnectors()[0]
   476             start_connector.Connect((wire, 0), False)
   475             start_connector.Connect((wire, 0), False)
   477             end_connector.Connect((wire, -1), False)
   476             end_connector.Connect((wire, -1), False)
   478             wire.ConnectStartPoint(None, start_connector)
   477             wire.ConnectStartPoint(None, start_connector)
   479             wire.ConnectEndPoint(None, end_connector)
   478             wire.ConnectEndPoint(None, end_connector)
   480             self.Wires.append(wire)
   479             self.AddWires(wire)
   481             self.Elements.append(wire)
       
   482             rung.SelectElement(wire)
   480             rung.SelectElement(wire)
   483             # Create RightPowerRail
   481             # Create RightPowerRail
   484             id = self.GetNewId()
   482             id = self.GetNewId()
   485             rightpowerrail = LD_PowerRail(self, RIGHTRAIL, id)
   483             rightpowerrail = LD_PowerRail(self, RIGHTRAIL, id)
   486             rightpowerrail.SetPosition(startx, starty)
   484             rightpowerrail.SetPosition(startx, starty)
   487             self.Elements.append(rightpowerrail)
   485             self.AddBlock(rightpowerrail)
   488             self.Blocks.append(rightpowerrail)
       
   489             rung.SelectElement(rightpowerrail)
   486             rung.SelectElement(rightpowerrail)
   490             self.Controler.AddCurrentElementEditingPowerRail(id, RIGHTRAIL)
   487             self.Controler.AddCurrentElementEditingPowerRail(id, RIGHTRAIL)
   491             # Create Wire between LeftPowerRail and Coil
   488             # Create Wire between LeftPowerRail and Coil
   492             wire = Wire(self)
   489             wire = Wire(self)
   493             start_connector = rightpowerrail.GetConnectors()[0]
   490             start_connector = rightpowerrail.GetConnectors()[0]
   494             end_connector = coil.GetConnectors()["output"]
   491             end_connector = coil.GetConnectors()["output"]
   495             start_connector.Connect((wire, 0), False)
   492             start_connector.Connect((wire, 0), False)
   496             end_connector.Connect((wire, -1), False)
   493             end_connector.Connect((wire, -1), False)
   497             wire.ConnectStartPoint(None, start_connector)
   494             wire.ConnectStartPoint(None, start_connector)
   498             wire.ConnectEndPoint(None, end_connector)
   495             wire.ConnectEndPoint(None, end_connector)
   499             self.Wires.append(wire)
   496             self.AddWires(wire)
   500             self.Elements.append(wire)
       
   501             rung.SelectElement(wire)
   497             rung.SelectElement(wire)
   502             self.RefreshPosition(coil)
   498             self.RefreshPosition(coil)
   503             self.Rungs.append(rung)
   499             self.Rungs.append(rung)
   504             self.Refresh()
   500             self.Refresh()
   505 
   501 
   506     def AddContact(self):
   502     def AddLadderContact(self):
   507         wires = []
   503         wires = []
   508         if self.SelectedElement in self.Wires:
   504         if self.IsWire(self.SelectedElement):
   509             left_element = self.SelectedElement.EndConnected
   505             left_element = self.SelectedElement.EndConnected
   510             if not isinstance(left_element.GetParentBlock(), LD_Coil):
   506             if not isinstance(left_element.GetParentBlock(), LD_Coil):
   511                 wires.append(self.SelectedElement)
   507                 wires.append(self.SelectedElement)
   512         elif self.SelectedElement and self.SelectedElement not in self.Elements:
   508         elif self.SelectedElement and isinstance(self.SelectedElement,Graphic_Group):
   513             if False not in [element in self.Wires for element in self.SelectedElement.GetElements()]:
   509             if False not in [self.IsWire(element) for element in self.SelectedElement.GetElements()]:
   514                 for element in self.SelectedElement.GetElements():
   510                 for element in self.SelectedElement.GetElements():
   515                     wires.append(element)
   511                     wires.append(element)
   516         if len(wires) > 0:
   512         if len(wires) > 0:
   517             dialog = LDElementDialog(self.Parent, "contact")
   513             dialog = LDElementDialog(self.Parent, "contact")
   518             varlist = []
   514             varlist = []
   527                 values = dialog.GetValues()
   523                 values = dialog.GetValues()
   528                 points = wires[0].GetSelectedSegmentPoints()
   524                 points = wires[0].GetSelectedSegmentPoints()
   529                 id = self.GetNewId()
   525                 id = self.GetNewId()
   530                 contact = LD_Contact(self, values["type"], values["name"], id)
   526                 contact = LD_Contact(self, values["type"], values["name"], id)
   531                 contact.SetPosition(0, points[0].y - (LD_ELEMENT_SIZE[1] + 1) / 2)
   527                 contact.SetPosition(0, points[0].y - (LD_ELEMENT_SIZE[1] + 1) / 2)
   532                 self.Elements.append(contact)
   528                 self.AddBlock(contact)
   533                 self.Blocks.append(contact)
       
   534                 self.Controler.AddCurrentElementEditingContact(id)
   529                 self.Controler.AddCurrentElementEditingContact(id)
   535                 rungindex = self.FindRung(wires[0])
   530                 rungindex = self.FindRung(wires[0])
   536                 rung = self.Rungs[rungindex]
   531                 rung = self.Rungs[rungindex]
   537                 old_bbox = rung.GetBoundingBox()
   532                 old_bbox = rung.GetBoundingBox()
   538                 rung.SelectElement(contact)
   533                 rung.SelectElement(contact)
   555                         idx = right_elements.index(wire.StartConnected)
   550                         idx = right_elements.index(wire.StartConnected)
   556                         right_index[idx] = min(right_index[idx], wire.StartConnected.GetWireIndex(wire))
   551                         right_index[idx] = min(right_index[idx], wire.StartConnected.GetWireIndex(wire))
   557                     wire.SetSelectedSegment(None)
   552                     wire.SetSelectedSegment(None)
   558                     wire.Clean()
   553                     wire.Clean()
   559                     rung.SelectElement(wire)
   554                     rung.SelectElement(wire)
   560                     self.Wires.remove(wire)
   555                     self.RemoveWire(wire)
   561                     self.Elements.remove(wire)
       
   562                 wires = []
   556                 wires = []
   563                 right_wires = []
   557                 right_wires = []
   564                 for i, left_element in enumerate(left_elements):
   558                 for i, left_element in enumerate(left_elements):
   565                     wire = Wire(self)
   559                     wire = Wire(self)
   566                     wires.append(wire)
   560                     wires.append(wire)
   576                     connectors["output"].Connect((wire, -1), False)
   570                     connectors["output"].Connect((wire, -1), False)
   577                     wire.ConnectStartPoint(None, right_element)
   571                     wire.ConnectStartPoint(None, right_element)
   578                     wire.ConnectEndPoint(None, connectors["output"])
   572                     wire.ConnectEndPoint(None, connectors["output"])
   579                 right_wires.reverse()
   573                 right_wires.reverse()
   580                 for wire in wires:
   574                 for wire in wires:
   581                     self.Wires.append(wire)
   575                     self.AddWire(wire)
   582                     self.Elements.append(wire)
       
   583                     rung.SelectElement(wire)
   576                     rung.SelectElement(wire)
   584                 self.RefreshPosition(contact)
   577                 self.RefreshPosition(contact)
   585                 if len(right_wires) > 1:
   578                 if len(right_wires) > 1:
   586                     group = Graphic_Group(self)
   579                     group = Graphic_Group(self)
   587                     group.SetSelected(False)
   580                     group.SetSelected(False)
   599         else:
   592         else:
   600             message = wxMessageDialog(self, "You must select the wire where a contact should be added!", "Error", wxOK|wxICON_ERROR)
   593             message = wxMessageDialog(self, "You must select the wire where a contact should be added!", "Error", wxOK|wxICON_ERROR)
   601             message.ShowModal()
   594             message.ShowModal()
   602             message.Destroy()
   595             message.Destroy()
   603 
   596 
   604     def AddBranch(self):
   597     def AddLadderBranch(self):
   605         blocks = []
   598         blocks = []
   606         if self.SelectedElement in self.Blocks:
   599         if self.IsBlock(self.SelectedElement):
   607             blocks = [self.SelectedElement]
   600             blocks = [self.SelectedElement]
   608         elif self.SelectedElement not in self.Elements:
   601         elif isinstance(self.SelectedElement, Graphic_Group):
   609             elements = self.SelectedElement.GetElements()
   602             elements = self.SelectedElement.GetElements()
   610             for element in elements:
   603             for element in elements:
   611                 blocks.append(element)
   604                 blocks.append(element)
   612         if len(blocks) > 0:
   605         if len(blocks) > 0:
   613             blocks_infos = []
   606             blocks_infos = []
   762                             # Create Coil
   755                             # Create Coil
   763                             id = self.GetNewId()
   756                             id = self.GetNewId()
   764                             coil = LD_Coil(self, values["type"], values["name"], id)
   757                             coil = LD_Coil(self, values["type"], values["name"], id)
   765                             pos = blocks[0].GetPosition()
   758                             pos = blocks[0].GetPosition()
   766                             coil.SetPosition(pos[0], pos[1] + LD_LINE_SIZE)
   759                             coil.SetPosition(pos[0], pos[1] + LD_LINE_SIZE)
   767                             self.Elements.append(coil)
   760                             self.AddBlock(coil)
   768                             self.Blocks.append(coil)
       
   769                             rung.SelectElement(coil)
   761                             rung.SelectElement(coil)
   770                             self.Controler.AddCurrentElementEditingCoil(id)
   762                             self.Controler.AddCurrentElementEditingCoil(id)
   771                             coil_connectors = coil.GetConnectors()
   763                             coil_connectors = coil.GetConnectors()
   772                             # Create Wire between LeftPowerRail and Coil
   764                             # Create Wire between LeftPowerRail and Coil
   773                             wire = Wire(self)
   765                             wire = Wire(self)
   774                             connectors[index + 1].Connect((wire, 0), False)
   766                             connectors[index + 1].Connect((wire, 0), False)
   775                             coil_connectors["output"].Connect((wire, -1), False)
   767                             coil_connectors["output"].Connect((wire, -1), False)
   776                             wire.ConnectStartPoint(None, connectors[index + 1])
   768                             wire.ConnectStartPoint(None, connectors[index + 1])
   777                             wire.ConnectEndPoint(None, coil_connectors["output"])
   769                             wire.ConnectEndPoint(None, coil_connectors["output"])
   778                             self.Wires.append(wire)
   770                             self.AddWire(wire)
   779                             self.Elements.append(wire)
       
   780                             rung.SelectElement(wire)
   771                             rung.SelectElement(wire)
   781                             left_elements.reverse()
   772                             left_elements.reverse()
   782                             for i, left_element in enumerate(left_elements):
   773                             for i, left_element in enumerate(left_elements):
   783                                 # Create Wire between LeftPowerRail and Coil
   774                                 # Create Wire between LeftPowerRail and Coil
   784                                 new_wire = Wire(self)
   775                                 new_wire = Wire(self)
   803                                     left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False)
   794                                     left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False)
   804                                     new_wire.ConnectStartPoint(None, right_element)
   795                                     new_wire.ConnectStartPoint(None, right_element)
   805                                     new_wire.ConnectEndPoint(None, left_element)
   796                                     new_wire.ConnectEndPoint(None, left_element)
   806                     wires.reverse()
   797                     wires.reverse()
   807                     for wire in wires:
   798                     for wire in wires:
   808                         self.Wires.append(wire)
   799                         self.AddWire(wire)
   809                         self.Elements.append(wire)
       
   810                         rung.SelectElement(wire)
   800                         rung.SelectElement(wire)
   811                     right_elements.reverse()
   801                     right_elements.reverse()
   812                 for block in blocks:
   802                 for block in blocks:
   813                     self.RefreshPosition(block)
   803                     self.RefreshPosition(block)
   814                 for right_element in right_elements:
   804                 for right_element in right_elements:
   825         else:
   815         else:
   826             message = wxMessageDialog(self, "You must select the block or group of blocks around which a branch should be added!", "Error", wxOK|wxICON_ERROR)
   816             message = wxMessageDialog(self, "You must select the block or group of blocks around which a branch should be added!", "Error", wxOK|wxICON_ERROR)
   827             message.ShowModal()
   817             message.ShowModal()
   828             message.Destroy()
   818             message.Destroy()
   829 
   819 
   830     def AddBlock(self):
   820     def AddLadderBlock(self):
   831         message = wxMessageDialog(self, "This option isn't available yet!", "Warning", wxOK|wxICON_EXCLAMATION)
   821         message = wxMessageDialog(self, "This option isn't available yet!", "Warning", wxOK|wxICON_EXCLAMATION)
   832         message.ShowModal()
   822         message.ShowModal()
   833         message.Destroy()
   823         message.Destroy()
   834 
   824 
   835 #-------------------------------------------------------------------------------
   825 #-------------------------------------------------------------------------------
   849             left_elements = [(wire.EndConnected, wire.EndConnected.GetWireIndex(wire)) for wire in input_wires]
   839             left_elements = [(wire.EndConnected, wire.EndConnected.GetWireIndex(wire)) for wire in input_wires]
   850             right_elements = [(wire.StartConnected, wire.StartConnected.GetWireIndex(wire)) for wire in output_wires]
   840             right_elements = [(wire.StartConnected, wire.StartConnected.GetWireIndex(wire)) for wire in output_wires]
   851             for wire in input_wires:
   841             for wire in input_wires:
   852                 wire.Clean()
   842                 wire.Clean()
   853                 rung.SelectElement(wire)
   843                 rung.SelectElement(wire)
   854                 self.Wires.remove(wire)
   844                 self.RemoveWire(wire)
   855                 self.Elements.remove(wire)
       
   856             for wire in output_wires:
   845             for wire in output_wires:
   857                 wire.Clean()
   846                 wire.Clean()
   858                 rung.SelectElement(wire)
   847                 rung.SelectElement(wire)
   859                 self.Wires.remove(wire)
   848                 self.RemoveWire(wire)
   860                 self.Elements.remove(wire)
       
   861             rung.SelectElement(contact)
   849             rung.SelectElement(contact)
   862             contact.Clean()
   850             contact.Clean()
   863             left_elements.reverse()
   851             left_elements.reverse()
   864             right_elements.reverse()
   852             right_elements.reverse()
   865             powerrail = len(left_elements) == 1 and isinstance(left_elements[0][0].GetParentBlock(), LD_PowerRail)
   853             powerrail = len(left_elements) == 1 and isinstance(left_elements[0][0].GetParentBlock(), LD_PowerRail)
   874                             index = left_powerrail.GetConnectorIndex(wire.EndConnected)
   862                             index = left_powerrail.GetConnectorIndex(wire.EndConnected)
   875                             left_powerrail.DeleteConnector(index)
   863                             left_powerrail.DeleteConnector(index)
   876                             wire_removed.append(wire)
   864                             wire_removed.append(wire)
   877                     for wire in wire_removed:
   865                     for wire in wire_removed:
   878                         wire.Clean()
   866                         wire.Clean()
   879                         self.Wires.remove(wire)
   867                         self.RemoveWire(wire)
   880                         self.Elements.remove(wire)
       
   881                         rung.SelectElement(wire)
   868                         rung.SelectElement(wire)
   882             wires = []
   869             wires = []
   883             for left_element, left_index in left_elements:
   870             for left_element, left_index in left_elements:
   884                 for right_element, right_index in right_elements:
   871                 for right_element, right_index in right_elements:
   885                     wire = Wire(self)
   872                     wire = Wire(self)
   888                     left_element.InsertConnect(left_index, (wire, -1), False)
   875                     left_element.InsertConnect(left_index, (wire, -1), False)
   889                     wire.ConnectStartPoint(None, right_element)
   876                     wire.ConnectStartPoint(None, right_element)
   890                     wire.ConnectEndPoint(None, left_element)
   877                     wire.ConnectEndPoint(None, left_element)
   891             wires.reverse()
   878             wires.reverse()
   892             for wire in wires:
   879             for wire in wires:
   893                 self.Wires.append(wire)
   880                 self.RemoveWire(wire)
   894                 self.Elements.append(wire)
       
   895                 rung.SelectElement(wire)
   881                 rung.SelectElement(wire)
   896             right_elements.reverse()
   882             right_elements.reverse()
   897             for right_element, right_index in right_elements:
   883             for right_element, right_index in right_elements:
   898                 self.RefreshPosition(right_element.GetParentBlock())
   884                 self.RefreshPosition(right_element.GetParentBlock())
   899             self.Blocks.remove(contact)
   885             self.RemoveBlock(contact)
   900             self.Elements.remove(contact)
       
   901             self.Controler.RemoveCurrentElementEditingInstance(contact.GetId())
   886             self.Controler.RemoveCurrentElementEditingInstance(contact.GetId())
   902             rung.RefreshBoundingBox()
   887             rung.RefreshBoundingBox()
   903             new_bbox = rung.GetBoundingBox()
   888             new_bbox = rung.GetBoundingBox()
   904             self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1)
   889             self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1)
   905             self.SelectedElement = None
   890             self.SelectedElement = None
   910         left_elements = [wire.EndConnected for wire in input_wires]
   895         left_elements = [wire.EndConnected for wire in input_wires]
   911         rung.SelectElement(element)
   896         rung.SelectElement(element)
   912         element.Clean()
   897         element.Clean()
   913         for wire in input_wires:
   898         for wire in input_wires:
   914             wire.Clean()
   899             wire.Clean()
   915             self.Wires.remove(wire)
   900             self.RemoveWire(wire)
   916             self.Elements.remove(wire)
       
   917             rung.SelectElement(wire)
   901             rung.SelectElement(wire)
   918         self.Blocks.remove(element)
   902         self.RemoveBlock(element)
   919         self.Elements.remove(element)
       
   920         self.Controler.RemoveCurrentElementEditingInstance(element.GetId())
   903         self.Controler.RemoveCurrentElementEditingInstance(element.GetId())
   921         for left_element in left_elements:
   904         for left_element in left_elements:
   922             block = left_element.GetParentBlock()
   905             block = left_element.GetParentBlock()
   923             if len(left_element.GetWires()) == 0:
   906             if len(left_element.GetWires()) == 0:
   924                 self.RecursiveDeletion(block, rung)
   907                 self.RecursiveDeletion(block, rung)
   960                                     block.Move(0, startpoint.y - endpoint.y)
   943                                     block.Move(0, startpoint.y - endpoint.y)
   961                                     self.RefreshPosition(block)
   944                                     self.RefreshPosition(block)
   962                 self.RecursiveDeletion(coil, rung)
   945                 self.RecursiveDeletion(coil, rung)
   963             else:
   946             else:
   964                 for element in rung.GetElements():
   947                 for element in rung.GetElements():
   965                     if element in self.Wires:
   948                     if self.IsWire(element):
   966                         element.Clean()
   949                         element.Clean()
   967                         self.Wires.remove(element)
   950                         self.RemoveWire(element)
   968                         self.Elements.remove(element)
       
   969                 for element in rung.GetElements():
   951                 for element in rung.GetElements():
   970                     if element in self.Blocks:
   952                     if self.IsBlock(element):
   971                         self.Controler.RemoveCurrentElementEditingInstance(element.GetId())
   953                         self.Controler.RemoveCurrentElementEditingInstance(element.GetId())
   972                         self.Blocks.remove(element)
   954                         self.RemoveBlock(element)
   973                         self.Elements.remove(element)
       
   974                 self.Controler.RemoveCurrentElementEditingInstance(self.Comments[rungindex].GetId())
   955                 self.Controler.RemoveCurrentElementEditingInstance(self.Comments[rungindex].GetId())
   975                 self.Elements.remove(self.Comments[rungindex])
   956                 self.RemoveComment(self.RungComments[rungindex])
   976                 self.Comments.pop(rungindex)
   957                 self.RungComments.pop(rungindex)
   977                 self.Rungs.pop(rungindex)
   958                 self.Rungs.pop(rungindex)
   978                 if rungindex < len(self.Rungs):
   959                 if rungindex < len(self.Rungs):
   979                     next_bbox = self.Rungs[rungindex].GetBoundingBox()
   960                     next_bbox = self.Rungs[rungindex].GetBoundingBox()
   980                     self.RefreshRungs(old_bbox.y - next_bbox.y, rungindex)
   961                     self.RefreshRungs(old_bbox.y - next_bbox.y, rungindex)
   981             self.SelectedElement = None
   962             self.SelectedElement = None
   985             Viewer.DeleteWire(self, wire)
   966             Viewer.DeleteWire(self, wire)
   986         else:
   967         else:
   987             wires = []
   968             wires = []
   988             left_elements = []
   969             left_elements = []
   989             right_elements = []
   970             right_elements = []
   990             if wire in self.Wires:
   971             if self.IsWire(wire):
   991                 wires = [wire]
   972                 wires = [wire]
   992             elif wire not in self.Elements:
   973             elif isinstance(wire, Graphic_Group):
   993                 for element in wire.GetElements():
   974                 for element in wire.GetElements():
   994                     if element in self.Wires:
   975                     if self.IsWire(element):
   995                         wires.append(element)
   976                         wires.append(element)
   996                     else:
   977                     else:
   997                         wires = []
   978                         wires = []
   998                         break
   979                         break
   999             if len(wires) > 0:
   980             if len(wires) > 0:
  1007                         left_elements.append(wire.EndConnected)
   988                         left_elements.append(wire.EndConnected)
  1008                     if wire.StartConnected not in right_elements:
   989                     if wire.StartConnected not in right_elements:
  1009                         right_elements.append(wire.StartConnected)
   990                         right_elements.append(wire.StartConnected)
  1010                     if connections == (False, False) or connections == (False, True) and isinstance(left_block, LD_PowerRail):
   991                     if connections == (False, False) or connections == (False, True) and isinstance(left_block, LD_PowerRail):
  1011                         wire.Clean()
   992                         wire.Clean()
  1012                         self.Wires.remove(wire)
   993                         self.RemoveWire(wire)
  1013                         self.Elements.remove(wire)
       
  1014                         rung.SelectElement(wire)
   994                         rung.SelectElement(wire)
  1015                 for left_element in left_elements:
   995                 for left_element in left_elements:
  1016                     left_block = left_element.GetParentBlock()
   996                     left_block = left_element.GetParentBlock()
  1017                     if isinstance(left_block, LD_PowerRail):
   997                     if isinstance(left_block, LD_PowerRail):
  1018                         if len(left_element.GetWires()) == 0:
   998                         if len(left_element.GetWires()) == 0:
  1151                     self.RefreshPosition(wire.StartConnected.GetParentBlock())
  1131                     self.RefreshPosition(wire.StartConnected.GetParentBlock())
  1152     
  1132     
  1153     def RefreshRungs(self, movey, fromidx):
  1133     def RefreshRungs(self, movey, fromidx):
  1154         if movey != 0:
  1134         if movey != 0:
  1155             for i in xrange(fromidx, len(self.Rungs)):
  1135             for i in xrange(fromidx, len(self.Rungs)):
  1156                 self.Comments[i].Move(0, movey)
  1136                 self.RungComments[i].Move(0, movey)
  1157                 self.Comments[i].RefreshModel()
  1137                 self.RungComments[i].RefreshModel()
  1158                 self.Rungs[i].Move(0, movey)
  1138                 self.Rungs[i].Move(0, movey)
  1159                 for element in self.Rungs[i].GetElements():
  1139                 for element in self.Rungs[i].GetElements():
  1160                     if element in self.Blocks:
  1140                     if self.IsBlock(element):
  1161                         self.RefreshPosition(element)
  1141                         self.RefreshPosition(element)
  1162 
  1142 
  1163 #-------------------------------------------------------------------------------
  1143 #-------------------------------------------------------------------------------
  1164 #                          Edit element content functions
  1144 #                          Edit element content functions
  1165 #-------------------------------------------------------------------------------
  1145 #-------------------------------------------------------------------------------