Laurent@814: #!/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. andrej@1571: # andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD andrej@1696: # Copyright (C) 2017: Andrey Skvortsov andrej@1571: # andrej@1571: # See COPYING file for copyrights details. andrej@1571: # 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. andrej@1571: # 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. andrej@1571: # 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@1245: from graphics.GraphicCommons import CONNECTOR, CONTINUATION Laurent@1245: from graphics.FBD_Objects import FBD_Connector Laurent@1245: from BlockPreviewDialog import BlockPreviewDialog Laurent@814: andrej@1782: # ------------------------------------------------------------------------------- Laurent@1245: # Set Connection Parameters Dialog andrej@1782: # ------------------------------------------------------------------------------- Laurent@814: Laurent@1249: Laurent@1245: class ConnectionDialog(BlockPreviewDialog): andrej@1736: """ andrej@1736: Class that implements a dialog for defining parameters of a connection graphic andrej@1736: element andrej@1736: """ andrej@1730: Laurent@1245: def __init__(self, parent, controller, tagname, apply_button=False): Laurent@1245: """ Laurent@1245: Constructor Laurent@1245: @param parent: Parent wx.Window of dialog for modal Laurent@1245: @param controller: Reference to project controller Laurent@1245: @param tagname: Tagname of project POU edited Laurent@1245: @param apply_button: Enable button for applying connector modification Laurent@1245: to all connector having the same name in POU (default: False) Laurent@1245: """ andrej@1730: BlockPreviewDialog.__init__(self, parent, controller, tagname, andrej@1768: title=_('Connection Properties')) andrej@1730: Laurent@1250: # Init common sizers andrej@1696: self._init_sizers(2, 0, 7, 1, 0, None) andrej@1730: Laurent@1245: # Create label for connection type Laurent@814: type_label = wx.StaticText(self, label=_('Type:')) Laurent@1250: self.LeftGridSizer.AddWindow(type_label, flag=wx.GROW) andrej@1730: Laurent@1245: # Create radio buttons for selecting connection type Laurent@1246: self.TypeRadioButtons = {} Laurent@1245: first = True Laurent@1245: for type, label in [(CONNECTOR, _('Connector')), Laurent@1245: (CONTINUATION, _('Continuation'))]: andrej@1730: radio_button = wx.RadioButton(self, label=label, andrej@1768: style=(wx.RB_GROUP if first else 0)) Laurent@1245: radio_button.SetValue(first) Laurent@1245: self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button) Laurent@1250: self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW) Laurent@1246: self.TypeRadioButtons[type] = radio_button Laurent@1245: first = False andrej@1730: Laurent@1245: # Create label for connection name Laurent@814: name_label = wx.StaticText(self, label=_('Name:')) Laurent@1250: self.LeftGridSizer.AddWindow(name_label, flag=wx.GROW) andrej@1730: Laurent@1245: # Create text control for defining connection name Laurent@814: self.ConnectionName = wx.TextCtrl(self) andrej@1740: self.ConnectionName.SetMinSize(wx.Size(200, -1)) Laurent@814: self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.ConnectionName) Laurent@1250: self.LeftGridSizer.AddWindow(self.ConnectionName, flag=wx.GROW) andrej@1730: Laurent@1245: # Add preview panel and associated label to sizers andrej@1740: self.Preview.SetMinSize(wx.Size(-1, 100)) andrej@1696: self.LeftGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW) andrej@1696: self.LeftGridSizer.AddWindow(self.Preview, flag=wx.GROW) andrej@1730: Laurent@1245: # 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@1696: self.ColumnSizer.RemoveSizer(self.RightGridSizer) andrej@1730: Laurent@1245: # Add button for applying connection name modification to all connection Laurent@1245: # of POU Laurent@856: if apply_button: Laurent@856: self.ApplyToAllButton = wx.Button(self, label=_("Propagate Name")) Laurent@856: self.ApplyToAllButton.SetToolTipString( Laurent@856: _("Apply name modification to all continuations with the same name")) Laurent@856: self.Bind(wx.EVT_BUTTON, self.OnApplyToAll, self.ApplyToAllButton) andrej@1696: self.ButtonSizer.AddWindow(self.ApplyToAllButton, flag=wx.LEFT) Laurent@1245: else: Laurent@1245: self.ConnectionName.ChangeValue( Laurent@1245: controller.GenerateNewName( Laurent@1245: tagname, None, "Connection%d", 0)) andrej@1696: self.Fit() andrej@1730: Laurent@1245: # Connector radio button is default control having keyboard focus Laurent@1246: self.TypeRadioButtons[CONNECTOR].SetFocus() andrej@1730: Laurent@1249: def GetConnectionType(self): Laurent@1249: """ Laurent@1249: Return type selected for connection Laurent@1249: @return: Type selected (CONNECTOR or CONTINUATION) Laurent@1249: """ Laurent@1249: return (CONNECTOR Laurent@1249: if self.TypeRadioButtons[CONNECTOR].GetValue() Laurent@1249: else CONTINUATION) andrej@1730: Laurent@814: def SetValues(self, values): Laurent@1245: """ Laurent@1245: Set default connection parameters Laurent@1245: @param values: Connection parameters values Laurent@1245: """ Laurent@1245: # For each parameters defined, set corresponding control value Laurent@814: for name, value in values.items(): andrej@1730: Laurent@1245: # Parameter is connection type Laurent@814: if name == "type": Laurent@1246: self.TypeRadioButtons[value].SetValue(True) andrej@1730: Laurent@1245: # Parameter is connection name Laurent@814: elif name == "name": Laurent@814: self.ConnectionName.SetValue(value) andrej@1730: Laurent@1245: # Refresh preview panel Laurent@814: self.RefreshPreview() andrej@1730: Laurent@814: def GetValues(self): Laurent@1245: """ Laurent@1245: Return connection parameters defined in dialog Laurent@1245: @return: {parameter_name: parameter_value,...} Laurent@1245: """ Laurent@1245: values = { Laurent@1249: "type": self.GetConnectionType(), Laurent@1245: "name": self.ConnectionName.GetValue()} Laurent@1245: values["width"], values["height"] = self.Element.GetSize() Laurent@814: return values Laurent@814: Laurent@1245: def TestConnectionName(self): Laurent@1245: """ Laurent@1245: Test that connection name is valid Laurent@1245: @return: True if connection name is valid Laurent@1245: """ Laurent@814: message = None andrej@1730: Laurent@1245: # Get connection name typed by user Laurent@814: connection_name = self.ConnectionName.GetValue() andrej@1730: Laurent@1245: # Test that a name have been defined Laurent@814: if connection_name == "": Laurent@814: message = _("Form isn't complete. Name must be filled!") andrej@1730: Laurent@1245: # If an error have been identify, show error message dialog Laurent@814: if message is not None: Laurent@1245: self.ShowErrorMessage(message) Laurent@1245: # Test failed Laurent@856: return False andrej@1730: Laurent@1245: # Return result of element name test Laurent@1245: return self.TestElementName(connection_name) andrej@1730: Laurent@856: def OnOK(self, event): Laurent@1245: """ Laurent@1245: Called when dialog OK button is pressed Laurent@1245: Test if connection name is valid Laurent@1245: @param event: wx.Event from OK button Laurent@1245: """ Laurent@1245: # Close dialog if connection name is valid Laurent@1245: if self.TestConnectionName(): Laurent@814: self.EndModal(wx.ID_OK) Laurent@814: Laurent@856: def OnApplyToAll(self, event): Laurent@1245: """ Laurent@1245: Called when Apply To All button is pressed Laurent@1245: Test if connection name is valid Laurent@1245: @param event: wx.Event from OK button Laurent@1245: """ Laurent@1245: # Close dialog if connection name is valid Laurent@1245: if self.TestConnectionName(): Laurent@856: self.EndModal(wx.ID_YESTOALL) Laurent@856: Laurent@814: def OnTypeChanged(self, event): Laurent@1245: """ Laurent@1245: Called when connection type changed Laurent@1245: @param event: wx.RadioButtonEvent Laurent@1245: """ Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnNameChanged(self, event): Laurent@1245: """ Laurent@1245: Called when connection name value changed Laurent@1245: @param event: wx.TextEvent Laurent@1245: """ Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() andrej@1730: Laurent@814: def RefreshPreview(self): Laurent@1245: """ Laurent@1245: Refresh preview panel of graphic element Laurent@1245: Override BlockPreviewDialog function Laurent@1245: """ Laurent@1245: # Set graphic element displayed, creating a FBD connection element andrej@1730: self.Element = FBD_Connector(self.Preview, andrej@1768: self.GetConnectionType(), andrej@1768: self.ConnectionName.GetValue()) andrej@1730: Laurent@1245: # Call BlockPreviewDialog function Laurent@1245: BlockPreviewDialog.RefreshPreview(self)