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 <andrej.skvortzov@gmail.com>
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: 
andrej@1853: 
andrej@1853: from __future__ import absolute_import
Laurent@814: import wx
Laurent@814: 
andrej@1850: from graphics.GraphicCommons import LEFTRAIL, RIGHTRAIL
Laurent@1249: from graphics.LD_Objects import LD_PowerRail
andrej@1853: from dialogs.BlockPreviewDialog import BlockPreviewDialog
Laurent@814: 
andrej@1782: # -------------------------------------------------------------------------------
Laurent@1249: #                    Set Ladder Power Rail Parameters Dialog
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@1249: 
Laurent@1249: class LDPowerRailDialog(BlockPreviewDialog):
andrej@1736:     """
andrej@1736:     Class that implements a dialog for defining parameters of a power rail graphic
andrej@1736:     element
andrej@1736:     """
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,
andrej@1768:                                     title=_('Power Rail Properties'))
andrej@1730: 
Laurent@1250:         # Init common sizers
Laurent@1250:         self._init_sizers(2, 0, 5, None, 2, 1)
andrej@1730: 
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)
andrej@1730: 
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'))]:
andrej@1730:             radio_button = wx.RadioButton(self, label=label,
andrej@1768:                                           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
andrej@1730: 
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)
andrej@1730: 
Laurent@1249:         # Create spin control for defining power rail pin number
Laurent@814:         self.PinNumber = wx.SpinCtrl(self, min=1, max=50,
andrej@1768:                                      style=wx.SP_ARROW_KEYS)
Laurent@1357:         self.PinNumber.SetValue(1)
Laurent@814:         self.Bind(wx.EVT_SPINCTRL, self.OnPinNumberChanged, self.PinNumber)
Laurent@1250:         self.LeftGridSizer.AddWindow(self.PinNumber, flag=wx.GROW)
andrej@1730: 
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)
andrej@1730: 
Laurent@1249:         # 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.Fit()
andrej@1730: 
Laurent@1249:         # Left Power Rail radio button is default control having keyboard focus
Laurent@1249:         self.TypeRadioButtons[LEFTRAIL].SetFocus()
andrej@1730: 
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@1259:         return self.Element.GetMinSize(True)
andrej@1730: 
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)
andrej@1730: 
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():
andrej@1730: 
Laurent@1249:             # Parameter is power rail type
Laurent@1249:             if name == "type":
Laurent@1249:                 self.TypeRadioButtons[value].SetValue(True)
andrej@1730: 
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:         """
Edouard@2591:         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:         """
Edouard@2591:         self.RefreshPreview()
Laurent@814:         event.Skip()
Laurent@814: 
Edouard@2572:     def DrawPreview(self):
Laurent@1249:         """
Laurent@1249:         Refresh preview panel of graphic element
Laurent@1249:         Override BlockPreviewDialog function
Laurent@1249:         """
andrej@1730: 
Laurent@1249:         # Set graphic element displayed, creating a power rail element
andrej@1730:         self.Element = LD_PowerRail(self.Preview,
andrej@1768:                                     self.GetPowerRailType(),
andrej@1768:                                     connectors=self.PinNumber.GetValue())
andrej@1730: 
Edouard@2572:         return BlockPreviewDialog.DrawPreview(self)