/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/controls.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/UI/controls.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 79 - (hide annotations)
Mon Feb 4 19:50:28 2002 UTC (23 years, 1 month ago) by bh
Original Path: trunk/thuban/Thuban/UI/controls.py
File MIME type: text/x-python
File size: 7689 byte(s)
	* Thuban/UI/controls.py (RecordTable, RecordGridCtrl): New classes
	implementing a grid for a single row of a thuban table.

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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26