Laurent@814: # -*- coding: utf-8 -*- Laurent@814: Laurent@814: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor Laurent@814: #based on the plcopen standard. Laurent@814: # Laurent@814: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD Laurent@814: # Laurent@814: #See COPYING file for copyrights details. Laurent@814: # Laurent@814: #This library is free software; you can redistribute it and/or Laurent@814: #modify it under the terms of the GNU General Public Laurent@814: #License as published by the Free Software Foundation; either Laurent@814: #version 2.1 of the License, or (at your option) any later version. Laurent@814: # Laurent@814: #This library is distributed in the hope that it will be useful, Laurent@814: #but WITHOUT ANY WARRANTY; without even the implied warranty of Laurent@814: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Laurent@814: #General Public License for more details. Laurent@814: # Laurent@814: #You should have received a copy of the GNU General Public Laurent@814: #License along with this library; if not, write to the Free Software Laurent@814: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Laurent@814: Laurent@814: import wx Laurent@814: Laurent@1249: from graphics.GraphicCommons import LEFTRAIL, RIGHTRAIL, LD_LINE_SIZE Laurent@1249: from graphics.LD_Objects import LD_PowerRail Laurent@1249: from BlockPreviewDialog import BlockPreviewDialog Laurent@814: Laurent@814: #------------------------------------------------------------------------------- Laurent@1249: # Set Ladder Power Rail Parameters Dialog Laurent@814: #------------------------------------------------------------------------------- Laurent@814: Laurent@1249: """ Laurent@1249: Class that implements a dialog for defining parameters of a power rail graphic Laurent@1249: element Laurent@1249: """ Laurent@1249: Laurent@1249: class LDPowerRailDialog(BlockPreviewDialog): Laurent@814: Laurent@1249: def __init__(self, parent, controller, tagname): Laurent@1249: """ Laurent@1249: Constructor Laurent@1249: @param parent: Parent wx.Window of dialog for modal Laurent@1249: @param controller: Reference to project controller Laurent@1249: @param tagname: Tagname of project POU edited Laurent@1249: """ Laurent@1249: BlockPreviewDialog.__init__(self, parent, controller, tagname, Laurent@1249: size=wx.Size(350, 260), title=_('Power Rail Properties')) Laurent@814: Laurent@1250: # Init common sizers Laurent@1250: self._init_sizers(2, 0, 5, None, 2, 1) Laurent@814: Laurent@1249: # Create label for connection type Laurent@814: type_label = wx.StaticText(self, label=_('Type:')) Laurent@1250: self.LeftGridSizer.AddWindow(type_label, flag=wx.GROW) Laurent@814: Laurent@1249: # Create radio buttons for selecting power rail type Laurent@1249: self.TypeRadioButtons = {} Laurent@1249: first = True Laurent@1249: for type, label in [(LEFTRAIL, _('Left PowerRail')), Laurent@1249: (RIGHTRAIL, _('Right PowerRail'))]: Laurent@1249: radio_button = wx.RadioButton(self, label=label, Laurent@1249: style=(wx.RB_GROUP if first else 0)) Laurent@1249: radio_button.SetValue(first) Laurent@1249: self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button) Laurent@1250: self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW) Laurent@1249: self.TypeRadioButtons[type] = radio_button Laurent@1249: first = False Laurent@814: Laurent@1249: # Create label for power rail pin number Laurent@814: pin_number_label = wx.StaticText(self, label=_('Pin number:')) Laurent@1250: self.LeftGridSizer.AddWindow(pin_number_label, flag=wx.GROW) Laurent@814: Laurent@1249: # Create spin control for defining power rail pin number Laurent@814: self.PinNumber = wx.SpinCtrl(self, min=1, max=50, Laurent@814: style=wx.SP_ARROW_KEYS) Laurent@814: self.Bind(wx.EVT_SPINCTRL, self.OnPinNumberChanged, self.PinNumber) Laurent@1250: self.LeftGridSizer.AddWindow(self.PinNumber, flag=wx.GROW) Laurent@814: Laurent@1249: # 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) Laurent@814: Laurent@1249: # Add buttons sizer to sizers Laurent@1250: self.MainSizer.AddSizer(self.ButtonSizer, border=20, Laurent@814: flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) Laurent@814: Laurent@1249: # Left Power Rail radio button is default control having keyboard focus Laurent@1249: self.TypeRadioButtons[LEFTRAIL].SetFocus() Laurent@1249: Laurent@1249: def GetMinElementSize(self): Laurent@1249: """ Laurent@1249: Get minimal graphic element size Laurent@1249: @return: Tuple containing minimal size (width, height) or None if no Laurent@1249: element defined Laurent@1249: """ Laurent@1249: return (2, LD_LINE_SIZE * self.PinNumber.GetValue()) Laurent@1249: Laurent@1249: def GetPowerRailType(self): Laurent@1249: """ Laurent@1249: Return type selected for power rail Laurent@1249: @return: Type selected (LEFTRAIL or RIGHTRAIL) Laurent@1249: """ Laurent@1249: return (LEFTRAIL Laurent@1249: if self.TypeRadioButtons[LEFTRAIL].GetValue() Laurent@1249: else RIGHTRAIL) Laurent@1249: Laurent@1249: def SetValues(self, values): Laurent@1249: """ Laurent@1249: Set default power rail parameters Laurent@1249: @param values: Power rail parameters values Laurent@1249: """ Laurent@1249: # For each parameters defined, set corresponding control value Laurent@1249: for name, value in values.items(): Laurent@1249: Laurent@1249: # Parameter is power rail type Laurent@1249: if name == "type": Laurent@1249: self.TypeRadioButtons[value].SetValue(True) Laurent@1249: Laurent@1249: # Parameter is power rail pin number Laurent@1249: elif name == "pin_number": Laurent@1249: self.PinNumber.SetValue(value) Laurent@814: Laurent@814: def GetValues(self): Laurent@1249: """ Laurent@1249: Return power rail parameters defined in dialog Laurent@1249: @return: {parameter_name: parameter_value,...} Laurent@1249: """ Laurent@1249: values = { Laurent@1249: "type": self.GetPowerRailType(), Laurent@1249: "pin_number": self.PinNumber.GetValue()} Laurent@1249: values["width"], values["height"] = self.Element.GetSize() Laurent@814: return values Laurent@814: Laurent@814: def OnTypeChanged(self, event): Laurent@1249: """ Laurent@1249: Called when power rail type changed Laurent@1249: @param event: wx.RadioButtonEvent Laurent@1249: """ Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnPinNumberChanged(self, event): Laurent@1249: """ Laurent@1249: Called when power rail pin number value changed Laurent@1249: @param event: wx.SpinEvent Laurent@1249: """ Laurent@814: self.RefreshPreview() Laurent@814: event.Skip() Laurent@814: Laurent@814: def RefreshPreview(self): Laurent@1249: """ Laurent@1249: Refresh preview panel of graphic element Laurent@1249: Override BlockPreviewDialog function Laurent@1249: """ Laurent@1249: Laurent@1249: # Set graphic element displayed, creating a power rail element Laurent@1249: self.Element = LD_PowerRail(self.Preview, Laurent@1249: self.GetPowerRailType(), Laurent@1249: connectors = self.PinNumber.GetValue()) Laurent@1249: Laurent@1249: # Call BlockPreviewDialog function Laurent@1249: BlockPreviewDialog.RefreshPreview(self)