author | Laurent Bessard |
Tue, 23 Apr 2013 12:50:53 +0200 | |
changeset 1048 | b450202605ab |
parent 961 | 069466072e9d |
child 1196 | d2f4061cdff5 |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import wx |
|
26 |
import wx.grid |
|
27 |
||
28 |
class CustomGrid(wx.grid.Grid): |
|
29 |
||
30 |
def __init__(self, *args, **kwargs): |
|
31 |
wx.grid.Grid.__init__(self, *args, **kwargs) |
|
32 |
||
33 |
self.Editable = True |
|
34 |
||
35 |
self.AddButton = None |
|
36 |
self.DeleteButton = None |
|
37 |
self.UpButton = None |
|
38 |
self.DownButton = None |
|
39 |
||
40 |
self.SetFont(wx.Font(12, 77, wx.NORMAL, wx.NORMAL, False, 'Sans')) |
|
41 |
self.SetLabelFont(wx.Font(10, 77, wx.NORMAL, wx.NORMAL, False, 'Sans')) |
|
42 |
self.SetSelectionBackground(wx.WHITE) |
|
43 |
self.SetSelectionForeground(wx.BLACK) |
|
44 |
self.DisableDragRowSize() |
|
45 |
||
46 |
self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnSelectCell) |
|
47 |
self.Bind(wx.grid.EVT_GRID_EDITOR_HIDDEN, self.OnEditorHidden) |
|
48 |
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
|
49 |
||
950
bf896711921c
Fixed bug when closing Beremiz and grid editor is shown on Windows
Laurent Bessard
parents:
814
diff
changeset
|
50 |
def SetFocus(self): |
bf896711921c
Fixed bug when closing Beremiz and grid editor is shown on Windows
Laurent Bessard
parents:
814
diff
changeset
|
51 |
if self: |
bf896711921c
Fixed bug when closing Beremiz and grid editor is shown on Windows
Laurent Bessard
parents:
814
diff
changeset
|
52 |
wx.grid.Grid.SetFocus(self) |
bf896711921c
Fixed bug when closing Beremiz and grid editor is shown on Windows
Laurent Bessard
parents:
814
diff
changeset
|
53 |
|
814 | 54 |
def SetDefaultValue(self, default_value): |
55 |
self.DefaultValue = default_value |
|
56 |
||
57 |
def SetEditable(self, editable=True): |
|
58 |
self.Editable = editable |
|
59 |
self.RefreshButtons() |
|
60 |
||
61 |
def SetButtons(self, buttons): |
|
62 |
for name in ["Add", "Delete", "Up", "Down"]: |
|
63 |
button = buttons.get(name, None) |
|
64 |
setattr(self, "%sButton" % name, button) |
|
65 |
if button is not None: |
|
66 |
button.Bind(wx.EVT_BUTTON, getattr(self, "On%sButton" % name)) |
|
67 |
||
68 |
def RefreshButtons(self): |
|
69 |
if self: |
|
70 |
rows = self.Table.GetNumberRows() |
|
71 |
row = self.GetGridCursorRow() |
|
72 |
if self.AddButton is not None: |
|
73 |
self.AddButton.Enable(self.Editable) |
|
74 |
if self.DeleteButton is not None: |
|
75 |
self.DeleteButton.Enable(self.Editable and rows > 0) |
|
76 |
if self.UpButton is not None: |
|
77 |
self.UpButton.Enable(self.Editable and row > 0) |
|
78 |
if self.DownButton is not None: |
|
79 |
self.DownButton.Enable(self.Editable and 0 <= row < rows - 1) |
|
80 |
||
81 |
def CloseEditControl(self): |
|
82 |
row, col = self.GetGridCursorRow(), self.GetGridCursorCol() |
|
83 |
if row != -1 and col != -1: |
|
84 |
self.SetGridCursor(row, col) |
|
85 |
||
86 |
def AddRow(self): |
|
87 |
self.CloseEditControl() |
|
88 |
new_row = self.GetGridCursorRow() + 1 |
|
89 |
col = max(self.GetGridCursorCol(), 0) |
|
90 |
if getattr(self, "_AddRow", None) is not None: |
|
91 |
new_row = self._AddRow(new_row) |
|
92 |
else: |
|
93 |
self.Table.InsertRow(new_row, self.DefaultValue.copy()) |
|
94 |
self.Table.ResetView(self) |
|
961 | 95 |
if new_row is not None: |
96 |
self.SetSelectedRow(new_row) |
|
814 | 97 |
|
98 |
def DeleteRow(self): |
|
99 |
self.CloseEditControl() |
|
100 |
row = self.GetGridCursorRow() |
|
101 |
if row >= 0: |
|
102 |
col = self.GetGridCursorCol() |
|
103 |
if getattr(self, "_DeleteRow", None) is not None: |
|
104 |
self._DeleteRow(row) |
|
105 |
else: |
|
106 |
self.Table.RemoveRow(row) |
|
107 |
self.Table.ResetView(self) |
|
961 | 108 |
self.SetSelectedRow(min(row, self.Table.GetNumberRows() - 1)) |
814 | 109 |
|
110 |
def MoveRow(self, row, move): |
|
111 |
self.CloseEditControl() |
|
112 |
col = self.GetGridCursorCol() |
|
113 |
if getattr(self, "_MoveRow", None) is not None: |
|
114 |
new_row = self._MoveRow(row, move) |
|
115 |
else: |
|
116 |
new_row = self.Table.MoveRow(row, move) |
|
117 |
if new_row != row: |
|
118 |
self.Table.ResetView(self) |
|
119 |
if new_row != row: |
|
961 | 120 |
self.SetSelectedRow(new_row) |
121 |
||
122 |
def SetSelectedRow(self, row): |
|
123 |
col = self.GetGridCursorCol() |
|
124 |
self.SetGridCursor(row, col) |
|
125 |
self.MakeCellVisible(row, col) |
|
126 |
self.RefreshButtons() |
|
127 |
||
814 | 128 |
def OnAddButton(self, event): |
129 |
self.AddRow() |
|
130 |
self.SetFocus() |
|
131 |
event.Skip() |
|
132 |
||
133 |
def OnDeleteButton(self, event): |
|
134 |
self.DeleteRow() |
|
135 |
self.SetFocus() |
|
136 |
event.Skip() |
|
137 |
||
138 |
def OnUpButton(self, event): |
|
139 |
self.MoveRow(self.GetGridCursorRow(), -1) |
|
140 |
self.SetFocus() |
|
141 |
event.Skip() |
|
142 |
||
143 |
def OnDownButton(self, event): |
|
144 |
self.MoveRow(self.GetGridCursorRow(), 1) |
|
145 |
self.SetFocus() |
|
146 |
event.Skip() |
|
147 |
||
148 |
def OnSelectCell(self, event): |
|
149 |
wx.CallAfter(self.RefreshButtons) |
|
150 |
event.Skip() |
|
151 |
||
152 |
def OnEditorHidden(self, event): |
|
153 |
wx.CallAfter(self.SetFocus) |
|
154 |
event.Skip() |
|
155 |
||
156 |
def OnKeyDown(self, event): |
|
157 |
key_handled = False |
|
158 |
keycode = event.GetKeyCode() |
|
159 |
if keycode == wx.WXK_TAB: |
|
160 |
row = self.GetGridCursorRow() |
|
161 |
col = self.GetGridCursorCol() |
|
162 |
if event.ShiftDown(): |
|
163 |
if row < 0 or col == 0: |
|
164 |
self.Navigate(wx.NavigationKeyEvent.IsBackward) |
|
165 |
key_handled = True |
|
166 |
elif row < 0 or col == self.Table.GetNumberCols() - 1: |
|
167 |
self.Navigate(wx.NavigationKeyEvent.IsForward) |
|
168 |
key_handled = True |
|
169 |
elif keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD) and self.Editable: |
|
170 |
self.AddRow() |
|
171 |
key_handled = True |
|
172 |
elif keycode in (wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE) and self.Editable: |
|
173 |
self.DeleteRow() |
|
174 |
key_handled = True |
|
175 |
elif keycode == wx.WXK_UP and event.ShiftDown() and self.Editable: |
|
176 |
self.MoveRow(self.GetGridCursorRow(), -1) |
|
177 |
key_handled = True |
|
178 |
elif keycode == wx.WXK_DOWN and event.ShiftDown() and self.Editable: |
|
179 |
self.MoveRow(self.GetGridCursorRow(), 1) |
|
180 |
key_handled = True |
|
181 |
if not key_handled: |
|
182 |
event.Skip() |