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