1 |
bh |
79 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
bh |
50 |
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
|
9 |
|
|
"""Common Thuban specific control widgets""" |
10 |
|
|
|
11 |
|
|
__version__ = "$Revision$" |
12 |
|
|
|
13 |
|
|
from wxPython.wx import wxListCtrl, wxLC_REPORT, wxLIST_AUTOSIZE_USEHEADER, \ |
14 |
bh |
79 |
EVT_LIST_ITEM_SELECTED, true, false |
15 |
|
|
from wxPython.grid import wxPyGridTableBase, wxGrid, wxGRID_VALUE_STRING, \ |
16 |
|
|
wxGridTableMessage, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, \ |
17 |
|
|
wxGRIDTABLE_NOTIFY_ROWS_DELETED, wxGRIDTABLE_REQUEST_VIEW_GET_VALUES |
18 |
bh |
50 |
|
19 |
jan |
374 |
from Thuban import _ |
20 |
|
|
|
21 |
bh |
79 |
# FIXME: the wx_value_type_map should be moved from tableview to a |
22 |
|
|
# separate module |
23 |
|
|
from tableview import wx_value_type_map |
24 |
bh |
50 |
|
25 |
bh |
79 |
|
26 |
|
|
|
27 |
bh |
50 |
class RecordListCtrl(wxListCtrl): |
28 |
|
|
|
29 |
|
|
"""List Control showing a single record from a thuban table""" |
30 |
|
|
|
31 |
|
|
def __init__(self, parent, id): |
32 |
|
|
wxListCtrl.__init__(self, parent, id, style = wxLC_REPORT) |
33 |
|
|
|
34 |
jan |
374 |
self.InsertColumn(0, _("Field")) |
35 |
frank |
420 |
self.SetColumnWidth(0, 200) |
36 |
jan |
374 |
self.InsertColumn(1, _("Value")) |
37 |
frank |
420 |
self.SetColumnWidth(1, 100) |
38 |
bh |
50 |
|
39 |
|
|
# vaues maps row numbers to the corresponding python values |
40 |
|
|
self.values = {} |
41 |
|
|
|
42 |
|
|
def fill_list(self, table, shape): |
43 |
|
|
"""Fill self with the contents shape's record from table""" |
44 |
|
|
self.DeleteAllItems() |
45 |
|
|
values = {} |
46 |
|
|
|
47 |
|
|
if shape is not None: |
48 |
|
|
num_cols = table.field_count() |
49 |
|
|
|
50 |
|
|
names = [] |
51 |
|
|
for i in range(num_cols): |
52 |
|
|
type, name, length, decc = table.field_info(i) |
53 |
|
|
names.append(name) |
54 |
|
|
record = table.read_record(shape) |
55 |
|
|
|
56 |
|
|
for i in range(len(names)): |
57 |
|
|
name = names[i] |
58 |
|
|
value = record[name] |
59 |
|
|
self.InsertStringItem(i, name) |
60 |
|
|
self.SetStringItem(i, 1, str(value)) |
61 |
|
|
values[i] = value |
62 |
|
|
|
63 |
|
|
self.values = values |
64 |
|
|
|
65 |
|
|
class SelectableRecordListCtrl(RecordListCtrl): |
66 |
|
|
|
67 |
|
|
def __init__(self, parent, id): |
68 |
|
|
RecordListCtrl.__init__(self, parent, id) |
69 |
|
|
|
70 |
|
|
# selected is the index of the selected record or -1 if none is |
71 |
|
|
# selected |
72 |
|
|
self.selected = -1 |
73 |
|
|
EVT_LIST_ITEM_SELECTED(self, self.GetId(), self.OnItemSelected) |
74 |
|
|
|
75 |
|
|
def OnItemSelected(self, event): |
76 |
|
|
"""Event handler. Update the selected instvar""" |
77 |
|
|
self.selected = event.m_itemIndex |
78 |
|
|
|
79 |
|
|
def GetValue(self): |
80 |
|
|
"""Return the currently selected value. None if no value is selected""" |
81 |
|
|
if self.selected >= 0: |
82 |
|
|
return self.values[self.selected] |
83 |
|
|
else: |
84 |
|
|
return None |
85 |
bh |
79 |
|
86 |
|
|
|
87 |
|
|
class RecordTable(wxPyGridTableBase): |
88 |
|
|
|
89 |
|
|
"""Wrapper that makes a Thuban table record look like a table for a |
90 |
|
|
wxGrid |
91 |
|
|
""" |
92 |
|
|
|
93 |
|
|
def __init__(self, table = None, record = None): |
94 |
|
|
wxPyGridTableBase.__init__(self) |
95 |
|
|
self.num_cols = 1 |
96 |
|
|
self.num_rows = 0 |
97 |
|
|
self.table = None |
98 |
|
|
self.record_index = record |
99 |
|
|
self.record = None |
100 |
|
|
self.SetTable(table, record) |
101 |
|
|
|
102 |
|
|
def SetTable(self, table, record_index): |
103 |
|
|
old_num_rows = self.num_rows |
104 |
|
|
if record_index is not None: |
105 |
|
|
self.table = table |
106 |
|
|
self.record_index = record_index |
107 |
|
|
self.record = table.read_record(record_index) |
108 |
|
|
|
109 |
|
|
# we have one row for each field in the table |
110 |
|
|
self.num_rows = table.field_count() |
111 |
|
|
|
112 |
|
|
# extract the field types and names of the row we're showing. |
113 |
|
|
self.rows = [] |
114 |
|
|
for i in range(self.num_rows): |
115 |
|
|
type, name, len, decc = table.field_info(i) |
116 |
|
|
self.rows.append((name, wx_value_type_map[type], len, decc)) |
117 |
|
|
self.notify_get_values() |
118 |
|
|
else: |
119 |
|
|
# make the grid empty |
120 |
|
|
self.num_rows = 0 |
121 |
|
|
self.rows = [] |
122 |
|
|
|
123 |
|
|
# notify the views if the number of rows has changed |
124 |
|
|
if self.num_rows > old_num_rows: |
125 |
|
|
self.notify_append_rows(self.num_rows - old_num_rows) |
126 |
|
|
elif self.num_rows < old_num_rows: |
127 |
|
|
self.notify_delete_rows(0, old_num_rows - self.num_rows) |
128 |
|
|
|
129 |
|
|
def notify_append_rows(self, num): |
130 |
|
|
"""Tell the view that num rows were appended""" |
131 |
|
|
self.send_view_message(wxGRIDTABLE_NOTIFY_ROWS_APPENDED, num) |
132 |
|
|
|
133 |
|
|
def notify_delete_rows(self, start, num): |
134 |
|
|
"""Tell the view that num rows were deleted starting at start""" |
135 |
|
|
self.send_view_message(wxGRIDTABLE_NOTIFY_ROWS_DELETED, start, num) |
136 |
|
|
|
137 |
|
|
def notify_get_values(self): |
138 |
|
|
"""Tell the view that the grid's values have to be updated""" |
139 |
|
|
self.send_view_message(wxGRIDTABLE_REQUEST_VIEW_GET_VALUES) |
140 |
|
|
|
141 |
|
|
def send_view_message(self, msgid, *args): |
142 |
|
|
"""Send the message msgid to the view with arguments args""" |
143 |
|
|
view = self.GetView() |
144 |
|
|
if view: |
145 |
|
|
#print "send_view_message", msgid, args |
146 |
|
|
msg = apply(wxGridTableMessage, (self, msgid) + args) |
147 |
|
|
view.ProcessTableMessage(msg) |
148 |
|
|
|
149 |
|
|
# |
150 |
|
|
# required methods for the wxPyGridTableBase interface |
151 |
|
|
# |
152 |
|
|
|
153 |
|
|
def GetNumberRows(self): |
154 |
|
|
return self.num_rows |
155 |
|
|
|
156 |
|
|
def GetNumberCols(self): |
157 |
|
|
return self.num_cols |
158 |
|
|
|
159 |
|
|
def IsEmptyCell(self, row, col): |
160 |
|
|
return row >= self.num_rows or col >= self.num_cols |
161 |
|
|
|
162 |
|
|
# Get/Set values in the table. The Python version of these |
163 |
|
|
# methods can handle any data-type, (as long as the Editor and |
164 |
|
|
# Renderer understands the type too,) not just strings as in the |
165 |
|
|
# C++ version. |
166 |
|
|
def GetValue(self, row, col): |
167 |
|
|
if row < self.num_rows: |
168 |
|
|
return self.record[self.rows[row][0]] |
169 |
|
|
return "" |
170 |
|
|
|
171 |
|
|
def SetValue(self, row, col, value): |
172 |
|
|
if row < self.num_rows: |
173 |
|
|
name = self.rows[row][0] |
174 |
bh |
274 |
self.record[name] = value |
175 |
|
|
self.table.write_record(self.record_index, {name: value}) |
176 |
bh |
79 |
|
177 |
|
|
# |
178 |
|
|
# Some optional methods |
179 |
|
|
# |
180 |
|
|
|
181 |
|
|
# Called when the grid needs to display labels |
182 |
|
|
def GetColLabelValue(self, col): |
183 |
jan |
374 |
return _("Value") |
184 |
bh |
79 |
|
185 |
|
|
def GetRowLabelValue(self, row): |
186 |
|
|
if row < self.num_rows: |
187 |
|
|
return self.rows[row][0] |
188 |
|
|
return "" |
189 |
|
|
|
190 |
|
|
# Called to determine the kind of editor/renderer to use by |
191 |
|
|
# default, doesn't necessarily have to be the same type used |
192 |
|
|
# nativly by the editor/renderer if they know how to convert. |
193 |
|
|
def GetTypeName(self, row, col): |
194 |
bh |
255 |
# for some reason row and col may be negative sometimes, but |
195 |
|
|
# it's probably a wx bug (filed as #593189 on sourceforge) |
196 |
|
|
if 0 <= row < self.num_rows: |
197 |
bh |
79 |
return self.rows[row][1] |
198 |
|
|
return wxGRID_VALUE_STRING |
199 |
|
|
|
200 |
|
|
# Called to determine how the data can be fetched and stored by the |
201 |
|
|
# editor and renderer. This allows you to enforce some type-safety |
202 |
|
|
# in the grid. |
203 |
|
|
def CanGetValueAs(self, row, col, typeName): |
204 |
|
|
# perhaps we should allow conversion int->double? |
205 |
|
|
return self.GetTypeName(row, col) == typeName |
206 |
|
|
|
207 |
|
|
def CanSetValueAs(self, row, col, typeName): |
208 |
|
|
return self.CanGetValueAs(row, col, typeName) |
209 |
|
|
|
210 |
|
|
|
211 |
bh |
255 |
|
212 |
bh |
79 |
class RecordGridCtrl(wxGrid): |
213 |
|
|
|
214 |
|
|
"""Grid view for a RecordTable""" |
215 |
|
|
|
216 |
|
|
def __init__(self, parent, table = None, record = None): |
217 |
|
|
wxGrid.__init__(self, parent, -1) |
218 |
|
|
|
219 |
|
|
self.table = RecordTable(table, record) |
220 |
|
|
|
221 |
|
|
# The second parameter means that the grid is to take ownership |
222 |
|
|
# of the table and will destroy it when done. Otherwise you |
223 |
|
|
# would need to keep a reference to it and call it's Destroy |
224 |
|
|
# method later. |
225 |
|
|
self.SetTable(self.table, true) |
226 |
|
|
|
227 |
|
|
#self.SetMargins(0,0) |
228 |
|
|
self.AutoSizeColumn(0, true) |
229 |
|
|
|
230 |
|
|
#self.SetSelectionMode(wxGrid.wxGridSelectRows) |
231 |
bh |
255 |
|
232 |
bh |
79 |
#EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
233 |
|
|
#EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
234 |
|
|
|
235 |
|
|
def SetTableRecord(self, table, record): |
236 |
|
|
self.table.SetTable(table, record) |