andrej@1571: #!/usr/bin/env python Laurent@814: # -*- coding: utf-8 -*- Laurent@814: andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. Laurent@814: # andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD andrej@1696: # Copyright (C) 2017: Andrey Skvortsov Laurent@814: # andrej@1571: # See COPYING file for copyrights details. Laurent@814: # andrej@1571: # This program is free software; you can redistribute it and/or andrej@1571: # modify it under the terms of the GNU General Public License andrej@1571: # as published by the Free Software Foundation; either version 2 andrej@1571: # of the License, or (at your option) any later version. Laurent@814: # andrej@1571: # This program is distributed in the hope that it will be useful, andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1571: # GNU General Public License for more details. Laurent@814: # andrej@1571: # You should have received a copy of the GNU General Public License andrej@1571: # along with this program; if not, write to the Free Software andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Laurent@814: Laurent@814: import wx Laurent@814: Laurent@1250: from graphics.SFC_Objects import SFC_Step Laurent@1250: from BlockPreviewDialog import BlockPreviewDialog Laurent@814: andrej@1782: # ------------------------------------------------------------------------------- Laurent@1250: # Set SFC Step Parameters Dialog andrej@1782: # ------------------------------------------------------------------------------- Laurent@814: Laurent@1250: Laurent@1250: class SFCStepDialog(BlockPreviewDialog): andrej@1736: """ andrej@1736: Class that implements a dialog for defining parameters of a SFC step graphic andrej@1736: element andrej@1736: """ andrej@1730: Laurent@1250: def __init__(self, parent, controller, tagname, initial=False): Laurent@1250: """ Laurent@1250: Constructor Laurent@1250: @param parent: Parent wx.Window of dialog for modal Laurent@1250: @param controller: Reference to project controller Laurent@1250: @param tagname: Tagname of project POU edited Laurent@1250: @param initial: True if step is initial (default: False) Laurent@1250: """ andrej@1740: BlockPreviewDialog.__init__(self, parent, controller, tagname, andrej@1768: title=_('Edit Step')) andrej@1730: Laurent@1250: # Init common sizers Laurent@1250: self._init_sizers(2, 0, 6, None, 2, 1) andrej@1730: Laurent@1250: # Create label for SFC step name Laurent@1250: name_label = wx.StaticText(self, label=_('Name:')) Laurent@1250: self.LeftGridSizer.AddWindow(name_label, flag=wx.GROW) andrej@1730: Laurent@1250: # Create text control for defining SFC step name Laurent@814: self.StepName = wx.TextCtrl(self) Laurent@814: self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.StepName) Laurent@1250: self.LeftGridSizer.AddWindow(self.StepName, flag=wx.GROW) andrej@1730: Laurent@1250: # Create label for SFC step connectors Laurent@814: connectors_label = wx.StaticText(self, label=_('Connectors:')) Laurent@1250: self.LeftGridSizer.AddWindow(connectors_label, flag=wx.GROW) andrej@1730: Laurent@1250: # Create check boxes for defining connectors available on SFC step Laurent@1250: self.ConnectorsCheckBox = {} Laurent@1250: for name, label in [("input", _("Input")), Laurent@1250: ("output", _("Output")), Laurent@1250: ("action", _("Action"))]: Laurent@1250: check_box = wx.CheckBox(self, label=label) surkovsv93@1599: if name == "output" or (name == "input" and not initial): surkovsv93@1599: check_box.SetValue(True) Laurent@1250: self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, check_box) Laurent@1250: self.LeftGridSizer.AddWindow(check_box, flag=wx.GROW) Laurent@1250: self.ConnectorsCheckBox[name] = check_box andrej@1730: Laurent@1250: # Add preview panel and associated label to sizers Laurent@1250: self.RightGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW) Laurent@1250: self.RightGridSizer.AddWindow(self.Preview, flag=wx.GROW) andrej@1730: Laurent@1250: # Add buttons sizer to sizers andrej@1768: self.MainSizer.AddSizer( andrej@1768: self.ButtonSizer, border=20, andrej@1768: flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT) andrej@1730: Laurent@1250: # Save flag that indicates that step is initial Laurent@1250: self.Initial = initial andrej@1730: Laurent@1252: # Set default name for step Laurent@1252: self.StepName.ChangeValue(controller.GenerateNewName( Laurent@1252: tagname, None, "Step%d", 0)) andrej@1696: andrej@1696: self.Fit() andrej@1730: Laurent@1250: # Step name text control is default control having keyboard focus Laurent@814: self.StepName.SetFocus() andrej@1730: Laurent@814: def SetValues(self, values): Laurent@1250: """ Laurent@1250: Set default block parameters Laurent@1250: @param values: Block parameters values Laurent@1250: """ Laurent@1250: # For each parameters defined, set corresponding control value Laurent@1250: for name, value in values.items(): andrej@1730: Laurent@1250: # Parameter is step name Laurent@1250: if name == "name": Laurent@1250: self.StepName.ChangeValue(value) andrej@1730: Laurent@1250: # Set value of other controls Laurent@1250: else: Laurent@1250: control = self.ConnectorsCheckBox.get(name, None) Laurent@1250: if control is not None: Laurent@1250: control.SetValue(value) andrej@1730: Laurent@1250: # Refresh preview panel Laurent@814: self.RefreshPreview() andrej@1730: Laurent@814: def GetValues(self): Laurent@1250: """ Laurent@1250: Return step parameters defined in dialog Laurent@1250: @return: {parameter_name: parameter_value,...} Laurent@1250: """ Laurent@1250: values = {"name": self.StepName.GetValue()} Laurent@1250: values.update({ Laurent@1250: name: control.IsChecked() Laurent@1250: for name, control in self.ConnectorsCheckBox.iteritems()}) Laurent@1250: values["width"], values["height"] = self.Element.GetSize() Laurent@814: return values andrej@1730: Laurent@1250: def OnOK(self, event): Laurent@1250: """ Laurent@1250: Called when dialog OK button is pressed Laurent@1250: Test if step name defined is valid Laurent@1250: @param event: wx.Event from OK button Laurent@1250: """ Laurent@1250: message = None andrej@1730: Laurent@1250: # Get step name typed by user Laurent@1250: step_name = self.StepName.GetValue() andrej@1730: Laurent@1250: # Test that a name have been defined Laurent@1250: if step_name == "": Laurent@1250: message = _("Form isn't complete. Name must be filled!") andrej@1730: Laurent@1250: # If an error have been identify, show error message dialog Laurent@1250: if message is not None: Laurent@1250: self.ShowErrorMessage(message) andrej@1730: Laurent@1250: # Test step name validity Laurent@1250: elif self.TestElementName(step_name): Laurent@1250: # Call BlockPreviewDialog function Laurent@1250: BlockPreviewDialog.OnOK(self, event) andrej@1730: Laurent@814: def OnConnectorsChanged(self, event): Laurent@1250: """ Laurent@1250: Called when a step connector value changed Laurent@1250: @param event: wx.CheckBoxEvent Laurent@1250: """ Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnNameChanged(self, event): Laurent@1250: """ Laurent@1250: Called when step name value changed Laurent@1250: @param event: wx.TextEvent Laurent@1250: """ Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() andrej@1730: Laurent@814: def RefreshPreview(self): Laurent@1250: """ Laurent@1250: Refresh preview panel of graphic element Laurent@1250: Override BlockPreviewDialog function Laurent@1250: """ Laurent@1250: # Set graphic element displayed, creating a SFC step element andrej@1730: self.Element = SFC_Step(self.Preview, andrej@1730: self.StepName.GetValue(), Laurent@1250: self.Initial) andrej@1730: Laurent@1250: # Update connectors of SFC step element according to check boxes value Laurent@1250: for name, control in self.ConnectorsCheckBox.iteritems(): Laurent@1250: if control.IsChecked(): Laurent@1250: getattr(self.Element, "Add" + name.capitalize())() Laurent@1250: else: Laurent@1250: getattr(self.Element, "Remove" + name.capitalize())() andrej@1730: Laurent@1250: # Call BlockPreviewDialog function Laurent@1250: BlockPreviewDialog.RefreshPreview(self)