dialogs/ConnectionDialog.py
changeset 1730 64d8f52bc8c8
parent 1696 8043f32de7b8
child 1736 7e61baa047f0
equal deleted inserted replaced
1726:d51af006fa6b 1730:64d8f52bc8c8
    37 Class that implements a dialog for defining parameters of a connection graphic
    37 Class that implements a dialog for defining parameters of a connection graphic
    38 element
    38 element
    39 """
    39 """
    40 
    40 
    41 class ConnectionDialog(BlockPreviewDialog):
    41 class ConnectionDialog(BlockPreviewDialog):
    42     
    42 
    43     def __init__(self, parent, controller, tagname, apply_button=False):
    43     def __init__(self, parent, controller, tagname, apply_button=False):
    44         """
    44         """
    45         Constructor
    45         Constructor
    46         @param parent: Parent wx.Window of dialog for modal
    46         @param parent: Parent wx.Window of dialog for modal
    47         @param controller: Reference to project controller
    47         @param controller: Reference to project controller
    48         @param tagname: Tagname of project POU edited
    48         @param tagname: Tagname of project POU edited
    49         @param apply_button: Enable button for applying connector modification
    49         @param apply_button: Enable button for applying connector modification
    50         to all connector having the same name in POU (default: False)
    50         to all connector having the same name in POU (default: False)
    51         """
    51         """
    52         BlockPreviewDialog.__init__(self, parent, controller, tagname, 
    52         BlockPreviewDialog.__init__(self, parent, controller, tagname,
    53               title=_('Connection Properties'))
    53               title=_('Connection Properties'))
    54         
    54 
    55         # Init common sizers
    55         # Init common sizers
    56         self._init_sizers(2, 0, 7, 1, 0, None)
    56         self._init_sizers(2, 0, 7, 1, 0, None)
    57         
    57 
    58         # Create label for connection type
    58         # Create label for connection type
    59         type_label = wx.StaticText(self, label=_('Type:'))
    59         type_label = wx.StaticText(self, label=_('Type:'))
    60         self.LeftGridSizer.AddWindow(type_label, flag=wx.GROW)
    60         self.LeftGridSizer.AddWindow(type_label, flag=wx.GROW)
    61         
    61 
    62         # Create radio buttons for selecting connection type
    62         # Create radio buttons for selecting connection type
    63         self.TypeRadioButtons = {}
    63         self.TypeRadioButtons = {}
    64         first = True
    64         first = True
    65         for type, label in [(CONNECTOR, _('Connector')),
    65         for type, label in [(CONNECTOR, _('Connector')),
    66                             (CONTINUATION, _('Continuation'))]:
    66                             (CONTINUATION, _('Continuation'))]:
    67             radio_button = wx.RadioButton(self, label=label, 
    67             radio_button = wx.RadioButton(self, label=label,
    68                   style=(wx.RB_GROUP if first else 0))
    68                   style=(wx.RB_GROUP if first else 0))
    69             radio_button.SetValue(first)
    69             radio_button.SetValue(first)
    70             self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button)
    70             self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button)
    71             self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW)
    71             self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW)
    72             self.TypeRadioButtons[type] = radio_button
    72             self.TypeRadioButtons[type] = radio_button
    73             first = False
    73             first = False
    74         
    74 
    75         # Create label for connection name
    75         # Create label for connection name
    76         name_label = wx.StaticText(self, label=_('Name:'))
    76         name_label = wx.StaticText(self, label=_('Name:'))
    77         self.LeftGridSizer.AddWindow(name_label, flag=wx.GROW)
    77         self.LeftGridSizer.AddWindow(name_label, flag=wx.GROW)
    78         
    78 
    79         # Create text control for defining connection name
    79         # Create text control for defining connection name
    80         self.ConnectionName = wx.TextCtrl(self)
    80         self.ConnectionName = wx.TextCtrl(self)
    81         self.ConnectionName.SetMinSize(wx.Size(200,-1))
    81         self.ConnectionName.SetMinSize(wx.Size(200,-1))
    82         self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.ConnectionName)
    82         self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.ConnectionName)
    83         self.LeftGridSizer.AddWindow(self.ConnectionName, flag=wx.GROW)
    83         self.LeftGridSizer.AddWindow(self.ConnectionName, flag=wx.GROW)
    84         
    84 
    85         # Add preview panel and associated label to sizers
    85         # Add preview panel and associated label to sizers
    86         self.Preview.SetMinSize(wx.Size(-1,100))
    86         self.Preview.SetMinSize(wx.Size(-1,100))
    87         self.LeftGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW)
    87         self.LeftGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW)
    88         self.LeftGridSizer.AddWindow(self.Preview, flag=wx.GROW)
    88         self.LeftGridSizer.AddWindow(self.Preview, flag=wx.GROW)
    89         
    89 
    90         # Add buttons sizer to sizers
    90         # Add buttons sizer to sizers
    91         self.MainSizer.AddSizer(self.ButtonSizer, border=20, 
    91         self.MainSizer.AddSizer(self.ButtonSizer, border=20,
    92               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
    92               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
    93         self.ColumnSizer.RemoveSizer(self.RightGridSizer)
    93         self.ColumnSizer.RemoveSizer(self.RightGridSizer)
    94         
    94 
    95         # Add button for applying connection name modification to all connection
    95         # Add button for applying connection name modification to all connection
    96         # of POU
    96         # of POU
    97         if apply_button:
    97         if apply_button:
    98             self.ApplyToAllButton = wx.Button(self, label=_("Propagate Name"))
    98             self.ApplyToAllButton = wx.Button(self, label=_("Propagate Name"))
    99             self.ApplyToAllButton.SetToolTipString(
    99             self.ApplyToAllButton.SetToolTipString(
   103         else:
   103         else:
   104             self.ConnectionName.ChangeValue(
   104             self.ConnectionName.ChangeValue(
   105                 controller.GenerateNewName(
   105                 controller.GenerateNewName(
   106                         tagname, None, "Connection%d", 0))
   106                         tagname, None, "Connection%d", 0))
   107         self.Fit()
   107         self.Fit()
   108         
   108 
   109         # Connector radio button is default control having keyboard focus
   109         # Connector radio button is default control having keyboard focus
   110         self.TypeRadioButtons[CONNECTOR].SetFocus()
   110         self.TypeRadioButtons[CONNECTOR].SetFocus()
   111     
   111 
   112     def GetConnectionType(self):
   112     def GetConnectionType(self):
   113         """
   113         """
   114         Return type selected for connection
   114         Return type selected for connection
   115         @return: Type selected (CONNECTOR or CONTINUATION)
   115         @return: Type selected (CONNECTOR or CONTINUATION)
   116         """
   116         """
   117         return (CONNECTOR
   117         return (CONNECTOR
   118                 if self.TypeRadioButtons[CONNECTOR].GetValue()
   118                 if self.TypeRadioButtons[CONNECTOR].GetValue()
   119                 else CONTINUATION)
   119                 else CONTINUATION)
   120     
   120 
   121     def SetValues(self, values):
   121     def SetValues(self, values):
   122         """
   122         """
   123         Set default connection parameters
   123         Set default connection parameters
   124         @param values: Connection parameters values
   124         @param values: Connection parameters values
   125         """
   125         """
   126         # For each parameters defined, set corresponding control value
   126         # For each parameters defined, set corresponding control value
   127         for name, value in values.items():
   127         for name, value in values.items():
   128             
   128 
   129             # Parameter is connection type
   129             # Parameter is connection type
   130             if name == "type":
   130             if name == "type":
   131                 self.TypeRadioButtons[value].SetValue(True)
   131                 self.TypeRadioButtons[value].SetValue(True)
   132             
   132 
   133             # Parameter is connection name
   133             # Parameter is connection name
   134             elif name == "name":
   134             elif name == "name":
   135                 self.ConnectionName.SetValue(value)
   135                 self.ConnectionName.SetValue(value)
   136         
   136 
   137         # Refresh preview panel
   137         # Refresh preview panel
   138         self.RefreshPreview()
   138         self.RefreshPreview()
   139     
   139 
   140     def GetValues(self):
   140     def GetValues(self):
   141         """
   141         """
   142         Return connection parameters defined in dialog
   142         Return connection parameters defined in dialog
   143         @return: {parameter_name: parameter_value,...}
   143         @return: {parameter_name: parameter_value,...}
   144         """
   144         """
   152         """
   152         """
   153         Test that connection name is valid
   153         Test that connection name is valid
   154         @return: True if connection name is valid
   154         @return: True if connection name is valid
   155         """
   155         """
   156         message = None
   156         message = None
   157         
   157 
   158         # Get connection name typed by user
   158         # Get connection name typed by user
   159         connection_name = self.ConnectionName.GetValue()
   159         connection_name = self.ConnectionName.GetValue()
   160         
   160 
   161         # Test that a name have been defined
   161         # Test that a name have been defined
   162         if connection_name == "":
   162         if connection_name == "":
   163             message = _("Form isn't complete. Name must be filled!")
   163             message = _("Form isn't complete. Name must be filled!")
   164         
   164 
   165         # If an error have been identify, show error message dialog
   165         # If an error have been identify, show error message dialog
   166         if message is not None:
   166         if message is not None:
   167             self.ShowErrorMessage(message)
   167             self.ShowErrorMessage(message)
   168             # Test failed
   168             # Test failed
   169             return False
   169             return False
   170         
   170 
   171         # Return result of element name test
   171         # Return result of element name test
   172         return self.TestElementName(connection_name)
   172         return self.TestElementName(connection_name)
   173         
   173 
   174     def OnOK(self, event):
   174     def OnOK(self, event):
   175         """
   175         """
   176         Called when dialog OK button is pressed
   176         Called when dialog OK button is pressed
   177         Test if connection name is valid
   177         Test if connection name is valid
   178         @param event: wx.Event from OK button
   178         @param event: wx.Event from OK button
   204         Called when connection name value changed
   204         Called when connection name value changed
   205         @param event: wx.TextEvent
   205         @param event: wx.TextEvent
   206         """
   206         """
   207         self.RefreshPreview()
   207         self.RefreshPreview()
   208         event.Skip()
   208         event.Skip()
   209         
   209 
   210     def RefreshPreview(self):
   210     def RefreshPreview(self):
   211         """
   211         """
   212         Refresh preview panel of graphic element
   212         Refresh preview panel of graphic element
   213         Override BlockPreviewDialog function
   213         Override BlockPreviewDialog function
   214         """
   214         """
   215         # Set graphic element displayed, creating a FBD connection element
   215         # Set graphic element displayed, creating a FBD connection element
   216         self.Element = FBD_Connector(self.Preview, 
   216         self.Element = FBD_Connector(self.Preview,
   217                 self.GetConnectionType(),
   217                 self.GetConnectionType(),
   218                 self.ConnectionName.GetValue())
   218                 self.ConnectionName.GetValue())
   219         
   219 
   220         # Call BlockPreviewDialog function
   220         # Call BlockPreviewDialog function
   221         BlockPreviewDialog.RefreshPreview(self)
   221         BlockPreviewDialog.RefreshPreview(self)
   222