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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 50 by bh, Mon Sep 10 16:04:21 2001 UTC revision 255 by bh, Wed Aug 14 15:08:01 2002 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001 by Intevation GmbH  # Copyright (c) 2001, 2002 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 11  Line 11 
11  __version__ = "$Revision$"  __version__ = "$Revision$"
12    
13  from wxPython.wx import wxListCtrl, wxLC_REPORT, wxLIST_AUTOSIZE_USEHEADER, \  from wxPython.wx import wxListCtrl, wxLC_REPORT, wxLIST_AUTOSIZE_USEHEADER, \
14       EVT_LIST_ITEM_SELECTED       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    
19    # 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    
23    
24    
25  class RecordListCtrl(wxListCtrl):  class RecordListCtrl(wxListCtrl):
# Line 72  class SelectableRecordListCtrl(RecordLis Line 80  class SelectableRecordListCtrl(RecordLis
80              return self.values[self.selected]              return self.values[self.selected]
81          else:          else:
82              return None              return None
83    
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.table = None
96            self.record_index = record
97            self.record = None
98            self.SetTable(table, record)
99    
100        def SetTable(self, table, record_index):
101            old_num_rows = self.num_rows
102            if record_index is not None:
103                self.table = table
104                self.record_index = record_index
105                self.record = table.read_record(record_index)
106    
107                # we have one row for each field in the table
108                self.num_rows = table.field_count()
109    
110                # extract the field types and names of the row we're showing.
111                self.rows = []
112                for i in range(self.num_rows):
113                    type, name, len, decc = table.field_info(i)
114                    self.rows.append((name, wx_value_type_map[type], len, decc))
115                self.notify_get_values()
116            else:
117                # make the grid empty
118                self.num_rows = 0
119                self.rows = []
120    
121            # notify the views if the number of rows has changed
122            if self.num_rows > old_num_rows:
123                self.notify_append_rows(self.num_rows - old_num_rows)
124            elif self.num_rows < old_num_rows:
125                self.notify_delete_rows(0, old_num_rows - self.num_rows)
126    
127        def notify_append_rows(self, num):
128            """Tell the view that num rows were appended"""
129            self.send_view_message(wxGRIDTABLE_NOTIFY_ROWS_APPENDED, num)
130    
131        def notify_delete_rows(self, start, num):
132            """Tell the view that num rows were deleted starting at start"""
133            self.send_view_message(wxGRIDTABLE_NOTIFY_ROWS_DELETED, start, num)
134    
135        def notify_get_values(self):
136            """Tell the view that the grid's values have to be updated"""
137            self.send_view_message(wxGRIDTABLE_REQUEST_VIEW_GET_VALUES)
138    
139        def send_view_message(self, msgid, *args):
140            """Send the message msgid to the view with arguments args"""
141            view = self.GetView()
142            if view:
143                #print "send_view_message", msgid, args
144                msg = apply(wxGridTableMessage, (self, msgid) + args)
145                view.ProcessTableMessage(msg)
146    
147        #
148        # required methods for the wxPyGridTableBase interface
149        #
150    
151        def GetNumberRows(self):
152            return self.num_rows
153    
154        def GetNumberCols(self):
155            return self.num_cols
156    
157        def IsEmptyCell(self, row, col):
158            return row >= self.num_rows or col >= self.num_cols
159    
160        # Get/Set values in the table.  The Python version of these
161        # methods can handle any data-type, (as long as the Editor and
162        # Renderer understands the type too,) not just strings as in the
163        # C++ version.
164        def GetValue(self, row, col):
165            if row < self.num_rows:
166                return self.record[self.rows[row][0]]
167            return ""
168    
169        def SetValue(self, row, col, value):
170            if row < self.num_rows:
171                name = self.rows[row][0]
172                print "Set value of field %s to %s" % (name, value)
173    
174        #
175        # Some optional methods
176        #
177    
178        # Called when the grid needs to display labels
179        def GetColLabelValue(self, col):
180            return "Value"
181    
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            # 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                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    
209    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            self.SetTable(self.table, true)
223    
224            #self.SetMargins(0,0)
225            self.AutoSizeColumn(0, true)
226    
227            #self.SetSelectionMode(wxGrid.wxGridSelectRows)
228    
229            #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)

Legend:
Removed from v.50  
changed lines
  Added in v.255

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26