dialogs/LDElementDialog.py
changeset 409 34c9f624c2fe
child 534 d506a353b3d3
equal deleted inserted replaced
408:0e389fa5b160 409:34c9f624c2fe
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     5 #based on the plcopen standard. 
       
     6 #
       
     7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 import wx
       
    26 
       
    27 from graphics import *
       
    28 
       
    29 #-------------------------------------------------------------------------------
       
    30 #                        Edit Ladder Element Properties Dialog
       
    31 #-------------------------------------------------------------------------------
       
    32 
       
    33 
       
    34 [ID_LDELEMENTDIALOG, ID_LDELEMENTDIALOGSPACER, 
       
    35  ID_LDELEMENTDIALOGNAME, ID_LDELEMENTDIALOGRADIOBUTTON1, 
       
    36  ID_LDELEMENTDIALOGRADIOBUTTON2, ID_LDELEMENTDIALOGRADIOBUTTON3,
       
    37  ID_LDELEMENTDIALOGRADIOBUTTON4, ID_LDELEMENTDIALOGRADIOBUTTON5,
       
    38  ID_LDELEMENTDIALOGRADIOBUTTON6, ID_LDELEMENTDIALOGPREVIEW,
       
    39  ID_LDELEMENTDIALOGSTATICTEXT1, ID_LDELEMENTDIALOGSTATICTEXT2, 
       
    40  ID_LDELEMENTDIALOGSTATICTEXT3, 
       
    41 ] = [wx.NewId() for _init_ctrls in range(13)]
       
    42 
       
    43 class LDElementDialog(wx.Dialog):
       
    44     
       
    45     if wx.VERSION < (2, 6, 0):
       
    46         def Bind(self, event, function, id = None):
       
    47             if id is not None:
       
    48                 event(self, id, function)
       
    49             else:
       
    50                 event(self, function)
       
    51     
       
    52     def _init_coll_flexGridSizer1_Items(self, parent):
       
    53         parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
    54         parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
       
    55         
       
    56     def _init_coll_flexGridSizer1_Growables(self, parent):
       
    57         parent.AddGrowableCol(0)
       
    58         parent.AddGrowableRow(0)
       
    59     
       
    60     def _init_coll_MainSizer_Items(self, parent):
       
    61         parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
       
    62         parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)
       
    63     
       
    64     def _init_coll_LeftGridSizer_Items(self, parent):
       
    65         parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
       
    66         parent.AddSizer(self.RadioButtonSizer, 0, border=0, flag=wx.GROW)
       
    67         parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
       
    68         parent.AddWindow(self.ElementName, 0, border=0, flag=wx.GROW)
       
    69         parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW)
       
    70         
       
    71     def _init_coll_LeftGridSizer_Growables(self, parent):
       
    72         parent.AddGrowableCol(0)
       
    73         parent.AddGrowableRow(7)
       
    74     
       
    75     def _init_coll_RadioButtonSizer_Items(self, parent):
       
    76         parent.AddWindow(self.radioButton1, 0, border=0, flag=wx.GROW)
       
    77         parent.AddWindow(self.radioButton2, 0, border=0, flag=wx.GROW)
       
    78         parent.AddWindow(self.radioButton3, 0, border=0, flag=wx.GROW)
       
    79         parent.AddWindow(self.radioButton4, 0, border=0, flag=wx.GROW)
       
    80         parent.AddWindow(self.radioButton5, 0, border=0, flag=wx.GROW)
       
    81         parent.AddWindow(self.radioButton6, 0, border=0, flag=wx.GROW)
       
    82         
       
    83     def _init_coll_RightGridSizer_Items(self, parent):
       
    84         parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
       
    85         parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)
       
    86         
       
    87     def _init_coll_RightGridSizer_Growables(self, parent):
       
    88         parent.AddGrowableCol(0)
       
    89         parent.AddGrowableRow(1)
       
    90 
       
    91     def _init_sizers(self):
       
    92         self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
       
    93         self.MainSizer = wx.BoxSizer(wx.HORIZONTAL)
       
    94         self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=5, vgap=5)
       
    95         self.RadioButtonSizer = wx.BoxSizer(wx.VERTICAL)
       
    96         self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
       
    97 
       
    98         self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
       
    99         self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
       
   100         self._init_coll_MainSizer_Items(self.MainSizer)
       
   101         self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
       
   102         self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
       
   103         self._init_coll_RadioButtonSizer_Items(self.RadioButtonSizer)
       
   104         self._init_coll_RightGridSizer_Items(self.RightGridSizer)
       
   105         self._init_coll_RightGridSizer_Growables(self.RightGridSizer)
       
   106 
       
   107         self.SetSizer(self.flexGridSizer1)
       
   108 
       
   109     def _init_ctrls(self, prnt, ctrler, title, extra_size = 0):
       
   110         wx.Dialog.__init__(self, id=ID_LDELEMENTDIALOG,
       
   111               name='LDElementDialog', parent=prnt, pos=wx.Point(376, 223),
       
   112               size=wx.Size(350, 260 + extra_size), style=wx.DEFAULT_DIALOG_STYLE,
       
   113               title=title)
       
   114         self.SetClientSize(wx.Size(350, 260 + extra_size))
       
   115 
       
   116         self.staticText1 = wx.StaticText(id=ID_LDELEMENTDIALOGSTATICTEXT1,
       
   117               label=_('Modifier:'), name='staticText1', parent=self,
       
   118               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   119 
       
   120         self.staticText2 = wx.StaticText(id=ID_LDELEMENTDIALOGSTATICTEXT2,
       
   121               label=_('Name:'), name='staticText2', parent=self,
       
   122               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   123 
       
   124         self.staticText3 = wx.StaticText(id=ID_LDELEMENTDIALOGSTATICTEXT3,
       
   125               label=_('Preview:'), name='staticText3', parent=self,
       
   126               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   127 
       
   128         self.radioButton1 = wx.RadioButton(id=ID_LDELEMENTDIALOGRADIOBUTTON1,
       
   129               label=_("Normal"), name='radioButton1', parent=self,
       
   130               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=wx.RB_GROUP)
       
   131         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_LDELEMENTDIALOGRADIOBUTTON1)
       
   132         self.radioButton1.SetValue(True)
       
   133 
       
   134         self.radioButton2 = wx.RadioButton(id=ID_LDELEMENTDIALOGRADIOBUTTON2,
       
   135               label=_("Negated"), name='radioButton2', parent=self, 
       
   136               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
       
   137         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_LDELEMENTDIALOGRADIOBUTTON2)
       
   138 
       
   139         self.radioButton3 = wx.RadioButton(id=ID_LDELEMENTDIALOGRADIOBUTTON3,
       
   140               label=_("Set"), name='radioButton3', parent=self,
       
   141               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
       
   142         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_LDELEMENTDIALOGRADIOBUTTON3)
       
   143 
       
   144         self.radioButton4 = wx.RadioButton(id=ID_LDELEMENTDIALOGRADIOBUTTON4,
       
   145               label=_("Reset"), name='radioButton4', parent=self, 
       
   146               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
       
   147         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_LDELEMENTDIALOGRADIOBUTTON4)
       
   148 
       
   149         self.radioButton5 = wx.RadioButton(id=ID_LDELEMENTDIALOGRADIOBUTTON5,
       
   150               label=_("Rising Edge"), name='radioButton5', parent=self, 
       
   151               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
       
   152         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_LDELEMENTDIALOGRADIOBUTTON5)
       
   153 
       
   154         self.radioButton6 = wx.RadioButton(id=ID_LDELEMENTDIALOGRADIOBUTTON6,
       
   155               label=_("Falling Edge"), name='radioButton6', parent=self, 
       
   156               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
       
   157         self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_LDELEMENTDIALOGRADIOBUTTON6)
       
   158 
       
   159         self.ElementName = wx.ComboBox(id=ID_LDELEMENTDIALOGNAME,
       
   160               name='Name', parent=self, pos=wx.Point(0, 0),
       
   161               size=wx.Size(0, 28), style=wx.CB_READONLY)
       
   162         self.Bind(wx.EVT_COMBOBOX, self.OnNameChanged, id=ID_LDELEMENTDIALOGNAME)
       
   163 
       
   164         self.Preview = wx.Panel(id=ID_LDELEMENTDIALOGPREVIEW,
       
   165               name='Preview', parent=self, pos=wx.Point(0, 0),
       
   166               size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
       
   167         self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
       
   168         setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
       
   169         setattr(self.Preview, "GetScaling", lambda:None)
       
   170         setattr(self.Preview, "IsOfType", ctrler.IsOfType)
       
   171 
       
   172         self.Spacer = wx.Panel(id=ID_LDELEMENTDIALOGSPACER,
       
   173               name='Spacer', parent=self, pos=wx.Point(0, 0),
       
   174               size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)
       
   175 
       
   176         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
   177         
       
   178         if wx.VERSION >= (2, 5, 0):
       
   179             self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
       
   180         else:
       
   181             wx.EVT_PAINT(self.Preview, self.OnPaint)
       
   182         
       
   183         self._init_sizers()
       
   184 
       
   185     def __init__(self, parent, controler, type):
       
   186         self.Type = type
       
   187         if type == "contact":
       
   188             self._init_ctrls(parent, controler, _("Edit Contact Values"))
       
   189             self.Element = LD_Contact(self.Preview, CONTACT_NORMAL, "")
       
   190             self.radioButton3.Hide()
       
   191             self.radioButton4.Hide()
       
   192         elif type == "coil":
       
   193             self._init_ctrls(parent, controler, _("Edit Coil Values"), 50)
       
   194             self.Element = LD_Coil(self.Preview, COIL_NORMAL, "")
       
   195             
       
   196     
       
   197     def SetPreviewFont(self, font):
       
   198         self.Preview.SetFont(font)
       
   199     
       
   200     def SetElementSize(self, size):
       
   201         min_width, min_height = self.Element.GetMinSize()
       
   202         width, height = max(min_width, size[0]), max(min_height, size[1])
       
   203         self.Element.SetSize(width, height)
       
   204         
       
   205     def SetVariables(self, vars):
       
   206         self.ElementName.Clear()
       
   207         for name in vars:
       
   208             self.ElementName.Append(name)
       
   209         self.ElementName.Enable(self.ElementName.GetCount() > 0)
       
   210 
       
   211     def SetValues(self, values):
       
   212         for name, value in values.items():
       
   213             if name == "name":
       
   214                 self.Element.SetName(value)
       
   215                 self.ElementName.SetStringSelection(value)
       
   216             elif name == "type":
       
   217                 self.Element.SetType(value)
       
   218                 if self.Type == "contact":
       
   219                     if value == CONTACT_NORMAL:
       
   220                         self.radioButton1.SetValue(True)
       
   221                     elif value == CONTACT_REVERSE:
       
   222                         self.radioButton2.SetValue(True)
       
   223                     elif value == CONTACT_RISING:
       
   224                         self.radioButton5.SetValue(True)
       
   225                     elif value == CONTACT_FALLING:
       
   226                         self.radioButton6.SetValue(True)
       
   227                 elif self.Type == "coil":
       
   228                     if value == COIL_NORMAL:
       
   229                         self.radioButton1.SetValue(True)
       
   230                     elif value == COIL_REVERSE:
       
   231                         self.radioButton2.SetValue(True)
       
   232                     elif value == COIL_SET:
       
   233                         self.radioButton3.SetValue(True)
       
   234                     elif value == COIL_RESET:
       
   235                         self.radioButton4.SetValue(True)
       
   236                     elif value == COIL_RISING:
       
   237                         self.radioButton5.SetValue(True)
       
   238                     elif value == COIL_FALLING:
       
   239                         self.radioButton6.SetValue(True)
       
   240 
       
   241     def GetValues(self):
       
   242         values = {}
       
   243         values["name"] = self.Element.GetName()
       
   244         values["type"] = self.Element.GetType()
       
   245         values["width"], values["height"] = self.Element.GetSize()
       
   246         return values
       
   247 
       
   248     def OnTypeChanged(self, event):
       
   249         if self.Type == "contact":
       
   250             if self.radioButton1.GetValue():
       
   251                 self.Element.SetType(CONTACT_NORMAL)
       
   252             elif self.radioButton2.GetValue():
       
   253                 self.Element.SetType(CONTACT_REVERSE)
       
   254             elif self.radioButton5.GetValue():
       
   255                 self.Element.SetType(CONTACT_RISING)
       
   256             elif self.radioButton6.GetValue():
       
   257                 self.Element.SetType(CONTACT_FALLING)
       
   258         elif self.Type == "coil":
       
   259             if self.radioButton1.GetValue():
       
   260                 self.Element.SetType(COIL_NORMAL)
       
   261             elif self.radioButton2.GetValue():
       
   262                 self.Element.SetType(COIL_REVERSE)
       
   263             elif self.radioButton3.GetValue():
       
   264                 self.Element.SetType(COIL_SET)
       
   265             elif self.radioButton4.GetValue():
       
   266                 self.Element.SetType(COIL_RESET)
       
   267             elif self.radioButton5.GetValue():
       
   268                 self.Element.SetType(COIL_RISING)
       
   269             elif self.radioButton6.GetValue():
       
   270                 self.Element.SetType(COIL_FALLING)
       
   271         self.RefreshPreview()
       
   272         event.Skip()
       
   273 
       
   274     def OnNameChanged(self, event):
       
   275         self.Element.SetName(self.ElementName.GetStringSelection())
       
   276         self.RefreshPreview()
       
   277         event.Skip()
       
   278 
       
   279     def RefreshPreview(self):
       
   280         dc = wx.ClientDC(self.Preview)
       
   281         dc.SetFont(self.Preview.GetFont())
       
   282         dc.Clear()
       
   283         clientsize = self.Preview.GetClientSize()
       
   284         width, height = self.Element.GetSize()
       
   285         self.Element.SetPosition((clientsize.width - width) / 2, (clientsize.height - height) / 2)
       
   286         self.Element.Draw(dc)
       
   287 
       
   288     def OnPaint(self, event):
       
   289         self.RefreshPreview()
       
   290         event.Skip()