author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 11 Oct 2018 09:41:15 +0300 | |
changeset 2447 | 1c04a50dc7ff |
parent 1881 | 091005ec69c4 |
child 3303 | 0ffb41625592 |
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:
1342
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:
1342
diff
changeset
|
5 |
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1342
diff
changeset
|
6 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1342
diff
changeset
|
7 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1342
diff
changeset
|
8 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1342
diff
changeset
|
9 |
# See COPYING file for copyrights details. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1342
diff
changeset
|
10 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1342
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:
1342
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:
1342
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:
1342
diff
changeset
|
14 |
# of the License, or (at your option) any later version. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1342
diff
changeset
|
15 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1342
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:
1342
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:
1342
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:
1342
diff
changeset
|
19 |
# GNU General Public License for more details. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1342
diff
changeset
|
20 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1342
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:
1342
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:
1342
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:
1878
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:
1878
diff
changeset
|
26 |
from __future__ import absolute_import |
814 | 27 |
import wx |
28 |
import wx.grid |
|
29 |
||
1342
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
30 |
if wx.Platform == '__WXMSW__': |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
31 |
ROW_HEIGHT = 20 |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
32 |
else: |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
33 |
ROW_HEIGHT = 28 |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
34 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1735
diff
changeset
|
35 |
|
814 | 36 |
class CustomTable(wx.grid.PyGridTableBase): |
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
37 |
|
814 | 38 |
""" |
39 |
A custom wx.grid.Grid Table using user supplied data |
|
40 |
""" |
|
41 |
def __init__(self, parent, data, colnames): |
|
42 |
# The base class must be initialized *first* |
|
43 |
wx.grid.PyGridTableBase.__init__(self) |
|
44 |
self.data = data |
|
45 |
self.colnames = colnames |
|
46 |
self.Highlights = {} |
|
47 |
self.Parent = parent |
|
48 |
# XXX |
|
49 |
# we need to store the row length and collength to |
|
50 |
# see if the table has changed size |
|
51 |
self._rows = self.GetNumberRows() |
|
52 |
self._cols = self.GetNumberCols() |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
53 |
|
814 | 54 |
def GetNumberCols(self): |
55 |
return len(self.colnames) |
|
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 GetNumberRows(self): |
58 |
return len(self.data) |
|
59 |
||
60 |
def GetColLabelValue(self, col, translate=True): |
|
61 |
if col < len(self.colnames): |
|
62 |
if translate: |
|
63 |
return _(self.colnames[col]) |
|
64 |
return self.colnames[col] |
|
65 |
||
66 |
def GetRowLabelValue(self, row, translate=True): |
|
67 |
return row |
|
68 |
||
69 |
def GetValue(self, row, col): |
|
70 |
if row < self.GetNumberRows(): |
|
71 |
return self.data[row].get(self.GetColLabelValue(col, False), "") |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
72 |
|
814 | 73 |
def SetValue(self, row, col, value): |
74 |
if col < len(self.colnames): |
|
75 |
self.data[row][self.GetColLabelValue(col, False)] = value |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
76 |
|
814 | 77 |
def GetValueByName(self, row, colname): |
78 |
if row < self.GetNumberRows(): |
|
79 |
return self.data[row].get(colname) |
|
80 |
||
81 |
def SetValueByName(self, row, colname, value): |
|
82 |
if row < self.GetNumberRows(): |
|
83 |
self.data[row][colname] = value |
|
84 |
||
85 |
def ResetView(self, grid): |
|
86 |
""" |
|
87 |
(wx.grid.Grid) -> Reset the grid view. Call this to |
|
88 |
update the grid if rows and columns have been added or deleted |
|
89 |
""" |
|
90 |
grid.CloseEditControl() |
|
91 |
grid.BeginBatch() |
|
92 |
for current, new, delmsg, addmsg in [ |
|
1878
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
93 |
( |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
94 |
self._rows, |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
95 |
self.GetNumberRows(), |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
96 |
wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
97 |
wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
98 |
), |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
99 |
( |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
100 |
self._cols, |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
101 |
self.GetNumberCols(), |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
102 |
wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
103 |
wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
104 |
), |
814 | 105 |
]: |
106 |
if new < current: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
107 |
msg = wx.grid.GridTableMessage(self, delmsg, new, current-new) |
814 | 108 |
grid.ProcessTableMessage(msg) |
109 |
elif new > current: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
110 |
msg = wx.grid.GridTableMessage(self, addmsg, new-current) |
814 | 111 |
grid.ProcessTableMessage(msg) |
112 |
self.UpdateValues(grid) |
|
113 |
grid.EndBatch() |
|
114 |
||
115 |
self._rows = self.GetNumberRows() |
|
116 |
self._cols = self.GetNumberCols() |
|
117 |
# update the column rendering scheme |
|
118 |
self._updateColAttrs(grid) |
|
119 |
||
120 |
# update the scrollbars and the displayed part of the grid |
|
121 |
grid.AdjustScrollbars() |
|
122 |
grid.ForceRefresh() |
|
123 |
||
124 |
def UpdateValues(self, grid): |
|
125 |
"""Update all displayed values""" |
|
126 |
# This sends an event to the grid table to update all of the values |
|
127 |
msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES) |
|
128 |
grid.ProcessTableMessage(msg) |
|
129 |
||
130 |
def _updateColAttrs(self, grid): |
|
131 |
""" |
|
132 |
wx.grid.Grid -> update the column attributes to add the |
|
133 |
appropriate renderer given the column name. |
|
134 |
||
135 |
Otherwise default to the default renderer. |
|
136 |
""" |
|
137 |
for row in range(self.GetNumberRows()): |
|
138 |
row_highlights = self.Highlights.get(row, {}) |
|
139 |
for col in range(self.GetNumberCols()): |
|
140 |
colname = self.GetColLabelValue(col, False) |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
141 |
|
814 | 142 |
grid.SetReadOnly(row, col, True) |
143 |
grid.SetCellEditor(row, col, None) |
|
144 |
grid.SetCellRenderer(row, col, None) |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
145 |
|
814 | 146 |
highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1] |
147 |
grid.SetCellBackgroundColour(row, col, highlight_colours[0]) |
|
148 |
grid.SetCellTextColour(row, col, highlight_colours[1]) |
|
149 |
self.ResizeRow(grid, row) |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
150 |
|
814 | 151 |
def ResizeRow(self, grid, row): |
1342
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
152 |
if grid.GetRowSize(row) < ROW_HEIGHT: |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
153 |
grid.SetRowMinimalHeight(row, ROW_HEIGHT) |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
154 |
grid.AutoSizeRow(row, False) |
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
155 |
|
814 | 156 |
def SetData(self, data): |
157 |
self.data = data |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
158 |
|
814 | 159 |
def GetData(self): |
160 |
return self.data |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
161 |
|
814 | 162 |
def GetCurrentIndex(self): |
163 |
return self.CurrentIndex |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
164 |
|
814 | 165 |
def SetCurrentIndex(self, index): |
166 |
self.CurrentIndex = index |
|
1735
c02818d7e29f
clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
167 |
|
814 | 168 |
def AppendRow(self, row_content): |
169 |
self.data.append(row_content) |
|
170 |
||
171 |
def InsertRow(self, index, row_content): |
|
172 |
self.data.insert(index, row_content) |
|
173 |
||
174 |
def MoveRow(self, row_index, move): |
|
175 |
new_index = max(0, min(row_index + move, len(self.data) - 1)) |
|
176 |
if new_index != row_index: |
|
177 |
self.data.insert(new_index, self.data.pop(row_index)) |
|
178 |
return new_index |
|
179 |
||
180 |
def RemoveRow(self, row_index): |
|
181 |
self.data.pop(row_index) |
|
182 |
||
183 |
def GetRow(self, row_index): |
|
184 |
return self.data[row_index] |
|
185 |
||
186 |
def Empty(self): |
|
187 |
self.data = [] |
|
188 |
||
189 |
def AddHighlight(self, infos, highlight_type): |
|
190 |
row_highlights = self.Highlights.setdefault(infos[0], {}) |
|
191 |
col_highlights = row_highlights.setdefault(infos[1], []) |
|
192 |
col_highlights.append(highlight_type) |
|
193 |
||
194 |
def RemoveHighlight(self, infos, highlight_type): |
|
195 |
row_highlights = self.Highlights.get(infos[0]) |
|
196 |
if row_highlights is not None: |
|
197 |
col_highlights = row_highlights.get(infos[1]) |
|
198 |
if col_highlights is not None and highlight_type in col_highlights: |
|
199 |
col_highlights.remove(highlight_type) |
|
200 |
if len(col_highlights) == 0: |
|
201 |
row_highlights.pop(infos[1]) |
|
202 |
||
203 |
def ClearHighlights(self, highlight_type=None): |
|
204 |
if highlight_type is None: |
|
205 |
self.Highlights = {} |
|
206 |
else: |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset
|
207 |
for _row, row_highlights in self.Highlights.iteritems(): |
814 | 208 |
row_items = row_highlights.items() |
209 |
for col, col_highlights in row_items: |
|
210 |
if highlight_type in col_highlights: |
|
211 |
col_highlights.remove(highlight_type) |
|
212 |
if len(col_highlights) == 0: |
|
213 |
row_highlights.pop(col) |