45 |
45 |
46 def GetElementsTableColnames(): |
46 def GetElementsTableColnames(): |
47 _ = lambda x : x |
47 _ = lambda x : x |
48 return ["#", _("Name"), _("Type"), _("Initial Value")] |
48 return ["#", _("Name"), _("Type"), _("Initial Value")] |
49 |
49 |
50 class ElementsTable(wx.grid.PyGridTableBase): |
50 class ElementsTable(CustomTable): |
51 |
51 |
52 """ |
52 """ |
53 A custom wx.grid.Grid Table using user supplied data |
53 A custom wx.grid.Grid Table using user supplied data |
54 """ |
54 """ |
55 def __init__(self, parent, data, colnames): |
55 def __init__(self, parent, data, colnames): |
56 # The base class must be initialized *first* |
56 # The base class must be initialized *first* |
57 wx.grid.PyGridTableBase.__init__(self) |
57 CustomTable.__init__(self, parent, data, colnames) |
58 self.data = data |
|
59 self.old_value = None |
58 self.old_value = None |
60 self.colnames = colnames |
59 |
61 self.Highlights = {} |
|
62 self.Parent = parent |
|
63 # XXX |
|
64 # we need to store the row length and collength to |
|
65 # see if the table has changed size |
|
66 self._rows = self.GetNumberRows() |
|
67 self._cols = self.GetNumberCols() |
|
68 |
|
69 def GetNumberCols(self): |
|
70 return len(self.colnames) |
|
71 |
|
72 def GetNumberRows(self): |
|
73 return len(self.data) |
|
74 |
|
75 def GetColLabelValue(self, col, translate=True): |
|
76 if col < len(self.colnames): |
|
77 if translate: |
|
78 return _(self.colnames[col]) |
|
79 return self.colnames[col] |
|
80 |
|
81 def GetRowLabelValues(self, row, translate=True): |
|
82 return row |
|
83 |
|
84 def GetValue(self, row, col): |
60 def GetValue(self, row, col): |
85 if row < self.GetNumberRows(): |
61 if row < self.GetNumberRows(): |
86 if col == 0: |
62 if col == 0: |
87 return row + 1 |
63 return row + 1 |
88 name = str(self.data[row].get(self.GetColLabelValue(col, False), "")) |
64 name = str(self.data[row].get(self.GetColLabelValue(col, False), "")) |
93 colname = self.GetColLabelValue(col, False) |
69 colname = self.GetColLabelValue(col, False) |
94 if colname == "Name": |
70 if colname == "Name": |
95 self.old_value = self.data[row][colname] |
71 self.old_value = self.data[row][colname] |
96 self.data[row][colname] = value |
72 self.data[row][colname] = value |
97 |
73 |
98 def GetValueByName(self, row, colname): |
|
99 if row < self.GetNumberRows(): |
|
100 return self.data[row].get(colname) |
|
101 |
|
102 def SetValueByName(self, row, colname, value): |
|
103 if row < self.GetNumberRows(): |
|
104 self.data[row][colname] = value |
|
105 |
|
106 def GetOldValue(self): |
74 def GetOldValue(self): |
107 return self.old_value |
75 return self.old_value |
108 |
76 |
109 def ResetView(self, grid): |
|
110 """ |
|
111 (wx.grid.Grid) -> Reset the grid view. Call this to |
|
112 update the grid if rows and columns have been added or deleted |
|
113 """ |
|
114 grid.BeginBatch() |
|
115 for current, new, delmsg, addmsg in [ |
|
116 (self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED), |
|
117 (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED), |
|
118 ]: |
|
119 if new < current: |
|
120 msg = wx.grid.GridTableMessage(self,delmsg,new,current-new) |
|
121 grid.ProcessTableMessage(msg) |
|
122 elif new > current: |
|
123 msg = wx.grid.GridTableMessage(self,addmsg,new-current) |
|
124 grid.ProcessTableMessage(msg) |
|
125 self.UpdateValues(grid) |
|
126 grid.EndBatch() |
|
127 |
|
128 self._rows = self.GetNumberRows() |
|
129 self._cols = self.GetNumberCols() |
|
130 # update the column rendering scheme |
|
131 self._updateColAttrs(grid) |
|
132 |
|
133 # update the scrollbars and the displayed part of the grid |
|
134 grid.AdjustScrollbars() |
|
135 grid.ForceRefresh() |
|
136 |
|
137 def UpdateValues(self, grid): |
|
138 """Update all displayed values""" |
|
139 # This sends an event to the grid table to update all of the values |
|
140 msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES) |
|
141 grid.ProcessTableMessage(msg) |
|
142 |
|
143 def _updateColAttrs(self, grid): |
77 def _updateColAttrs(self, grid): |
144 """ |
78 """ |
145 wx.grid.Grid -> update the column attributes to add the |
79 wx.grid.Grid -> update the column attributes to add the |
146 appropriate renderer given the column name. |
80 appropriate renderer given the column name. |
147 |
81 |
171 grid.SetCellRenderer(row, col, renderer) |
105 grid.SetCellRenderer(row, col, renderer) |
172 |
106 |
173 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1] |
107 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1] |
174 grid.SetCellBackgroundColour(row, col, highlight_colours[0]) |
108 grid.SetCellBackgroundColour(row, col, highlight_colours[0]) |
175 grid.SetCellTextColour(row, col, highlight_colours[1]) |
109 grid.SetCellTextColour(row, col, highlight_colours[1]) |
176 if wx.Platform == '__WXMSW__': |
110 self.ResizeRow(grid, row) |
177 grid.SetRowMinimalHeight(row, 20) |
111 |
178 else: |
|
179 grid.SetRowMinimalHeight(row, 28) |
|
180 grid.AutoSizeRow(row, False) |
|
181 |
|
182 def SetData(self, data): |
|
183 self.data = data |
|
184 |
|
185 def GetData(self): |
|
186 return self.data |
|
187 |
|
188 def AppendRow(self, row_content): |
|
189 self.data.append(row_content) |
|
190 |
|
191 def InsertRow(self, row_index, row_content): |
|
192 self.data.insert(row_index, row_content) |
|
193 |
|
194 def RemoveRow(self, row_index): |
|
195 self.data.pop(row_index) |
|
196 |
|
197 def MoveRow(self, idx, move): |
|
198 new_idx = max(0, min(idx + move, len(self.data) - 1)) |
|
199 if new_idx != idx: |
|
200 self.data.insert(new_idx, self.data.pop(idx)) |
|
201 return new_idx |
|
202 |
|
203 def GetRow(self, row_index): |
|
204 return self.data[row_index] |
|
205 |
|
206 def Empty(self): |
|
207 self.data = [] |
|
208 self.editors = [] |
|
209 |
|
210 def AddHighlight(self, infos, highlight_type): |
112 def AddHighlight(self, infos, highlight_type): |
211 row_highlights = self.Highlights.setdefault(infos[0], {}) |
113 row_highlights = self.Highlights.setdefault(infos[0], {}) |
212 if infos[1] == "initial": |
114 if infos[1] == "initial": |
213 col_highlights = row_highlights.setdefault("initial value", []) |
115 col_highlights = row_highlights.setdefault("initial value", []) |
214 else: |
116 else: |
215 col_highlights = row_highlights.setdefault(infos[1], []) |
117 col_highlights = row_highlights.setdefault(infos[1], []) |
216 col_highlights.append(highlight_type) |
118 col_highlights.append(highlight_type) |
217 |
|
218 def ClearHighlights(self, highlight_type=None): |
|
219 if highlight_type is None: |
|
220 self.Highlights = {} |
|
221 else: |
|
222 for row, row_highlights in self.Highlights.iteritems(): |
|
223 row_items = row_highlights.items() |
|
224 for col, col_highlights in row_items: |
|
225 if highlight_type in col_highlights: |
|
226 col_highlights.remove(highlight_type) |
|
227 if len(col_highlights) == 0: |
|
228 row_highlights.pop(col) |
|
229 |
119 |
230 #------------------------------------------------------------------------------- |
120 #------------------------------------------------------------------------------- |
231 # Datatype Editor class |
121 # Datatype Editor class |
232 #------------------------------------------------------------------------------- |
122 #------------------------------------------------------------------------------- |
233 |
123 |