author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 17 Aug 2017 16:26:32 +0300 | |
changeset 1765 | ccf59c1f0b45 |
parent 1745 | f9d32913bad4 |
child 1768 | 691083b5682a |
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 |
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
8 |
# Copyright (C) 2017: Andrey Skvortsov <andrej.skvortzov@gmail.com> |
814 | 9 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
10 |
# See COPYING file for copyrights details. |
814 | 11 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
12 |
# 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
|
13 |
# 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
|
14 |
# 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
|
15 |
# of the License, or (at your option) any later version. |
814 | 16 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
17 |
# 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
|
18 |
# 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
|
19 |
# 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
|
20 |
# GNU General Public License for more details. |
814 | 21 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1495
diff
changeset
|
22 |
# 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
|
23 |
# 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
|
24 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 25 |
|
26 |
import re |
|
27 |
||
28 |
import wx |
|
29 |
||
30 |
#------------------------------------------------------------------------------- |
|
31 |
# Helpers |
|
32 |
#------------------------------------------------------------------------------- |
|
33 |
||
34 |
MICROSECONDS = 0.001 |
|
35 |
MILLISECONDS = 1 |
|
36 |
SECOND = 1000 |
|
37 |
MINUTE = 60 * SECOND |
|
38 |
HOUR = 60 * MINUTE |
|
39 |
DAY = 24 * HOUR |
|
40 |
||
879
55b8a16ead2b
Fix resource task interval error highlighting
Laurent Bessard
parents:
831
diff
changeset
|
41 |
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 | 42 |
|
43 |
CONTROLS = [ |
|
44 |
("Days", _('Days:')), |
|
45 |
("Hours", _('Hours:')), |
|
46 |
("Minutes", _('Minutes:')), |
|
47 |
("Seconds", _('Seconds:')), |
|
48 |
("Milliseconds", _('Milliseconds:')), |
|
49 |
("Microseconds", _('Microseconds:')), |
|
50 |
] |
|
51 |
||
52 |
#------------------------------------------------------------------------------- |
|
53 |
# Edit Duration Value Dialog |
|
54 |
#------------------------------------------------------------------------------- |
|
55 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
56 |
|
814 | 57 |
class DurationEditorDialog(wx.Dialog): |
58 |
||
59 |
def __init__(self, parent): |
|
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
60 |
wx.Dialog.__init__(self, parent, title=_('Edit Duration')) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
61 |
|
814 | 62 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) |
63 |
main_sizer.AddGrowableCol(0) |
|
64 |
main_sizer.AddGrowableRow(0) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
65 |
|
814 | 66 |
controls_sizer = wx.FlexGridSizer(cols=len(CONTROLS), hgap=10, rows=2, vgap=10) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
67 |
main_sizer.AddSizer(controls_sizer, border=20, |
1745
f9d32913bad4
clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
68 |
flag=wx.TOP | wx.LEFT | wx.RIGHT | wx.GROW) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
69 |
|
814 | 70 |
controls = [] |
71 |
for i, (name, label) in enumerate(CONTROLS): |
|
72 |
controls_sizer.AddGrowableCol(i) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
73 |
|
814 | 74 |
st = wx.StaticText(self, label=label) |
75 |
txtctrl = wx.TextCtrl(self, value='0', style=wx.TE_PROCESS_ENTER) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
76 |
self.Bind(wx.EVT_TEXT_ENTER, |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
77 |
self.GetControlValueTestFunction(txtctrl), |
814 | 78 |
txtctrl) |
79 |
setattr(self, name, txtctrl) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
80 |
|
814 | 81 |
controls.append((st, txtctrl)) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
82 |
|
814 | 83 |
for st, txtctrl in controls: |
84 |
controls_sizer.AddWindow(st, flag=wx.GROW) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
85 |
|
814 | 86 |
for st, txtctrl in controls: |
87 |
controls_sizer.AddWindow(txtctrl, flag=wx.GROW) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
88 |
|
1745
f9d32913bad4
clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
89 |
button_sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE) |
814 | 90 |
self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton()) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
91 |
main_sizer.AddSizer(button_sizer, border=20, |
1745
f9d32913bad4
clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
92 |
flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
93 |
|
814 | 94 |
self.SetSizer(main_sizer) |
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
95 |
self.Fit() |
814 | 96 |
self.Days.SetFocus() |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
97 |
|
814 | 98 |
def SetDuration(self, value): |
99 |
result = IEC_TIME_MODEL.match(value.upper()) |
|
100 |
if result is not None: |
|
101 |
values = result.groups() |
|
102 |
for control, index in [(self.Days, 1), (self.Hours, 2), |
|
103 |
(self.Minutes, 3), (self.Seconds, 4)]: |
|
104 |
value = values[index] |
|
105 |
if value is not None: |
|
106 |
control.SetValue(value) |
|
107 |
else: |
|
108 |
control.SetValue("0") |
|
109 |
milliseconds = values[5] |
|
110 |
if milliseconds is not None: |
|
111 |
self.Milliseconds.SetValue("%d" % int(float(milliseconds))) |
|
112 |
self.Microseconds.SetValue("%.3f" % ((float(milliseconds) % MILLISECONDS) / MICROSECONDS)) |
|
113 |
else: |
|
114 |
self.Milliseconds.SetValue("0") |
|
115 |
self.Microseconds.SetValue("0") |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
116 |
|
814 | 117 |
def GetControlValueTestFunction(self, control): |
118 |
def OnValueChanged(event): |
|
119 |
try: |
|
120 |
value = float(control.GetValue()) |
|
121 |
except ValueError, e: |
|
1745
f9d32913bad4
clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
122 |
message = wx.MessageDialog(self, _("Invalid value!\nYou must fill a numeric value."), _("Error"), wx.OK | wx.ICON_ERROR) |
814 | 123 |
message.ShowModal() |
124 |
message.Destroy() |
|
125 |
event.Skip() |
|
1702
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
126 |
self.OnCloseDialog() |
814 | 127 |
return OnValueChanged |
128 |
||
129 |
def GetDuration(self): |
|
130 |
milliseconds = 0 |
|
131 |
for control, factor in [(self.Days, DAY), (self.Hours, HOUR), |
|
132 |
(self.Minutes, MINUTE), (self.Seconds, SECOND), |
|
133 |
(self.Milliseconds, MILLISECONDS), (self.Microseconds, MICROSECONDS)]: |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
134 |
|
814 | 135 |
milliseconds += float(control.GetValue()) * factor |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
136 |
|
814 | 137 |
not_null = False |
138 |
duration = "T#" |
|
139 |
for value, format in [(int(milliseconds) / DAY, "%dd"), |
|
140 |
((int(milliseconds) % DAY) / HOUR, "%dh"), |
|
141 |
((int(milliseconds) % HOUR) / MINUTE, "%dm"), |
|
142 |
((int(milliseconds) % MINUTE) / SECOND, "%ds")]: |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
143 |
|
814 | 144 |
if value > 0 or not_null: |
145 |
duration += format % value |
|
146 |
not_null = True |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
147 |
|
814 | 148 |
duration += "%gms" % (milliseconds % SECOND) |
149 |
return duration |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
150 |
|
814 | 151 |
def OnOK(self, event): |
1702
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
152 |
self.OnCloseDialog() |
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
153 |
|
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
154 |
def OnCloseDialog(self): |
814 | 155 |
errors = [] |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
156 |
for control, name in [(self.Days, _("days")), (self.Hours, _("hours")), |
1495
078047c3ab85
add i18n for some strings in DiscoveryDialog, DurationEditDialog, ProjectDialog and CodeFileEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
879
diff
changeset
|
157 |
(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
|
158 |
(self.Milliseconds, _("milliseconds"))]: |
814 | 159 |
try: |
160 |
value = float(control.GetValue()) |
|
161 |
except ValueError, e: |
|
162 |
errors.append(name) |
|
163 |
if len(errors) > 0: |
|
164 |
if len(errors) == 1: |
|
165 |
message = _("Field %s hasn't a valid value!") % errors[0] |
|
166 |
else: |
|
167 |
message = _("Fields %s haven't a valid value!") % ",".join(errors) |
|
1745
f9d32913bad4
clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
168 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR) |
814 | 169 |
dialog.ShowModal() |
170 |
dialog.Destroy() |
|
171 |
else: |
|
172 |
self.EndModal(wx.ID_OK) |