author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Fri, 01 Oct 2021 15:32:38 +0200 | |
branch | wxPython4 |
changeset 3324 | 13779d34293b |
parent 1881 | 091005ec69c4 |
child 3750 | f62625418bff |
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
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:
1568
diff
changeset
|
23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 24 |
|
1881
091005ec69c4
fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
25 |
|
091005ec69c4
fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
26 |
from __future__ import absolute_import |
814 | 27 |
import wx |
28 |
import wx.grid |
|
29 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1735
diff
changeset
|
30 |
|
814 | 31 |
class CustomGrid(wx.grid.Grid): |
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
32 |
|
814 | 33 |
def __init__(self, *args, **kwargs): |
34 |
wx.grid.Grid.__init__(self, *args, **kwargs) |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
35 |
|
814 | 36 |
self.Editable = True |
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
37 |
|
814 | 38 |
self.AddButton = None |
39 |
self.DeleteButton = None |
|
40 |
self.UpButton = None |
|
41 |
self.DownButton = None |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
42 |
|
1568
ad7c7df6cbeb
fix wrong font family in CustromGrid control
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1197
diff
changeset
|
43 |
self.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, 'Sans')) |
ad7c7df6cbeb
fix wrong font family in CustromGrid control
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1197
diff
changeset
|
44 |
self.SetLabelFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, 'Sans')) |
814 | 45 |
self.SetSelectionBackground(wx.WHITE) |
46 |
self.SetSelectionForeground(wx.BLACK) |
|
47 |
self.DisableDragRowSize() |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
48 |
|
814 | 49 |
self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnSelectCell) |
50 |
self.Bind(wx.grid.EVT_GRID_EDITOR_HIDDEN, self.OnEditorHidden) |
|
51 |
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
52 |
|
950
bf896711921c
Fixed bug when closing Beremiz and grid editor is shown on Windows
Laurent Bessard
parents:
814
diff
changeset
|
53 |
def SetFocus(self): |
bf896711921c
Fixed bug when closing Beremiz and grid editor is shown on Windows
Laurent Bessard
parents:
814
diff
changeset
|
54 |
if self: |
bf896711921c
Fixed bug when closing Beremiz and grid editor is shown on Windows
Laurent Bessard
parents:
814
diff
changeset
|
55 |
wx.grid.Grid.SetFocus(self) |
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
56 |
|
814 | 57 |
def SetDefaultValue(self, default_value): |
58 |
self.DefaultValue = default_value |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
59 |
|
814 | 60 |
def SetEditable(self, editable=True): |
61 |
self.Editable = editable |
|
62 |
self.RefreshButtons() |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
63 |
|
814 | 64 |
def SetButtons(self, buttons): |
65 |
for name in ["Add", "Delete", "Up", "Down"]: |
|
66 |
button = buttons.get(name, None) |
|
67 |
setattr(self, "%sButton" % name, button) |
|
68 |
if button is not None: |
|
69 |
button.Bind(wx.EVT_BUTTON, getattr(self, "On%sButton" % name)) |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
70 |
|
814 | 71 |
def RefreshButtons(self): |
72 |
if self: |
|
73 |
rows = self.Table.GetNumberRows() |
|
74 |
row = self.GetGridCursorRow() |
|
75 |
if self.AddButton is not None: |
|
76 |
self.AddButton.Enable(self.Editable) |
|
77 |
if self.DeleteButton is not None: |
|
78 |
self.DeleteButton.Enable(self.Editable and rows > 0) |
|
79 |
if self.UpButton is not None: |
|
80 |
self.UpButton.Enable(self.Editable and row > 0) |
|
81 |
if self.DownButton is not None: |
|
82 |
self.DownButton.Enable(self.Editable and 0 <= row < rows - 1) |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
83 |
|
814 | 84 |
def CloseEditControl(self): |
85 |
row, col = self.GetGridCursorRow(), self.GetGridCursorCol() |
|
86 |
if row != -1 and col != -1: |
|
87 |
self.SetGridCursor(row, col) |
|
88 |
||
89 |
def AddRow(self): |
|
90 |
self.CloseEditControl() |
|
91 |
new_row = self.GetGridCursorRow() + 1 |
|
92 |
col = max(self.GetGridCursorCol(), 0) |
|
93 |
if getattr(self, "_AddRow", None) is not None: |
|
94 |
new_row = self._AddRow(new_row) |
|
95 |
else: |
|
96 |
self.Table.InsertRow(new_row, self.DefaultValue.copy()) |
|
97 |
self.Table.ResetView(self) |
|
961 | 98 |
if new_row is not None: |
1196
d2f4061cdff5
Fixed bug in when deleting row in CustomGrid
Laurent Bessard
parents:
961
diff
changeset
|
99 |
self.SetSelectedCell(new_row, col) |
814 | 100 |
|
101 |
def DeleteRow(self): |
|
102 |
self.CloseEditControl() |
|
103 |
row = self.GetGridCursorRow() |
|
104 |
if row >= 0: |
|
105 |
col = self.GetGridCursorCol() |
|
106 |
if getattr(self, "_DeleteRow", None) is not None: |
|
107 |
self._DeleteRow(row) |
|
108 |
else: |
|
109 |
self.Table.RemoveRow(row) |
|
110 |
self.Table.ResetView(self) |
|
1196
d2f4061cdff5
Fixed bug in when deleting row in CustomGrid
Laurent Bessard
parents:
961
diff
changeset
|
111 |
if self.Table.GetNumberRows() > 0: |
d2f4061cdff5
Fixed bug in when deleting row in CustomGrid
Laurent Bessard
parents:
961
diff
changeset
|
112 |
self.SetSelectedCell(min(row, self.Table.GetNumberRows() - 1), col) |
814 | 113 |
|
114 |
def MoveRow(self, row, move): |
|
115 |
self.CloseEditControl() |
|
116 |
col = self.GetGridCursorCol() |
|
117 |
if getattr(self, "_MoveRow", None) is not None: |
|
118 |
new_row = self._MoveRow(row, move) |
|
119 |
else: |
|
120 |
new_row = self.Table.MoveRow(row, move) |
|
121 |
if new_row != row: |
|
122 |
self.Table.ResetView(self) |
|
123 |
if new_row != row: |
|
1197
eb118ed5cbe0
Fixed bug in when deleting row in CustomGrid
Laurent Bessard
parents:
1196
diff
changeset
|
124 |
self.SetSelectedCell(new_row, col) |
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
125 |
|
1196
d2f4061cdff5
Fixed bug in when deleting row in CustomGrid
Laurent Bessard
parents:
961
diff
changeset
|
126 |
def SetSelectedCell(self, row, col): |
961 | 127 |
self.SetGridCursor(row, col) |
128 |
self.MakeCellVisible(row, col) |
|
129 |
self.RefreshButtons() |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
130 |
|
814 | 131 |
def OnAddButton(self, event): |
132 |
self.AddRow() |
|
133 |
self.SetFocus() |
|
134 |
event.Skip() |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
135 |
|
814 | 136 |
def OnDeleteButton(self, event): |
137 |
self.DeleteRow() |
|
138 |
self.SetFocus() |
|
139 |
event.Skip() |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
140 |
|
814 | 141 |
def OnUpButton(self, event): |
142 |
self.MoveRow(self.GetGridCursorRow(), -1) |
|
143 |
self.SetFocus() |
|
144 |
event.Skip() |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
145 |
|
814 | 146 |
def OnDownButton(self, event): |
147 |
self.MoveRow(self.GetGridCursorRow(), 1) |
|
148 |
self.SetFocus() |
|
149 |
event.Skip() |
|
150 |
||
151 |
def OnSelectCell(self, event): |
|
152 |
wx.CallAfter(self.RefreshButtons) |
|
153 |
event.Skip() |
|
154 |
||
155 |
def OnEditorHidden(self, event): |
|
156 |
wx.CallAfter(self.SetFocus) |
|
157 |
event.Skip() |
|
158 |
||
159 |
def OnKeyDown(self, event): |
|
160 |
key_handled = False |
|
161 |
keycode = event.GetKeyCode() |
|
162 |
if keycode == wx.WXK_TAB: |
|
163 |
row = self.GetGridCursorRow() |
|
164 |
col = self.GetGridCursorCol() |
|
165 |
if event.ShiftDown(): |
|
166 |
if row < 0 or col == 0: |
|
167 |
self.Navigate(wx.NavigationKeyEvent.IsBackward) |
|
168 |
key_handled = True |
|
169 |
elif row < 0 or col == self.Table.GetNumberCols() - 1: |
|
170 |
self.Navigate(wx.NavigationKeyEvent.IsForward) |
|
171 |
key_handled = True |
|
172 |
elif keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD) and self.Editable: |
|
173 |
self.AddRow() |
|
174 |
key_handled = True |
|
175 |
elif keycode in (wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE) and self.Editable: |
|
176 |
self.DeleteRow() |
|
177 |
key_handled = True |
|
178 |
elif keycode == wx.WXK_UP and event.ShiftDown() and self.Editable: |
|
179 |
self.MoveRow(self.GetGridCursorRow(), -1) |
|
180 |
key_handled = True |
|
181 |
elif keycode == wx.WXK_DOWN and event.ShiftDown() and self.Editable: |
|
182 |
self.MoveRow(self.GetGridCursorRow(), 1) |
|
183 |
key_handled = True |
|
184 |
if not key_handled: |
|
185 |
event.Skip() |