|
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 re |
|
26 |
|
27 import wx |
|
28 |
|
29 #------------------------------------------------------------------------------- |
|
30 # Helpers |
|
31 #------------------------------------------------------------------------------- |
|
32 |
|
33 MICROSECONDS = 0.001 |
|
34 MILLISECONDS = 1 |
|
35 SECOND = 1000 |
|
36 MINUTE = 60 * SECOND |
|
37 HOUR = 60 * MINUTE |
|
38 DAY = 24 * HOUR |
|
39 |
|
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]+)?"}) |
|
41 |
|
42 CONTROLS = [ |
|
43 ("Days", _('Days:')), |
|
44 ("Hours", _('Hours:')), |
|
45 ("Minutes", _('Minutes:')), |
|
46 ("Seconds", _('Seconds:')), |
|
47 ("Milliseconds", _('Milliseconds:')), |
|
48 ("Microseconds", _('Microseconds:')), |
|
49 ] |
|
50 |
|
51 #------------------------------------------------------------------------------- |
|
52 # Edit Duration Value Dialog |
|
53 #------------------------------------------------------------------------------- |
|
54 |
|
55 class DurationEditorDialog(wx.Dialog): |
|
56 |
|
57 def __init__(self, parent): |
|
58 wx.Dialog.__init__(self, parent, |
|
59 size=wx.Size(700, 200), title=_('Edit Duration')) |
|
60 |
|
61 main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) |
|
62 main_sizer.AddGrowableCol(0) |
|
63 main_sizer.AddGrowableRow(0) |
|
64 |
|
65 controls_sizer = wx.FlexGridSizer(cols=len(CONTROLS), hgap=10, rows=2, vgap=10) |
|
66 main_sizer.AddSizer(controls_sizer, border=20, |
|
67 flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW) |
|
68 |
|
69 controls = [] |
|
70 for i, (name, label) in enumerate(CONTROLS): |
|
71 controls_sizer.AddGrowableCol(i) |
|
72 |
|
73 st = wx.StaticText(self, label=label) |
|
74 txtctrl = wx.TextCtrl(self, value='0', style=wx.TE_PROCESS_ENTER) |
|
75 self.Bind(wx.EVT_TEXT_ENTER, |
|
76 self.GetControlValueTestFunction(txtctrl), |
|
77 txtctrl) |
|
78 setattr(self, name, txtctrl) |
|
79 |
|
80 controls.append((st, txtctrl)) |
|
81 |
|
82 for st, txtctrl in controls: |
|
83 controls_sizer.AddWindow(st, flag=wx.GROW) |
|
84 |
|
85 for st, txtctrl in controls: |
|
86 controls_sizer.AddWindow(txtctrl, flag=wx.GROW) |
|
87 |
|
88 button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
|
89 self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton()) |
|
90 main_sizer.AddSizer(button_sizer, border=20, |
|
91 flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
|
92 |
|
93 self.SetSizer(main_sizer) |
|
94 |
|
95 self.Days.SetFocus() |
|
96 |
|
97 def SetDuration(self, value): |
|
98 result = IEC_TIME_MODEL.match(value.upper()) |
|
99 if result is not None: |
|
100 values = result.groups() |
|
101 for control, index in [(self.Days, 1), (self.Hours, 2), |
|
102 (self.Minutes, 3), (self.Seconds, 4)]: |
|
103 value = values[index] |
|
104 if value is not None: |
|
105 control.SetValue(value) |
|
106 else: |
|
107 control.SetValue("0") |
|
108 milliseconds = values[5] |
|
109 if milliseconds is not None: |
|
110 self.Milliseconds.SetValue("%d" % int(float(milliseconds))) |
|
111 self.Microseconds.SetValue("%.3f" % ((float(milliseconds) % MILLISECONDS) / MICROSECONDS)) |
|
112 else: |
|
113 self.Milliseconds.SetValue("0") |
|
114 self.Microseconds.SetValue("0") |
|
115 |
|
116 def GetControlValueTestFunction(self, control): |
|
117 def OnValueChanged(event): |
|
118 try: |
|
119 value = float(control.GetValue()) |
|
120 except ValueError, e: |
|
121 message = wx.MessageDialog(self, _("Invalid value!\nYou must fill a numeric value."), _("Error"), wx.OK|wx.ICON_ERROR) |
|
122 message.ShowModal() |
|
123 message.Destroy() |
|
124 event.Skip() |
|
125 return OnValueChanged |
|
126 |
|
127 def GetDuration(self): |
|
128 milliseconds = 0 |
|
129 for control, factor in [(self.Days, DAY), (self.Hours, HOUR), |
|
130 (self.Minutes, MINUTE), (self.Seconds, SECOND), |
|
131 (self.Milliseconds, MILLISECONDS), (self.Microseconds, MICROSECONDS)]: |
|
132 |
|
133 milliseconds += float(control.GetValue()) * factor |
|
134 |
|
135 not_null = False |
|
136 duration = "T#" |
|
137 for value, format in [(int(milliseconds) / DAY, "%dd"), |
|
138 ((int(milliseconds) % DAY) / HOUR, "%dh"), |
|
139 ((int(milliseconds) % HOUR) / MINUTE, "%dm"), |
|
140 ((int(milliseconds) % MINUTE) / SECOND, "%ds")]: |
|
141 |
|
142 if value > 0 or not_null: |
|
143 duration += format % value |
|
144 not_null = True |
|
145 |
|
146 duration += "%gms" % (milliseconds % SECOND) |
|
147 return duration |
|
148 |
|
149 def OnOK(self, event): |
|
150 errors = [] |
|
151 for control, name in [(self.Days, "days"), (self.Hours, "hours"), |
|
152 (self.Minutes, "minutes"), (self.Seconds, "seconds"), |
|
153 (self.Milliseconds, "milliseconds")]: |
|
154 try: |
|
155 value = float(control.GetValue()) |
|
156 except ValueError, e: |
|
157 errors.append(name) |
|
158 if len(errors) > 0: |
|
159 if len(errors) == 1: |
|
160 message = _("Field %s hasn't a valid value!") % errors[0] |
|
161 else: |
|
162 message = _("Fields %s haven't a valid value!") % ",".join(errors) |
|
163 dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) |
|
164 dialog.ShowModal() |
|
165 dialog.Destroy() |
|
166 else: |
|
167 self.EndModal(wx.ID_OK) |