Viewer.py
changeset 114 06454545e5d0
parent 111 0ec40799ba11
child 118 0c53d6a36013
equal deleted inserted replaced
113:9eeaebd867aa 114:06454545e5d0
    30 from Dialogs import *
    30 from Dialogs import *
    31 
    31 
    32 SCROLLBAR_UNIT = 10
    32 SCROLLBAR_UNIT = 10
    33 WINDOW_BORDER = 10
    33 WINDOW_BORDER = 10
    34 SCROLL_ZONE = 10
    34 SCROLL_ZONE = 10
       
    35 
       
    36 def AppendMenu(parent, help, id, kind, text):
       
    37     if wx.VERSION >= (2, 6, 0):
       
    38         parent.Append(help=help, id=id, kind=kind, text=text)
       
    39     else:
       
    40         parent.Append(helpString=help, id=id, kind=kind, item=text)
    35 
    41 
    36 #-------------------------------------------------------------------------------
    42 #-------------------------------------------------------------------------------
    37 #                       Graphic elements Viewer base class
    43 #                       Graphic elements Viewer base class
    38 #-------------------------------------------------------------------------------
    44 #-------------------------------------------------------------------------------
    39 
    45 
   117 manipulating graphic elements
   123 manipulating graphic elements
   118 """
   124 """
   119 
   125 
   120 class Viewer(wx.ScrolledWindow):
   126 class Viewer(wx.ScrolledWindow):
   121     
   127     
       
   128     if wx.VERSION < (2, 6, 0):
       
   129         def Bind(self, event, function, id = None):
       
   130             if id is not None:
       
   131                 event(self, id, function)
       
   132             else:
       
   133                 event(self, function)
       
   134     
   122     # Create Contextual Menu items
   135     # Create Contextual Menu items
   123     def _init_coll_ContextualMenu_Items(self, parent):
   136     def _init_coll_ContextualMenu_Items(self, parent):
   124         # Create menu items
   137         # Create menu items
   125         parent.Append(help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS0,
   138         AppendMenu(parent, help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS0,
   126               kind=wx.ITEM_RADIO, text=u'No Modifier')
   139               kind=wx.ITEM_RADIO, text=u'No Modifier')
   127         parent.Append(help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS1,
   140         AppendMenu(parent, help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS1,
   128               kind=wx.ITEM_RADIO, text=u'Negated')
   141               kind=wx.ITEM_RADIO, text=u'Negated')
   129         parent.Append(help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS2,
   142         AppendMenu(parent, help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS2,
   130               kind=wx.ITEM_RADIO, text=u'Rising Edge')
   143               kind=wx.ITEM_RADIO, text=u'Rising Edge')
   131         parent.Append(help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS3,
   144         AppendMenu(parent, help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS3,
   132               kind=wx.ITEM_RADIO, text=u'Falling Edge')
   145               kind=wx.ITEM_RADIO, text=u'Falling Edge')
   133         parent.AppendSeparator()
   146         parent.AppendSeparator()
   134         parent.Append(help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS5,
   147         AppendMenu(parent, help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS5,
   135               kind=wx.ITEM_NORMAL, text=u'Add Wire Segment')
   148               kind=wx.ITEM_NORMAL, text=u'Add Wire Segment')
   136         parent.Append(help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS6,
   149         AppendMenu(parent, help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS6,
   137               kind=wx.ITEM_NORMAL, text=u'Delete Wire Segment')
   150               kind=wx.ITEM_NORMAL, text=u'Delete Wire Segment')
   138         parent.AppendSeparator()
   151         parent.AppendSeparator()
   139         parent.Append(help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS8,
   152         AppendMenu(parent, help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS8,
   140               kind=wx.ITEM_NORMAL, text=u'Add Divergence Branch')
   153               kind=wx.ITEM_NORMAL, text=u'Add Divergence Branch')
   141         parent.Append(help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS9,
   154         AppendMenu(parent, help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS9,
   142               kind=wx.ITEM_NORMAL, text=u'Delete Divergence Branch')
   155               kind=wx.ITEM_NORMAL, text=u'Delete Divergence Branch')
   143         parent.AppendSeparator()
   156         parent.AppendSeparator()
   144         parent.Append(help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS11,
   157         AppendMenu(parent, help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS11,
   145               kind=wx.ITEM_NORMAL, text=u'Edit Block')
   158               kind=wx.ITEM_NORMAL, text=u'Edit Block')
   146         parent.Append(help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS12,
   159         AppendMenu(parent, help='', id=ID_FBDVIEWERCONTEXTUALMENUITEMS12,
   147               kind=wx.ITEM_NORMAL, text=u'Delete')
   160               kind=wx.ITEM_NORMAL, text=u'Delete')
   148         # Link menu event to corresponding called functions
   161         # Link menu event to corresponding called functions
   149         self.Bind(wx.EVT_MENU, self.OnNoModifierMenu,
   162         self.Bind(wx.EVT_MENU, self.OnNoModifierMenu,
   150               id=ID_FBDVIEWERCONTEXTUALMENUITEMS0)
   163               id=ID_FBDVIEWERCONTEXTUALMENUITEMS0)
   151         self.Bind(wx.EVT_MENU, self.OnNegatedMenu,
   164         self.Bind(wx.EVT_MENU, self.OnNegatedMenu,
   183         self.SetBackgroundColour(wx.Colour(255,255,255))
   196         self.SetBackgroundColour(wx.Colour(255,255,255))
   184         self.ResetView()
   197         self.ResetView()
   185         self.Scaling = None
   198         self.Scaling = None
   186         #self.Scaling = (8, 8)
   199         #self.Scaling = (8, 8)
   187         self.DrawGrid = True
   200         self.DrawGrid = True
   188         self.current_id = 0    
   201         self.current_id = 0
   189         
   202         
   190         # Initialize Block, Wire and Comment numbers
   203         # Initialize Block, Wire and Comment numbers
   191         self.block_id = self.wire_id = self.comment_id = 0
   204         self.block_id = self.wire_id = self.comment_id = 0
   192         
   205         
   193         # Initialize Viewer mode to Selection mode
   206         # Initialize Viewer mode to Selection mode
   194         self.Mode = MODE_SELECTION
   207         self.Mode = MODE_SELECTION
   195         self.SavedMode = False
   208         self.SavedMode = False
       
   209         self.CurrentLanguage = "FBD"
   196         
   210         
   197         self.ParentWindow = window
   211         self.ParentWindow = window
   198         self.Controler = controler
   212         self.Controler = controler
   199         
   213         
   200         self.SetDropTarget(ViewerDropTarget(self))
   214         self.SetDropTarget(ViewerDropTarget(self))
   225             dc = wx.BufferedPaintDC(self)
   239             dc = wx.BufferedPaintDC(self)
   226         else:
   240         else:
   227             dc = wx.ClientDC(self)
   241             dc = wx.ClientDC(self)
   228         if wx.Platform != '__WXMSW__':
   242         if wx.Platform != '__WXMSW__':
   229             dc.SetFont(wx.NORMAL_FONT)
   243             dc.SetFont(wx.NORMAL_FONT)
   230         self.DoPrepareDC(dc)
   244         if wx.VERSION >= (2, 6, 0):
       
   245             self.DoPrepareDC(dc)
       
   246         else:
       
   247             self.PrepareDC(dc)
   231         return dc
   248         return dc
   232 
   249 
   233 #-------------------------------------------------------------------------------
   250 #-------------------------------------------------------------------------------
   234 #                         Element management functions
   251 #                         Element management functions
   235 #-------------------------------------------------------------------------------
   252 #-------------------------------------------------------------------------------
  1054                 self.SelectedElement.Move(0, scaling[1])
  1071                 self.SelectedElement.Move(0, scaling[1])
  1055                 self.SelectedElement.RefreshModel()
  1072                 self.SelectedElement.RefreshModel()
  1056                 self.RefreshBuffer()
  1073                 self.RefreshBuffer()
  1057                 self.RefreshScrollBars()
  1074                 self.RefreshScrollBars()
  1058                 self.Refresh(False)
  1075                 self.Refresh(False)
       
  1076         elif keycode == wx.WXK_SPACE and self.SelectedElement is not None and self.SelectedElement.Dragging:
       
  1077             self.CopyBlock(self.SelectedElement, self.SelectedElement.Pos)
       
  1078             self.RefreshBuffer()
       
  1079             self.RefreshScrollBars()
       
  1080             self.Refresh()
  1059         else:
  1081         else:
  1060             event.Skip()
  1082             event.Skip()
  1061 
  1083 
  1062 #-------------------------------------------------------------------------------
  1084 #-------------------------------------------------------------------------------
  1063 #                          Model adding functions
  1085 #                          Model adding functions
  1130             self.RefreshScrollBars()
  1152             self.RefreshScrollBars()
  1131             self.Refresh(False)
  1153             self.Refresh(False)
  1132         dialog.Destroy()
  1154         dialog.Destroy()
  1133 
  1155 
  1134     def AddNewComment(self, bbox):
  1156     def AddNewComment(self, bbox):
  1135         dialog = wx.TextEntryDialog(self.ParentWindow, "Add a new comment", "Please enter comment text", "", wx.OK|wx.CANCEL|wx.TE_MULTILINE)
  1157         if wx.VERSION >= (2, 5, 0):
       
  1158             dialog = wx.TextEntryDialog(self.ParentWindow, "Edit comment", "Please enter comment text", "", wx.OK|wx.CANCEL|wx.TE_MULTILINE)
       
  1159         else:
       
  1160             dialog = wx.TextEntryDialog(self.ParentWindow, "Edit comment", "Please enter comment text", "", wx.OK|wx.CANCEL)
  1136         if dialog.ShowModal() == wx.ID_OK:
  1161         if dialog.ShowModal() == wx.ID_OK:
  1137             value = dialog.GetValue()
  1162             value = dialog.GetValue()
  1138             id = self.GetNewId()
  1163             id = self.GetNewId()
  1139             comment = Comment(self, value, id)
  1164             comment = Comment(self, value, id)
  1140             comment.SetPosition(bbox.x, bbox.y)
  1165             comment.SetPosition(bbox.x, bbox.y)
  1551             self.RefreshScrollBars()
  1576             self.RefreshScrollBars()
  1552             self.Refresh(False)
  1577             self.Refresh(False)
  1553         dialog.Destroy()
  1578         dialog.Destroy()
  1554 
  1579 
  1555     def EditCommentContent(self, comment):
  1580     def EditCommentContent(self, comment):
  1556         dialog = wx.TextEntryDialog(self.ParentWindow, "Edit comment", "Please enter comment text", comment.GetContent(), wx.OK|wx.CANCEL|wx.TE_MULTILINE)
  1581         if wx.VERSION >= (2, 5, 0):
       
  1582             dialog = wx.TextEntryDialog(self.ParentWindow, "Edit comment", "Please enter comment text", comment.GetContent(), wx.OK|wx.CANCEL|wx.TE_MULTILINE)
       
  1583         else:
       
  1584             dialog = wx.TextEntryDialog(self.ParentWindow, "Edit comment", "Please enter comment text", comment.GetContent(), wx.OK|wx.CANCEL)
  1557         if dialog.ShowModal() == wx.ID_OK:
  1585         if dialog.ShowModal() == wx.ID_OK:
  1558             value = dialog.GetValue()
  1586             value = dialog.GetValue()
  1559             comment.SetContent(value)
  1587             comment.SetContent(value)
  1560             comment.RefreshModel()
  1588             comment.RefreshModel()
  1561             self.RefreshBuffer()
  1589             self.RefreshBuffer()
  1820 #-------------------------------------------------------------------------------
  1848 #-------------------------------------------------------------------------------
  1821 #                            Editing functions
  1849 #                            Editing functions
  1822 #-------------------------------------------------------------------------------
  1850 #-------------------------------------------------------------------------------
  1823     
  1851     
  1824     def Cut(self):
  1852     def Cut(self):
  1825         pass
  1853         if self.IsBlock(self.SelectedElement):
       
  1854             self.ParentWindow.SetCopyBuffer(self.SelectedElement.Clone())
       
  1855             self.SelectedElement.Delete()
       
  1856             self.RefreshBuffer()
       
  1857             self.RefreshScrollBars()
       
  1858             self.Refresh()
  1826         
  1859         
  1827     def Copy(self):
  1860     def Copy(self):
  1828         pass
  1861         if self.IsBlock(self.SelectedElement):
       
  1862             self.ParentWindow.SetCopyBuffer(self.SelectedElement.Clone())
  1829     
  1863     
  1830     def Paste(self):
  1864     def Paste(self):
  1831         pass
  1865         element = self.ParentWindow.GetCopyBuffer()
       
  1866         if element is not None and self.CanAddBlock(element):
       
  1867             block = self.CopyBlock(element, wx.Point(*self.CalcUnscrolledPosition(30, 30)))
       
  1868             if self.SelectedElement is not None:
       
  1869                 self.SelectedElement.SetSelected(False)
       
  1870             self.SelectedElement = block
       
  1871             self.SelectedElement.SetSelected(True)
       
  1872             self.RefreshBuffer()
       
  1873             self.RefreshScrollBars()
       
  1874             self.Refresh()
       
  1875 
       
  1876     def CanAddBlock(self, block):
       
  1877         if self.CurrentLanguage == "SFC":
       
  1878             return True
       
  1879         elif self.CurrentLanguage == "LD" and not isinstance(block, (SFC_Step, SFC_Transition, SFC_Divergence, SFC_Jump, SFC_ActionBlock)):
       
  1880             return True
       
  1881         elif self.CurrentLanguage == "FBD" and isinstance(block, (FBD_Block, FBD_Variable, FBD_Connector, Comment)):
       
  1882             return True
       
  1883         return False
       
  1884 
       
  1885     def CopyBlock(self, element, pos):
       
  1886         id = self.GetNewId()
       
  1887         block = element.Clone(id, pos)
       
  1888         self.AddBlock(block)
       
  1889         if isinstance(block, Comment):
       
  1890             self.Controler.AddCurrentElementEditingComment(id)
       
  1891             self.RefreshCommentModel(comment)
       
  1892         elif isinstance(block, FBD_Block):
       
  1893             self.Controler.AddCurrentElementEditingBlock(id, block.GetType(), None)
       
  1894             self.RefreshBlockModel(block)
       
  1895         elif isinstance(block, FBD_Variable):
       
  1896             self.Controler.AddCurrentElementEditingVariable(id, block.GetType())
       
  1897             self.RefreshVariableModel(block)
       
  1898         elif isinstance(block, FBD_Connector):
       
  1899             self.Controler.AddCurrentElementEditingConnection(id, block.GetType())
       
  1900             self.RefreshConnectionModel(block)
       
  1901         elif isinstance(block, LD_Contact):
       
  1902             self.Controler.AddCurrentElementEditingContact(id)
       
  1903             self.RefreshContactModel(block)
       
  1904         elif isinstance(block, LD_Coil):
       
  1905             self.Controler.AddCurrentElementEditingCoil(id)
       
  1906             self.RefreshCoilModel(block)
       
  1907         elif isinstance(block, LD_PowerRail):
       
  1908             self.Controler.AddCurrentElementEditingPowerRail(id, block.GetType())
       
  1909             self.RefreshPowerRailModel(block)
       
  1910         elif isinstance(block, SFC_Step):
       
  1911             self.Controler.AddCurrentElementEditingStep(id)
       
  1912             self.RefreshStepModel(block)    
       
  1913         elif isinstance(block, SFC_Transition):
       
  1914             self.Controler.AddCurrentElementEditingTransition(id)
       
  1915             self.RefreshTransitionModel(block)       
       
  1916         elif isinstance(block, SFC_Divergence):
       
  1917             self.Controler.AddCurrentElementEditingDivergence(id, block.GetType())
       
  1918             self.RefreshActionDivergenceModel(block)
       
  1919         elif isinstance(block, SFC_Jump):
       
  1920             self.Controler.AddCurrentElementEditingJump(id)
       
  1921             self.RefreshJumpModel(block)       
       
  1922         elif isinstance(block, SFC_ActionBlock):
       
  1923             self.Controler.AddCurrentElementEditingActionBlock(id)
       
  1924             self.RefreshActionBlockModel(block)
       
  1925         return block
       
  1926             
  1832 
  1927 
  1833 #-------------------------------------------------------------------------------
  1928 #-------------------------------------------------------------------------------
  1834 #                            Drawing functions
  1929 #                            Drawing functions
  1835 #-------------------------------------------------------------------------------
  1930 #-------------------------------------------------------------------------------
  1836 
  1931