/[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 1035 - (hide annotations)
Mon May 26 17:03:08 2003 UTC (21 years, 9 months ago) by jan
Original Path: trunk/thuban/Thuban/UI/controls.py
File MIME type: text/x-python
File size: 7689 byte(s)
Replace the true/false of wxWindows by True/False of Python 2.2.1.

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