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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2700 - (show annotations)
Mon Sep 18 14:27:02 2006 UTC (18 years, 5 months ago) by dpinte
Original Path: trunk/thuban/Thuban/UI/controls.py
File MIME type: text/x-python
File size: 7443 byte(s)
2006-09-18 Didrik Pinte <dpinte@itae.be>
    
        * wxPython 2.6 update : wx 2.4 syntax has been updated to 2.6


1 # Copyright (c) 2001, 2002, 2003 by Intevation GmbH
2 # 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 import wx
14 from wx import grid
15
16 from Thuban import _
17
18 # 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
22
23
24 class RecordListCtrl(wx.ListCtrl):
25
26 """List Control showing a single record from a thuban table"""
27
28 def __init__(self, parent, id):
29 wx.ListCtrl.__init__(self, parent, id, style = wx.LC_REPORT)
30
31 self.InsertColumn(0, _("Field"))
32 self.SetColumnWidth(0, 200)
33 self.InsertColumn(1, _("Value"))
34 self.SetColumnWidth(1, 100)
35
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 for col in table.Columns():
47 names.append(col.name)
48 record = table.ReadRowAsDict(shape)
49
50 for i in range(len(names)):
51 name = names[i]
52 value = record[name]
53 self.InsertStringItem(i, name)
54 self.SetStringItem(i, 1, str(value))
55 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 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, id=self.GetId())
68
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
80
81 class RecordTable(grid.PyGridTableBase):
82
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 grid.PyGridTableBase.__init__(self)
89 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 self.record = table.ReadRowAsDict(record_index)
102
103 # we have one row for each field in the table
104 self.num_rows = table.NumColumns()
105
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 col = table.Column(i)
110 self.rows.append((col.name, wx_value_type_map[col.type]))
111 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 self.send_view_message(grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, num)
126
127 def notify_delete_rows(self, start, num):
128 """Tell the view that num rows were deleted starting at start"""
129 self.send_view_message(grid.GRIDTABLE_NOTIFY_ROWS_DELETED, start, num)
130
131 def notify_get_values(self):
132 """Tell the view that the grid's values have to be updated"""
133 self.send_view_message(grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
134
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 msg = apply(grid.GridTableMessage, (self, msgid) + args)
141 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 self.record[name] = value
169 self.table.write_record(self.record_index, {name: value})
170
171 #
172 # Some optional methods
173 #
174
175 # Called when the grid needs to display labels
176 def GetColLabelValue(self, col):
177 return _("Value")
178
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 # 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 return self.rows[row][1]
192 return grid.GRID_VALUE_STRING
193
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
206 class RecordGridCtrl(grid.Grid):
207
208 """Grid view for a RecordTable"""
209
210 def __init__(self, parent, table = None, record = None):
211 grid.Grid.__init__(self, parent, -1)
212
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 self.SetTable(self.table, True)
220
221 #self.SetMargins(0,0)
222 self.AutoSizeColumn(0, True)
223
224 #self.SetSelectionMode(wxGrid.wxGridSelectRows)
225
226 #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