author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 08 Jun 2017 17:30:23 +0300 | |
changeset 1694 | acb767d6ac85 |
parent 1571 | 486f94a8032c |
child 1735 | c02818d7e29f |
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 |
|
25 |
import wx |
|
26 |
import wx.grid |
|
27 |
||
1342
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
28 |
if wx.Platform == '__WXMSW__': |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
29 |
ROW_HEIGHT = 20 |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
30 |
else: |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
31 |
ROW_HEIGHT = 28 |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
32 |
|
814 | 33 |
class CustomTable(wx.grid.PyGridTableBase): |
34 |
||
35 |
""" |
|
36 |
A custom wx.grid.Grid Table using user supplied data |
|
37 |
""" |
|
38 |
def __init__(self, parent, data, colnames): |
|
39 |
# The base class must be initialized *first* |
|
40 |
wx.grid.PyGridTableBase.__init__(self) |
|
41 |
self.data = data |
|
42 |
self.colnames = colnames |
|
43 |
self.Highlights = {} |
|
44 |
self.Parent = parent |
|
45 |
# XXX |
|
46 |
# we need to store the row length and collength to |
|
47 |
# see if the table has changed size |
|
48 |
self._rows = self.GetNumberRows() |
|
49 |
self._cols = self.GetNumberCols() |
|
50 |
||
51 |
def GetNumberCols(self): |
|
52 |
return len(self.colnames) |
|
53 |
||
54 |
def GetNumberRows(self): |
|
55 |
return len(self.data) |
|
56 |
||
57 |
def GetColLabelValue(self, col, translate=True): |
|
58 |
if col < len(self.colnames): |
|
59 |
if translate: |
|
60 |
return _(self.colnames[col]) |
|
61 |
return self.colnames[col] |
|
62 |
||
63 |
def GetRowLabelValue(self, row, translate=True): |
|
64 |
return row |
|
65 |
||
66 |
def GetValue(self, row, col): |
|
67 |
if row < self.GetNumberRows(): |
|
68 |
return self.data[row].get(self.GetColLabelValue(col, False), "") |
|
69 |
||
70 |
def SetValue(self, row, col, value): |
|
71 |
if col < len(self.colnames): |
|
72 |
self.data[row][self.GetColLabelValue(col, False)] = value |
|
73 |
||
74 |
def GetValueByName(self, row, colname): |
|
75 |
if row < self.GetNumberRows(): |
|
76 |
return self.data[row].get(colname) |
|
77 |
||
78 |
def SetValueByName(self, row, colname, value): |
|
79 |
if row < self.GetNumberRows(): |
|
80 |
self.data[row][colname] = value |
|
81 |
||
82 |
def ResetView(self, grid): |
|
83 |
""" |
|
84 |
(wx.grid.Grid) -> Reset the grid view. Call this to |
|
85 |
update the grid if rows and columns have been added or deleted |
|
86 |
""" |
|
87 |
grid.CloseEditControl() |
|
88 |
grid.BeginBatch() |
|
89 |
for current, new, delmsg, addmsg in [ |
|
90 |
(self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED), |
|
91 |
(self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED), |
|
92 |
]: |
|
93 |
if new < current: |
|
94 |
msg = wx.grid.GridTableMessage(self,delmsg,new,current-new) |
|
95 |
grid.ProcessTableMessage(msg) |
|
96 |
elif new > current: |
|
97 |
msg = wx.grid.GridTableMessage(self,addmsg,new-current) |
|
98 |
grid.ProcessTableMessage(msg) |
|
99 |
self.UpdateValues(grid) |
|
100 |
grid.EndBatch() |
|
101 |
||
102 |
self._rows = self.GetNumberRows() |
|
103 |
self._cols = self.GetNumberCols() |
|
104 |
# update the column rendering scheme |
|
105 |
self._updateColAttrs(grid) |
|
106 |
||
107 |
# update the scrollbars and the displayed part of the grid |
|
108 |
grid.AdjustScrollbars() |
|
109 |
grid.ForceRefresh() |
|
110 |
||
111 |
def UpdateValues(self, grid): |
|
112 |
"""Update all displayed values""" |
|
113 |
# This sends an event to the grid table to update all of the values |
|
114 |
msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES) |
|
115 |
grid.ProcessTableMessage(msg) |
|
116 |
||
117 |
def _updateColAttrs(self, grid): |
|
118 |
""" |
|
119 |
wx.grid.Grid -> update the column attributes to add the |
|
120 |
appropriate renderer given the column name. |
|
121 |
||
122 |
Otherwise default to the default renderer. |
|
123 |
""" |
|
124 |
for row in range(self.GetNumberRows()): |
|
125 |
row_highlights = self.Highlights.get(row, {}) |
|
126 |
for col in range(self.GetNumberCols()): |
|
127 |
colname = self.GetColLabelValue(col, False) |
|
128 |
||
129 |
grid.SetReadOnly(row, col, True) |
|
130 |
grid.SetCellEditor(row, col, None) |
|
131 |
grid.SetCellRenderer(row, col, None) |
|
132 |
||
133 |
highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1] |
|
134 |
grid.SetCellBackgroundColour(row, col, highlight_colours[0]) |
|
135 |
grid.SetCellTextColour(row, col, highlight_colours[1]) |
|
136 |
self.ResizeRow(grid, row) |
|
137 |
||
138 |
def ResizeRow(self, grid, row): |
|
1342
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
139 |
if grid.GetRowSize(row) < ROW_HEIGHT: |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
140 |
grid.SetRowMinimalHeight(row, ROW_HEIGHT) |
c17507a10807
Fixed various latency issues removing unnecessary calls
Laurent Bessard
parents:
814
diff
changeset
|
141 |
grid.AutoSizeRow(row, False) |
814 | 142 |
|
143 |
def SetData(self, data): |
|
144 |
self.data = data |
|
145 |
||
146 |
def GetData(self): |
|
147 |
return self.data |
|
148 |
||
149 |
def GetCurrentIndex(self): |
|
150 |
return self.CurrentIndex |
|
151 |
||
152 |
def SetCurrentIndex(self, index): |
|
153 |
self.CurrentIndex = index |
|
154 |
||
155 |
def AppendRow(self, row_content): |
|
156 |
self.data.append(row_content) |
|
157 |
||
158 |
def InsertRow(self, index, row_content): |
|
159 |
self.data.insert(index, row_content) |
|
160 |
||
161 |
def MoveRow(self, row_index, move): |
|
162 |
new_index = max(0, min(row_index + move, len(self.data) - 1)) |
|
163 |
if new_index != row_index: |
|
164 |
self.data.insert(new_index, self.data.pop(row_index)) |
|
165 |
return new_index |
|
166 |
||
167 |
def RemoveRow(self, row_index): |
|
168 |
self.data.pop(row_index) |
|
169 |
||
170 |
def GetRow(self, row_index): |
|
171 |
return self.data[row_index] |
|
172 |
||
173 |
def Empty(self): |
|
174 |
self.data = [] |
|
175 |
||
176 |
def AddHighlight(self, infos, highlight_type): |
|
177 |
row_highlights = self.Highlights.setdefault(infos[0], {}) |
|
178 |
col_highlights = row_highlights.setdefault(infos[1], []) |
|
179 |
col_highlights.append(highlight_type) |
|
180 |
||
181 |
def RemoveHighlight(self, infos, highlight_type): |
|
182 |
row_highlights = self.Highlights.get(infos[0]) |
|
183 |
if row_highlights is not None: |
|
184 |
col_highlights = row_highlights.get(infos[1]) |
|
185 |
if col_highlights is not None and highlight_type in col_highlights: |
|
186 |
col_highlights.remove(highlight_type) |
|
187 |
if len(col_highlights) == 0: |
|
188 |
row_highlights.pop(infos[1]) |
|
189 |
||
190 |
def ClearHighlights(self, highlight_type=None): |
|
191 |
if highlight_type is None: |
|
192 |
self.Highlights = {} |
|
193 |
else: |
|
194 |
for row, row_highlights in self.Highlights.iteritems(): |
|
195 |
row_items = row_highlights.items() |
|
196 |
for col, col_highlights in row_items: |
|
197 |
if highlight_type in col_highlights: |
|
198 |
col_highlights.remove(highlight_type) |
|
199 |
if len(col_highlights) == 0: |
|
200 |
row_highlights.pop(col) |