dialogs/LDPowerRailDialog.py
changeset 409 34c9f624c2fe
child 466 68aacce44fe0
equal deleted inserted replaced
408:0e389fa5b160 409:34c9f624c2fe
       
     1 # -*- coding: utf-8 -*-
       
     2 
       
     3 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     4 #based on the plcopen standard. 
       
     5 #
       
     6 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     7 #
       
     8 #See COPYING file for copyrights details.
       
     9 #
       
    10 #This library is free software; you can redistribute it and/or
       
    11 #modify it under the terms of the GNU General Public
       
    12 #License as published by the Free Software Foundation; either
       
    13 #version 2.1 of the License, or (at your option) any later version.
       
    14 #
       
    15 #This library is distributed in the hope that it will be useful,
       
    16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    18 #General Public License for more details.
       
    19 #
       
    20 #You should have received a copy of the GNU General Public
       
    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
       
    23 
       
    24 import wx
       
    25 
       
    26 from graphics import *
       
    27 
       
    28 
       
    29 #-------------------------------------------------------------------------------
       
    30 #                      Edit Ladder Power Rail Properties Dialog
       
    31 #-------------------------------------------------------------------------------
       
    32 
       
    33 
       
    34 [ID_LDPOWERRAILDIALOG, ID_LDPOWERRAILDIALOGSPACER, 
       
    35  ID_LDPOWERRAILDIALOGTYPE, ID_LDPOWERRAILDIALOGRADIOBUTTON1, 
       
    36  ID_LDPOWERRAILDIALOGRADIOBUTTON2, ID_LDPOWERRAILDIALOGPREVIEW,
       
    37  ID_LDPOWERRAILDIALOGSTATICTEXT1, ID_LDPOWERRAILDIALOGSTATICTEXT2, 
       
    38  ID_LDPOWERRAILDIALOGSTATICTEXT3, ID_LDPOWERRAILDIALOGPINNUMBER,
       
    39 ] = [wx.NewId() for _init_ctrls in range(10)]
       
    40 
       
    41 class LDPowerRailDialog(wx.Dialog):
       
    42     
       
    43     if wx.VERSION < (2, 6, 0):
       
    44         def Bind(self, event, function, id = None):
       
    45             if id is not None:
       
    46                 event(self, id, function)
       
    47             else:
       
    48                 event(self, function)
       
    49     
       
    50     def _init_coll_flexGridSizer1_Items(self, parent):
       
    51         parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
    52         parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
       
    53         
       
    54     def _init_coll_flexGridSizer1_Growables(self, parent):
       
    55         parent.AddGrowableCol(0)
       
    56         parent.AddGrowableRow(0)
       
    57     
       
    58     def _init_coll_MainSizer_Items(self, parent):
       
    59         parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
       
    60         parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)
       
    61     
       
    62     def _init_coll_LeftGridSizer_Items(self, parent):
       
    63         parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
       
    64         parent.AddWindow(self.radioButton1, 0, border=0, flag=wx.GROW)
       
    65         parent.AddWindow(self.radioButton2, 0, border=0, flag=wx.GROW)
       
    66         parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
       
    67         parent.AddWindow(self.PinNumber, 0, border=0, flag=wx.GROW)
       
    68         parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW)
       
    69     
       
    70     def _init_coll_LeftGridSizer_Growables(self, parent):
       
    71         parent.AddGrowableCol(0)
       
    72         parent.AddGrowableRow(5)
       
    73             
       
    74     def _init_coll_RightGridSizer_Items(self, parent):
       
    75         parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
       
    76         parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)
       
    77         
       
    78     def _init_coll_RightGridSizer_Growables(self, parent):
       
    79         parent.AddGrowableCol(0)
       
    80         parent.AddGrowableRow(1)
       
    81 
       
    82     def _init_sizers(self):
       
    83         self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
       
    84         self.MainSizer = wx.BoxSizer(wx.HORIZONTAL)
       
    85         self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=6, vgap=5)
       
    86         self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
       
    87 
       
    88         self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
       
    89         self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
       
    90         self._init_coll_MainSizer_Items(self.MainSizer)
       
    91         self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
       
    92         self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
       
    93         self._init_coll_RightGridSizer_Items(self.RightGridSizer)
       
    94         self._init_coll_RightGridSizer_Growables(self.RightGridSizer)
       
    95 
       
    96         self.SetSizer(self.flexGridSizer1)
       
    97 
       
    98     def _init_ctrls(self, prnt, ctrler):
       
    99         wx.Dialog.__init__(self, id=ID_LDPOWERRAILDIALOG,
       
   100               name='PowerRailDialog', parent=prnt, pos=wx.Point(376, 223),
       
   101               size=wx.Size(350, 260), style=wx.DEFAULT_DIALOG_STYLE,
       
   102               title=_('Power Rail Properties'))
       
   103         self.SetClientSize(wx.Size(350, 260))
       
   104 
       
   105         self.staticText1 = wx.StaticText(id=ID_LDPOWERRAILDIALOGSTATICTEXT1,
       
   106               label=_('Type:'), name='staticText1', parent=self,
       
   107               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   108 
       
   109         self.staticText2 = wx.StaticText(id=ID_LDPOWERRAILDIALOGSTATICTEXT2,
       
   110               label=_('Pin number:'), name='staticText2', parent=self,
       
   111               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   112 
       
   113         self.staticText3 = wx.StaticText(id=ID_LDPOWERRAILDIALOGSTATICTEXT3,
       
   114               label=_('Preview:'), name='staticText3', parent=self,
       
   115               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   116 
       
   117         self.radioButton1 = wx.RadioButton(id=ID_LDPOWERRAILDIALOGRADIOBUTTON1,
       
   118               label=_('Left PowerRail'), name='radioButton1', parent=self,
       
   119               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=wx.RB_GROUP)
       
   120         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_LDPOWERRAILDIALOGRADIOBUTTON1)
       
   121         self.radioButton1.SetValue(True)
       
   122 
       
   123         self.radioButton2 = wx.RadioButton(id=ID_LDPOWERRAILDIALOGRADIOBUTTON2,
       
   124               label=_('Right PowerRail'), name='radioButton2', parent=self, 
       
   125               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
       
   126         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_LDPOWERRAILDIALOGRADIOBUTTON2)
       
   127 
       
   128         self.PinNumber = wx.SpinCtrl(id=ID_LDPOWERRAILDIALOGPINNUMBER,
       
   129               name='PinNumber', parent=self, pos=wx.Point(0, 0),
       
   130               size=wx.Size(0, 24), style=wx.SP_ARROW_KEYS, min=1, max=50)
       
   131         self.Bind(wx.EVT_SPINCTRL, self.OnPinNumberChanged, id=ID_LDPOWERRAILDIALOGPINNUMBER)
       
   132 
       
   133         self.Preview = wx.Panel(id=ID_LDPOWERRAILDIALOGPREVIEW,
       
   134               name='Preview', parent=self, pos=wx.Point(0, 0),
       
   135               size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
       
   136         self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
       
   137         setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
       
   138         setattr(self.Preview, "GetScaling", lambda:None)
       
   139         setattr(self.Preview, "IsOfType", ctrler.IsOfType)
       
   140 
       
   141         self.Spacer = wx.Panel(id=ID_LDELEMENTDIALOGSPACER,
       
   142               name='Spacer', parent=self, pos=wx.Point(0, 0),
       
   143               size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)
       
   144 
       
   145         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
   146         
       
   147         if wx.VERSION >= (2, 5, 0):
       
   148             self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
       
   149         else:
       
   150             wx.EVT_PAINT(self.Preview, self.OnPaint)
       
   151         
       
   152         self._init_sizers()
       
   153 
       
   154     def __init__(self, parent, controler, type = LEFTRAIL, number = 1):
       
   155         self._init_ctrls(parent, controler)
       
   156         self.Type = type
       
   157         if type == LEFTRAIL:
       
   158             self.radioButton1.SetValue(True)
       
   159         elif type == RIGHTRAIL:
       
   160             self.radioButton2.SetValue(True)
       
   161         self.PinNumber.SetValue(number)
       
   162         
       
   163         self.PowerRailMinSize = (0, 0)
       
   164         self.PowerRail = None
       
   165 
       
   166     def SetPreviewFont(self, font):
       
   167         self.Preview.SetFont(font)
       
   168 
       
   169     def SetMinSize(self, size):
       
   170         self.PowerRailMinSize = size
       
   171         self.RefreshPreview()    
       
   172 
       
   173     def GetValues(self):
       
   174         values = {}
       
   175         values["type"] = self.Type
       
   176         values["number"] = self.PinNumber.GetValue()
       
   177         values["width"], values["height"] = self.PowerRail.GetSize()
       
   178         return values
       
   179 
       
   180     def OnTypeChanged(self, event):
       
   181         if self.radioButton1.GetValue():
       
   182             self.Type = LEFTRAIL
       
   183         elif self.radioButton2.GetValue():
       
   184             self.Type = RIGHTRAIL
       
   185         self.RefreshPreview()
       
   186         event.Skip()
       
   187 
       
   188     def OnPinNumberChanged(self, event):
       
   189         self.RefreshPreview()
       
   190         event.Skip()
       
   191 
       
   192     def RefreshPreview(self):
       
   193         dc = wx.ClientDC(self.Preview)
       
   194         dc.SetFont(self.Preview.GetFont())
       
   195         dc.Clear()
       
   196         self.PowerRail = LD_PowerRail(self.Preview, self.Type, connectors = [True for i in xrange(self.PinNumber.GetValue())])
       
   197         min_width, min_height = 2, LD_LINE_SIZE * self.PinNumber.GetValue()
       
   198         width, height = max(min_width, self.PowerRailMinSize[0]), max(min_height, self.PowerRailMinSize[1])
       
   199         self.PowerRail.SetSize(width, height)
       
   200         clientsize = self.Preview.GetClientSize()
       
   201         self.PowerRail.SetPosition((clientsize.width - width) / 2, (clientsize.height - height) / 2)
       
   202         self.PowerRail.Draw(dc)
       
   203 
       
   204     def OnPaint(self, event):
       
   205         self.RefreshPreview()
       
   206         event.Skip()