laurent@564: #!/usr/bin/env python laurent@564: # -*- coding: utf-8 -*- laurent@564: laurent@564: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor laurent@564: #based on the plcopen standard. laurent@564: # laurent@564: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD laurent@564: # laurent@564: #See COPYING file for copyrights details. laurent@564: # laurent@564: #This library is free software; you can redistribute it and/or laurent@564: #modify it under the terms of the GNU General Public laurent@564: #License as published by the Free Software Foundation; either laurent@564: #version 2.1 of the License, or (at your option) any later version. laurent@564: # laurent@564: #This library is distributed in the hope that it will be useful, laurent@564: #but WITHOUT ANY WARRANTY; without even the implied warranty of laurent@564: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU laurent@564: #General Public License for more details. laurent@564: # laurent@564: #You should have received a copy of the GNU General Public laurent@564: #License along with this library; if not, write to the Free Software laurent@564: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA laurent@564: laurent@564: import re laurent@564: laurent@564: import wx laurent@564: laurent@584: MICROSECONDS = 0.001 laurent@584: MILLISECONDS = 1 laurent@564: SECOND = 1000 laurent@564: MINUTE = 60 * SECOND laurent@564: HOUR = 60 * MINUTE laurent@564: DAY = 24 * HOUR laurent@564: laurent@564: 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]+)?"}) laurent@564: laurent@564: #------------------------------------------------------------------------------- laurent@564: # Edit Duration Value Dialog laurent@564: #------------------------------------------------------------------------------- laurent@564: laurent@564: [ID_DURATIONEDITORDIALOG, ID_DURATIONEDITORDIALOGDAYSLABEL, laurent@564: ID_DURATIONEDITORDIALOGDAYS, ID_DURATIONEDITORDIALOGHOURSLABEL, laurent@564: ID_DURATIONEDITORDIALOGHOURS, ID_DURATIONEDITORDIALOGMINUTESLABEL, laurent@564: ID_DURATIONEDITORDIALOGMINUTES, ID_DURATIONEDITORDIALOGSECONDSLABEL, laurent@564: ID_DURATIONEDITORDIALOGSECONDS, ID_DURATIONEDITORDIALOGMILLISECONDSLABEL, laurent@584: ID_DURATIONEDITORDIALOGMILLISECONDS, ID_DURATIONEDITORDIALOGMICROSECONDSLABEL, laurent@584: ID_DURATIONEDITORDIALOGMICROSECONDS, laurent@584: ] = [wx.NewId() for _init_ctrls in range(13)] laurent@564: laurent@564: class DurationEditorDialog(wx.Dialog): laurent@564: laurent@564: if wx.VERSION < (2, 6, 0): laurent@564: def Bind(self, event, function, id = None): laurent@564: if id is not None: laurent@564: event(self, id, function) laurent@564: else: laurent@564: event(self, function) laurent@564: laurent@564: def _init_coll_MainSizer_Items(self, parent): laurent@564: parent.AddSizer(self.ControlsSizer, 0, border=20, flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW) laurent@564: parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW) laurent@564: laurent@564: def _init_coll_MainSizer_Growables(self, parent): laurent@564: parent.AddGrowableCol(0) laurent@564: parent.AddGrowableRow(0) laurent@564: laurent@564: def _init_coll_ControlsSizer_Items(self, parent): laurent@564: parent.AddWindow(self.DaysLabel, 0, border=0, flag=wx.GROW) laurent@564: parent.AddWindow(self.HoursLabel, 0, border=0, flag=wx.GROW) laurent@564: parent.AddWindow(self.MinutesLabel, 0, border=0, flag=wx.GROW) laurent@564: parent.AddWindow(self.SecondsLabel, 0, border=0, flag=wx.GROW) laurent@564: parent.AddWindow(self.MillisecondsLabel, 0, border=0, flag=wx.GROW) laurent@584: parent.AddWindow(self.MicrosecondsLabel, 0, border=0, flag=wx.GROW) laurent@564: parent.AddWindow(self.Days, 0, border=0, flag=wx.GROW) laurent@564: parent.AddWindow(self.Hours, 0, border=0, flag=wx.GROW) laurent@564: parent.AddWindow(self.Minutes, 0, border=0, flag=wx.GROW) laurent@564: parent.AddWindow(self.Seconds, 0, border=0, flag=wx.GROW) laurent@564: parent.AddWindow(self.Milliseconds, 0, border=0, flag=wx.GROW) laurent@584: parent.AddWindow(self.Microseconds, 0, border=0, flag=wx.GROW) laurent@564: laurent@564: def _init_coll_ControlsSizer_Growables(self, parent): laurent@564: parent.AddGrowableCol(0) laurent@564: parent.AddGrowableCol(1) laurent@564: parent.AddGrowableCol(2) laurent@564: parent.AddGrowableCol(3) laurent@564: parent.AddGrowableCol(4) laurent@584: parent.AddGrowableCol(5) laurent@564: laurent@564: def _init_sizers(self): laurent@564: self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) laurent@584: self.ControlsSizer = wx.FlexGridSizer(cols=6, hgap=10, rows=2, vgap=10) laurent@564: laurent@564: self._init_coll_MainSizer_Items(self.MainSizer) laurent@564: self._init_coll_MainSizer_Growables(self.MainSizer) laurent@564: self._init_coll_ControlsSizer_Items(self.ControlsSizer) laurent@564: self._init_coll_ControlsSizer_Growables(self.ControlsSizer) laurent@564: laurent@564: self.SetSizer(self.MainSizer) laurent@564: laurent@564: def _init_ctrls(self, prnt): laurent@564: wx.Dialog.__init__(self, id=ID_DURATIONEDITORDIALOG, laurent@564: name='DurationEditorDialog', parent=prnt, laurent@584: size=wx.Size(700, 200), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER, laurent@564: title=_('Edit Duration')) laurent@564: laurent@564: self.DaysLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGDAYSLABEL, laurent@564: label=_('Days:'), name='DaysLabel', parent=self, laurent@564: pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) laurent@564: laurent@564: self.Days = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGDAYS, value='0', laurent@564: name='Days', parent=self, pos=wx.Point(0, 0), laurent@564: size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER) laurent@564: self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Days), id=ID_DURATIONEDITORDIALOGDAYS) laurent@564: laurent@564: self.HoursLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGHOURSLABEL, laurent@564: label=_('Hours:'), name='HoursLabel', parent=self, laurent@564: pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) laurent@564: laurent@564: self.Hours = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGHOURS, value='0', laurent@564: name='Hours', parent=self, pos=wx.Point(0, 0), laurent@564: size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER) laurent@564: self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Hours), id=ID_DURATIONEDITORDIALOGHOURS) laurent@564: laurent@564: self.MinutesLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGMINUTESLABEL, laurent@564: label=_('Minutes:'), name='MinutesLabel', parent=self, laurent@564: pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) laurent@564: laurent@564: self.Minutes = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGMINUTES, value='0', laurent@564: name='Minutes', parent=self, pos=wx.Point(0, 0), laurent@564: size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER) laurent@564: self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Minutes), id=ID_DURATIONEDITORDIALOGMINUTES) laurent@564: laurent@564: self.SecondsLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGSECONDSLABEL, laurent@564: label=_('Seconds:'), name='SecondsLabel', parent=self, laurent@564: pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) laurent@564: laurent@564: self.Seconds = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGSECONDS, value='0', laurent@564: name='Seconds', parent=self, pos=wx.Point(0, 0), laurent@564: size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER) laurent@564: self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Seconds), id=ID_DURATIONEDITORDIALOGSECONDS) laurent@564: laurent@564: self.MillisecondsLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGMILLISECONDSLABEL, laurent@564: label=_('Milliseconds:'), name='MillisecondsLabel', parent=self, laurent@564: pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) laurent@564: laurent@564: self.Milliseconds = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGMILLISECONDS, value='0', laurent@564: name='Milliseconds', parent=self, pos=wx.Point(0, 0), laurent@564: size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER) laurent@564: self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Milliseconds), id=ID_DURATIONEDITORDIALOGMILLISECONDS) laurent@564: laurent@584: self.MicrosecondsLabel = wx.StaticText(id=ID_DURATIONEDITORDIALOGMICROSECONDSLABEL, laurent@584: label=_('Microseconds:'), name='MicrosecondsLabel', parent=self, laurent@584: pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) laurent@584: laurent@584: self.Microseconds = wx.TextCtrl(id=ID_DURATIONEDITORDIALOGMICROSECONDS, value='0', laurent@584: name='Microseconds', parent=self, pos=wx.Point(0, 0), laurent@584: size=wx.Size(0, 24), style=wx.TE_PROCESS_ENTER) laurent@584: self.Bind(wx.EVT_TEXT_ENTER, self.GetControlValueTestFunction(self.Milliseconds), id=ID_DURATIONEDITORDIALOGMICROSECONDS) laurent@584: laurent@564: self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) laurent@564: if wx.VERSION >= (2, 5, 0): laurent@564: self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) laurent@564: else: laurent@564: self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) laurent@564: laurent@564: self._init_sizers() laurent@564: laurent@564: def __init__(self, parent): laurent@564: self._init_ctrls(parent) laurent@564: laurent@577: self.Days.SetFocus() laurent@577: laurent@564: def SetDuration(self, value): laurent@564: result = IEC_TIME_MODEL.match(value.upper()) laurent@564: if result is not None: laurent@564: values = result.groups() laurent@564: for control, index in [(self.Days, 1), (self.Hours, 2), laurent@584: (self.Minutes, 3), (self.Seconds, 4)]: laurent@564: value = values[index] laurent@564: if value is not None: laurent@584: control.SetValue(value) laurent@584: else: laurent@584: control.SetValue("0") laurent@584: milliseconds = values[5] laurent@584: if milliseconds is not None: laurent@584: self.Milliseconds.SetValue("%d" % int(float(milliseconds))) laurent@584: self.Microseconds.SetValue("%.3f" % ((float(milliseconds) % MILLISECONDS) / MICROSECONDS)) laurent@584: else: laurent@584: self.Milliseconds.SetValue("0") laurent@584: self.Microseconds.SetValue("0") laurent@564: laurent@564: def GetControlValueTestFunction(self, control): laurent@564: def OnValueChanged(event): laurent@564: try: laurent@564: value = float(control.GetValue()) laurent@564: except ValueError, e: laurent@564: message = wx.MessageDialog(self, _("Invalid value!\nYou must fill a numeric value."), _("Error"), wx.OK|wx.ICON_ERROR) laurent@564: message.ShowModal() laurent@564: message.Destroy() laurent@564: event.Skip() laurent@564: return OnValueChanged laurent@564: laurent@564: def GetDuration(self): laurent@564: milliseconds = 0 laurent@564: for control, factor in [(self.Days, DAY), (self.Hours, HOUR), laurent@564: (self.Minutes, MINUTE), (self.Seconds, SECOND), laurent@584: (self.Milliseconds, MILLISECONDS), (self.Microseconds, MICROSECONDS)]: laurent@564: laurent@564: milliseconds += float(control.GetValue()) * factor laurent@564: laurent@564: not_null = False laurent@564: duration = "T#" laurent@564: for value, format in [(int(milliseconds) / DAY, "%dd"), laurent@564: ((int(milliseconds) % DAY) / HOUR, "%dh"), laurent@564: ((int(milliseconds) % HOUR) / MINUTE, "%dm"), laurent@564: ((int(milliseconds) % MINUTE) / SECOND, "%ds")]: laurent@564: laurent@564: if value > 0 or not_null: laurent@564: duration += format % value laurent@564: not_null = True laurent@564: laurent@564: duration += "%gms" % (milliseconds % SECOND) laurent@564: return duration laurent@564: laurent@564: def OnOK(self, event): laurent@564: errors = [] laurent@564: for control, name in [(self.Days, "days"), (self.Hours, "hours"), laurent@564: (self.Minutes, "minutes"), (self.Seconds, "seconds"), laurent@564: (self.Milliseconds, "milliseconds")]: laurent@564: try: laurent@564: value = float(control.GetValue()) laurent@564: except ValueError, e: laurent@564: errors.append(name) laurent@564: if len(errors) > 0: laurent@564: if len(errors) == 1: laurent@564: message = _("Field %s hasn't a valid value!") % errors[0] laurent@564: else: laurent@564: message = _("Fields %s haven't a valid value!") % ",".join(errors) laurent@564: dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) laurent@564: dialog.ShowModal() laurent@564: dialog.Destroy() laurent@564: else: laurent@564: self.EndModal(wx.ID_OK)