dialogs/FBDVariableDialog.py
changeset 1784 64beb9e9c749
parent 1782 5b6ad7a7fd9d
child 1847 6198190bc121
equal deleted inserted replaced
1729:31e63e25b4cc 1784:64beb9e9c749
    27 
    27 
    28 from graphics.GraphicCommons import INPUT, INOUT, OUTPUT
    28 from graphics.GraphicCommons import INPUT, INOUT, OUTPUT
    29 from graphics.FBD_Objects import FBD_Variable
    29 from graphics.FBD_Objects import FBD_Variable
    30 from BlockPreviewDialog import BlockPreviewDialog
    30 from BlockPreviewDialog import BlockPreviewDialog
    31 
    31 
    32 #-------------------------------------------------------------------------------
    32 # -------------------------------------------------------------------------------
    33 #                                    Helpers
    33 #                                    Helpers
    34 #-------------------------------------------------------------------------------
    34 # -------------------------------------------------------------------------------
    35 
    35 
    36 # Dictionaries containing correspondence between variable block class and string
    36 # Dictionaries containing correspondence between variable block class and string
    37 # to be shown in Class combo box in both sense
    37 # to be shown in Class combo box in both sense
    38 VARIABLE_CLASSES_DICT = {INPUT : _("Input"),
    38 VARIABLE_CLASSES_DICT = {
    39                          INOUT : _("InOut"),
    39     INPUT:  _("Input"),
    40                          OUTPUT : _("Output")}
    40     INOUT:  _("InOut"),
       
    41     OUTPUT: _("Output")
       
    42 }
       
    43 
    41 VARIABLE_CLASSES_DICT_REVERSE = dict(
    44 VARIABLE_CLASSES_DICT_REVERSE = dict(
    42     [(value, key) for key, value in VARIABLE_CLASSES_DICT.iteritems()])
    45     [(value, key) for key, value in VARIABLE_CLASSES_DICT.iteritems()])
    43 
    46 
    44 #-------------------------------------------------------------------------------
    47 # -------------------------------------------------------------------------------
    45 #                        Set Variable Parameters Dialog
    48 #                        Set Variable Parameters Dialog
    46 #-------------------------------------------------------------------------------
    49 # -------------------------------------------------------------------------------
    47 
    50 
    48 """
       
    49 Class that implements a dialog for defining parameters of a FBD variable graphic
       
    50 element
       
    51 """
       
    52 
    51 
    53 class FBDVariableDialog(BlockPreviewDialog):
    52 class FBDVariableDialog(BlockPreviewDialog):
       
    53     """
       
    54     Class that implements a dialog for defining parameters of a FBD variable graphic
       
    55     element
       
    56     """
    54 
    57 
    55     def __init__(self, parent, controller, tagname, exclude_input=False):
    58     def __init__(self, parent, controller, tagname, exclude_input=False):
    56         """
    59         """
    57         Constructor
    60         Constructor
    58         @param parent: Parent wx.Window of dialog for modal
    61         @param parent: Parent wx.Window of dialog for modal
    59         @param controller: Reference to project controller
    62         @param controller: Reference to project controller
    60         @param tagname: Tagname of project POU edited
    63         @param tagname: Tagname of project POU edited
    61         @param exclude_input: Exclude input from variable class selection
    64         @param exclude_input: Exclude input from variable class selection
    62         """
    65         """
    63         BlockPreviewDialog.__init__(self, parent, controller, tagname,
    66         BlockPreviewDialog.__init__(self, parent, controller, tagname,
    64               title=_('Variable Properties'))
    67                                     title=_('Variable Properties'))
    65         
    68 
    66         # Init common sizers
    69         # Init common sizers
    67         self._init_sizers(4, 2, 4, None, 3, 2)
    70         self._init_sizers(4, 2, 4, None, 3, 2)
    68         
    71 
    69         # Create label for variable class
    72         # Create label for variable class
    70         class_label = wx.StaticText(self, label=_('Class:'))
    73         class_label = wx.StaticText(self, label=_('Class:'))
    71         self.LeftGridSizer.AddWindow(class_label, flag=wx.GROW)
    74         self.LeftGridSizer.AddWindow(class_label, flag=wx.GROW)
    72         
    75 
    73         # Create a combo box for defining variable class
    76         # Create a combo box for defining variable class
    74         self.Class = wx.ComboBox(self, style=wx.CB_READONLY)
    77         self.Class = wx.ComboBox(self, style=wx.CB_READONLY)
    75         self.Bind(wx.EVT_COMBOBOX, self.OnClassChanged, self.Class)
    78         self.Bind(wx.EVT_COMBOBOX, self.OnClassChanged, self.Class)
    76         self.LeftGridSizer.AddWindow(self.Class, flag=wx.GROW)
    79         self.LeftGridSizer.AddWindow(self.Class, flag=wx.GROW)
    77         
    80 
    78         # Create label for variable execution order
    81         # Create label for variable execution order
    79         execution_order_label = wx.StaticText(self, 
    82         execution_order_label = wx.StaticText(self,
    80               label=_('Execution Order:'))
    83                                               label=_('Execution Order:'))
    81         self.LeftGridSizer.AddWindow(execution_order_label, flag=wx.GROW)
    84         self.LeftGridSizer.AddWindow(execution_order_label, flag=wx.GROW)
    82         
    85 
    83         # Create spin control for defining variable execution order
    86         # Create spin control for defining variable execution order
    84         self.ExecutionOrder = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS)
    87         self.ExecutionOrder = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS)
    85         self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged, 
    88         self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged,
    86                   self.ExecutionOrder)
    89                   self.ExecutionOrder)
    87         self.LeftGridSizer.AddWindow(self.ExecutionOrder, flag=wx.GROW)
    90         self.LeftGridSizer.AddWindow(self.ExecutionOrder, flag=wx.GROW)
    88         
    91 
    89         # Create label for variable expression
    92         # Create label for variable expression
    90         name_label = wx.StaticText(self, label=_('Expression:'))
    93         name_label = wx.StaticText(self, label=_('Expression:'))
    91         self.RightGridSizer.AddWindow(name_label, border=5, 
    94         self.RightGridSizer.AddWindow(name_label, border=5,
    92               flag=wx.GROW|wx.BOTTOM)
    95                                       flag=wx.GROW | wx.BOTTOM)
    93         
    96 
    94         # Create text control for defining variable expression
    97         # Create text control for defining variable expression
    95         self.Expression = wx.TextCtrl(self)
    98         self.Expression = wx.TextCtrl(self)
    96         self.Bind(wx.EVT_TEXT, self.OnExpressionChanged, self.Expression)
    99         self.Bind(wx.EVT_TEXT, self.OnExpressionChanged, self.Expression)
    97         self.RightGridSizer.AddWindow(self.Expression, flag=wx.GROW)
   100         self.RightGridSizer.AddWindow(self.Expression, flag=wx.GROW)
    98         
   101 
    99         # Create a list box to selected variable expression in the list of
   102         # Create a list box to selected variable expression in the list of
   100         # variables defined in POU
   103         # variables defined in POU
   101         self.VariableName = wx.ListBox(self, size=wx.Size(-1,120),
   104         self.VariableName = wx.ListBox(self, size=wx.Size(-1, 120),
   102               style=wx.LB_SINGLE|wx.LB_SORT)
   105                                        style=wx.LB_SINGLE | wx.LB_SORT)
   103         self.Bind(wx.EVT_LISTBOX, self.OnNameChanged, self.VariableName)
   106         self.Bind(wx.EVT_LISTBOX, self.OnNameChanged, self.VariableName)
   104         self.RightGridSizer.AddWindow(self.VariableName, border=4, flag=wx.GROW|wx.TOP)
   107         self.RightGridSizer.AddWindow(self.VariableName, border=4, flag=wx.GROW | wx.TOP)
   105         
   108 
   106         # Add preview panel and associated label to sizers
   109         # Add preview panel and associated label to sizers
   107         self.MainSizer.AddWindow(self.PreviewLabel, border=20,
   110         self.MainSizer.AddWindow(self.PreviewLabel, border=20,
   108               flag=wx.GROW|wx.LEFT|wx.RIGHT)
   111                                  flag=wx.GROW | wx.LEFT | wx.RIGHT)
   109         self.MainSizer.AddWindow(self.Preview, border=20,
   112         self.MainSizer.AddWindow(self.Preview, border=20,
   110               flag=wx.GROW|wx.LEFT|wx.RIGHT)
   113                                  flag=wx.GROW | wx.LEFT | wx.RIGHT)
   111         
   114 
   112         # Add buttons sizer to sizers
   115         # Add buttons sizer to sizers
   113         self.MainSizer.AddSizer(self.ButtonSizer, border=20, 
   116         self.MainSizer.AddSizer(
   114               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
   117             self.ButtonSizer, border=20,
   115         
   118             flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT)
       
   119 
   116         # Set options that can be selected in class combo box
   120         # Set options that can be selected in class combo box
   117         for var_class, choice in VARIABLE_CLASSES_DICT.iteritems():
   121         for var_class, choice in VARIABLE_CLASSES_DICT.iteritems():
   118             if not exclude_input or var_class != INPUT:
   122             if not exclude_input or var_class != INPUT:
   119                 self.Class.Append(choice)
   123                 self.Class.Append(choice)
   120         self.Class.SetSelection(0)
   124         self.Class.SetSelection(0)
   121         
   125 
   122         # Extract list of variables defined in POU
   126         # Extract list of variables defined in POU
   123         self.RefreshVariableList()
   127         self.RefreshVariableList()
   124         
   128 
   125         # Refresh values in name list box
   129         # Refresh values in name list box
   126         self.RefreshNameList()
   130         self.RefreshNameList()
   127         
   131 
   128         self.Preview.SetInitialSize(wx.Size(-1, 60))
   132         self.Preview.SetInitialSize(wx.Size(-1, 60))
   129         self.Fit()
   133         self.Fit()
   130 
   134 
   131         # Class combo box is default control having keyboard focus
   135         # Class combo box is default control having keyboard focus
   132         self.Class.SetFocus()
   136         self.Class.SetFocus()
   136         Called to refresh names in name list box
   140         Called to refresh names in name list box
   137         """
   141         """
   138         # Get variable class to select POU variable applicable
   142         # Get variable class to select POU variable applicable
   139         var_class = VARIABLE_CLASSES_DICT_REVERSE[
   143         var_class = VARIABLE_CLASSES_DICT_REVERSE[
   140                             self.Class.GetStringSelection()]
   144                             self.Class.GetStringSelection()]
   141         
   145 
   142         # Refresh names in name list box by selecting variables in POU variables
   146         # Refresh names in name list box by selecting variables in POU variables
   143         # list that can be applied to variable class
   147         # list that can be applied to variable class
   144         self.VariableName.Clear()
   148         self.VariableName.Clear()
   145         for name, (var_type, value_type) in self.VariableList.iteritems():
   149         for name, (var_type, value_type) in self.VariableList.iteritems():
   146             if var_type != "Input" or var_class == INPUT:
   150             if var_type != "Input" or var_class == INPUT:
   147                 self.VariableName.Append(name)
   151                 self.VariableName.Append(name)
   148         
   152 
   149         # Get variable expression and select corresponding value in name list
   153         # Get variable expression and select corresponding value in name list
   150         # box if it exists
   154         # box if it exists
   151         selected = self.Expression.GetValue()
   155         selected = self.Expression.GetValue()
   152         if (selected != "" and 
   156         if selected != "" and self.VariableName.FindString(selected) != wx.NOT_FOUND:
   153             self.VariableName.FindString(selected) != wx.NOT_FOUND):
       
   154             self.VariableName.SetStringSelection(selected)
   157             self.VariableName.SetStringSelection(selected)
   155         else:
   158         else:
   156             self.VariableName.SetSelection(wx.NOT_FOUND)
   159             self.VariableName.SetSelection(wx.NOT_FOUND)
   157         
   160 
   158         # Disable name list box if no name present inside
   161         # Disable name list box if no name present inside
   159         self.VariableName.Enable(self.VariableName.GetCount() > 0)
   162         self.VariableName.Enable(self.VariableName.GetCount() > 0)
   160             
   163 
   161     def SetValues(self, values):
   164     def SetValues(self, values):
   162         """
   165         """
   163         Set default variable parameters
   166         Set default variable parameters
   164         @param values: Variable parameters values
   167         @param values: Variable parameters values
   165         """
   168         """
   166         
   169 
   167         # Get class parameter value
   170         # Get class parameter value
   168         var_class = values.get("class", None)
   171         var_class = values.get("class", None)
   169         if var_class is not None:
   172         if var_class is not None:
   170             # Set class selected in class combo box
   173             # Set class selected in class combo box
   171             self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[var_class])
   174             self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[var_class])
   172             # Refresh names in name list box according to var class
   175             # Refresh names in name list box according to var class
   173             self.RefreshNameList()
   176             self.RefreshNameList()
   174         
   177 
   175         # For each parameters defined, set corresponding control value
   178         # For each parameters defined, set corresponding control value
   176         for name, value in values.items():
   179         for name, value in values.items():
   177             
   180 
   178             # Parameter is variable expression
   181             # Parameter is variable expression
   179             if name == "expression":
   182             if name == "expression":
   180                 # Set expression text control value
   183                 # Set expression text control value
   181                 self.Expression.ChangeValue(value)
   184                 self.Expression.ChangeValue(value)
   182                 # Select corresponding text in name list box if it exists
   185                 # Select corresponding text in name list box if it exists
   183                 if self.VariableName.FindString(value) != wx.NOT_FOUND:
   186                 if self.VariableName.FindString(value) != wx.NOT_FOUND:
   184                     self.VariableName.SetStringSelection(value)
   187                     self.VariableName.SetStringSelection(value)
   185                 else:
   188                 else:
   186                     self.VariableName.SetSelection(wx.NOT_FOUND)
   189                     self.VariableName.SetSelection(wx.NOT_FOUND)
   187             
   190 
   188             # Parameter is variable execution order
   191             # Parameter is variable execution order
   189             elif name == "executionOrder":
   192             elif name == "executionOrder":
   190                 self.ExecutionOrder.SetValue(value)
   193                 self.ExecutionOrder.SetValue(value)
   191         
   194 
   192         # Refresh preview panel
   195         # Refresh preview panel
   193         self.RefreshPreview()
   196         self.RefreshPreview()
   194         self.Fit()
   197         self.Fit()
   195         
   198 
   196     def GetValues(self):
   199     def GetValues(self):
   197         """
   200         """
   198         Return block parameters defined in dialog
   201         Return block parameters defined in dialog
   199         @return: {parameter_name: parameter_value,...}
   202         @return: {parameter_name: parameter_value,...}
   200         """
   203         """
   213         Called when dialog OK button is pressed
   216         Called when dialog OK button is pressed
   214         Test if parameters defined are valid
   217         Test if parameters defined are valid
   215         @param event: wx.Event from OK button
   218         @param event: wx.Event from OK button
   216         """
   219         """
   217         message = None
   220         message = None
   218         
   221 
   219         # Test that an expression have been selected or typed by user
   222         # Test that an expression have been selected or typed by user
   220         value = self.Expression.GetValue()
   223         value = self.Expression.GetValue()
   221         if value == "":
   224         if value == "":
   222             message = _("At least a variable or an expression must be selected!")
   225             message = _("At least a variable or an expression must be selected!")
   223         
   226 
   224         # Show error message if an error is detected
   227         # Show error message if an error is detected
   225         if message is not None:
   228         if message is not None:
   226             self.ShowErrorMessage(message)
   229             self.ShowErrorMessage(message)
   227         
   230 
   228         else:
   231         else:
   229             # Call BlockPreviewDialog function
   232             # Call BlockPreviewDialog function
   230             BlockPreviewDialog.OnOK(self, event)
   233             BlockPreviewDialog.OnOK(self, event)
   231 
   234 
   232     def OnClassChanged(self, event):
   235     def OnClassChanged(self, event):
   234         Called when variable class value changed
   237         Called when variable class value changed
   235         @param event: wx.ComboBoxEvent
   238         @param event: wx.ComboBoxEvent
   236         """
   239         """
   237         # Refresh name list box values
   240         # Refresh name list box values
   238         self.RefreshNameList()
   241         self.RefreshNameList()
   239         
   242 
   240         self.RefreshPreview()
   243         self.RefreshPreview()
   241         event.Skip()
   244         event.Skip()
   242 
   245 
   243     def OnNameChanged(self, event):
   246     def OnNameChanged(self, event):
   244         """
   247         """
   247         """
   250         """
   248         # Change expression test control value to the value selected in name
   251         # Change expression test control value to the value selected in name
   249         # list box if value selected is valid
   252         # list box if value selected is valid
   250         if self.VariableName.GetSelection() != wx.NOT_FOUND:
   253         if self.VariableName.GetSelection() != wx.NOT_FOUND:
   251             self.Expression.ChangeValue(self.VariableName.GetStringSelection())
   254             self.Expression.ChangeValue(self.VariableName.GetStringSelection())
   252         
   255 
   253         self.RefreshPreview()
   256         self.RefreshPreview()
   254         event.Skip()
   257         event.Skip()
   255     
   258 
   256     def OnExpressionChanged(self, event):
   259     def OnExpressionChanged(self, event):
   257         """
   260         """
   258         Called when expression text control is changed by user
   261         Called when expression text control is changed by user
   259         @param event: wx.ListBoxEvent
   262         @param event: wx.ListBoxEvent
   260         """
   263         """
   261         # Select the corresponding value in name list box if it exists
   264         # Select the corresponding value in name list box if it exists
   262         self.VariableName.SetSelection(
   265         self.VariableName.SetSelection(
   263             self.VariableName.FindString(self.Expression.GetValue()))
   266             self.VariableName.FindString(self.Expression.GetValue()))
   264         
   267 
   265         self.RefreshPreview()
   268         self.RefreshPreview()
   266         event.Skip()
   269         event.Skip()
   267     
   270 
   268     def OnExecutionOrderChanged(self, event):
   271     def OnExecutionOrderChanged(self, event):
   269         """
   272         """
   270         Called when block execution control value changed
   273         Called when block execution control value changed
   271         @param event: wx.SpinEvent
   274         @param event: wx.SpinEvent
   272         """
   275         """
   273         self.RefreshPreview()
   276         self.RefreshPreview()
   274         event.Skip()
   277         event.Skip()
   275     
   278 
   276     def RefreshPreview(self):
   279     def RefreshPreview(self):
   277         """
   280         """
   278         Refresh preview panel of graphic element
   281         Refresh preview panel of graphic element
   279         Override BlockPreviewDialog function
   282         Override BlockPreviewDialog function
   280         """
   283         """
   281         # Get expression value to put in FBD variable element
   284         # Get expression value to put in FBD variable element
   282         name = self.Expression.GetValue()
   285         name = self.Expression.GetValue()
   283         
   286 
   284         # Set graphic element displayed, creating a FBD variable element
   287         # Set graphic element displayed, creating a FBD variable element
   285         self.Element = FBD_Variable(self.Preview, 
   288         self.Element = FBD_Variable(
   286                     VARIABLE_CLASSES_DICT_REVERSE[
   289             self.Preview,
   287                         self.Class.GetStringSelection()], 
   290             VARIABLE_CLASSES_DICT_REVERSE[self.Class.GetStringSelection()],
   288                     name, 
   291             name,
   289                     self.VariableList.get(name, ("", ""))[1], 
   292             self.VariableList.get(name, ("", ""))[1],
   290                     executionOrder = self.ExecutionOrder.GetValue())
   293             executionOrder=self.ExecutionOrder.GetValue())
   291         
   294 
   292         # Call BlockPreviewDialog function
   295         # Call BlockPreviewDialog function
   293         BlockPreviewDialog.RefreshPreview(self)
   296         BlockPreviewDialog.RefreshPreview(self)
   294         
       
   295