diff -r 31e63e25b4cc -r 64beb9e9c749 dialogs/FBDBlockDialog.py --- a/dialogs/FBDBlockDialog.py Mon Aug 21 20:17:19 2017 +0000 +++ b/dialogs/FBDBlockDialog.py Mon Aug 21 23:22:58 2017 +0300 @@ -31,24 +31,25 @@ from controls.LibraryPanel import LibraryPanel from BlockPreviewDialog import BlockPreviewDialog -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- # Helpers -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- + def GetBlockTypeDefaultNameModel(blocktype): return re.compile("%s[0-9]+" % blocktype if blocktype is not None else ".*") -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- # Set Block Parameters Dialog -#------------------------------------------------------------------------------- - -""" -Class that implements a dialog for defining parameters of a FBD block graphic -element -""" +# ------------------------------------------------------------------------------- + class FBDBlockDialog(BlockPreviewDialog): - + """ + Class that implements a dialog for defining parameters of a FBD block graphic + element + """ + def __init__(self, parent, controller, tagname): """ Constructor @@ -57,84 +58,84 @@ @param tagname: Tagname of project POU edited """ BlockPreviewDialog.__init__(self, parent, controller, tagname, - title=_('Block Properties')) - + title=_('Block Properties')) + # Init common sizers self._init_sizers(2, 0, 1, 0, 3, 2) - + # Create static box around library panel type_staticbox = wx.StaticBox(self, label=_('Type:')) left_staticboxsizer = wx.StaticBoxSizer(type_staticbox, wx.VERTICAL) self.LeftGridSizer.AddSizer(left_staticboxsizer, border=5, flag=wx.GROW) - + # Create Library panel and add it to static box self.LibraryPanel = LibraryPanel(self) self.LibraryPanel.SetInitialSize(wx.Size(-1, 400)) - + # Set function to call when selection in Library panel changed - setattr(self.LibraryPanel, "_OnTreeItemSelected", - self.OnLibraryTreeItemSelected) - left_staticboxsizer.AddWindow(self.LibraryPanel, 1, border=5, - flag=wx.GROW|wx.TOP) - + setattr(self.LibraryPanel, "_OnTreeItemSelected", + self.OnLibraryTreeItemSelected) + left_staticboxsizer.AddWindow(self.LibraryPanel, 1, border=5, + flag=wx.GROW | wx.TOP) + # Create sizer for other block parameters top_right_gridsizer = wx.FlexGridSizer(cols=2, hgap=0, rows=4, vgap=5) top_right_gridsizer.AddGrowableCol(1) self.RightGridSizer.AddSizer(top_right_gridsizer, flag=wx.GROW) - + # Create label for block name name_label = wx.StaticText(self, label=_('Name:')) - top_right_gridsizer.AddWindow(name_label, - flag=wx.ALIGN_CENTER_VERTICAL) - + top_right_gridsizer.AddWindow(name_label, + flag=wx.ALIGN_CENTER_VERTICAL) + # Create text control for defining block name self.BlockName = wx.TextCtrl(self) self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.BlockName) top_right_gridsizer.AddWindow(self.BlockName, flag=wx.GROW) - + # Create label for extended block input number inputs_label = wx.StaticText(self, label=_('Inputs:')) - top_right_gridsizer.AddWindow(inputs_label, - flag=wx.ALIGN_CENTER_VERTICAL) - + top_right_gridsizer.AddWindow(inputs_label, + flag=wx.ALIGN_CENTER_VERTICAL) + # Create spin control for defining extended block input number self.Inputs = wx.SpinCtrl(self, min=2, max=20, - style=wx.SP_ARROW_KEYS) + style=wx.SP_ARROW_KEYS) self.Bind(wx.EVT_SPINCTRL, self.OnInputsChanged, self.Inputs) top_right_gridsizer.AddWindow(self.Inputs, flag=wx.GROW) - + # Create label for block execution order - execution_order_label = wx.StaticText(self, - label=_('Execution Order:')) - top_right_gridsizer.AddWindow(execution_order_label, - flag=wx.ALIGN_CENTER_VERTICAL) - + execution_order_label = wx.StaticText(self, + label=_('Execution Order:')) + top_right_gridsizer.AddWindow(execution_order_label, + flag=wx.ALIGN_CENTER_VERTICAL) + # Create spin control for defining block execution order self.ExecutionOrder = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS) - self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged, + self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged, self.ExecutionOrder) top_right_gridsizer.AddWindow(self.ExecutionOrder, flag=wx.GROW) - + # Create label for block execution control - execution_control_label = wx.StaticText(self, - label=_('Execution Control:')) - top_right_gridsizer.AddWindow(execution_control_label, - flag=wx.ALIGN_CENTER_VERTICAL) - + execution_control_label = wx.StaticText(self, + label=_('Execution Control:')) + top_right_gridsizer.AddWindow(execution_control_label, + flag=wx.ALIGN_CENTER_VERTICAL) + # Create check box to enable block execution control self.ExecutionControl = wx.CheckBox(self) - self.Bind(wx.EVT_CHECKBOX, self.OnExecutionOrderChanged, + self.Bind(wx.EVT_CHECKBOX, self.OnExecutionOrderChanged, self.ExecutionControl) top_right_gridsizer.AddWindow(self.ExecutionControl, flag=wx.GROW) - + # Add preview panel and associated label to sizers self.RightGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW) self.RightGridSizer.AddWindow(self.Preview, flag=wx.GROW) - + # Add buttons sizer to sizers - self.MainSizer.AddSizer(self.ButtonSizer, border=20, - flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) - + self.MainSizer.AddSizer(self.ButtonSizer, border=20, + flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT) + # Dictionary containing correspondence between parameter exchanged and # control to fill with parameter value self.ParamsControl = { @@ -142,20 +143,20 @@ "executionOrder": self.ExecutionOrder, "executionControl": self.ExecutionControl } - + # Init controls value and sensibility self.BlockName.SetValue("") self.BlockName.Enable(False) self.Inputs.Enable(False) - + # Variable containing last name typed self.CurrentBlockName = None - + # Refresh Library panel values self.LibraryPanel.SetBlockList(controller.GetBlockTypes(tagname)) self.Fit() self.LibraryPanel.SetFocus() - + def SetValues(self, values): """ Set default block parameters @@ -163,38 +164,38 @@ """ # Extract block type defined in parameters blocktype = values.get("type", None) - - # Select block type in library panel + + # Select block type in library panel if blocktype is not None: - self.LibraryPanel.SelectTreeItem(blocktype, + self.LibraryPanel.SelectTreeItem(blocktype, values.get("inputs", None)) - + # Define regular expression for determine if block name is block # default name default_name_model = GetBlockTypeDefaultNameModel(blocktype) - + # For each parameters defined, set corresponding control value for name, value in values.items(): - + # Parameter is block name if name == "name": if value != "": # Set default graphic element name for testing self.DefaultElementName = value - + # Test if block name is type default block name and save # block name if not (name have been typed by user) if default_name_model.match(value) is None: self.CurrentBlockName = value - + self.BlockName.ChangeValue(value) - + # Set value of other controls else: control = self.ParamsControl.get(name, None) if control is not None: control.SetValue(value) - + # Refresh preview panel self.RefreshPreview() @@ -211,7 +212,7 @@ name: control.GetValue() for name, control in self.ParamsControl.iteritems()}) return values - + def OnOK(self, event): """ Called when dialog OK button is pressed @@ -219,31 +220,31 @@ @param event: wx.Event from OK button """ message = None - + # Get block type selected selected = self.LibraryPanel.GetSelectedBlock() - + # Get block type name and if block is a function block block_name = self.BlockName.GetValue() name_enabled = self.BlockName.IsEnabled() - + # Test that a type has been selected for block if selected is None: message = _("Form isn't complete. Valid block type must be selected!") - + # Test, if block is a function block, that a name have been defined elif name_enabled and block_name == "": message = _("Form isn't complete. Name must be filled!") - + # Show error message if an error is detected if message is not None: self.ShowErrorMessage(message) - + # Test block name validity if necessary elif not name_enabled or self.TestElementName(block_name): # Call BlockPreviewDialog function BlockPreviewDialog.OnOK(self, event) - + def OnLibraryTreeItemSelected(self, event): """ Called when block type selected in library panel @@ -251,12 +252,12 @@ """ # Get type selected in library panel values = self.LibraryPanel.GetSelectedBlock() - + # Get block type informations - blocktype = (self.Controller.GetBlockType(values["type"], + blocktype = (self.Controller.GetBlockType(values["type"], values["inputs"]) if values is not None else None) - + # Set input number spin control according to block type informations if blocktype is not None: self.Inputs.SetValue(len(blocktype["inputs"])) @@ -264,12 +265,12 @@ else: self.Inputs.SetValue(2) self.Inputs.Enable(False) - + # Update block name with default value if block type is a function and # current block name wasn't typed by user if blocktype is not None and blocktype["type"] != "function": self.BlockName.Enable(True) - + if self.CurrentBlockName is None: # Generate new block name according to block type, taking # default element name if it was already a default name for this @@ -277,21 +278,21 @@ default_name_model = GetBlockTypeDefaultNameModel(values["type"]) block_name = ( self.DefaultElementName - if (self.DefaultElementName is not None and + if (self.DefaultElementName is not None and default_name_model.match(self.DefaultElementName)) else self.Controller.GenerateNewName( self.TagName, None, values["type"]+"%d", 0)) else: block_name = self.CurrentBlockName - + self.BlockName.ChangeValue(block_name) else: self.BlockName.Enable(False) self.BlockName.ChangeValue("") - + # Refresh preview panel self.RefreshPreview() - + def OnNameChanged(self, event): """ Called when block name value changed @@ -302,7 +303,7 @@ self.CurrentBlockName = self.BlockName.GetValue() self.RefreshPreview() event.Skip() - + def OnInputsChanged(self, event): """ Called when block inputs number changed @@ -311,7 +312,7 @@ if self.Inputs.IsEnabled(): self.RefreshPreview() event.Skip() - + def OnExecutionOrderChanged(self, event): """ Called when block execution order value changed @@ -319,7 +320,7 @@ """ self.RefreshPreview() event.Skip() - + def OnExecutionControlChanged(self, event): """ Called when block execution control value changed @@ -327,7 +328,7 @@ """ self.RefreshPreview() event.Skip() - + def RefreshPreview(self): """ Refresh preview panel of graphic element @@ -335,22 +336,21 @@ """ # Get type selected in library panel values = self.LibraryPanel.GetSelectedBlock() - + # If a block type is selected in library panel if values is not None: # Set graphic element displayed, creating a FBD block element - self.Element = FBD_Block(self.Preview, values["type"], - (self.BlockName.GetValue() - if self.BlockName.IsEnabled() - else ""), - extension = self.Inputs.GetValue(), - inputs = values["inputs"], - executionControl = self.ExecutionControl.GetValue(), - executionOrder = self.ExecutionOrder.GetValue()) - + self.Element = FBD_Block( + self.Preview, values["type"], + (self.BlockName.GetValue() if self.BlockName.IsEnabled() else ""), + extension=self.Inputs.GetValue(), + inputs=values["inputs"], + executionControl=self.ExecutionControl.GetValue(), + executionOrder=self.ExecutionOrder.GetValue()) + # Reset graphic element displayed else: - self.Element = None - + self.Element = None + # Call BlockPreviewDialog function BlockPreviewDialog.RefreshPreview(self)