dialogs/LDPowerRailDialog.py
branch1.1 Korean release
changeset 1280 72a826dfcfbb
parent 1259 8350222a81c3
child 1357 b8d577c7eb4b
--- a/dialogs/LDPowerRailDialog.py	Wed Mar 13 12:34:55 2013 +0900
+++ b/dialogs/LDPowerRailDialog.py	Wed Jul 31 10:45:07 2013 +0900
@@ -23,126 +23,142 @@
 
 import wx
 
-from graphics import *
+from graphics.GraphicCommons import LEFTRAIL, RIGHTRAIL, LD_LINE_SIZE
+from graphics.LD_Objects import LD_PowerRail
+from BlockPreviewDialog import BlockPreviewDialog
 
 #-------------------------------------------------------------------------------
-#                      Edit Ladder Power Rail Properties Dialog
+#                    Set Ladder Power Rail Parameters Dialog
 #-------------------------------------------------------------------------------
 
-class LDPowerRailDialog(wx.Dialog):
+"""
+Class that implements a dialog for defining parameters of a power rail graphic
+element
+"""
+
+class LDPowerRailDialog(BlockPreviewDialog):
     
-    def __init__(self, parent, controller, type = LEFTRAIL, number = 1):
-        wx.Dialog.__init__(self, parent, size=wx.Size(350, 260),
-              title=_('Power Rail Properties'))
+    def __init__(self, parent, controller, tagname):
+        """
+        Constructor
+        @param parent: Parent wx.Window of dialog for modal
+        @param controller: Reference to project controller
+        @param tagname: Tagname of project POU edited
+        """
+        BlockPreviewDialog.__init__(self, parent, controller, tagname,
+              size=wx.Size(350, 260), title=_('Power Rail Properties'))
         
-        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
-        main_sizer.AddGrowableCol(0)
-        main_sizer.AddGrowableRow(0)
+        # Init common sizers
+        self._init_sizers(2, 0, 5, None, 2, 1)
         
-        column_sizer = wx.BoxSizer(wx.HORIZONTAL)
-        main_sizer.AddSizer(column_sizer, border=20, 
-              flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
+        # Create label for connection type
+        type_label = wx.StaticText(self, label=_('Type:'))
+        self.LeftGridSizer.AddWindow(type_label, flag=wx.GROW)
         
-        left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=5, vgap=5)
-        left_gridsizer.AddGrowableCol(0)
-        column_sizer.AddSizer(left_gridsizer, 1, border=5, 
-              flag=wx.GROW|wx.RIGHT)
+        # Create radio buttons for selecting power rail type
+        self.TypeRadioButtons = {}
+        first = True
+        for type, label in [(LEFTRAIL, _('Left PowerRail')),
+                            (RIGHTRAIL, _('Right PowerRail'))]:
+            radio_button = wx.RadioButton(self, label=label, 
+                  style=(wx.RB_GROUP if first else 0))
+            radio_button.SetValue(first)
+            self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button)
+            self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW)
+            self.TypeRadioButtons[type] = radio_button
+            first = False
         
-        type_label = wx.StaticText(self, label=_('Type:'))
-        left_gridsizer.AddWindow(type_label, flag=wx.GROW)
+        # Create label for power rail pin number
+        pin_number_label = wx.StaticText(self, label=_('Pin number:'))
+        self.LeftGridSizer.AddWindow(pin_number_label, flag=wx.GROW)
         
-        self.LeftPowerRail = wx.RadioButton(self,
-              label=_('Left PowerRail'), style=wx.RB_GROUP)
-        self.LeftPowerRail.SetValue(True)
-        self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.LeftPowerRail)
-        left_gridsizer.AddWindow(self.LeftPowerRail, flag=wx.GROW)
-        
-        self.RightPowerRail = wx.RadioButton(self, label=_('Right PowerRail'))
-        self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.RightPowerRail)
-        left_gridsizer.AddWindow(self.RightPowerRail, flag=wx.GROW)
-        
-        pin_number_label = wx.StaticText(self, label=_('Pin number:'))
-        left_gridsizer.AddWindow(pin_number_label, flag=wx.GROW)
-        
+        # Create spin control for defining power rail pin number
         self.PinNumber = wx.SpinCtrl(self, min=1, max=50,
               style=wx.SP_ARROW_KEYS)
         self.Bind(wx.EVT_SPINCTRL, self.OnPinNumberChanged, self.PinNumber)
-        left_gridsizer.AddWindow(self.PinNumber, flag=wx.GROW)
+        self.LeftGridSizer.AddWindow(self.PinNumber, flag=wx.GROW)
         
-        right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
-        right_gridsizer.AddGrowableCol(0)
-        right_gridsizer.AddGrowableRow(1)
-        column_sizer.AddSizer(right_gridsizer, 1, border=5, 
-              flag=wx.GROW|wx.LEFT)
+        # Add preview panel and associated label to sizers
+        self.RightGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW)
+        self.RightGridSizer.AddWindow(self.Preview, flag=wx.GROW)
         
-        preview_label = wx.StaticText(self, label=_('Preview:'))
-        right_gridsizer.AddWindow(preview_label, flag=wx.GROW)
-        
-        self.Preview = wx.Panel(self,
-              style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
-        self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
-        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
-        setattr(self.Preview, "GetScaling", lambda:None)
-        setattr(self.Preview, "IsOfType", controller.IsOfType)
-        self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
-        right_gridsizer.AddWindow(self.Preview, flag=wx.GROW)
-        
-        button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
-        main_sizer.AddSizer(button_sizer, border=20, 
+        # Add buttons sizer to sizers
+        self.MainSizer.AddSizer(self.ButtonSizer, border=20, 
               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
         
-        self.SetSizer(main_sizer)
-        
-        self.Type = type
-        if type == LEFTRAIL:
-            self.LeftPowerRail.SetValue(True)
-        elif type == RIGHTRAIL:
-            self.RightPowerRail.SetValue(True)
-        self.PinNumber.SetValue(number)
-        
-        self.PowerRailMinSize = (0, 0)
-        self.PowerRail = None
-        
-        self.LeftPowerRail.SetFocus()
-
-    def SetPreviewFont(self, font):
-        self.Preview.SetFont(font)
-
-    def SetMinSize(self, size):
-        self.PowerRailMinSize = size
-        self.RefreshPreview()    
+        # Left Power Rail radio button is default control having keyboard focus
+        self.TypeRadioButtons[LEFTRAIL].SetFocus()
+    
+    def GetMinElementSize(self):
+        """
+        Get minimal graphic element size
+        @return: Tuple containing minimal size (width, height) or None if no
+        element defined
+        """
+        return self.Element.GetMinSize(True)
+    
+    def GetPowerRailType(self):
+        """
+        Return type selected for power rail
+        @return: Type selected (LEFTRAIL or RIGHTRAIL)
+        """
+        return (LEFTRAIL
+                if self.TypeRadioButtons[LEFTRAIL].GetValue()
+                else RIGHTRAIL)
+    
+    def SetValues(self, values):
+        """
+        Set default power rail parameters
+        @param values: Power rail parameters values
+        """
+        # For each parameters defined, set corresponding control value
+        for name, value in values.items():
+            
+            # Parameter is power rail type
+            if name == "type":
+                self.TypeRadioButtons[value].SetValue(True)
+            
+            # Parameter is power rail pin number
+            elif name == "pin_number":
+                self.PinNumber.SetValue(value)
 
     def GetValues(self):
-        values = {}
-        values["type"] = self.Type
-        values["number"] = self.PinNumber.GetValue()
-        values["width"], values["height"] = self.PowerRail.GetSize()
+        """
+        Return power rail parameters defined in dialog
+        @return: {parameter_name: parameter_value,...}
+        """
+        values = {
+            "type": self.GetPowerRailType(),
+            "pin_number": self.PinNumber.GetValue()}
+        values["width"], values["height"] = self.Element.GetSize()
         return values
 
     def OnTypeChanged(self, event):
-        if self.LeftPowerRail.GetValue():
-            self.Type = LEFTRAIL
-        elif self.RightPowerRail.GetValue():
-            self.Type = RIGHTRAIL
+        """
+        Called when power rail type changed
+        @param event: wx.RadioButtonEvent
+        """
         self.RefreshPreview()
         event.Skip()
 
     def OnPinNumberChanged(self, event):
+        """
+        Called when power rail pin number value changed
+        @param event: wx.SpinEvent
+        """
         self.RefreshPreview()
         event.Skip()
 
     def RefreshPreview(self):
-        dc = wx.ClientDC(self.Preview)
-        dc.SetFont(self.Preview.GetFont())
-        dc.Clear()
-        self.PowerRail = LD_PowerRail(self.Preview, self.Type, connectors = self.PinNumber.GetValue())
-        min_width, min_height = 2, LD_LINE_SIZE * self.PinNumber.GetValue()
-        width, height = max(min_width, self.PowerRailMinSize[0]), max(min_height, self.PowerRailMinSize[1])
-        self.PowerRail.SetSize(width, height)
-        clientsize = self.Preview.GetClientSize()
-        self.PowerRail.SetPosition((clientsize.width - width) / 2, (clientsize.height - height) / 2)
-        self.PowerRail.Draw(dc)
-
-    def OnPaint(self, event):
-        self.RefreshPreview()
-        event.Skip()
+        """
+        Refresh preview panel of graphic element
+        Override BlockPreviewDialog function
+        """
+        
+        # Set graphic element displayed, creating a power rail element
+        self.Element = LD_PowerRail(self.Preview, 
+                self.GetPowerRailType(), 
+                connectors = self.PinNumber.GetValue())
+        
+        # Call BlockPreviewDialog function
+        BlockPreviewDialog.RefreshPreview(self)