2562 # Editing functions |
2562 # Editing functions |
2563 #------------------------------------------------------------------------------- |
2563 #------------------------------------------------------------------------------- |
2564 |
2564 |
2565 def Cut(self): |
2565 def Cut(self): |
2566 if not self.Debug and (self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement) or isinstance(self.SelectedElement, Graphic_Group)): |
2566 if not self.Debug and (self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement) or isinstance(self.SelectedElement, Graphic_Group)): |
2567 self.ParentWindow.SetCopyBuffer(self.SelectedElement.Clone(self)) |
2567 blocks, wires = self.SelectedElement.GetDefinition() |
|
2568 text = self.Controler.GetEditedElementInstancesCopy(self.TagName, blocks, wires, self.Debug) |
|
2569 self.ParentWindow.SetCopyBuffer(text) |
2568 rect = self.SelectedElement.GetRedrawRect(1, 1) |
2570 rect = self.SelectedElement.GetRedrawRect(1, 1) |
2569 self.SelectedElement.Delete() |
2571 self.SelectedElement.Delete() |
2570 self.SelectedElement = None |
2572 self.SelectedElement = None |
2571 self.RefreshBuffer() |
2573 self.RefreshBuffer() |
2572 self.RefreshScrollBars() |
2574 self.RefreshScrollBars() |
2574 self.ParentWindow.RefreshInstancesTree() |
2576 self.ParentWindow.RefreshInstancesTree() |
2575 self.RefreshRect(self.GetScrolledRect(rect), False) |
2577 self.RefreshRect(self.GetScrolledRect(rect), False) |
2576 |
2578 |
2577 def Copy(self): |
2579 def Copy(self): |
2578 if not self.Debug and (self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement) or isinstance(self.SelectedElement, Graphic_Group)): |
2580 if not self.Debug and (self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement) or isinstance(self.SelectedElement, Graphic_Group)): |
2579 self.ParentWindow.SetCopyBuffer(self.SelectedElement.Clone(self)) |
2581 blocks, wires = self.SelectedElement.GetDefinition() |
|
2582 text = self.Controler.GetEditedElementInstancesCopy(self.TagName, blocks, wires, self.Debug) |
|
2583 self.ParentWindow.SetCopyBuffer(text) |
2580 |
2584 |
2581 def Paste(self): |
2585 def Paste(self): |
2582 element = self.ParentWindow.GetCopyBuffer() |
2586 if not self.Debug: |
2583 if not self.Debug and element is not None and self.CanAddElement(element): |
2587 element = self.ParentWindow.GetCopyBuffer() |
2584 mouse_pos = self.ScreenToClient(wx.GetMousePosition()) |
2588 mouse_pos = self.ScreenToClient(wx.GetMousePosition()) |
2585 if wx.Rect(0, 0, *self.GetClientSize()).InsideXY(mouse_pos.x, mouse_pos.y): |
2589 middle = wx.Rect(0, 0, *self.GetClientSize()).InsideXY(mouse_pos.x, mouse_pos.y) |
|
2590 if middle: |
2586 x, y = self.CalcUnscrolledPosition(mouse_pos.x, mouse_pos.y) |
2591 x, y = self.CalcUnscrolledPosition(mouse_pos.x, mouse_pos.y) |
2587 block_size = element.GetSize() |
|
2588 x = int(float(x) / self.ViewScale[0]) - block_size[0] / 2 |
|
2589 y = int(float(y) / self.ViewScale[1]) - block_size[1] / 2 |
|
2590 else: |
2592 else: |
2591 x, y = self.CalcUnscrolledPosition(0, 0) |
2593 x, y = self.CalcUnscrolledPosition(0, 0) |
2592 x = int(x / self.ViewScale[0]) + 30 |
2594 new_pos = [int(x / self.ViewScale[0]), int(y / self.ViewScale[1])] |
2593 y = int(y / self.ViewScale[1]) + 30 |
2595 result = self.Controler.PasteEditedElementInstances(self.TagName, element, new_pos, middle, self.Debug) |
2594 if self.Scaling is not None: |
2596 if not isinstance(result, (StringType, UnicodeType)): |
2595 new_pos = wx.Point(max(round_scaling(30, self.Scaling[0], 1), |
2597 self.RefreshBuffer() |
2596 round_scaling(x, self.Scaling[0])), |
2598 self.RefreshView(result) |
2597 max(round_scaling(30, self.Scaling[1], 1), |
2599 self.ParentWindow.RefreshVariablePanel(self.TagName) |
2598 round_scaling(y, self.Scaling[1]))) |
2600 self.ParentWindow.RefreshInstancesTree() |
2599 else: |
2601 else: |
2600 new_pos = wx.Point(max(30, x), max(30, y)) |
2602 message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR) |
2601 block = self.CopyBlock(element, new_pos) |
2603 message.ShowModal() |
2602 self.RefreshVisibleElements() |
2604 message.Destroy() |
2603 if self.SelectedElement is not None: |
|
2604 self.SelectedElement.SetSelected(False) |
|
2605 self.SelectedElement = block |
|
2606 self.SelectedElement.SetSelected(True) |
|
2607 self.RefreshBuffer() |
|
2608 self.RefreshScrollBars() |
|
2609 self.ParentWindow.RefreshVariablePanel(self.TagName) |
|
2610 self.ParentWindow.RefreshInstancesTree() |
|
2611 else: |
|
2612 message = wx.MessageDialog(self, "You can't paste the element in buffer here!", "Error", wx.OK|wx.ICON_ERROR) |
|
2613 message.ShowModal() |
|
2614 message.Destroy() |
|
2615 |
2605 |
2616 def CanAddElement(self, block): |
2606 def CanAddElement(self, block): |
2617 if isinstance(block, Graphic_Group): |
2607 if isinstance(block, Graphic_Group): |
2618 return block.CanAddBlocks(self) |
2608 return block.CanAddBlocks(self) |
2619 elif self.CurrentLanguage == "SFC": |
2609 elif self.CurrentLanguage == "SFC": |
2623 elif self.CurrentLanguage == "FBD" and isinstance(block, (FBD_Block, FBD_Variable, FBD_Connector, Comment)): |
2613 elif self.CurrentLanguage == "FBD" and isinstance(block, (FBD_Block, FBD_Variable, FBD_Connector, Comment)): |
2624 return True |
2614 return True |
2625 return False |
2615 return False |
2626 |
2616 |
2627 def GenerateNewName(self, element, exclude={}): |
2617 def GenerateNewName(self, element, exclude={}): |
2628 names = exclude.copy() |
2618 if isinstance(element, SFC_Step): |
2629 if isinstance(element, FBD_Block): |
|
2630 names.update(dict([(varname.upper(), True) for varname in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)])) |
|
2631 format = "Block%d" |
|
2632 elif isinstance(element, SFC_Step): |
|
2633 names.update(dict([(block.GetName().upper(), True) for block in self.Blocks if isinstance(block, SFC_Step)])) |
|
2634 format = "Step%d" |
2619 format = "Step%d" |
2635 i = 1 |
2620 else: |
2636 while names.get((format%i).upper(), False): |
2621 format = "Block%d" |
2637 i += 1 |
2622 return self.Controler.GenerateNewName(self.TagName, element.GetName(), format, exclude, self.Debug) |
2638 return format%i |
|
2639 |
2623 |
2640 def IsNamedElement(self, element): |
2624 def IsNamedElement(self, element): |
2641 return isinstance(element, FBD_Block) and element.GetName() != "" or isinstance(element, SFC_Step) |
2625 return isinstance(element, FBD_Block) and element.GetName() != "" or isinstance(element, SFC_Step) |
2642 |
2626 |
2643 def CopyBlock(self, element, pos): |
2627 def CopyBlock(self, element, pos): |