author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 19 Jan 2017 13:56:09 +0300 | |
changeset 1639 | 1953c268a194 |
parent 1571 | 486f94a8032c |
child 1696 | 8043f32de7b8 |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
4 |
# This file is part of Beremiz, a Integrated Development Environment for |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
5 |
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
814 | 6 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
7 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
814 | 8 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
9 |
# See COPYING file for copyrights details. |
814 | 10 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
11 |
# This program is free software; you can redistribute it and/or |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
12 |
# modify it under the terms of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
13 |
# as published by the Free Software Foundation; either version 2 |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
14 |
# of the License, or (at your option) any later version. |
814 | 15 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
16 |
# This program is distributed in the hope that it will be useful, |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
19 |
# GNU General Public License for more details. |
814 | 20 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
21 |
# You should have received a copy of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
22 |
# along with this program; if not, write to the Free Software |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 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 |
||
879
55b8a16ead2b
Fix resource task interval error highlighting
Laurent Bessard
parents:
831
diff
changeset
|
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]+)?"}) |
814 | 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 = [] |
|
1495
078047c3ab85
add i18n for some strings in DiscoveryDialog, DurationEditDialog, ProjectDialog and CodeFileEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
879
diff
changeset
|
151 |
for control, name in [(self.Days, _("days")), (self.Hours, _("hours")), |
078047c3ab85
add i18n for some strings in DiscoveryDialog, DurationEditDialog, ProjectDialog and CodeFileEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
879
diff
changeset
|
152 |
(self.Minutes, _("minutes")), (self.Seconds, _("seconds")), |
078047c3ab85
add i18n for some strings in DiscoveryDialog, DurationEditDialog, ProjectDialog and CodeFileEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
879
diff
changeset
|
153 |
(self.Milliseconds, _("milliseconds"))]: |
814 | 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) |