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