author | Edouard Tisserant |
Fri, 06 Jul 2018 15:07:40 +0200 | |
branch | nevow_service_rework |
changeset 2217 | a603d1ba287b |
parent 1881 | 091005ec69c4 |
child 2234 | 3da336a9664c |
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 |
814 | 28 |
import re |
29 |
||
30 |
import wx |
|
31 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
32 |
# ------------------------------------------------------------------------------- |
814 | 33 |
# Helpers |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
34 |
# ------------------------------------------------------------------------------- |
814 | 35 |
|
36 |
MICROSECONDS = 0.001 |
|
37 |
MILLISECONDS = 1 |
|
38 |
SECOND = 1000 |
|
39 |
MINUTE = 60 * SECOND |
|
40 |
HOUR = 60 * MINUTE |
|
41 |
DAY = 24 * HOUR |
|
42 |
||
879
55b8a16ead2b
Fix resource task interval error highlighting
Laurent Bessard
parents:
831
diff
changeset
|
43 |
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 | 44 |
|
45 |
CONTROLS = [ |
|
46 |
("Days", _('Days:')), |
|
47 |
("Hours", _('Hours:')), |
|
48 |
("Minutes", _('Minutes:')), |
|
49 |
("Seconds", _('Seconds:')), |
|
50 |
("Milliseconds", _('Milliseconds:')), |
|
51 |
("Microseconds", _('Microseconds:')), |
|
52 |
] |
|
53 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
54 |
# ------------------------------------------------------------------------------- |
814 | 55 |
# 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
|
56 |
# ------------------------------------------------------------------------------- |
814 | 57 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
58 |
|
814 | 59 |
class DurationEditorDialog(wx.Dialog): |
60 |
||
61 |
def __init__(self, parent): |
|
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
62 |
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
|
63 |
|
814 | 64 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) |
65 |
main_sizer.AddGrowableCol(0) |
|
66 |
main_sizer.AddGrowableRow(0) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
67 |
|
814 | 68 |
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
|
69 |
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
|
70 |
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
|
71 |
|
814 | 72 |
controls = [] |
73 |
for i, (name, label) in enumerate(CONTROLS): |
|
74 |
controls_sizer.AddGrowableCol(i) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
75 |
|
814 | 76 |
st = wx.StaticText(self, label=label) |
77 |
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
|
78 |
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
|
79 |
self.GetControlValueTestFunction(txtctrl), |
814 | 80 |
txtctrl) |
81 |
setattr(self, name, txtctrl) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
82 |
|
814 | 83 |
controls.append((st, txtctrl)) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
84 |
|
814 | 85 |
for st, txtctrl in controls: |
86 |
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
|
87 |
|
814 | 88 |
for st, txtctrl in controls: |
89 |
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
|
90 |
|
1745
f9d32913bad4
clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
91 |
button_sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE) |
814 | 92 |
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
|
93 |
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
|
94 |
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
|
95 |
|
814 | 96 |
self.SetSizer(main_sizer) |
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
97 |
self.Fit() |
814 | 98 |
self.Days.SetFocus() |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
99 |
|
814 | 100 |
def SetDuration(self, value): |
101 |
result = IEC_TIME_MODEL.match(value.upper()) |
|
102 |
if result is not None: |
|
103 |
values = result.groups() |
|
104 |
for control, index in [(self.Days, 1), (self.Hours, 2), |
|
105 |
(self.Minutes, 3), (self.Seconds, 4)]: |
|
106 |
value = values[index] |
|
107 |
if value is not None: |
|
108 |
control.SetValue(value) |
|
109 |
else: |
|
110 |
control.SetValue("0") |
|
111 |
milliseconds = values[5] |
|
112 |
if milliseconds is not None: |
|
113 |
self.Milliseconds.SetValue("%d" % int(float(milliseconds))) |
|
114 |
self.Microseconds.SetValue("%.3f" % ((float(milliseconds) % MILLISECONDS) / MICROSECONDS)) |
|
115 |
else: |
|
116 |
self.Milliseconds.SetValue("0") |
|
117 |
self.Microseconds.SetValue("0") |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
118 |
|
814 | 119 |
def GetControlValueTestFunction(self, control): |
120 |
def OnValueChanged(event): |
|
121 |
try: |
|
1846
14b40afccd69
remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
122 |
float(control.GetValue()) |
14b40afccd69
remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
123 |
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
|
124 |
message = wx.MessageDialog(self, _("Invalid value!\nYou must fill a numeric value."), _("Error"), wx.OK | wx.ICON_ERROR) |
814 | 125 |
message.ShowModal() |
126 |
message.Destroy() |
|
127 |
event.Skip() |
|
1702
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
128 |
self.OnCloseDialog() |
814 | 129 |
return OnValueChanged |
130 |
||
131 |
def GetDuration(self): |
|
132 |
milliseconds = 0 |
|
133 |
for control, factor in [(self.Days, DAY), (self.Hours, HOUR), |
|
134 |
(self.Minutes, MINUTE), (self.Seconds, SECOND), |
|
135 |
(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
|
136 |
|
814 | 137 |
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
|
138 |
|
814 | 139 |
not_null = False |
140 |
duration = "T#" |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1745
diff
changeset
|
141 |
for value, format in [((int(milliseconds) / DAY), "%dd"), |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1745
diff
changeset
|
142 |
((int(milliseconds) % DAY) / HOUR, "%dh"), |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1745
diff
changeset
|
143 |
((int(milliseconds) % HOUR) / MINUTE, "%dm"), |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1745
diff
changeset
|
144 |
((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
|
145 |
|
814 | 146 |
if value > 0 or not_null: |
147 |
duration += format % value |
|
148 |
not_null = True |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
149 |
|
814 | 150 |
duration += "%gms" % (milliseconds % SECOND) |
151 |
return duration |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
152 |
|
814 | 153 |
def OnOK(self, event): |
1702
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
154 |
self.OnCloseDialog() |
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
155 |
|
bbb2c38969ca
close DurationEditorDialog on ENTER key
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1696
diff
changeset
|
156 |
def OnCloseDialog(self): |
814 | 157 |
errors = [] |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1702
diff
changeset
|
158 |
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
|
159 |
(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
|
160 |
(self.Milliseconds, _("milliseconds"))]: |
814 | 161 |
try: |
1846
14b40afccd69
remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
162 |
float(control.GetValue()) |
14b40afccd69
remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
163 |
except ValueError: |
814 | 164 |
errors.append(name) |
165 |
if len(errors) > 0: |
|
166 |
if len(errors) == 1: |
|
167 |
message = _("Field %s hasn't a valid value!") % errors[0] |
|
168 |
else: |
|
169 |
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
|
170 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR) |
814 | 171 |
dialog.ShowModal() |
172 |
dialog.Destroy() |
|
173 |
else: |
|
174 |
self.EndModal(wx.ID_OK) |