diff -r 31e63e25b4cc -r 64beb9e9c749 editors/LDViewer.py --- a/editors/LDViewer.py Mon Aug 21 20:17:19 2017 +0000 +++ b/editors/LDViewer.py Mon Aug 21 23:22:58 2017 +0300 @@ -28,6 +28,7 @@ from Viewer import * + def ExtractNextBlocks(block, block_list): current_list = [block] while len(current_list) > 0: @@ -49,7 +50,8 @@ block_list.append(next) next_list.append(next) current_list = next_list - + + def CalcBranchSize(elements, stops): branch_size = 0 stop_list = stops @@ -58,19 +60,19 @@ element_tree = {} for element in elements: if element not in element_tree: - element_tree[element] = {"parents":["start"], "children":[], "weight":None} + element_tree[element] = {"parents": ["start"], "children": [], "weight": None} GenerateTree(element, element_tree, stop_list) elif element_tree[element]: element_tree[element]["parents"].append("start") - remove_stops = {"start":[], "stop":[]} + remove_stops = {"start": [], "stop": []} for element, values in element_tree.items(): if "stop" in values["children"]: removed = [] for child in values["children"]: if child != "stop": -## if child in elements: -## RemoveElement(child, element_tree) -## removed.append(child) + # if child in elements: + # RemoveElement(child, element_tree) + # removed.append(child) if "start" in element_tree[child]["parents"]: if element not in remove_stops["stop"]: remove_stops["stop"].append(element) @@ -89,16 +91,17 @@ branch_size += values["weight"] else: return 1 - #print branch_size return branch_size + def RemoveElement(remove, element_tree): if remove in element_tree and element_tree[remove]: for child in element_tree[remove]["children"]: if child != "stop": RemoveElement(child, element_tree) element_tree.pop(remove) -## element_tree[remove] = None +# element_tree[remove] = None + def GenerateTree(element, element_tree, stop_list): if element in element_tree: @@ -115,20 +118,21 @@ for wire, handle in connector.GetWires(): next = wire.EndConnected.GetParentBlock() if isinstance(next, LD_PowerRail) and next.GetType() == LEFTRAIL or next in stop_list: -## for remove in element_tree[element]["children"]: -## RemoveElement(remove, element_tree) -## element_tree[element]["children"] = ["stop"] + # for remove in element_tree[element]["children"]: + # RemoveElement(remove, element_tree) + # element_tree[element]["children"] = ["stop"] element_tree[element]["children"].append("stop") -## elif element_tree[element]["children"] == ["stop"]: -## element_tree[next] = None + # elif element_tree[element]["children"] == ["stop"]: + # element_tree[next] = None elif next not in element_tree or element_tree[next]: element_tree[element]["children"].append(next) if next in element_tree: element_tree[next]["parents"].append(element) else: - element_tree[next] = {"parents":[element], "children":[], "weight":None} + element_tree[next] = {"parents": [element], "children": [], "weight": None} GenerateTree(next, element_tree, stop_list) + def CalcWeight(element, element_tree): weight = 0 parts = None @@ -156,26 +160,25 @@ element_tree[element]["weight"] = max(1, weight / parts) -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- # Ladder Diagram Graphic elements Viewer class -#------------------------------------------------------------------------------- - - -""" -Class derived from Viewer class that implements a Viewer of Ladder Diagram -""" +# ------------------------------------------------------------------------------- + class LD_Viewer(Viewer): - - def __init__(self, parent, tagname, window, controler, debug = False, instancepath = ""): + """ + Class derived from Viewer class that implements a Viewer of Ladder Diagram + """ + + def __init__(self, parent, tagname, window, controler, debug=False, instancepath=""): Viewer.__init__(self, parent, tagname, window, controler, debug, instancepath) self.Rungs = [] self.RungComments = [] self.CurrentLanguage = "LD" -#------------------------------------------------------------------------------- -# Refresh functions -#------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------- + # Refresh functions + # ------------------------------------------------------------------------------- def ResetView(self): self.Rungs = [] @@ -194,7 +197,7 @@ self.RungComments.insert(i, None) else: self.RungComments.insert(i, None) - + def loadInstance(self, instance, ids, selection): Viewer.loadInstance(self, instance, ids, selection) if self.GetDrawingMode() != FREEDRAWING_MODE: @@ -212,7 +215,10 @@ if rung not in rungs: rungs.append(rung) if len(rungs) > 1: - raise ValueError, _("Ladder element with id %d is on more than one rung.")%instance["id"] + raise ValueError( + _("Ladder element with id %d is on more than one rung.") + % instance["id"]) + element = self.FindElementById(instance["id"]) element_connectors = element.GetConnectors() self.Rungs[rungs[0]].SelectElement(element) @@ -228,9 +234,12 @@ if rung not in rungs: rungs.append(rung) if len(rungs) > 1: - raise ValueError, _("Ladder element with id %d is on more than one rung.")%instance["id"] + raise ValueError( + _("Ladder element with id %d is on more than one rung.") + % instance["id"]) + element = self.FindElementById(instance["id"]) - element_connectors = element.GetConnectors() + element_connectors = element.GetConnectors() self.Rungs[rungs[0]].SelectElement(element) for wire, num in element_connectors["inputs"][0].GetWires(): self.Rungs[rungs[0]].SelectElement(wire) @@ -240,7 +249,7 @@ pos = element.GetPosition() i = 0 inserted = False - while i < len(self.RungComments) and not inserted: + while i < len(self.RungComments) and not inserted: ipos = self.RungComments[i].GetPosition() if pos[1] < ipos[1]: self.RungComments.insert(i, element) @@ -248,10 +257,10 @@ i += 1 if not inserted: self.RungComments.append(element) - -#------------------------------------------------------------------------------- -# Search Element functions -#------------------------------------------------------------------------------- + + # ------------------------------------------------------------------------------- + # Search Element functions + # ------------------------------------------------------------------------------- def FindRung(self, element): for i, rung in enumerate(self.Rungs): @@ -259,10 +268,10 @@ return i return None - def FindElement(self, event, exclude_group = False, connectors = True): + def FindElement(self, event, exclude_group=False, connectors=True): if self.GetDrawingMode() == FREEDRAWING_MODE: return Viewer.FindElement(self, event, exclude_group, connectors) - + dc = self.GetLogicalDC() pos = event.GetLogicalPosition(dc) if self.SelectedElement and not isinstance(self.SelectedElement, (Graphic_Group, Wire)): @@ -286,16 +295,16 @@ def SearchElements(self, bbox): if self.GetDrawingMode() == FREEDRAWING_MODE: return Viewer.SearchElements(self, bbox) - + elements = [] for element in self.Blocks.values() + self.Comments.values(): if element.IsInSelection(bbox): elements.append(element) return elements -#------------------------------------------------------------------------------- -# Mouse event functions -#------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------- + # Mouse event functions + # ------------------------------------------------------------------------------- def OnViewerLeftDown(self, event): if self.GetDrawingMode() == FREEDRAWING_MODE: @@ -345,7 +354,7 @@ self.SelectedElement.SetElements(elements) self.SelectedElement.SetSelected(True) elif self.Mode == MODE_SELECTION and self.SelectedElement: - dc = self.GetLogicalDC() + dc = self.GetLogicalDC() if not isinstance(self.SelectedElement, Graphic_Group): if self.IsWire(self.SelectedElement): result = self.SelectedElement.TestSegment(event.GetLogicalPosition(dc), True) @@ -383,9 +392,9 @@ wx.CallAfter(self.SetCurrentCursor, 0) event.Skip() -#------------------------------------------------------------------------------- -# Keyboard event functions -#------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------- + # Keyboard event functions + # ------------------------------------------------------------------------------- def OnChar(self, event): if self.GetDrawingMode() == FREEDRAWING_MODE: @@ -434,9 +443,9 @@ else: event.Skip() -#------------------------------------------------------------------------------- -# Model adding functions from Drop Target -#------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------- + # Model adding functions from Drop Target + # ------------------------------------------------------------------------------- def AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type): if var_type == "BOOL": @@ -474,9 +483,9 @@ else: Viewer.AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type) -#------------------------------------------------------------------------------- -# Adding element functions -#------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------- + # Adding element functions + # ------------------------------------------------------------------------------- def AddLadderRung(self): dialog = LDElementDialog(self.ParentWindow, self.Controler, "coil") @@ -491,7 +500,7 @@ if returntype == "BOOL": varlist.append(self.Controler.GetEditedElementName(self.TagName)) dialog.SetVariables(varlist) - dialog.SetValues({"name":"","type":COIL_NORMAL}) + dialog.SetValues({"name": "", "type": COIL_NORMAL}) if dialog.ShowModal() == wx.ID_OK: values = dialog.GetValues() startx, starty = LD_OFFSET[0], 0 @@ -500,7 +509,7 @@ starty = bbox.y + bbox.height starty += LD_OFFSET[1] rung = Graphic_Group(self) - + # Create comment id = self.GetNewId() comment = Comment(self, _("Comment"), id) @@ -511,7 +520,7 @@ self.Controler.AddEditedElementComment(self.TagName, id) self.RefreshCommentModel(comment) starty += LD_COMMENT_DEFAULTSIZE[1] + LD_OFFSET[1] - + # Create LeftPowerRail id = self.GetNewId() leftpowerrail = LD_PowerRail(self, LEFTRAIL, id) @@ -521,7 +530,7 @@ rung.SelectElement(leftpowerrail) self.Controler.AddEditedElementPowerRail(self.TagName, id, LEFTRAIL) self.RefreshPowerRailModel(leftpowerrail) - + # Create Coil id = self.GetNewId() coil = LD_Coil(self, values["type"], values["name"], id) @@ -530,7 +539,7 @@ self.AddBlock(coil) rung.SelectElement(coil) self.Controler.AddEditedElementCoil(self.TagName, id) - + # Create Wire between LeftPowerRail and Coil wire = Wire(self) start_connector = coil_connectors["inputs"][0] @@ -541,7 +550,7 @@ wire.ConnectEndPoint(None, end_connector) self.AddWire(wire) rung.SelectElement(wire) - + # Create RightPowerRail id = self.GetNewId() rightpowerrail = LD_PowerRail(self, RIGHTRAIL, id) @@ -550,7 +559,7 @@ self.AddBlock(rightpowerrail) rung.SelectElement(rightpowerrail) self.Controler.AddEditedElementPowerRail(self.TagName, id, RIGHTRAIL) - + # Create Wire between LeftPowerRail and Coil wire = Wire(self) start_connector = rightpowerrail_connectors["inputs"][0] @@ -574,7 +583,7 @@ left_element = self.SelectedElement.EndConnected if not isinstance(left_element.GetParentBlock(), LD_Coil): wires.append(self.SelectedElement) - elif self.SelectedElement and isinstance(self.SelectedElement,Graphic_Group): + elif self.SelectedElement and isinstance(self.SelectedElement, Graphic_Group): if False not in [self.IsWire(element) for element in self.SelectedElement.GetElements()]: for element in self.SelectedElement.GetElements(): wires.append(element) @@ -588,7 +597,7 @@ if var.Class != "Output" and var.Type == "BOOL": varlist.append(var.Name) dialog.SetVariables(varlist) - dialog.SetValues({"name":"","type":CONTACT_NORMAL}) + dialog.SetValues({"name": "", "type": CONTACT_NORMAL}) if dialog.ShowModal() == wx.ID_OK: values = dialog.GetValues() points = wires[0].GetSelectedSegmentPoints() @@ -663,7 +672,7 @@ self.RefreshVisibleElements() self.Refresh(False) else: - message = wx.MessageDialog(self, _("You must select the wire where a contact should be added!"), _("Error"), wx.OK|wx.ICON_ERROR) + message = wx.MessageDialog(self, _("You must select the wire where a contact should be added!"), _("Error"), wx.OK | wx.ICON_ERROR) message.ShowModal() message.Destroy() @@ -683,7 +692,7 @@ right_index = [] for block in blocks: connectors = block.GetConnectors() - block_infos = {"lefts":[],"rights":[]} + block_infos = {"lefts": [], "rights": []} block_infos.update(connectors) for connector in block_infos["inputs"]: for wire, handle in connector.GetWires(): @@ -778,7 +787,7 @@ if left_powerrail: powerrail = left_elements[0].GetParentBlock() index = 0 - for left_element in left_elements: + for left_element in left_elements: index = max(index, powerrail.GetConnectorIndex(left_element)) powerrail.InsertConnector(index + 1) powerrail.RefreshModel() @@ -805,17 +814,17 @@ if returntype == "BOOL": varlist.append(self.Controler.GetEditedElementName(self.TagName)) dialog.SetVariables(varlist) - dialog.SetValues({"name":"","type":COIL_NORMAL}) + dialog.SetValues({"name": "", "type": COIL_NORMAL}) if dialog.ShowModal() == wx.ID_OK: values = dialog.GetValues() powerrail = right_elements[0].GetParentBlock() index = 0 - for right_element in right_elements: + for right_element in right_elements: index = max(index, powerrail.GetConnectorIndex(right_element)) powerrail.InsertConnector(index + 1) powerrail.RefreshModel() connectors = powerrail.GetConnectors() - + # Create Coil id = self.GetNewId() coil = LD_Coil(self, values["type"], values["name"], id) @@ -825,7 +834,7 @@ rung.SelectElement(coil) self.Controler.AddEditedElementCoil(self.TagName, id) coil_connectors = coil.GetConnectors() - + # Create Wire between LeftPowerRail and Coil wire = Wire(self) connectors["inputs"][index + 1].Connect((wire, 0), False) @@ -835,7 +844,7 @@ self.AddWire(wire) rung.SelectElement(wire) left_elements.reverse() - + for i, left_element in enumerate(left_elements): # Create Wire between LeftPowerRail and Coil new_wire = Wire(self) @@ -844,7 +853,7 @@ left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False) new_wire.ConnectStartPoint(None, coil_connectors["inputs"][0]) new_wire.ConnectEndPoint(None, left_element) - + self.RefreshPosition(coil) else: left_elements.reverse() @@ -879,22 +888,22 @@ self.RefreshVisibleElements() self.Refresh(False) else: - message = wx.MessageDialog(self, _("The group of block must be coherent!"), _("Error"), wx.OK|wx.ICON_ERROR) + message = wx.MessageDialog(self, _("The group of block must be coherent!"), _("Error"), wx.OK | wx.ICON_ERROR) message.ShowModal() message.Destroy() else: - message = wx.MessageDialog(self, _("You must select the block or group of blocks around which a branch should be added!"), _("Error"), wx.OK|wx.ICON_ERROR) + message = wx.MessageDialog(self, _("You must select the block or group of blocks around which a branch should be added!"), _("Error"), wx.OK | wx.ICON_ERROR) message.ShowModal() message.Destroy() def AddLadderBlock(self): - message = wx.MessageDialog(self, _("This option isn't available yet!"), _("Warning"), wx.OK|wx.ICON_EXCLAMATION) + message = wx.MessageDialog(self, _("This option isn't available yet!"), _("Warning"), wx.OK | wx.ICON_EXCLAMATION) message.ShowModal() message.Destroy() -#------------------------------------------------------------------------------- -# Delete element functions -#------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------- + # Delete element functions + # ------------------------------------------------------------------------------- def DeleteContact(self, contact): if self.GetDrawingMode() == FREEDRAWING_MODE: @@ -1080,16 +1089,16 @@ self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1) self.SelectedElement = None -#------------------------------------------------------------------------------- -# Refresh element position functions -#------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------- + # Refresh element position functions + # ------------------------------------------------------------------------------- def RefreshPosition(self, element, recursive=True): # If element is LeftPowerRail, no need to update position if isinstance(element, LD_PowerRail) and element.GetType() == LEFTRAIL: element.RefreshModel() return - + # Extract max position of the elements connected to input connectors = element.GetConnectors() position = element.GetPosition() @@ -1103,7 +1112,7 @@ pos = leftblock.GetPosition() size = leftblock.GetSize() maxx = max(maxx, pos[0] + size[0]) - + # Refresh position of element if isinstance(element, LD_Coil): interval = LD_WIRECOIL_SIZE @@ -1114,13 +1123,13 @@ movex = maxx + interval - position[0] element.Move(movex, 0) position = element.GetPosition() - + # Extract blocks connected to inputs blocks = [] for i, connector in enumerate(connectors["inputs"]): for j, (wire, handle) in enumerate(connector.GetWires()): blocks.append(wire.EndConnected.GetParentBlock()) - + for i, connector in enumerate(connectors["inputs"]): startpoint = connector.GetPosition(False) previous_blocks = [] @@ -1168,13 +1177,13 @@ previous_blocks.append(block) blocks.remove(block) ExtractNextBlocks(block, block_list) - + element.RefreshModel(False) if recursive: for connector in connectors["outputs"]: for wire, handle in connector.GetWires(): self.RefreshPosition(wire.StartConnected.GetParentBlock()) - + def RefreshRungs(self, movey, fromidx): if movey != 0: for i in xrange(fromidx, len(self.Rungs)): @@ -1185,11 +1194,10 @@ if self.IsBlock(element): self.RefreshPosition(element) -#------------------------------------------------------------------------------- -# Edit element content functions -#------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------- + # Edit element content functions + # ------------------------------------------------------------------------------- def EditPowerRailContent(self, powerrail): if self.GetDrawingMode() == FREEDRAWING_MODE: Viewer.EditPowerRailContent(self, powerrail) -