dialogs/BlockPreviewDialog.py
changeset 1244 336d515096b1
parent 1242 ec2c415fc65e
child 1245 d34ba528346b
equal deleted inserted replaced
1243:e77c95c4c7fc 1244:336d515096b1
    73         # Add default dialog buttons sizer
    73         # Add default dialog buttons sizer
    74         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
    74         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
    75         self.Bind(wx.EVT_BUTTON, self.OnOK, 
    75         self.Bind(wx.EVT_BUTTON, self.OnOK, 
    76                   self.ButtonSizer.GetAffirmativeButton())
    76                   self.ButtonSizer.GetAffirmativeButton())
    77         
    77         
    78         self.Block = None            # Graphic element to display in preview
    78         self.Element = None            # Graphic element to display in preview
    79         self.MinBlockSize = None     # Graphic element minimal size
    79         self.MinElementSize = None     # Graphic element minimal size
    80         self.DefaultBlockName = None # Graphic element name when opening dialog
    80         
       
    81         # Variable containing the graphic element name when dialog is opened
       
    82         self.DefaultElementName = None
    81         
    83         
    82     def __del__(self):
    84     def __del__(self):
    83         """
    85         """
    84         Destructor
    86         Destructor
    85         """
    87         """
    86         # Remove reference to project controller
    88         # Remove reference to project controller
    87         self.Controller = None
    89         self.Controller = None
    88     
    90     
    89     def SetMinBlockSize(self, size):
    91     def SetMinElementSize(self, size):
    90         """
    92         """
    91         Define minimal graphic element size
    93         Define minimal graphic element size
    92         @param size: wx.Size object containing minimal size
    94         @param size: Tuple containing minimal size (width, height)
    93         """
    95         """
    94         self.MinBlockSize = size
    96         self.MinElementSize = size
    95     
    97     
    96     def SetPreviewFont(self, font):
    98     def SetPreviewFont(self, font):
    97         """
    99         """
    98         Set font of Preview panel
   100         Set font of Preview panel
    99         @param font: wx.Font object containing font style
   101         @param font: wx.Font object containing font style
   100         """
   102         """
   101         self.Preview.SetFont(font)
   103         self.Preview.SetFont(font)
   102     
   104     
   103     def TestBlockName(self, block_name):
   105     def TestElementName(self, element_name):
   104         """
   106         """
   105         Text displayed graphic element name
   107         Text displayed graphic element name
   106         @param block_name: Graphic element name
   108         @param element_name: Graphic element name
   107         """
   109         """
   108         # Variable containing error message format
   110         # Variable containing error message format
   109         message_format = None
   111         message_format = None
   110         # Get graphic element name in upper case
   112         # Get graphic element name in upper case
   111         uppercase_block_name = block_name.upper()
   113         uppercase_element_name = element_name.upper()
   112         
   114         
   113         # Test if graphic element name is a valid identifier
   115         # Test if graphic element name is a valid identifier
   114         if not TestIdentifier(block_name):
   116         if not TestIdentifier(element_name):
   115             message_format = _("\"%s\" is not a valid identifier!")
   117             message_format = _("\"%s\" is not a valid identifier!")
   116         
   118         
   117         # Test that graphic element name isn't a keyword
   119         # Test that graphic element name isn't a keyword
   118         elif uppercase_block_name in IEC_KEYWORDS:
   120         elif uppercase_element_name in IEC_KEYWORDS:
   119             message_format = _("\"%s\" is a keyword. It can't be used!")
   121             message_format = _("\"%s\" is a keyword. It can't be used!")
   120         
   122         
   121         # Test that graphic element name isn't a POU name
   123         # Test that graphic element name isn't a POU name
   122         elif uppercase_block_name in self.Controller.GetProjectPouNames():
   124         elif uppercase_element_name in self.Controller.GetProjectPouNames():
   123             message_format = _("\"%s\" pou already exists!")
   125             message_format = _("\"%s\" pou already exists!")
   124         
   126         
   125         # Test that graphic element name isn't already used in POU by a variable
   127         # Test that graphic element name isn't already used in POU by a variable
   126         # or another graphic element
   128         # or another graphic element
   127         elif ((self.DefaultBlockName is None or 
   129         elif ((self.DefaultElementName is None or 
   128                self.DefaultBlockName.upper() != uppercase_block_name) and 
   130                self.DefaultElementName.upper() != uppercase_element_name) and 
   129               uppercase_block_name in self.Controller.GetEditedElementVariables(
   131               uppercase_element_name in self.Controller.\
   130                                                                 self.TagName)):
   132                     GetEditedElementVariables(self.TagName)):
   131             message_format = _("\"%s\" element for this pou already exists!")
   133             message_format = _("\"%s\" element for this pou already exists!")
   132         
   134         
   133         # If an error have been identify, show error message dialog
   135         # If an error have been identify, show error message dialog
   134         if message_format is not None:
   136         if message_format is not None:
   135             self.ShowErrorMessage(message_format % block_name)
   137             self.ShowErrorMessage(message_format % element_name)
   136             # Test failed
   138             # Test failed
   137             return False
   139             return False
   138         
   140         
   139         # Test succeed
   141         # Test succeed
   140         return True
   142         return True
   169         dc = wx.ClientDC(self.Preview)
   171         dc = wx.ClientDC(self.Preview)
   170         dc.SetFont(self.Preview.GetFont())
   172         dc.SetFont(self.Preview.GetFont())
   171         dc.Clear()
   173         dc.Clear()
   172         
   174         
   173         # Return immediately if no graphic element defined
   175         # Return immediately if no graphic element defined
   174         if self.Block is None:
   176         if self.Element is None:
   175             return
   177             return
   176         
   178         
   177         # Calculate block size according to graphic element min size due to its
   179         # Calculate block size according to graphic element min size due to its
   178         # parameters and graphic element min size defined
   180         # parameters and graphic element min size defined
   179         min_width, min_height = self.Block.GetMinSize()
   181         min_width, min_height = self.Element.GetMinSize()
   180         width = max(self.MinBlockSize[0], min_width)
   182         width = max(self.MinElementSize[0], min_width)
   181         height = max(self.MinBlockSize[1], min_height)
   183         height = max(self.MinElementSize[1], min_height)
   182         self.Block.SetSize(width, height)
   184         self.Element.SetSize(width, height)
   183         
   185         
   184         # Get Preview panel size
   186         # Get Preview panel size
   185         client_size = self.Preview.GetClientSize()
   187         client_size = self.Preview.GetClientSize()
   186         
   188         
   187         # If graphic element is too big to be displayed in preview panel,
   189         # If graphic element is too big to be displayed in preview panel,
   194         dc.SetUserScale(1.0 / scale, 1.0 / scale)
   196         dc.SetUserScale(1.0 / scale, 1.0 / scale)
   195         
   197         
   196         # Center graphic element in preview panel
   198         # Center graphic element in preview panel
   197         x = int(client_size.width * scale - width) / 2
   199         x = int(client_size.width * scale - width) / 2
   198         y = int(client_size.height * scale - height) / 2
   200         y = int(client_size.height * scale - height) / 2
   199         self.Block.SetPosition(x, y)
   201         self.Element.SetPosition(x, y)
   200         
   202         
   201         # Draw graphic element
   203         # Draw graphic element
   202         self.Block.Draw(dc)
   204         self.Element.Draw(dc)
   203     
   205     
   204     def OnPaint(self, event):
   206     def OnPaint(self, event):
   205         """
   207         """
   206         Called when Preview panel need to be redraw
   208         Called when Preview panel need to be redraw
   207         @param event: wx.PaintEvent
   209         @param event: wx.PaintEvent