author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Mon, 14 Aug 2017 23:34:22 +0300 | |
changeset 1737 | a39c2918c015 |
parent 1736 | 7e61baa047f0 |
child 1739 | ec153828ded2 |
permissions | -rw-r--r-- |
814 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1487
diff
changeset
|
3 |
# 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:
1487
diff
changeset
|
4 |
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
814 | 5 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1487
diff
changeset
|
6 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
814 | 7 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1487
diff
changeset
|
8 |
# See COPYING file for copyrights details. |
814 | 9 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1487
diff
changeset
|
10 |
# 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:
1487
diff
changeset
|
11 |
# 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:
1487
diff
changeset
|
12 |
# 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:
1487
diff
changeset
|
13 |
# of the License, or (at your option) any later version. |
814 | 14 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1487
diff
changeset
|
15 |
# 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:
1487
diff
changeset
|
16 |
# 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:
1487
diff
changeset
|
17 |
# 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:
1487
diff
changeset
|
18 |
# GNU General Public License for more details. |
814 | 19 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1487
diff
changeset
|
20 |
# 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:
1487
diff
changeset
|
21 |
# 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:
1487
diff
changeset
|
22 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 23 |
|
24 |
import re |
|
25 |
from types import TupleType |
|
26 |
||
27 |
import wx |
|
28 |
||
29 |
from controls import CustomEditableListBox |
|
30 |
||
31 |
#------------------------------------------------------------------------------- |
|
32 |
# Helpers |
|
33 |
#------------------------------------------------------------------------------- |
|
34 |
||
35 |
DIMENSION_MODEL = re.compile("([0-9]+)\.\.([0-9]+)$") |
|
36 |
||
37 |
#------------------------------------------------------------------------------- |
|
38 |
# Array Type Dialog |
|
39 |
#------------------------------------------------------------------------------- |
|
40 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
41 |
|
814 | 42 |
class ArrayTypeDialog(wx.Dialog): |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
43 |
|
814 | 44 |
def __init__(self, parent, datatypes, infos): |
1694
acb767d6ac85
make adjustable size of ArrayTypeDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
45 |
wx.Dialog.__init__(self, parent, title=_('Edit array type properties')) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
46 |
|
814 | 47 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) |
48 |
main_sizer.AddGrowableCol(0) |
|
49 |
main_sizer.AddGrowableRow(1) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
50 |
|
814 | 51 |
top_sizer = wx.BoxSizer(wx.HORIZONTAL) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
52 |
main_sizer.AddSizer(top_sizer, border=20, |
814 | 53 |
flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
54 |
|
814 | 55 |
basetype_label = wx.StaticText(self, label=_('Base Type:')) |
56 |
top_sizer.AddWindow(basetype_label, 1, flag=wx.ALIGN_BOTTOM) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
57 |
|
814 | 58 |
self.BaseType = wx.ComboBox(self, style=wx.CB_READONLY) |
59 |
top_sizer.AddWindow(self.BaseType, 1, flag=wx.GROW) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
60 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
61 |
self.Dimensions = CustomEditableListBox(self, label=_("Dimensions:"), |
814 | 62 |
style=wx.gizmos.EL_ALLOW_NEW| |
63 |
wx.gizmos.EL_ALLOW_EDIT| |
|
64 |
wx.gizmos.EL_ALLOW_DELETE) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
65 |
for func in ["_OnLabelEndEdit", |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
66 |
"_OnAddButton", |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
67 |
"_OnDelButton", |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
68 |
"_OnUpButton", |
814 | 69 |
"_OnDownButton"]: |
70 |
setattr(self.Dimensions, func, self.OnDimensionsChanged) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
71 |
main_sizer.AddSizer(self.Dimensions, border=20, |
814 | 72 |
flag=wx.GROW|wx.LEFT|wx.RIGHT) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
73 |
|
814 | 74 |
button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
75 |
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:
1726
diff
changeset
|
76 |
main_sizer.AddSizer(button_sizer, border=20, |
814 | 77 |
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:
1726
diff
changeset
|
78 |
|
814 | 79 |
self.SetSizer(main_sizer) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
80 |
|
814 | 81 |
for datatype in datatypes: |
82 |
self.BaseType.Append(datatype) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
83 |
|
814 | 84 |
if isinstance(infos, TupleType) and infos[0] == "array": |
85 |
self.BaseType.SetStringSelection(infos[1]) |
|
86 |
self.Dimensions.SetStrings(map(lambda x : "..".join(x), infos[2])) |
|
87 |
elif infos in datatypes: |
|
88 |
self.BaseType.SetStringSelection(infos) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
89 |
|
814 | 90 |
self.BaseType.SetFocus() |
1694
acb767d6ac85
make adjustable size of ArrayTypeDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
91 |
self.Fit() |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
92 |
|
814 | 93 |
def GetDimensions(self): |
1726
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
94 |
message = None |
814 | 95 |
dimensions_list = [] |
1726
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
96 |
dimension_strings = self.Dimensions.GetStrings() |
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
97 |
if len(dimension_strings) == 0: |
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
98 |
message = _("Empty dimension isn't allowed.") |
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
99 |
|
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
100 |
for dimensions in dimension_strings: |
814 | 101 |
result = DIMENSION_MODEL.match(dimensions) |
102 |
if result is None: |
|
1734
750eeb7230a1
clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
103 |
message = _("\"%s\" value isn't a valid array dimension!") % dimensions |
1726
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
104 |
break |
814 | 105 |
bounds = result.groups() |
106 |
if int(bounds[0]) >= int(bounds[1]): |
|
1734
750eeb7230a1
clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
107 |
message = _("\"%s\" value isn't a valid array dimension!\nRight value must be greater than left value.") % dimensions |
1726
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
108 |
break |
814 | 109 |
dimensions_list.append(bounds) |
1726
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
110 |
|
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
111 |
if message is not None: |
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
112 |
dlg = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) |
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
113 |
dlg.ShowModal() |
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
114 |
dlg.Destroy() |
d51af006fa6b
don't allow empty dimension for ARRAY types
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1694
diff
changeset
|
115 |
return None |
814 | 116 |
return dimensions_list |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
117 |
|
814 | 118 |
def OnDimensionsChanged(self, event): |
119 |
wx.CallAfter(self.GetDimensions) |
|
120 |
event.Skip() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
121 |
|
814 | 122 |
def OnOK(self, event): |
123 |
if self.GetDimensions() is not None: |
|
124 |
self.EndModal(wx.ID_OK) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1726
diff
changeset
|
125 |
|
814 | 126 |
def GetValue(self): |
127 |
return "array", self.BaseType.GetStringSelection(), self.GetDimensions() |