author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Fri, 10 Jul 2020 11:15:41 +0200 | |
branch | svghmi |
changeset 2992 | d48ad9c3ec91 |
parent 2439 | f0a040f1de1b |
child 3303 | 0ffb41625592 |
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 |
|
1881
091005ec69c4
fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
26 |
|
091005ec69c4
fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
27 |
from __future__ import absolute_import |
2437
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2301
diff
changeset
|
28 |
from __future__ import division |
814 | 29 |
import re |
30 |
||
31 |
import wx |
|
32 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
33 |
# ------------------------------------------------------------------------------- |
814 | 34 |
# Helpers |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
35 |
# ------------------------------------------------------------------------------- |
814 | 36 |
|
37 |
MICROSECONDS = 0.001 |
|
38 |
MILLISECONDS = 1 |
|
39 |
SECOND = 1000 |
|
40 |
MINUTE = 60 * SECOND |
|
41 |
HOUR = 60 * MINUTE |
|
42 |
DAY = 24 * HOUR |
|
43 |
||
2439
f0a040f1de1b
Fix pep8 warning: W605 invalid escape sequence ?x?
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2437
diff
changeset
|
44 |
IEC_TIME_MODEL = re.compile(r"(?:T|TIME)#(-)?(?:(%(float)s)D_?)?(?:(%(float)s)H_?)?(?:(%(float)s)M(?!S)_?)?(?:(%(float)s)S_?)?(?:(%(float)s)MS)?$" % {"float": r"[0-9]+(?:\.[0-9]+)?"}) |
814 | 45 |
|
46 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
47 |
# ------------------------------------------------------------------------------- |
814 | 48 |
# Edit Duration Value Dialog |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
49 |
# ------------------------------------------------------------------------------- |
814 | 50 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
51 |
|
814 | 52 |
class DurationEditorDialog(wx.Dialog): |
53 |
||
54 |
def __init__(self, parent): |
|
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
55 |
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
|
56 |
|
2301
5b8a7dd43f9f
Avoid usage of localized strings before initialization during import in many modules
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2235
diff
changeset
|
57 |
CONTROLS = [ |
5b8a7dd43f9f
Avoid usage of localized strings before initialization during import in many modules
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2235
diff
changeset
|
58 |
("Days", _('Days:')), |
5b8a7dd43f9f
Avoid usage of localized strings before initialization during import in many modules
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2235
diff
changeset
|
59 |
("Hours", _('Hours:')), |
5b8a7dd43f9f
Avoid usage of localized strings before initialization during import in many modules
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2235
diff
changeset
|
60 |
("Minutes", _('Minutes:')), |
5b8a7dd43f9f
Avoid usage of localized strings before initialization during import in many modules
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2235
diff
changeset
|
61 |
("Seconds", _('Seconds:')), |
5b8a7dd43f9f
Avoid usage of localized strings before initialization during import in many modules
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2235
diff
changeset
|
62 |
("Milliseconds", _('Milliseconds:')), |
5b8a7dd43f9f
Avoid usage of localized strings before initialization during import in many modules
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2235
diff
changeset
|
63 |
("Microseconds", _('Microseconds:')), |
5b8a7dd43f9f
Avoid usage of localized strings before initialization during import in many modules
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2235
diff
changeset
|
64 |
] |
5b8a7dd43f9f
Avoid usage of localized strings before initialization during import in many modules
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2235
diff
changeset
|
65 |
|
814 | 66 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) |
67 |
main_sizer.AddGrowableCol(0) |
|
68 |
main_sizer.AddGrowableRow(0) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
69 |
|
814 | 70 |
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
|
71 |
main_sizer.AddSizer(controls_sizer, border=20, |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1745
diff
changeset
|
72 |
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
|
73 |
|
814 | 74 |
controls = [] |
75 |
for i, (name, label) in enumerate(CONTROLS): |
|
76 |
controls_sizer.AddGrowableCol(i) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
77 |
|
814 | 78 |
st = wx.StaticText(self, label=label) |
79 |
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
|
80 |
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
|
81 |
self.GetControlValueTestFunction(txtctrl), |
814 | 82 |
txtctrl) |
83 |
setattr(self, name, txtctrl) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
84 |
|
814 | 85 |
controls.append((st, txtctrl)) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
86 |
|
814 | 87 |
for st, txtctrl in controls: |
88 |
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
|
89 |
|
814 | 90 |
for st, txtctrl in controls: |
91 |
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
|
92 |
|
1745
f9d32913bad4
clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
93 |
button_sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE) |
814 | 94 |
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
|
95 |
main_sizer.AddSizer(button_sizer, border=20, |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1745
diff
changeset
|
96 |
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
|
97 |
|
814 | 98 |
self.SetSizer(main_sizer) |
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
99 |
self.Fit() |
814 | 100 |
self.Days.SetFocus() |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
101 |
|
814 | 102 |
def SetDuration(self, value): |
103 |
result = IEC_TIME_MODEL.match(value.upper()) |
|
104 |
if result is not None: |
|
105 |
values = result.groups() |
|
106 |
for control, index in [(self.Days, 1), (self.Hours, 2), |
|
107 |
(self.Minutes, 3), (self.Seconds, 4)]: |
|
108 |
value = values[index] |
|
109 |
if value is not None: |
|
110 |
control.SetValue(value) |
|
111 |
else: |
|
112 |
control.SetValue("0") |
|
113 |
milliseconds = values[5] |
|
114 |
if milliseconds is not None: |
|
115 |
self.Milliseconds.SetValue("%d" % int(float(milliseconds))) |
|
116 |
self.Microseconds.SetValue("%.3f" % ((float(milliseconds) % MILLISECONDS) / MICROSECONDS)) |
|
117 |
else: |
|
118 |
self.Milliseconds.SetValue("0") |
|
119 |
self.Microseconds.SetValue("0") |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
120 |
|
814 | 121 |
def GetControlValueTestFunction(self, control): |
122 |
def OnValueChanged(event): |
|
123 |
try: |
|
1846
14b40afccd69
remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
124 |
float(control.GetValue()) |
14b40afccd69
remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
125 |
except ValueError: |
1745
f9d32913bad4
clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
126 |
message = wx.MessageDialog(self, _("Invalid value!\nYou must fill a numeric value."), _("Error"), wx.OK | wx.ICON_ERROR) |
814 | 127 |
message.ShowModal() |
128 |
message.Destroy() |
|
129 |
event.Skip() |
|
1702
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
130 |
self.OnCloseDialog() |
814 | 131 |
return OnValueChanged |
132 |
||
133 |
def GetDuration(self): |
|
134 |
milliseconds = 0 |
|
135 |
for control, factor in [(self.Days, DAY), (self.Hours, HOUR), |
|
136 |
(self.Minutes, MINUTE), (self.Seconds, SECOND), |
|
137 |
(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
|
138 |
|
814 | 139 |
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
|
140 |
|
814 | 141 |
not_null = False |
142 |
duration = "T#" |
|
2437
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2301
diff
changeset
|
143 |
for value, format in [((int(milliseconds) // DAY), "%dd"), |
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2301
diff
changeset
|
144 |
((int(milliseconds) % DAY) // HOUR, "%dh"), |
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2301
diff
changeset
|
145 |
((int(milliseconds) % HOUR) // MINUTE, "%dm"), |
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2301
diff
changeset
|
146 |
((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
|
147 |
|
814 | 148 |
if value > 0 or not_null: |
149 |
duration += format % value |
|
150 |
not_null = True |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
151 |
|
2235
d3751d43c390
Fix rounding of thousandths of microseconds in duration editor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2234
diff
changeset
|
152 |
duration += ("%f" % (milliseconds % SECOND)).rstrip("0").rstrip(".") + "ms" |
814 | 153 |
return duration |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
154 |
|
814 | 155 |
def OnOK(self, event): |
1702
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
156 |
self.OnCloseDialog() |
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
157 |
|
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
158 |
def OnCloseDialog(self): |
814 | 159 |
errors = [] |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
160 |
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
|
161 |
(self.Minutes, _("minutes")), (self.Seconds, _("seconds")), |
2234
3da336a9664c
Add checking value in microseconds field in duration editor dialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1881
diff
changeset
|
162 |
(self.Milliseconds, _("milliseconds")), |
3da336a9664c
Add checking value in microseconds field in duration editor dialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1881
diff
changeset
|
163 |
(self.Microseconds, _("microseconds"))]: |
814 | 164 |
try: |
1846
14b40afccd69
remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
165 |
float(control.GetValue()) |
14b40afccd69
remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
166 |
except ValueError: |
814 | 167 |
errors.append(name) |
168 |
if len(errors) > 0: |
|
169 |
if len(errors) == 1: |
|
170 |
message = _("Field %s hasn't a valid value!") % errors[0] |
|
171 |
else: |
|
172 |
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
|
173 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR) |
814 | 174 |
dialog.ShowModal() |
175 |
dialog.Destroy() |
|
176 |
else: |
|
177 |
self.EndModal(wx.ID_OK) |