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