dialogs/ArrayTypeDialog.py
changeset 507 42150e041dbe
child 534 d506a353b3d3
equal deleted inserted replaced
504:f88e0ebd8fe4 507:42150e041dbe
       
     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 import wx.gizmos
       
    29 
       
    30 DIMENSION_MODEL = re.compile("([0-9]+)\.\.([0-9]+)$")
       
    31 
       
    32 [ID_ARRAYTYPEDIALOG, ID_ARRAYTYPEDIALOGBASETYPE, 
       
    33  ID_ARRAYTYPEDIALOGDIMENSIONS, ID_ARRAYTYPEDIALOGDIALOGSTATICTEXT1, 
       
    34 ] = [wx.NewId() for _init_ctrls in range(4)]
       
    35 
       
    36 class ArrayTypeDialog(wx.Dialog):
       
    37     
       
    38     if wx.VERSION < (2, 6, 0):
       
    39         def Bind(self, event, function, id = None):
       
    40             if id is not None:
       
    41                 event(self, id, function)
       
    42             else:
       
    43                 event(self, function)
       
    44     
       
    45     def _init_coll_flexGridSizer1_Items(self, parent):
       
    46         parent.AddSizer(self.TopSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
    47         parent.AddWindow(self.Dimensions, 0, border=20, flag=wx.GROW|wx.ALIGN_RIGHT|wx.LEFT|wx.RIGHT)
       
    48         parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
       
    49         
       
    50     def _init_coll_flexGridSizer1_Growables(self, parent):
       
    51         parent.AddGrowableCol(0)
       
    52         parent.AddGrowableRow(1)
       
    53         
       
    54     def _init_coll_TopSizer_Items(self, parent):
       
    55         parent.AddWindow(self.staticText1, 1, border=0, flag=wx.GROW)
       
    56         parent.AddWindow(self.BaseType, 1, border=0, flag=wx.GROW)
       
    57     
       
    58     def _init_sizers(self):
       
    59         self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
       
    60         self.TopSizer = wx.BoxSizer(wx.HORIZONTAL)
       
    61         
       
    62         self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
       
    63         self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
       
    64         self._init_coll_TopSizer_Items(self.TopSizer)
       
    65         
       
    66         self.SetSizer(self.flexGridSizer1)
       
    67     
       
    68     def _init_ctrls(self, prnt):
       
    69         wx.Dialog.__init__(self, id=ID_ARRAYTYPEDIALOG,
       
    70               name='ArrayTypeDialog', parent=prnt, pos=wx.Point(376, 223),
       
    71               size=wx.Size(500, 300), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
       
    72               title=_('Edit array type properties'))
       
    73         self.SetClientSize(wx.Size(500, 300))
       
    74         
       
    75         self.staticText1 = wx.StaticText(id=ID_ARRAYTYPEDIALOGDIALOGSTATICTEXT1,
       
    76               label=_('Base Type:'), name='staticText1', parent=self,
       
    77               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
       
    78 
       
    79         self.BaseType = wx.ComboBox(id=ID_ARRAYTYPEDIALOGBASETYPE, 
       
    80               name='BaseType', parent=self, pos=wx.Point(0, 0),
       
    81               size=wx.Size(0, 28), style=wx.CB_READONLY)
       
    82         
       
    83         self.Dimensions = wx.gizmos.EditableListBox(id=ID_ARRAYTYPEDIALOGDIMENSIONS, 
       
    84               name='ArrayDimensions', parent=self, label=_("Dimensions:"), pos=wx.Point(0, 0),
       
    85               size=wx.Size(0, 24), style=wx.gizmos.EL_ALLOW_NEW | wx.gizmos.EL_ALLOW_EDIT | wx.gizmos.EL_ALLOW_DELETE)
       
    86         self.Dimensions.GetListCtrl().Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnDimensionsChanged)
       
    87         new_button = self.Dimensions.GetNewButton()
       
    88         new_button.SetToolTipString(_("New item"))
       
    89         new_button.Bind(wx.EVT_BUTTON, self.OnDimensionsChanged)
       
    90         del_button = self.Dimensions.GetDelButton()
       
    91         del_button.SetToolTipString(_("Delete item"))
       
    92         del_button.Bind(wx.EVT_BUTTON, self.OnDimensionsChanged)
       
    93         up_button = self.Dimensions.GetUpButton()
       
    94         up_button.SetToolTipString(_("Move up"))
       
    95         up_button.Bind(wx.EVT_BUTTON, self.OnDimensionsChanged)
       
    96         down_button = self.Dimensions.GetDownButton()
       
    97         down_button.SetToolTipString(_("Move down"))
       
    98         down_button.Bind(wx.EVT_BUTTON, self.OnDimensionsChanged)
       
    99         
       
   100         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
   101         if wx.VERSION >= (2, 5, 0):
       
   102             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
       
   103         else:
       
   104             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
       
   105         
       
   106         self._init_sizers()
       
   107 
       
   108     def __init__(self, parent, datatypes, infos):
       
   109         self._init_ctrls(parent)
       
   110         
       
   111         for datatype in datatypes:
       
   112             self.BaseType.Append(datatype)
       
   113         
       
   114         if isinstance(infos, TupleType) and infos[0] == "array":
       
   115             self.BaseType.SetStringSelection(infos[1])
       
   116             self.Dimensions.SetStrings(map(lambda x : "..".join(x), infos[2]))
       
   117         elif infos in datatypes:
       
   118             self.BaseType.SetStringSelection(infos)
       
   119         
       
   120     def GetDimensions(self):
       
   121         dimensions_list = []
       
   122         for dimensions in self.Dimensions.GetStrings():
       
   123             result = DIMENSION_MODEL.match(dimensions)
       
   124             if result is None:
       
   125                 message = wx.MessageDialog(self, _("\"%s\" value isn't a valid array dimension!")%dimensions, _("Error"), wx.OK|wx.ICON_ERROR)
       
   126                 message.ShowModal()
       
   127                 message.Destroy()
       
   128                 return None
       
   129             bounds = result.groups()
       
   130             if int(bounds[0]) >= int(bounds[1]):
       
   131                 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)
       
   132                 message.ShowModal()
       
   133                 message.Destroy()
       
   134                 return None
       
   135             dimensions_list.append(bounds)
       
   136         return dimensions_list
       
   137     
       
   138     def OnDimensionsChanged(self, event):
       
   139         wx.CallAfter(self.GetDimensions)
       
   140         event.Skip()
       
   141     
       
   142     def OnOK(self, event):
       
   143         if self.GetDimensions() is not None:
       
   144             self.EndModal(wx.ID_OK)
       
   145             
       
   146     def GetValue(self):
       
   147         return "array", self.BaseType.GetStringSelection(), self.GetDimensions()