laurent@409: # -*- coding: utf-8 -*- laurent@409: laurent@409: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor laurent@409: #based on the plcopen standard. laurent@409: # laurent@409: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD laurent@409: # laurent@409: #See COPYING file for copyrights details. laurent@409: # laurent@409: #This library is free software; you can redistribute it and/or laurent@409: #modify it under the terms of the GNU General Public laurent@409: #License as published by the Free Software Foundation; either laurent@409: #version 2.1 of the License, or (at your option) any later version. laurent@409: # laurent@409: #This library is distributed in the hope that it will be useful, laurent@409: #but WITHOUT ANY WARRANTY; without even the implied warranty of laurent@409: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU laurent@409: #General Public License for more details. laurent@409: # laurent@409: #You should have received a copy of the GNU General Public laurent@409: #License along with this library; if not, write to the Free Software laurent@409: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA laurent@409: laurent@409: import wx laurent@409: laurent@409: from graphics import * laurent@409: laurent@409: laurent@409: #------------------------------------------------------------------------------- laurent@409: # Edit Ladder Power Rail Properties Dialog laurent@409: #------------------------------------------------------------------------------- laurent@409: laurent@409: laurent@409: [ID_LDPOWERRAILDIALOG, ID_LDPOWERRAILDIALOGSPACER, laurent@409: ID_LDPOWERRAILDIALOGTYPE, ID_LDPOWERRAILDIALOGRADIOBUTTON1, laurent@409: ID_LDPOWERRAILDIALOGRADIOBUTTON2, ID_LDPOWERRAILDIALOGPREVIEW, laurent@409: ID_LDPOWERRAILDIALOGSTATICTEXT1, ID_LDPOWERRAILDIALOGSTATICTEXT2, laurent@409: ID_LDPOWERRAILDIALOGSTATICTEXT3, ID_LDPOWERRAILDIALOGPINNUMBER, laurent@409: ] = [wx.NewId() for _init_ctrls in range(10)] laurent@409: laurent@409: class LDPowerRailDialog(wx.Dialog): laurent@409: laurent@409: if wx.VERSION < (2, 6, 0): laurent@409: def Bind(self, event, function, id = None): laurent@409: if id is not None: laurent@409: event(self, id, function) laurent@409: else: laurent@409: event(self, function) laurent@409: laurent@409: def _init_coll_flexGridSizer1_Items(self, parent): laurent@409: parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) laurent@409: parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) laurent@409: laurent@409: def _init_coll_flexGridSizer1_Growables(self, parent): laurent@409: parent.AddGrowableCol(0) laurent@409: parent.AddGrowableRow(0) laurent@409: laurent@409: def _init_coll_MainSizer_Items(self, parent): laurent@409: parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT) laurent@409: parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT) laurent@409: laurent@409: def _init_coll_LeftGridSizer_Items(self, parent): laurent@409: parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.radioButton1, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.radioButton2, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.PinNumber, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW) laurent@409: laurent@409: def _init_coll_LeftGridSizer_Growables(self, parent): laurent@409: parent.AddGrowableCol(0) laurent@409: parent.AddGrowableRow(5) laurent@409: laurent@409: def _init_coll_RightGridSizer_Items(self, parent): laurent@409: parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW) laurent@409: parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW) laurent@409: laurent@409: def _init_coll_RightGridSizer_Growables(self, parent): laurent@409: parent.AddGrowableCol(0) laurent@409: parent.AddGrowableRow(1) laurent@409: laurent@409: def _init_sizers(self): laurent@409: self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) laurent@409: self.MainSizer = wx.BoxSizer(wx.HORIZONTAL) laurent@409: self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=6, vgap=5) laurent@409: self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) laurent@409: laurent@409: self._init_coll_flexGridSizer1_Items(self.flexGridSizer1) laurent@409: self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1) laurent@409: self._init_coll_MainSizer_Items(self.MainSizer) laurent@409: self._init_coll_LeftGridSizer_Items(self.LeftGridSizer) laurent@409: self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer) laurent@409: self._init_coll_RightGridSizer_Items(self.RightGridSizer) laurent@409: self._init_coll_RightGridSizer_Growables(self.RightGridSizer) laurent@409: laurent@409: self.SetSizer(self.flexGridSizer1) laurent@409: laurent@409: def _init_ctrls(self, prnt, ctrler): laurent@409: wx.Dialog.__init__(self, id=ID_LDPOWERRAILDIALOG, laurent@409: name='PowerRailDialog', parent=prnt, pos=wx.Point(376, 223), laurent@409: size=wx.Size(350, 260), style=wx.DEFAULT_DIALOG_STYLE, laurent@409: title=_('Power Rail Properties')) laurent@409: self.SetClientSize(wx.Size(350, 260)) laurent@409: laurent@409: self.staticText1 = wx.StaticText(id=ID_LDPOWERRAILDIALOGSTATICTEXT1, laurent@409: label=_('Type:'), name='staticText1', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) laurent@409: laurent@409: self.staticText2 = wx.StaticText(id=ID_LDPOWERRAILDIALOGSTATICTEXT2, laurent@409: label=_('Pin number:'), name='staticText2', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) laurent@409: laurent@409: self.staticText3 = wx.StaticText(id=ID_LDPOWERRAILDIALOGSTATICTEXT3, laurent@409: label=_('Preview:'), name='staticText3', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) laurent@409: laurent@409: self.radioButton1 = wx.RadioButton(id=ID_LDPOWERRAILDIALOGRADIOBUTTON1, laurent@409: label=_('Left PowerRail'), name='radioButton1', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.Size(0, 24), style=wx.RB_GROUP) laurent@409: self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_LDPOWERRAILDIALOGRADIOBUTTON1) laurent@409: self.radioButton1.SetValue(True) laurent@409: laurent@409: self.radioButton2 = wx.RadioButton(id=ID_LDPOWERRAILDIALOGRADIOBUTTON2, laurent@409: label=_('Right PowerRail'), name='radioButton2', parent=self, laurent@409: pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0) laurent@409: self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_LDPOWERRAILDIALOGRADIOBUTTON2) laurent@409: laurent@409: self.PinNumber = wx.SpinCtrl(id=ID_LDPOWERRAILDIALOGPINNUMBER, laurent@409: name='PinNumber', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(0, 24), style=wx.SP_ARROW_KEYS, min=1, max=50) laurent@409: self.Bind(wx.EVT_SPINCTRL, self.OnPinNumberChanged, id=ID_LDPOWERRAILDIALOGPINNUMBER) laurent@409: laurent@409: self.Preview = wx.Panel(id=ID_LDPOWERRAILDIALOGPREVIEW, laurent@409: name='Preview', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER) laurent@409: self.Preview.SetBackgroundColour(wx.Colour(255,255,255)) laurent@409: setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE) laurent@409: setattr(self.Preview, "GetScaling", lambda:None) laurent@409: setattr(self.Preview, "IsOfType", ctrler.IsOfType) laurent@409: laurent@466: self.Spacer = wx.Panel(id=ID_LDPOWERRAILDIALOGSPACER, laurent@409: name='Spacer', parent=self, pos=wx.Point(0, 0), laurent@409: size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL) laurent@409: laurent@409: self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) laurent@409: laurent@409: if wx.VERSION >= (2, 5, 0): laurent@409: self.Preview.Bind(wx.EVT_PAINT, self.OnPaint) laurent@409: else: laurent@409: wx.EVT_PAINT(self.Preview, self.OnPaint) laurent@409: laurent@409: self._init_sizers() laurent@409: laurent@409: def __init__(self, parent, controler, type = LEFTRAIL, number = 1): laurent@409: self._init_ctrls(parent, controler) laurent@409: self.Type = type laurent@409: if type == LEFTRAIL: laurent@409: self.radioButton1.SetValue(True) laurent@409: elif type == RIGHTRAIL: laurent@409: self.radioButton2.SetValue(True) laurent@409: self.PinNumber.SetValue(number) laurent@409: laurent@409: self.PowerRailMinSize = (0, 0) laurent@409: self.PowerRail = None laurent@409: laurent@409: def SetPreviewFont(self, font): laurent@409: self.Preview.SetFont(font) laurent@409: laurent@409: def SetMinSize(self, size): laurent@409: self.PowerRailMinSize = size laurent@409: self.RefreshPreview() laurent@409: laurent@409: def GetValues(self): laurent@409: values = {} laurent@409: values["type"] = self.Type laurent@409: values["number"] = self.PinNumber.GetValue() laurent@409: values["width"], values["height"] = self.PowerRail.GetSize() laurent@409: return values laurent@409: laurent@409: def OnTypeChanged(self, event): laurent@409: if self.radioButton1.GetValue(): laurent@409: self.Type = LEFTRAIL laurent@409: elif self.radioButton2.GetValue(): laurent@409: self.Type = RIGHTRAIL laurent@409: self.RefreshPreview() laurent@409: event.Skip() laurent@409: laurent@409: def OnPinNumberChanged(self, event): laurent@409: self.RefreshPreview() laurent@409: event.Skip() laurent@409: laurent@409: def RefreshPreview(self): laurent@409: dc = wx.ClientDC(self.Preview) laurent@409: dc.SetFont(self.Preview.GetFont()) laurent@409: dc.Clear() laurent@409: self.PowerRail = LD_PowerRail(self.Preview, self.Type, connectors = [True for i in xrange(self.PinNumber.GetValue())]) laurent@409: min_width, min_height = 2, LD_LINE_SIZE * self.PinNumber.GetValue() laurent@409: width, height = max(min_width, self.PowerRailMinSize[0]), max(min_height, self.PowerRailMinSize[1]) laurent@409: self.PowerRail.SetSize(width, height) laurent@409: clientsize = self.Preview.GetClientSize() laurent@409: self.PowerRail.SetPosition((clientsize.width - width) / 2, (clientsize.height - height) / 2) laurent@409: self.PowerRail.Draw(dc) laurent@409: laurent@409: def OnPaint(self, event): laurent@409: self.RefreshPreview() laurent@409: event.Skip()