dialogs/SFCTransitionDialog.py
changeset 1784 64beb9e9c749
parent 1782 5b6ad7a7fd9d
child 1847 6198190bc121
equal deleted inserted replaced
1729:31e63e25b4cc 1784:64beb9e9c749
    26 import wx
    26 import wx
    27 
    27 
    28 from graphics.SFC_Objects import SFC_Transition
    28 from graphics.SFC_Objects import SFC_Transition
    29 from BlockPreviewDialog import BlockPreviewDialog
    29 from BlockPreviewDialog import BlockPreviewDialog
    30 
    30 
    31 #-------------------------------------------------------------------------------
    31 # -------------------------------------------------------------------------------
    32 #                        Set Transition Parameters Dialog
    32 #                        Set Transition Parameters Dialog
    33 #-------------------------------------------------------------------------------
    33 # -------------------------------------------------------------------------------
    34 
    34 
    35 """
       
    36 Class that implements a dialog for defining parameters of a transition graphic
       
    37 element
       
    38 """
       
    39 
    35 
    40 class SFCTransitionDialog(BlockPreviewDialog):
    36 class SFCTransitionDialog(BlockPreviewDialog):
    41     
    37     """
       
    38     Class that implements a dialog for defining parameters of a transition graphic
       
    39     element
       
    40     """
       
    41 
    42     def __init__(self, parent, controller, tagname, connection=True):
    42     def __init__(self, parent, controller, tagname, connection=True):
    43         """
    43         """
    44         Constructor
    44         Constructor
    45         @param parent: Parent wx.Window of dialog for modal
    45         @param parent: Parent wx.Window of dialog for modal
    46         @param controller: Reference to project controller
    46         @param controller: Reference to project controller
    47         @param tagname: Tagname of project POU edited
    47         @param tagname: Tagname of project POU edited
    48         @param connection: True if transition value can be defined by a
    48         @param connection: True if transition value can be defined by a
    49         connection (default: True)
    49         connection (default: True)
    50         """
    50         """
    51         BlockPreviewDialog.__init__(self, parent, controller, tagname,
    51         BlockPreviewDialog.__init__(self, parent, controller, tagname,
    52               title=_('Edit transition'))
    52                                     title=_('Edit transition'))
    53         
    53 
    54         # Init common sizers
    54         # Init common sizers
    55         self._init_sizers(2, 0, 8, None, 2, 1)
    55         self._init_sizers(2, 0, 8, None, 2, 1)
    56         
    56 
    57         # Create label for transition type
    57         # Create label for transition type
    58         type_label = wx.StaticText(self, label=_('Type:'))
    58         type_label = wx.StaticText(self, label=_('Type:'))
    59         self.LeftGridSizer.AddWindow(type_label, flag=wx.GROW)
    59         self.LeftGridSizer.AddWindow(type_label, flag=wx.GROW)
    60         
    60 
    61         # Create combo box for selecting reference value
    61         # Create combo box for selecting reference value
    62         reference = wx.ComboBox(self, style=wx.CB_READONLY)
    62         reference = wx.ComboBox(self, style=wx.CB_READONLY)
    63         reference.Append("")
    63         reference.Append("")
    64         for transition in controller.GetEditedElementTransitions(tagname):
    64         for transition in controller.GetEditedElementTransitions(tagname):
    65             reference.Append(transition)
    65             reference.Append(transition)
    66         self.Bind(wx.EVT_COMBOBOX, self.OnReferenceChanged, reference)
    66         self.Bind(wx.EVT_COMBOBOX, self.OnReferenceChanged, reference)
    67         
    67 
    68         # Create Text control for defining inline value
    68         # Create Text control for defining inline value
    69         inline = wx.TextCtrl(self)
    69         inline = wx.TextCtrl(self)
    70         self.Bind(wx.EVT_TEXT, self.OnInlineChanged, inline)
    70         self.Bind(wx.EVT_TEXT, self.OnInlineChanged, inline)
    71         
    71 
    72         # Create radio buttons for selecting power rail type
    72         # Create radio buttons for selecting power rail type
    73         self.TypeRadioButtons = {}
    73         self.TypeRadioButtons = {}
    74         first = True
    74         first = True
    75         for type, label, control in [('reference', _('Reference'), reference),
    75         for type, label, control in [('reference', _('Reference'), reference),
    76                                      ('inline', _('Inline'), inline),
    76                                      ('inline', _('Inline'), inline),
    77                                      ('connection', _('Connection'), None)]:
    77                                      ('connection', _('Connection'), None)]:
    78             radio_button = wx.RadioButton(self, label=label, 
    78             radio_button = wx.RadioButton(self, label=label,
    79                   style=(wx.RB_GROUP if first else 0))
    79                                           style=(wx.RB_GROUP if first else 0))
    80             radio_button.SetValue(first)
    80             radio_button.SetValue(first)
    81             self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button)
    81             self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button)
    82             self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW)
    82             self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW)
    83             if control is not None:
    83             if control is not None:
    84                 control.Enable(first)
    84                 control.Enable(first)
    85                 self.LeftGridSizer.AddWindow(control, flag=wx.GROW)
    85                 self.LeftGridSizer.AddWindow(control, flag=wx.GROW)
    86             self.TypeRadioButtons[type] = (radio_button, control)
    86             self.TypeRadioButtons[type] = (radio_button, control)
    87             first = False
    87             first = False
    88         
    88 
    89         # Create label for transition priority
    89         # Create label for transition priority
    90         priority_label = wx.StaticText(self, label=_('Priority:'))
    90         priority_label = wx.StaticText(self, label=_('Priority:'))
    91         self.LeftGridSizer.AddWindow(priority_label, flag=wx.GROW)
    91         self.LeftGridSizer.AddWindow(priority_label, flag=wx.GROW)
    92         
    92 
    93         # Create spin control for defining priority value
    93         # Create spin control for defining priority value
    94         self.Priority = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS)
    94         self.Priority = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS)
    95         self.Bind(wx.EVT_TEXT, self.OnPriorityChanged, self.Priority)
    95         self.Bind(wx.EVT_TEXT, self.OnPriorityChanged, self.Priority)
    96         self.LeftGridSizer.AddWindow(self.Priority, flag=wx.GROW)
    96         self.LeftGridSizer.AddWindow(self.Priority, flag=wx.GROW)
    97         
    97 
    98         # Add preview panel and associated label to sizers
    98         # Add preview panel and associated label to sizers
    99         self.RightGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW)
    99         self.RightGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW)
   100         self.RightGridSizer.AddWindow(self.Preview, flag=wx.GROW)
   100         self.RightGridSizer.AddWindow(self.Preview, flag=wx.GROW)
   101         
   101 
   102         # Add buttons sizer to sizers
   102         # Add buttons sizer to sizers
   103         self.MainSizer.AddSizer(self.ButtonSizer, border=20, 
   103         self.MainSizer.AddSizer(
   104               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
   104             self.ButtonSizer, border=20,
       
   105             flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT)
   105 
   106 
   106         self.Fit()
   107         self.Fit()
   107         
   108 
   108         # Reference radio button is default control having keyboard focus
   109         # Reference radio button is default control having keyboard focus
   109         self.TypeRadioButtons["reference"][0].SetFocus()
   110         self.TypeRadioButtons["reference"][0].SetFocus()
   110     
   111 
   111     def GetTransitionType(self):
   112     def GetTransitionType(self):
   112         """
   113         """
   113         Return type selected for SFC transition and associated value
   114         Return type selected for SFC transition and associated value
   114         @return: Type selected and associated value (None if no value)
   115         @return: Type selected and associated value (None if no value)
   115         """
   116         """
   122                 elif isinstance(control, wx.TextCtrl):
   123                 elif isinstance(control, wx.TextCtrl):
   123                     return type, control.GetValue()
   124                     return type, control.GetValue()
   124                 else:
   125                 else:
   125                     return type, None
   126                     return type, None
   126         return None, None
   127         return None, None
   127     
   128 
   128     def SetValues(self, values):
   129     def SetValues(self, values):
   129         """
   130         """
   130         Set default SFC transition parameters
   131         Set default SFC transition parameters
   131         @param values: Transition parameters values
   132         @param values: Transition parameters values
   132         """
   133         """
   133         # Extract transition value according to type
   134         # Extract transition value according to type
   134         type_value = values.get("value", None)
   135         type_value = values.get("value", None)
   135         
   136 
   136         # For each parameters defined, set corresponding control value
   137         # For each parameters defined, set corresponding control value
   137         for name, value in values.items():
   138         for name, value in values.items():
   138             
   139 
   139             # Parameter is SFC transition priority
   140             # Parameter is SFC transition priority
   140             if name == "priority":
   141             if name == "priority":
   141                 self.Priority.SetValue(values["priority"])
   142                 self.Priority.SetValue(values["priority"])
   142             
   143 
   143             # Parameter is SFC transition type
   144             # Parameter is SFC transition type
   144             elif name == "type":
   145             elif name == "type":
   145                 for type, (radio, control) in self.TypeRadioButtons.iteritems():
   146                 for type, (radio, control) in self.TypeRadioButtons.iteritems():
   146                     radio.SetValue(type == value)
   147                     radio.SetValue(type == value)
   147                     if control is not None:
   148                     if control is not None:
   150                         if type == value:
   151                         if type == value:
   151                             if isinstance(control, wx.ComboBox):
   152                             if isinstance(control, wx.ComboBox):
   152                                 control.SetStringSelection(type_value)
   153                                 control.SetStringSelection(type_value)
   153                             elif isinstance(control, wx.TextCtrl):
   154                             elif isinstance(control, wx.TextCtrl):
   154                                 control.ChangeValue(type_value)
   155                                 control.ChangeValue(type_value)
   155         
   156 
   156         # Refresh preview panel
   157         # Refresh preview panel
   157         self.RefreshPreview()
   158         self.RefreshPreview()
   158         
   159 
   159     def GetValues(self):
   160     def GetValues(self):
   160         """
   161         """
   161         Return SFC transition parameters defined in dialog
   162         Return SFC transition parameters defined in dialog
   162         @return: {parameter_name: parameter_value,...}
   163         @return: {parameter_name: parameter_value,...}
   163         """
   164         """
   164         values = {"priority" : self.Priority.GetValue()}
   165         values = {"priority": self.Priority.GetValue()}
   165         values["type"], values["value"] = self.GetTransitionType()
   166         values["type"], values["value"] = self.GetTransitionType()
   166         values["width"], values["height"] = self.Element.GetSize()
   167         values["width"], values["height"] = self.Element.GetSize()
   167         return values
   168         return values
   168     
   169 
   169     def OnOK(self, event):
   170     def OnOK(self, event):
   170         """
   171         """
   171         Called when dialog OK button is pressed
   172         Called when dialog OK button is pressed
   172         Test if parameters defined are valid
   173         Test if parameters defined are valid
   173         @param event: wx.Event from OK button
   174         @param event: wx.Event from OK button
   174         """
   175         """
   175         message = None
   176         message = None
   176         
   177 
   177         # Get transition type and value associated
   178         # Get transition type and value associated
   178         type, value = self.GetTransitionType()
   179         type, value = self.GetTransitionType()
   179         
   180 
   180         # Test that value associated to type is defined
   181         # Test that value associated to type is defined
   181         if type != "connection" and value == "":
   182         if type != "connection" and value == "":
   182             message = _("Form isn't complete. %s must be filled!") % type
   183             message = _("Form isn't complete. %s must be filled!") % type
   183         
   184 
   184         # Show error message if an error is detected
   185         # Show error message if an error is detected
   185         if message is not None:
   186         if message is not None:
   186             self.ShowErrorMessage(message)
   187             self.ShowErrorMessage(message)
   187         
   188 
   188         else:
   189         else:
   189             # Call BlockPreviewDialog function
   190             # Call BlockPreviewDialog function
   190             BlockPreviewDialog.OnOK(self, event)
   191             BlockPreviewDialog.OnOK(self, event)
   191 
   192 
   192     def OnTypeChanged(self, event):
   193     def OnTypeChanged(self, event):
   196         """
   197         """
   197         # Refresh sensibility of control associated to transition types
   198         # Refresh sensibility of control associated to transition types
   198         for type, (radio, control) in self.TypeRadioButtons.iteritems():
   199         for type, (radio, control) in self.TypeRadioButtons.iteritems():
   199             if control is not None:
   200             if control is not None:
   200                 control.Enable(radio.GetValue())
   201                 control.Enable(radio.GetValue())
   201         
   202 
   202         # Refresh preview panel
   203         # Refresh preview panel
   203         self.RefreshPreview()
   204         self.RefreshPreview()
   204         event.Skip()
   205         event.Skip()
   205 
   206 
   206     def OnReferenceChanged(self, event):
   207     def OnReferenceChanged(self, event):
   234         """
   235         """
   235         # Set graphic element displayed, creating a SFC transition
   236         # Set graphic element displayed, creating a SFC transition
   236         self.Element = SFC_Transition(self.Preview)
   237         self.Element = SFC_Transition(self.Preview)
   237         self.Element.SetType(*self.GetTransitionType())
   238         self.Element.SetType(*self.GetTransitionType())
   238         self.Element.SetPriority(self.Priority.GetValue())
   239         self.Element.SetPriority(self.Priority.GetValue())
   239         
   240 
   240         # Call BlockPreviewDialog function
   241         # Call BlockPreviewDialog function
   241         BlockPreviewDialog.RefreshPreview(self)
   242         BlockPreviewDialog.RefreshPreview(self)