editors/Viewer.py
changeset 1782 5b6ad7a7fd9d
parent 1780 c52d1460cea8
child 1834 cd42b426028b
equal deleted inserted replaced
1781:b112bfdde5cc 1782:5b6ad7a7fd9d
   225     if y1 == y2:
   225     if y1 == y2:
   226         return cmp(x1, x2)
   226         return cmp(x1, x2)
   227     else:
   227     else:
   228         return cmp(y1, y2)
   228         return cmp(y1, y2)
   229 
   229 
   230 #-------------------------------------------------------------------------------
   230 # -------------------------------------------------------------------------------
   231 #                       Graphic elements Viewer base class
   231 #                       Graphic elements Viewer base class
   232 #-------------------------------------------------------------------------------
   232 # -------------------------------------------------------------------------------
   233 
   233 
   234 
   234 
   235 # ID Constants for alignment menu items
   235 # ID Constants for alignment menu items
   236 [
   236 [
   237     ID_VIEWERALIGNMENTMENUITEMS0, ID_VIEWERALIGNMENTMENUITEMS1,
   237     ID_VIEWERALIGNMENTMENUITEMS0, ID_VIEWERALIGNMENTMENUITEMS1,
   939         return self.MiniTextDC.GetTextExtent(text)
   939         return self.MiniTextDC.GetTextExtent(text)
   940 
   940 
   941     def GetMiniFont(self):
   941     def GetMiniFont(self):
   942         return self.MiniTextDC.GetFont()
   942         return self.MiniTextDC.GetFont()
   943 
   943 
   944 #-------------------------------------------------------------------------------
   944     # -------------------------------------------------------------------------------
   945 #                         Element management functions
   945     #                         Element management functions
   946 #-------------------------------------------------------------------------------
   946     # -------------------------------------------------------------------------------
   947 
   947 
   948     def AddBlock(self, block):
   948     def AddBlock(self, block):
   949         self.Blocks[block.GetId()] = block
   949         self.Blocks[block.GetId()] = block
   950 
   950 
   951     def AddWire(self, wire):
   951     def AddWire(self, wire):
  1082             height = min_height
  1082             height = min_height
  1083         if element.Size != (width, height):
  1083         if element.Size != (width, height):
  1084             element.SetSize(width, height)
  1084             element.SetSize(width, height)
  1085             element.RefreshModel()
  1085             element.RefreshModel()
  1086 
  1086 
  1087 #-------------------------------------------------------------------------------
  1087     # -------------------------------------------------------------------------------
  1088 #                              Reset functions
  1088     #                              Reset functions
  1089 #-------------------------------------------------------------------------------
  1089     # -------------------------------------------------------------------------------
  1090 
  1090 
  1091     # Resets Viewer lists
  1091     # Resets Viewer lists
  1092     def ResetView(self):
  1092     def ResetView(self):
  1093         self.Blocks = {}
  1093         self.Blocks = {}
  1094         self.Wires = {}
  1094         self.Wires = {}
  1202             self.PagePen = wx.TRANSPARENT_PEN
  1202             self.PagePen = wx.TRANSPARENT_PEN
  1203         if refresh:
  1203         if refresh:
  1204             self.RefreshVisibleElements()
  1204             self.RefreshVisibleElements()
  1205             self.Editor.Refresh(False)
  1205             self.Editor.Refresh(False)
  1206 
  1206 
  1207 
  1207     # -------------------------------------------------------------------------------
  1208 #-------------------------------------------------------------------------------
  1208     #                          Refresh functions
  1209 #                          Refresh functions
  1209     # -------------------------------------------------------------------------------
  1210 #-------------------------------------------------------------------------------
       
  1211 
  1210 
  1212     def ElementNeedRefresh(self, element):
  1211     def ElementNeedRefresh(self, element):
  1213         self.ElementRefreshList_lock.acquire()
  1212         self.ElementRefreshList_lock.acquire()
  1214         self.ElementRefreshList.append(element)
  1213         self.ElementRefreshList.append(element)
  1215         self.ElementRefreshList_lock.release()
  1214         self.ElementRefreshList_lock.release()
  1529         return self.Controler.IsEndType(type)
  1528         return self.Controler.IsEndType(type)
  1530 
  1529 
  1531     def GetBlockType(self, type, inputs=None):
  1530     def GetBlockType(self, type, inputs=None):
  1532         return self.Controler.GetBlockType(type, inputs, self.Debug)
  1531         return self.Controler.GetBlockType(type, inputs, self.Debug)
  1533 
  1532 
  1534 #-------------------------------------------------------------------------------
  1533     # -------------------------------------------------------------------------------
  1535 #                          Search Element functions
  1534     #                          Search Element functions
  1536 #-------------------------------------------------------------------------------
  1535     # -------------------------------------------------------------------------------
  1537 
  1536 
  1538     def FindBlock(self, event):
  1537     def FindBlock(self, event):
  1539         dc = self.GetLogicalDC()
  1538         dc = self.GetLogicalDC()
  1540         pos = event.GetLogicalPosition(dc)
  1539         pos = event.GetLogicalPosition(dc)
  1541         for block in self.Blocks.itervalues():
  1540         for block in self.Blocks.itervalues():
  1602             self.SelectedElement.SetSelected(False)
  1601             self.SelectedElement.SetSelected(False)
  1603         self.SelectedElement = Graphic_Group(self)
  1602         self.SelectedElement = Graphic_Group(self)
  1604         self.SelectedElement.SetElements(self.GetElements())
  1603         self.SelectedElement.SetElements(self.GetElements())
  1605         self.SelectedElement.SetSelected(True)
  1604         self.SelectedElement.SetSelected(True)
  1606 
  1605 
  1607 #-------------------------------------------------------------------------------
  1606     # -------------------------------------------------------------------------------
  1608 #                           Popup menu functions
  1607     #                           Popup menu functions
  1609 #-------------------------------------------------------------------------------
  1608     # -------------------------------------------------------------------------------
  1610 
  1609 
  1611     def GetForceVariableMenuFunction(self, iec_path, element):
  1610     def GetForceVariableMenuFunction(self, iec_path, element):
  1612         iec_type = self.GetDataType(iec_path)
  1611         iec_type = self.GetDataType(iec_path)
  1613 
  1612 
  1614         def ForceVariableFunction(event):
  1613         def ForceVariableFunction(event):
  1736         menu = wx.Menu(title='')
  1735         menu = wx.Menu(title='')
  1737         self.AddDefaultMenuItems(menu, block=block)
  1736         self.AddDefaultMenuItems(menu, block=block)
  1738         self.Editor.PopupMenu(menu)
  1737         self.Editor.PopupMenu(menu)
  1739         menu.Destroy()
  1738         menu.Destroy()
  1740 
  1739 
  1741 #-------------------------------------------------------------------------------
  1740     # -------------------------------------------------------------------------------
  1742 #                            Menu items functions
  1741     #                            Menu items functions
  1743 #-------------------------------------------------------------------------------
  1742     # -------------------------------------------------------------------------------
  1744 
  1743 
  1745     def OnAlignLeftMenu(self, event):
  1744     def OnAlignLeftMenu(self, event):
  1746         if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
  1745         if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
  1747             self.SelectedElement.AlignElements(ALIGN_LEFT, None)
  1746             self.SelectedElement.AlignElements(ALIGN_LEFT, None)
  1748             self.RefreshBuffer()
  1747             self.RefreshBuffer()
  1965     def GetClipboardCallBack(self, func):
  1964     def GetClipboardCallBack(self, func):
  1966         def ClipboardCallback(event):
  1965         def ClipboardCallback(event):
  1967             wx.CallAfter(func)
  1966             wx.CallAfter(func)
  1968         return ClipboardCallback
  1967         return ClipboardCallback
  1969 
  1968 
  1970 #-------------------------------------------------------------------------------
  1969     # -------------------------------------------------------------------------------
  1971 #                          Mouse event functions
  1970     #                          Mouse event functions
  1972 #-------------------------------------------------------------------------------
  1971     # -------------------------------------------------------------------------------
  1973 
  1972 
  1974     def OnViewerMouseEvent(self, event):
  1973     def OnViewerMouseEvent(self, event):
  1975         self.ResetBuffer()
  1974         self.ResetBuffer()
  1976         if event.Leaving() and self.ToolTipElement is not None:
  1975         if event.Leaving() and self.ToolTipElement is not None:
  1977             self.ToolTipElement.DestroyToolTip()
  1976             self.ToolTipElement.DestroyToolTip()
  2465                 else:
  2464                 else:
  2466                     items.append(
  2465                     items.append(
  2467                         (_(u'Variable'), self.GetAddToWireMenuCallBack(self.AddNewVariable, True)))
  2466                         (_(u'Variable'), self.GetAddToWireMenuCallBack(self.AddNewVariable, True)))
  2468         return items
  2467         return items
  2469 
  2468 
  2470 #-------------------------------------------------------------------------------
  2469     # -------------------------------------------------------------------------------
  2471 #                          Keyboard event functions
  2470     #                          Keyboard event functions
  2472 #-------------------------------------------------------------------------------
  2471     # -------------------------------------------------------------------------------
  2473 
  2472 
  2474     ARROW_KEY_MOVE = {
  2473     ARROW_KEY_MOVE = {
  2475         wx.WXK_LEFT: (-1, 0),
  2474         wx.WXK_LEFT: (-1, 0),
  2476         wx.WXK_RIGHT: (1, 0),
  2475         wx.WXK_RIGHT: (1, 0),
  2477         wx.WXK_UP: (0, -1),
  2476         wx.WXK_UP: (0, -1),
  2543             self.SetScale(self.CurrentScale - 1)
  2542             self.SetScale(self.CurrentScale - 1)
  2544             self.ParentWindow.RefreshDisplayMenu()
  2543             self.ParentWindow.RefreshDisplayMenu()
  2545         else:
  2544         else:
  2546             event.Skip()
  2545             event.Skip()
  2547 
  2546 
  2548 #-------------------------------------------------------------------------------
  2547     # -------------------------------------------------------------------------------
  2549 #                  Model adding functions from Drop Target
  2548     #                  Model adding functions from Drop Target
  2550 #-------------------------------------------------------------------------------
  2549     # -------------------------------------------------------------------------------
  2551 
  2550 
  2552     def AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type):
  2551     def AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type):
  2553         id = self.GetNewId()
  2552         id = self.GetNewId()
  2554         variable = FBD_Variable(self, var_class, var_name, var_type, id)
  2553         variable = FBD_Variable(self, var_class, var_name, var_type, id)
  2555         width, height = variable.GetMinSize()
  2554         width, height = variable.GetMinSize()
  2566         self.RefreshBuffer()
  2565         self.RefreshBuffer()
  2567         self.RefreshScrollBars()
  2566         self.RefreshScrollBars()
  2568         self.RefreshVisibleElements()
  2567         self.RefreshVisibleElements()
  2569         self.Editor.Refresh(False)
  2568         self.Editor.Refresh(False)
  2570 
  2569 
  2571 #-------------------------------------------------------------------------------
  2570     # -------------------------------------------------------------------------------
  2572 #                          Model adding functions
  2571     #                          Model adding functions
  2573 #-------------------------------------------------------------------------------
  2572     # -------------------------------------------------------------------------------
  2574 
  2573 
  2575     def GetScaledSize(self, width, height):
  2574     def GetScaledSize(self, width, height):
  2576         if self.Scaling is not None:
  2575         if self.Scaling is not None:
  2577             width = round(float(width) / float(self.Scaling[0]) + 0.4) * self.Scaling[0]
  2576             width = round(float(width) / float(self.Scaling[0]) + 0.4) * self.Scaling[0]
  2578             height = round(float(height) / float(self.Scaling[1]) + 0.4) * self.Scaling[1]
  2577             height = round(float(height) / float(self.Scaling[1]) + 0.4) * self.Scaling[1]
  2820             actionblock = SFC_ActionBlock(self, dialog.GetValues(), id)
  2819             actionblock = SFC_ActionBlock(self, dialog.GetValues(), id)
  2821             self.Controler.AddEditedElementActionBlock(self.TagName, id)
  2820             self.Controler.AddEditedElementActionBlock(self.TagName, id)
  2822             self.AddNewElement(actionblock, bbox, wire)
  2821             self.AddNewElement(actionblock, bbox, wire)
  2823         dialog.Destroy()
  2822         dialog.Destroy()
  2824 
  2823 
  2825 #-------------------------------------------------------------------------------
  2824     # -------------------------------------------------------------------------------
  2826 #                          Edit element content functions
  2825     #                          Edit element content functions
  2827 #-------------------------------------------------------------------------------
  2826     # -------------------------------------------------------------------------------
  2828 
  2827 
  2829     def EditBlockContent(self, block):
  2828     def EditBlockContent(self, block):
  2830         dialog = FBDBlockDialog(self.ParentWindow, self.Controler, self.TagName)
  2829         dialog = FBDBlockDialog(self.ParentWindow, self.Controler, self.TagName)
  2831         dialog.SetPreviewFont(self.GetFont())
  2830         dialog.SetPreviewFont(self.GetFont())
  2832         dialog.SetMinElementSize(block.GetSize())
  2831         dialog.SetMinElementSize(block.GetSize())
  3128             self.RefreshScrollBars()
  3127             self.RefreshScrollBars()
  3129             self.RefreshVisibleElements()
  3128             self.RefreshVisibleElements()
  3130             comment.Refresh(rect)
  3129             comment.Refresh(rect)
  3131         dialog.Destroy()
  3130         dialog.Destroy()
  3132 
  3131 
  3133 #-------------------------------------------------------------------------------
  3132     # -------------------------------------------------------------------------------
  3134 #                          Model update functions
  3133     #                          Model update functions
  3135 #-------------------------------------------------------------------------------
  3134     # -------------------------------------------------------------------------------
  3136 
  3135 
  3137     def RefreshBlockModel(self, block):
  3136     def RefreshBlockModel(self, block):
  3138         blockid = block.GetId()
  3137         blockid = block.GetId()
  3139         infos = {}
  3138         infos = {}
  3140         infos["type"] = block.GetType()
  3139         infos["type"] = block.GetType()
  3277         infos["x"], infos["y"] = actionblock.GetPosition()
  3276         infos["x"], infos["y"] = actionblock.GetPosition()
  3278         infos["width"], infos["height"] = actionblock.GetSize()
  3277         infos["width"], infos["height"] = actionblock.GetSize()
  3279         infos["connector"] = actionblock.GetConnector()
  3278         infos["connector"] = actionblock.GetConnector()
  3280         self.Controler.SetEditedElementActionBlockInfos(self.TagName, actionblockid, infos)
  3279         self.Controler.SetEditedElementActionBlockInfos(self.TagName, actionblockid, infos)
  3281 
  3280 
  3282 
  3281     # -------------------------------------------------------------------------------
  3283 #-------------------------------------------------------------------------------
  3282     #                          Model delete functions
  3284 #                          Model delete functions
  3283     # -------------------------------------------------------------------------------
  3285 #-------------------------------------------------------------------------------
       
  3286 
  3284 
  3287     def DeleteBlock(self, block):
  3285     def DeleteBlock(self, block):
  3288         elements = []
  3286         elements = []
  3289         for output in block.GetConnectors()["outputs"]:
  3287         for output in block.GetConnectors()["outputs"]:
  3290             for element in output.GetConnectedBlocks():
  3288             for element in output.GetConnectedBlocks():
  3427     def DeleteActionBlock(self, actionblock):
  3425     def DeleteActionBlock(self, actionblock):
  3428         actionblock.Clean()
  3426         actionblock.Clean()
  3429         self.RemoveBlock(actionblock)
  3427         self.RemoveBlock(actionblock)
  3430         self.Controler.RemoveEditedElementInstance(self.TagName, actionblock.GetId())
  3428         self.Controler.RemoveEditedElementInstance(self.TagName, actionblock.GetId())
  3431 
  3429 
  3432 
  3430     # -------------------------------------------------------------------------------
  3433 #-------------------------------------------------------------------------------
  3431     #                            Editing functions
  3434 #                            Editing functions
  3432     # -------------------------------------------------------------------------------
  3435 #-------------------------------------------------------------------------------
       
  3436 
  3433 
  3437     def Cut(self):
  3434     def Cut(self):
  3438         if not self.Debug and (self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement) or isinstance(self.SelectedElement, Graphic_Group)):
  3435         if not self.Debug and (self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement) or isinstance(self.SelectedElement, Graphic_Group)):
  3439             blocks, wires = self.SelectedElement.GetDefinition()
  3436             blocks, wires = self.SelectedElement.GetDefinition()
  3440             text = self.Controler.GetEditedElementInstancesCopy(self.TagName, blocks, wires, self.Debug)
  3437             text = self.Controler.GetEditedElementInstancesCopy(self.TagName, blocks, wires, self.Debug)
  3561                 self.RefreshJumpModel(block)
  3558                 self.RefreshJumpModel(block)
  3562             elif isinstance(block, SFC_ActionBlock):
  3559             elif isinstance(block, SFC_ActionBlock):
  3563                 self.Controler.AddEditedElementActionBlock(self.TagName, block.GetId())
  3560                 self.Controler.AddEditedElementActionBlock(self.TagName, block.GetId())
  3564                 self.RefreshActionBlockModel(block)
  3561                 self.RefreshActionBlockModel(block)
  3565 
  3562 
  3566 #-------------------------------------------------------------------------------
  3563     # -------------------------------------------------------------------------------
  3567 #                         Find and Replace functions
  3564     #                         Find and Replace functions
  3568 #-------------------------------------------------------------------------------
  3565     # -------------------------------------------------------------------------------
  3569 
  3566 
  3570     def Find(self, direction, search_params):
  3567     def Find(self, direction, search_params):
  3571         if self.SearchParams != search_params:
  3568         if self.SearchParams != search_params:
  3572             self.ClearHighlights(SEARCH_RESULT_HIGHLIGHT)
  3569             self.ClearHighlights(SEARCH_RESULT_HIGHLIGHT)
  3573 
  3570 
  3604         else:
  3601         else:
  3605             if self.CurrentFindHighlight is not None:
  3602             if self.CurrentFindHighlight is not None:
  3606                 self.RemoveHighlight(*self.CurrentFindHighlight)
  3603                 self.RemoveHighlight(*self.CurrentFindHighlight)
  3607             self.CurrentFindHighlight = None
  3604             self.CurrentFindHighlight = None
  3608 
  3605 
  3609 #-------------------------------------------------------------------------------
  3606     # -------------------------------------------------------------------------------
  3610 #                        Highlights showing functions
  3607     #                        Highlights showing functions
  3611 #-------------------------------------------------------------------------------
  3608     # -------------------------------------------------------------------------------
  3612 
  3609 
  3613     def OnRefreshHighlightsTimer(self, event):
  3610     def OnRefreshHighlightsTimer(self, event):
  3614         self.RefreshView()
  3611         self.RefreshView()
  3615         event.Skip()
  3612         event.Skip()
  3616 
  3613 
  3645             if infos[0] in ["comment", "io_variable", "block", "connector", "coil", "contact", "step", "transition", "jump", "action_block"]:
  3642             if infos[0] in ["comment", "io_variable", "block", "connector", "coil", "contact", "step", "transition", "jump", "action_block"]:
  3646                 block = self.FindElementById(infos[1])
  3643                 block = self.FindElementById(infos[1])
  3647                 if block is not None:
  3644                 if block is not None:
  3648                     block.AddHighlight(infos[2:], start, end, highlight_type)
  3645                     block.AddHighlight(infos[2:], start, end, highlight_type)
  3649 
  3646 
  3650 #-------------------------------------------------------------------------------
  3647     # -------------------------------------------------------------------------------
  3651 #                            Drawing functions
  3648     #                            Drawing functions
  3652 #-------------------------------------------------------------------------------
  3649     # -------------------------------------------------------------------------------
  3653 
  3650 
  3654     def OnScrollWindow(self, event):
  3651     def OnScrollWindow(self, event):
  3655         if self.Editor.HasCapture() and self.StartMousePos is not None:
  3652         if self.Editor.HasCapture() and self.StartMousePos is not None:
  3656             return
  3653             return
  3657         if wx.Platform == '__WXMSW__':
  3654         if wx.Platform == '__WXMSW__':