dialogs/LDPowerRailDialog.py
branch1.1 Korean release
changeset 1280 72a826dfcfbb
parent 1259 8350222a81c3
child 1357 b8d577c7eb4b
equal deleted inserted replaced
977:c8e008b8cefe 1280:72a826dfcfbb
    21 #License along with this library; if not, write to the Free Software
    21 #License along with this library; if not, write to the Free Software
    22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 
    23 
    24 import wx
    24 import wx
    25 
    25 
    26 from graphics import *
    26 from graphics.GraphicCommons import LEFTRAIL, RIGHTRAIL, LD_LINE_SIZE
       
    27 from graphics.LD_Objects import LD_PowerRail
       
    28 from BlockPreviewDialog import BlockPreviewDialog
    27 
    29 
    28 #-------------------------------------------------------------------------------
    30 #-------------------------------------------------------------------------------
    29 #                      Edit Ladder Power Rail Properties Dialog
    31 #                    Set Ladder Power Rail Parameters Dialog
    30 #-------------------------------------------------------------------------------
    32 #-------------------------------------------------------------------------------
    31 
    33 
    32 class LDPowerRailDialog(wx.Dialog):
    34 """
       
    35 Class that implements a dialog for defining parameters of a power rail graphic
       
    36 element
       
    37 """
       
    38 
       
    39 class LDPowerRailDialog(BlockPreviewDialog):
    33     
    40     
    34     def __init__(self, parent, controller, type = LEFTRAIL, number = 1):
    41     def __init__(self, parent, controller, tagname):
    35         wx.Dialog.__init__(self, parent, size=wx.Size(350, 260),
    42         """
    36               title=_('Power Rail Properties'))
    43         Constructor
       
    44         @param parent: Parent wx.Window of dialog for modal
       
    45         @param controller: Reference to project controller
       
    46         @param tagname: Tagname of project POU edited
       
    47         """
       
    48         BlockPreviewDialog.__init__(self, parent, controller, tagname,
       
    49               size=wx.Size(350, 260), title=_('Power Rail Properties'))
    37         
    50         
    38         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
    51         # Init common sizers
    39         main_sizer.AddGrowableCol(0)
    52         self._init_sizers(2, 0, 5, None, 2, 1)
    40         main_sizer.AddGrowableRow(0)
       
    41         
    53         
    42         column_sizer = wx.BoxSizer(wx.HORIZONTAL)
    54         # Create label for connection type
    43         main_sizer.AddSizer(column_sizer, border=20, 
    55         type_label = wx.StaticText(self, label=_('Type:'))
    44               flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
    56         self.LeftGridSizer.AddWindow(type_label, flag=wx.GROW)
    45         
    57         
    46         left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=5, vgap=5)
    58         # Create radio buttons for selecting power rail type
    47         left_gridsizer.AddGrowableCol(0)
    59         self.TypeRadioButtons = {}
    48         column_sizer.AddSizer(left_gridsizer, 1, border=5, 
    60         first = True
    49               flag=wx.GROW|wx.RIGHT)
    61         for type, label in [(LEFTRAIL, _('Left PowerRail')),
       
    62                             (RIGHTRAIL, _('Right PowerRail'))]:
       
    63             radio_button = wx.RadioButton(self, label=label, 
       
    64                   style=(wx.RB_GROUP if first else 0))
       
    65             radio_button.SetValue(first)
       
    66             self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button)
       
    67             self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW)
       
    68             self.TypeRadioButtons[type] = radio_button
       
    69             first = False
    50         
    70         
    51         type_label = wx.StaticText(self, label=_('Type:'))
    71         # Create label for power rail pin number
    52         left_gridsizer.AddWindow(type_label, flag=wx.GROW)
    72         pin_number_label = wx.StaticText(self, label=_('Pin number:'))
       
    73         self.LeftGridSizer.AddWindow(pin_number_label, flag=wx.GROW)
    53         
    74         
    54         self.LeftPowerRail = wx.RadioButton(self,
    75         # Create spin control for defining power rail pin number
    55               label=_('Left PowerRail'), style=wx.RB_GROUP)
       
    56         self.LeftPowerRail.SetValue(True)
       
    57         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.LeftPowerRail)
       
    58         left_gridsizer.AddWindow(self.LeftPowerRail, flag=wx.GROW)
       
    59         
       
    60         self.RightPowerRail = wx.RadioButton(self, label=_('Right PowerRail'))
       
    61         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.RightPowerRail)
       
    62         left_gridsizer.AddWindow(self.RightPowerRail, flag=wx.GROW)
       
    63         
       
    64         pin_number_label = wx.StaticText(self, label=_('Pin number:'))
       
    65         left_gridsizer.AddWindow(pin_number_label, flag=wx.GROW)
       
    66         
       
    67         self.PinNumber = wx.SpinCtrl(self, min=1, max=50,
    76         self.PinNumber = wx.SpinCtrl(self, min=1, max=50,
    68               style=wx.SP_ARROW_KEYS)
    77               style=wx.SP_ARROW_KEYS)
    69         self.Bind(wx.EVT_SPINCTRL, self.OnPinNumberChanged, self.PinNumber)
    78         self.Bind(wx.EVT_SPINCTRL, self.OnPinNumberChanged, self.PinNumber)
    70         left_gridsizer.AddWindow(self.PinNumber, flag=wx.GROW)
    79         self.LeftGridSizer.AddWindow(self.PinNumber, flag=wx.GROW)
    71         
    80         
    72         right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
    81         # Add preview panel and associated label to sizers
    73         right_gridsizer.AddGrowableCol(0)
    82         self.RightGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW)
    74         right_gridsizer.AddGrowableRow(1)
    83         self.RightGridSizer.AddWindow(self.Preview, flag=wx.GROW)
    75         column_sizer.AddSizer(right_gridsizer, 1, border=5, 
       
    76               flag=wx.GROW|wx.LEFT)
       
    77         
    84         
    78         preview_label = wx.StaticText(self, label=_('Preview:'))
    85         # Add buttons sizer to sizers
    79         right_gridsizer.AddWindow(preview_label, flag=wx.GROW)
    86         self.MainSizer.AddSizer(self.ButtonSizer, border=20, 
    80         
       
    81         self.Preview = wx.Panel(self,
       
    82               style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
       
    83         self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
       
    84         setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
       
    85         setattr(self.Preview, "GetScaling", lambda:None)
       
    86         setattr(self.Preview, "IsOfType", controller.IsOfType)
       
    87         self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
       
    88         right_gridsizer.AddWindow(self.Preview, flag=wx.GROW)
       
    89         
       
    90         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
    91         main_sizer.AddSizer(button_sizer, border=20, 
       
    92               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
    87               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
    93         
    88         
    94         self.SetSizer(main_sizer)
    89         # Left Power Rail radio button is default control having keyboard focus
    95         
    90         self.TypeRadioButtons[LEFTRAIL].SetFocus()
    96         self.Type = type
    91     
    97         if type == LEFTRAIL:
    92     def GetMinElementSize(self):
    98             self.LeftPowerRail.SetValue(True)
    93         """
    99         elif type == RIGHTRAIL:
    94         Get minimal graphic element size
   100             self.RightPowerRail.SetValue(True)
    95         @return: Tuple containing minimal size (width, height) or None if no
   101         self.PinNumber.SetValue(number)
    96         element defined
   102         
    97         """
   103         self.PowerRailMinSize = (0, 0)
    98         return self.Element.GetMinSize(True)
   104         self.PowerRail = None
    99     
   105         
   100     def GetPowerRailType(self):
   106         self.LeftPowerRail.SetFocus()
   101         """
   107 
   102         Return type selected for power rail
   108     def SetPreviewFont(self, font):
   103         @return: Type selected (LEFTRAIL or RIGHTRAIL)
   109         self.Preview.SetFont(font)
   104         """
   110 
   105         return (LEFTRAIL
   111     def SetMinSize(self, size):
   106                 if self.TypeRadioButtons[LEFTRAIL].GetValue()
   112         self.PowerRailMinSize = size
   107                 else RIGHTRAIL)
   113         self.RefreshPreview()    
   108     
       
   109     def SetValues(self, values):
       
   110         """
       
   111         Set default power rail parameters
       
   112         @param values: Power rail parameters values
       
   113         """
       
   114         # For each parameters defined, set corresponding control value
       
   115         for name, value in values.items():
       
   116             
       
   117             # Parameter is power rail type
       
   118             if name == "type":
       
   119                 self.TypeRadioButtons[value].SetValue(True)
       
   120             
       
   121             # Parameter is power rail pin number
       
   122             elif name == "pin_number":
       
   123                 self.PinNumber.SetValue(value)
   114 
   124 
   115     def GetValues(self):
   125     def GetValues(self):
   116         values = {}
   126         """
   117         values["type"] = self.Type
   127         Return power rail parameters defined in dialog
   118         values["number"] = self.PinNumber.GetValue()
   128         @return: {parameter_name: parameter_value,...}
   119         values["width"], values["height"] = self.PowerRail.GetSize()
   129         """
       
   130         values = {
       
   131             "type": self.GetPowerRailType(),
       
   132             "pin_number": self.PinNumber.GetValue()}
       
   133         values["width"], values["height"] = self.Element.GetSize()
   120         return values
   134         return values
   121 
   135 
   122     def OnTypeChanged(self, event):
   136     def OnTypeChanged(self, event):
   123         if self.LeftPowerRail.GetValue():
   137         """
   124             self.Type = LEFTRAIL
   138         Called when power rail type changed
   125         elif self.RightPowerRail.GetValue():
   139         @param event: wx.RadioButtonEvent
   126             self.Type = RIGHTRAIL
   140         """
   127         self.RefreshPreview()
   141         self.RefreshPreview()
   128         event.Skip()
   142         event.Skip()
   129 
   143 
   130     def OnPinNumberChanged(self, event):
   144     def OnPinNumberChanged(self, event):
       
   145         """
       
   146         Called when power rail pin number value changed
       
   147         @param event: wx.SpinEvent
       
   148         """
   131         self.RefreshPreview()
   149         self.RefreshPreview()
   132         event.Skip()
   150         event.Skip()
   133 
   151 
   134     def RefreshPreview(self):
   152     def RefreshPreview(self):
   135         dc = wx.ClientDC(self.Preview)
   153         """
   136         dc.SetFont(self.Preview.GetFont())
   154         Refresh preview panel of graphic element
   137         dc.Clear()
   155         Override BlockPreviewDialog function
   138         self.PowerRail = LD_PowerRail(self.Preview, self.Type, connectors = self.PinNumber.GetValue())
   156         """
   139         min_width, min_height = 2, LD_LINE_SIZE * self.PinNumber.GetValue()
   157         
   140         width, height = max(min_width, self.PowerRailMinSize[0]), max(min_height, self.PowerRailMinSize[1])
   158         # Set graphic element displayed, creating a power rail element
   141         self.PowerRail.SetSize(width, height)
   159         self.Element = LD_PowerRail(self.Preview, 
   142         clientsize = self.Preview.GetClientSize()
   160                 self.GetPowerRailType(), 
   143         self.PowerRail.SetPosition((clientsize.width - width) / 2, (clientsize.height - height) / 2)
   161                 connectors = self.PinNumber.GetValue())
   144         self.PowerRail.Draw(dc)
   162         
   145 
   163         # Call BlockPreviewDialog function
   146     def OnPaint(self, event):
   164         BlockPreviewDialog.RefreshPreview(self)
   147         self.RefreshPreview()
       
   148         event.Skip()