author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Tue, 15 Aug 2017 17:01:51 +0300 | |
changeset 1743 | c3c3d1318130 |
parent 1736 | 7e61baa047f0 |
child 1745 | f9d32913bad4 |
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
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:
1550
diff
changeset
|
23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 24 |
|
25 |
import wx |
|
26 |
||
27 |
from dialogs.BrowseLocationsDialog import BrowseLocationsDialog |
|
28 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
29 |
|
814 | 30 |
class LocationCellControl(wx.PyControl): |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
31 |
|
814 | 32 |
''' |
33 |
Custom cell editor control with a text box and a button that launches |
|
34 |
the BrowseLocationsDialog. |
|
35 |
''' |
|
36 |
def __init__(self, parent): |
|
37 |
wx.Control.__init__(self, parent) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
38 |
|
814 | 39 |
main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0) |
40 |
main_sizer.AddGrowableCol(0) |
|
41 |
main_sizer.AddGrowableRow(0) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
42 |
|
814 | 43 |
# create location text control |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
44 |
self.Location = wx.TextCtrl(self, size=wx.Size(0, -1), |
814 | 45 |
style=wx.TE_PROCESS_ENTER) |
46 |
self.Location.Bind(wx.EVT_KEY_DOWN, self.OnLocationChar) |
|
47 |
main_sizer.AddWindow(self.Location, flag=wx.GROW) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
48 |
|
814 | 49 |
# create browse button |
50 |
self.BrowseButton = wx.Button(self, label='...', size=wx.Size(30, -1)) |
|
51 |
self.BrowseButton.Bind(wx.EVT_BUTTON, self.OnBrowseButtonClick) |
|
52 |
main_sizer.AddWindow(self.BrowseButton, flag=wx.GROW) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
53 |
|
814 | 54 |
self.Bind(wx.EVT_SIZE, self.OnSize) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
55 |
|
814 | 56 |
self.SetSizer(main_sizer) |
57 |
||
855
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
58 |
self.Controller = None |
814 | 59 |
self.VarType = None |
60 |
self.Default = False |
|
61 |
||
855
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
62 |
def __del__(self): |
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
63 |
self.Controller = None |
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
64 |
|
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
65 |
def SetController(self, controller): |
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
66 |
self.Controller = controller |
814 | 67 |
|
68 |
def SetVarType(self, vartype): |
|
69 |
self.VarType = vartype |
|
70 |
||
855
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
71 |
def GetVarType(self): |
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
72 |
return self.VarType |
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
73 |
|
814 | 74 |
def SetValue(self, value): |
75 |
self.Default = value |
|
76 |
self.Location.SetValue(value) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
77 |
|
814 | 78 |
def GetValue(self): |
79 |
return self.Location.GetValue() |
|
80 |
||
81 |
def OnSize(self, event): |
|
82 |
self.Layout() |
|
83 |
||
84 |
def OnBrowseButtonClick(self, event): |
|
85 |
# pop up the location browser dialog |
|
855
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
86 |
dialog = BrowseLocationsDialog(self, self.VarType, self.Controller) |
814 | 87 |
if dialog.ShowModal() == wx.ID_OK: |
88 |
infos = dialog.GetValues() |
|
1004
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
89 |
else: |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
90 |
infos = None |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
91 |
dialog.Destroy() |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
92 |
|
1004
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
93 |
if infos is not None: |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
94 |
location = infos["location"] |
814 | 95 |
# set the location |
1004
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
96 |
if not infos["location"].startswith("%"): |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
97 |
dialog = wx.SingleChoiceDialog(self, |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
98 |
_("Select a variable class:"), _("Variable class"), |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
99 |
[_("Input"), _("Output"), _("Memory")], |
1004
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
100 |
wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL) |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
101 |
if dialog.ShowModal() == wx.ID_OK: |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
102 |
selected = dialog.GetSelection() |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
103 |
else: |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
104 |
selected = None |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
105 |
dialog.Destroy() |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
106 |
if selected is None: |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
107 |
self.Location.SetFocus() |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
108 |
return |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
109 |
if selected == 0: |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
110 |
location = "%I" + location |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
111 |
elif selected == 1: |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
112 |
location = "%Q" + location |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
113 |
else: |
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
114 |
location = "%M" + location |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
115 |
|
1004
bf3b6998f7b6
Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents:
855
diff
changeset
|
116 |
self.Location.SetValue(location) |
855
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
117 |
self.VarType = infos["IEC_type"] |
814 | 118 |
|
119 |
self.Location.SetFocus() |
|
120 |
||
121 |
def OnLocationChar(self, event): |
|
122 |
keycode = event.GetKeyCode() |
|
123 |
if keycode in [wx.WXK_RETURN, wx.WXK_TAB]: |
|
124 |
self.Parent.Parent.ProcessEvent(event) |
|
125 |
elif keycode == wx.WXK_ESCAPE: |
|
126 |
self.Location.SetValue(self.Default) |
|
127 |
self.Parent.Parent.CloseEditControl() |
|
128 |
else: |
|
129 |
event.Skip() |
|
130 |
||
131 |
def SetInsertionPoint(self, i): |
|
132 |
self.Location.SetInsertionPoint(i) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
133 |
|
814 | 134 |
def SetFocus(self): |
135 |
self.Location.SetFocus() |
|
136 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
137 |
|
814 | 138 |
class LocationCellEditor(wx.grid.PyGridCellEditor): |
139 |
''' |
|
140 |
Grid cell editor that uses LocationCellControl to display a browse button. |
|
141 |
''' |
|
142 |
def __init__(self, table, controller): |
|
143 |
wx.grid.PyGridCellEditor.__init__(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
144 |
|
814 | 145 |
self.Table = table |
146 |
self.Controller = controller |
|
147 |
||
148 |
def __del__(self): |
|
149 |
self.CellControl = None |
|
855
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
150 |
self.Controller = None |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
151 |
|
814 | 152 |
def Create(self, parent, id, evt_handler): |
153 |
self.CellControl = LocationCellControl(parent) |
|
154 |
self.SetControl(self.CellControl) |
|
155 |
if evt_handler: |
|
156 |
self.CellControl.PushEventHandler(evt_handler) |
|
157 |
||
158 |
def BeginEdit(self, row, col, grid): |
|
159 |
self.CellControl.Enable() |
|
855
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
160 |
self.CellControl.SetController(self.Controller) |
814 | 161 |
self.CellControl.SetValue(self.Table.GetValueByName(row, 'Location')) |
162 |
if isinstance(self.CellControl, LocationCellControl): |
|
855
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
163 |
self.CellControl.SetVarType(self.Table.GetValueByName(row, 'Type')) |
814 | 164 |
self.CellControl.SetFocus() |
165 |
||
1536
3518d10487db
fix issue, that was impossible to change variable location IEC-address, when wx 3.0 is used
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1004
diff
changeset
|
166 |
def EndEditInternal(self, row, col, grid, old_loc): |
814 | 167 |
loc = self.CellControl.GetValue() |
168 |
changed = loc != old_loc |
|
169 |
if changed: |
|
170 |
self.Table.SetValueByName(row, 'Location', loc) |
|
855
b30421d07e8c
Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents:
814
diff
changeset
|
171 |
self.Table.SetValueByName(row, 'Type', self.CellControl.GetVarType()) |
814 | 172 |
self.CellControl.Disable() |
173 |
return changed |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
174 |
|
1536
3518d10487db
fix issue, that was impossible to change variable location IEC-address, when wx 3.0 is used
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1004
diff
changeset
|
175 |
if wx.VERSION >= (3, 0, 0): |
3518d10487db
fix issue, that was impossible to change variable location IEC-address, when wx 3.0 is used
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1004
diff
changeset
|
176 |
def EndEdit(self, row, col, grid, oldval): |
1550
deadfe7377e9
fix not saving location address from variable panel
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1540
diff
changeset
|
177 |
return self.EndEditInternal(row, col, grid, oldval) |
1536
3518d10487db
fix issue, that was impossible to change variable location IEC-address, when wx 3.0 is used
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1004
diff
changeset
|
178 |
else: |
3518d10487db
fix issue, that was impossible to change variable location IEC-address, when wx 3.0 is used
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1004
diff
changeset
|
179 |
def EndEdit(self, row, col, grid): |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
180 |
old_loc = self.Table.GetValueByName(row, 'Location') |
1550
deadfe7377e9
fix not saving location address from variable panel
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1540
diff
changeset
|
181 |
return self.EndEditInternal(row, col, grid, old_loc) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1578
diff
changeset
|
182 |
|
814 | 183 |
def SetSize(self, rect): |
184 |
self.CellControl.SetDimensions(rect.x + 1, rect.y, |
|
185 |
rect.width, rect.height, |
|
186 |
wx.SIZE_ALLOW_MINUS_ONE) |
|
187 |
||
188 |
def Clone(self): |
|
189 |
return LocationCellEditor(self.Table, self.Controller) |