dialogs/ArrayTypeDialog.py
changeset 814 5743cbdff669
child 1487 789acd7cc347
equal deleted inserted replaced
813:1460273f40ed 814:5743cbdff669
       
     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 re
       
    25 from types import TupleType
       
    26 
       
    27 import wx
       
    28 
       
    29 from controls import CustomEditableListBox
       
    30 
       
    31 #-------------------------------------------------------------------------------
       
    32 #                                  Helpers
       
    33 #-------------------------------------------------------------------------------
       
    34 
       
    35 DIMENSION_MODEL = re.compile("([0-9]+)\.\.([0-9]+)$")
       
    36 
       
    37 #-------------------------------------------------------------------------------
       
    38 #                             Array Type Dialog
       
    39 #-------------------------------------------------------------------------------
       
    40 
       
    41 class ArrayTypeDialog(wx.Dialog):
       
    42     
       
    43     def __init__(self, parent, datatypes, infos):
       
    44         wx.Dialog.__init__(self, parent,
       
    45               size=wx.Size(500, 300), title=_('Edit array type properties'))
       
    46         
       
    47         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
       
    48         main_sizer.AddGrowableCol(0)
       
    49         main_sizer.AddGrowableRow(1)
       
    50         
       
    51         top_sizer = wx.BoxSizer(wx.HORIZONTAL)
       
    52         main_sizer.AddSizer(top_sizer, border=20, 
       
    53               flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
    54         
       
    55         basetype_label = wx.StaticText(self, label=_('Base Type:'))
       
    56         top_sizer.AddWindow(basetype_label, 1, flag=wx.ALIGN_BOTTOM)
       
    57         
       
    58         self.BaseType = wx.ComboBox(self, style=wx.CB_READONLY)
       
    59         top_sizer.AddWindow(self.BaseType, 1, flag=wx.GROW)
       
    60         
       
    61         self.Dimensions = CustomEditableListBox(self, label=_("Dimensions:"), 
       
    62               style=wx.gizmos.EL_ALLOW_NEW|
       
    63                     wx.gizmos.EL_ALLOW_EDIT|
       
    64                     wx.gizmos.EL_ALLOW_DELETE)
       
    65         for func in ["_OnLabelEndEdit", 
       
    66                      "_OnAddButton", 
       
    67                      "_OnDelButton", 
       
    68                      "_OnUpButton", 
       
    69                      "_OnDownButton"]:
       
    70             setattr(self.Dimensions, func, self.OnDimensionsChanged)
       
    71         main_sizer.AddSizer(self.Dimensions, border=20, 
       
    72               flag=wx.GROW|wx.LEFT|wx.RIGHT)
       
    73         
       
    74         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
    75         self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
       
    76         main_sizer.AddSizer(button_sizer, border=20, 
       
    77               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
       
    78         
       
    79         self.SetSizer(main_sizer)
       
    80         
       
    81         for datatype in datatypes:
       
    82             self.BaseType.Append(datatype)
       
    83         
       
    84         if isinstance(infos, TupleType) and infos[0] == "array":
       
    85             self.BaseType.SetStringSelection(infos[1])
       
    86             self.Dimensions.SetStrings(map(lambda x : "..".join(x), infos[2]))
       
    87         elif infos in datatypes:
       
    88             self.BaseType.SetStringSelection(infos)
       
    89         
       
    90         self.BaseType.SetFocus()
       
    91         
       
    92     def GetDimensions(self):
       
    93         dimensions_list = []
       
    94         for dimensions in self.Dimensions.GetStrings():
       
    95             result = DIMENSION_MODEL.match(dimensions)
       
    96             if result is None:
       
    97                 message = wx.MessageDialog(self, _("\"%s\" value isn't a valid array dimension!")%dimensions, _("Error"), wx.OK|wx.ICON_ERROR)
       
    98                 message.ShowModal()
       
    99                 message.Destroy()
       
   100                 return None
       
   101             bounds = result.groups()
       
   102             if int(bounds[0]) >= int(bounds[1]):
       
   103                 message = wx.MessageDialog(self, _("\"%s\" value isn't a valid array dimension!\nRight value must be greater than left value.")%dimensions, _("Error"), wx.OK|wx.ICON_ERROR)
       
   104                 message.ShowModal()
       
   105                 message.Destroy()
       
   106                 return None
       
   107             dimensions_list.append(bounds)
       
   108         return dimensions_list
       
   109     
       
   110     def OnDimensionsChanged(self, event):
       
   111         wx.CallAfter(self.GetDimensions)
       
   112         event.Skip()
       
   113     
       
   114     def OnOK(self, event):
       
   115         if self.GetDimensions() is not None:
       
   116             self.EndModal(wx.ID_OK)
       
   117             
       
   118     def GetValue(self):
       
   119         return "array", self.BaseType.GetStringSelection(), self.GetDimensions()