dialogs/DurationEditorDialog.py
changeset 714 131ea7f237b9
parent 584 c04e7af597d9
equal deleted inserted replaced
713:95a0a427f3ef 714:131ea7f237b9
    24 
    24 
    25 import re
    25 import re
    26 
    26 
    27 import wx
    27 import wx
    28 
    28 
       
    29 #-------------------------------------------------------------------------------
       
    30 #                                  Helpers
       
    31 #-------------------------------------------------------------------------------
       
    32 
    29 MICROSECONDS = 0.001
    33 MICROSECONDS = 0.001
    30 MILLISECONDS = 1
    34 MILLISECONDS = 1
    31 SECOND = 1000
    35 SECOND = 1000
    32 MINUTE = 60 * SECOND
    36 MINUTE = 60 * SECOND
    33 HOUR = 60 * MINUTE
    37 HOUR = 60 * MINUTE
    34 DAY = 24 * HOUR
    38 DAY = 24 * HOUR
    35 
    39 
    36 IEC_TIME_MODEL = re.compile("(?:(?:T|TIME)#)?(-)?(?:(%(float)s)D_?)?(?:(%(float)s)H_?)?(?:(%(float)s)M(?!S)_?)?(?:(%(float)s)S_?)?(?:(%(float)s)MS)?" % {"float": "[0-9]+(?:\.[0-9]+)?"})
    40 IEC_TIME_MODEL = re.compile("(?:(?:T|TIME)#)?(-)?(?:(%(float)s)D_?)?(?:(%(float)s)H_?)?(?:(%(float)s)M(?!S)_?)?(?:(%(float)s)S_?)?(?:(%(float)s)MS)?" % {"float": "[0-9]+(?:\.[0-9]+)?"})
    37 
    41 
       
    42 CONTROLS = [
       
    43     ("Days", _('Days:')),
       
    44     ("Hours", _('Hours:')),
       
    45     ("Minutes", _('Minutes:')),
       
    46     ("Seconds", _('Seconds:')),
       
    47     ("Milliseconds", _('Milliseconds:')),
       
    48     ("Microseconds", _('Microseconds:')),
       
    49 ]
       
    50 
    38 #-------------------------------------------------------------------------------
    51 #-------------------------------------------------------------------------------
    39 #                         Edit Duration Value Dialog
    52 #                         Edit Duration Value Dialog
    40 #-------------------------------------------------------------------------------
    53 #-------------------------------------------------------------------------------
    41 
    54 
    42 [ID_DURATIONEDITORDIALOG, ID_DURATIONEDITORDIALOGDAYSLABEL,
    55 class DurationEditorDialog(wx.Dialog):
    43  ID_DURATIONEDITORDIALOGDAYS, ID_DURATIONEDITORDIALOGHOURSLABEL, 
       
    44  ID_DURATIONEDITORDIALOGHOURS, ID_DURATIONEDITORDIALOGMINUTESLABEL,
       
    45  ID_DURATIONEDITORDIALOGMINUTES, ID_DURATIONEDITORDIALOGSECONDSLABEL, 
       
    46  ID_DURATIONEDITORDIALOGSECONDS, ID_DURATIONEDITORDIALOGMILLISECONDSLABEL,
       
    47  ID_DURATIONEDITORDIALOGMILLISECONDS, ID_DURATIONEDITORDIALOGMICROSECONDSLABEL,
       
    48  ID_DURATIONEDITORDIALOGMICROSECONDS, 
       
    49 ] = [wx.NewId() for _init_ctrls in range(13)]
       
    50 
    56 
    51 class DurationEditorDialog(wx.Dialog):
    57     def __init__(self, parent):
    52     
    58         wx.Dialog.__init__(self, parent, 
    53     if wx.VERSION < (2, 6, 0):
    59               size=wx.Size(700, 200), title=_('Edit Duration'))
    54         def Bind(self, event, function, id = None):
       
    55             if id is not None:
       
    56                 event(self, id, function)
       
    57             else:
       
    58                 event(self, function)
       
    59 
       
    60     def _init_coll_MainSizer_Items(self, parent):
       
    61         parent.AddSizer(self.ControlsSizer, 0, border=20, flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW)
       
    62         parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW)
       
    63         
    60         
    64     def _init_coll_MainSizer_Growables(self, parent):
    61         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
    65         parent.AddGrowableCol(0)
    62         main_sizer.AddGrowableCol(0)
    66         parent.AddGrowableRow(0)
    63         main_sizer.AddGrowableRow(0)
    67         
    64         
    68     def _init_coll_ControlsSizer_Items(self, parent):
    65         controls_sizer = wx.FlexGridSizer(cols=len(CONTROLS), hgap=10, rows=2, vgap=10)
    69         parent.AddWindow(self.DaysLabel, 0, border=0, flag=wx.GROW)
    66         main_sizer.AddSizer(controls_sizer, border=20, 
    70         parent.AddWindow(self.HoursLabel, 0, border=0, flag=wx.GROW)
    67               flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW)
    71         parent.AddWindow(self.MinutesLabel, 0, border=0, flag=wx.GROW)
       
    72         parent.AddWindow(self.SecondsLabel, 0, border=0, flag=wx.GROW)
       
    73         parent.AddWindow(self.MillisecondsLabel, 0, border=0, flag=wx.GROW)
       
    74         parent.AddWindow(self.MicrosecondsLabel, 0, border=0, flag=wx.GROW)
       
    75         parent.AddWindow(self.Days, 0, border=0, flag=wx.GROW)
       
    76         parent.AddWindow(self.Hours, 0, border=0, flag=wx.GROW)
       
    77         parent.AddWindow(self.Minutes, 0, border=0, flag=wx.GROW)
       
    78         parent.AddWindow(self.Seconds, 0, border=0, flag=wx.GROW)
       
    79         parent.AddWindow(self.Milliseconds, 0, border=0, flag=wx.GROW)
       
    80         parent.AddWindow(self.Microseconds, 0, border=0, flag=wx.GROW)
       
    81         
    68         
    82     def _init_coll_ControlsSizer_Growables(self, parent):
    69         controls = []
    83         parent.AddGrowableCol(0)
    70         for i, (name, label) in enumerate(CONTROLS):
    84         parent.AddGrowableCol(1)
    71             controls_sizer.AddGrowableCol(i)
    85         parent.AddGrowableCol(2)
    72             
    86         parent.AddGrowableCol(3)
    73             st = wx.StaticText(self, label=label)
    87         parent.AddGrowableCol(4)
    74             txtctrl = wx.TextCtrl(self, value='0', style=wx.TE_PROCESS_ENTER)
    88         parent.AddGrowableCol(5)
    75             self.Bind(wx.EVT_TEXT_ENTER, 
    89 
    76                       self.GetControlValueTestFunction(txtctrl), 
    90     def _init_sizers(self):
    77                       txtctrl)
    91         self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
    78             setattr(self, name, txtctrl)
    92         self.ControlsSizer = wx.FlexGridSizer(cols=6, hgap=10, rows=2, vgap=10)
       
    93         
    79         
    94         self._init_coll_MainSizer_Items(self.MainSizer)
    80             controls.append((st, txtctrl))
    95         self._init_coll_MainSizer_Growables(self.MainSizer)
    81             
    96         self._init_coll_ControlsSizer_Items(self.ControlsSizer)
    82         for st, txtctrl in controls:
    97         self._init_coll_ControlsSizer_Growables(self.ControlsSizer)
    83             controls_sizer.AddWindow(st, flag=wx.GROW)
       
    84             
       
    85         for st, txtctrl in controls:
       
    86             controls_sizer.AddWindow(txtctrl, flag=wx.GROW)
    98         
    87         
    99         self.SetSizer(self.MainSizer)
    88         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
   100 
    89         self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
   101     def _init_ctrls(self, prnt):
    90         main_sizer.AddSizer(button_sizer, border=20, 
   102         wx.Dialog.__init__(self, id=ID_DURATIONEDITORDIALOG, 
    91               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
   103               name='DurationEditorDialog', parent=prnt,  
       
   104               size=wx.Size(700, 200), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
       
   105               title=_('Edit Duration'))
       
   106         
    92         
   107         self.DaysLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGDAYSLABEL,
    93         self.SetSizer(main_sizer)
   108               label=_('Days:'), name='DaysLabel', parent=self,
       
   109               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   110         
       
   111         self.Days = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGDAYS, value='0',
       
   112               name='Days', parent=self, pos=wx.Point(0, 0),
       
   113               size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER)
       
   114         self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Days), id=ID_DURATIONEDITORDIALOGDAYS)
       
   115         
       
   116         self.HoursLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGHOURSLABEL,
       
   117               label=_('Hours:'), name='HoursLabel', parent=self,
       
   118               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   119         
       
   120         self.Hours = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGHOURS, value='0',
       
   121               name='Hours', parent=self, pos=wx.Point(0, 0),
       
   122               size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER)
       
   123         self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Hours), id=ID_DURATIONEDITORDIALOGHOURS)
       
   124         
       
   125         self.MinutesLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGMINUTESLABEL,
       
   126               label=_('Minutes:'), name='MinutesLabel', parent=self,
       
   127               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   128         
       
   129         self.Minutes = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGMINUTES, value='0',
       
   130               name='Minutes', parent=self, pos=wx.Point(0, 0),
       
   131               size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER)
       
   132         self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Minutes), id=ID_DURATIONEDITORDIALOGMINUTES)
       
   133         
       
   134         self.SecondsLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGSECONDSLABEL,
       
   135               label=_('Seconds:'), name='SecondsLabel', parent=self,
       
   136               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   137         
       
   138         self.Seconds = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGSECONDS, value='0',
       
   139               name='Seconds', parent=self, pos=wx.Point(0, 0),
       
   140               size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER)
       
   141         self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Seconds), id=ID_DURATIONEDITORDIALOGSECONDS)
       
   142         
       
   143         self.MillisecondsLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGMILLISECONDSLABEL,
       
   144               label=_('Milliseconds:'), name='MillisecondsLabel', parent=self,
       
   145               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   146         
       
   147         self.Milliseconds = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGMILLISECONDS, value='0',
       
   148               name='Milliseconds', parent=self, pos=wx.Point(0, 0),
       
   149               size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER)
       
   150         self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Milliseconds), id=ID_DURATIONEDITORDIALOGMILLISECONDS)
       
   151         
       
   152         self.MicrosecondsLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGMICROSECONDSLABEL,
       
   153               label=_('Microseconds:'), name='MicrosecondsLabel', parent=self,
       
   154               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
       
   155         
       
   156         self.Microseconds = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGMICROSECONDS, value='0',
       
   157               name='Microseconds', parent=self, pos=wx.Point(0, 0),
       
   158               size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER)
       
   159         self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Milliseconds), id=ID_DURATIONEDITORDIALOGMICROSECONDS)
       
   160         
       
   161         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
   162         if wx.VERSION >= (2, 5, 0):
       
   163             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
       
   164         else:
       
   165             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
       
   166         
       
   167         self._init_sizers()
       
   168     
       
   169     def __init__(self, parent):
       
   170         self._init_ctrls(parent)
       
   171         
    94         
   172         self.Days.SetFocus()
    95         self.Days.SetFocus()
   173         
    96         
   174     def SetDuration(self, value):
    97     def SetDuration(self, value):
   175         result = IEC_TIME_MODEL.match(value.upper())
    98         result = IEC_TIME_MODEL.match(value.upper())