/[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 2700 by dpinte, Mon Sep 18 14:27:02 2006 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001 by Intevation GmbH  # Copyright (c) 2001, 2002, 2003 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 10  Line 10 
10    
11  __version__ = "$Revision$"  __version__ = "$Revision$"
12    
13  from wxPython.wx import wxListCtrl, wxLC_REPORT, wxLIST_AUTOSIZE_USEHEADER, \  import wx
14       EVT_LIST_ITEM_SELECTED  from wx import grid
15    
16    from Thuban import _
17    
18  class RecordListCtrl(wxListCtrl):  # 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"""      """List Control showing a single record from a thuban table"""
27    
28      def __init__(self, parent, id):      def __init__(self, parent, id):
29          wxListCtrl.__init__(self, parent, id, style = wxLC_REPORT)          wx.ListCtrl.__init__(self, parent, id, style = wx.LC_REPORT)
30    
31          self.InsertColumn(0, "Field")          self.InsertColumn(0, _("Field"))
32          self.SetColumnWidth(0, wxLIST_AUTOSIZE_USEHEADER)          self.SetColumnWidth(0, 200)
33          self.InsertColumn(1, "Value")          self.InsertColumn(1, _("Value"))
34          self.SetColumnWidth(1, wxLIST_AUTOSIZE_USEHEADER)          self.SetColumnWidth(1, 100)
35    
36          # vaues maps row numbers to the corresponding python values          # vaues maps row numbers to the corresponding python values
37          self.values = {}          self.values = {}
# Line 35  class RecordListCtrl(wxListCtrl): Line 42  class RecordListCtrl(wxListCtrl):
42          values = {}          values = {}
43    
44          if shape is not None:          if shape is not None:
             num_cols = table.field_count()  
   
45              names = []              names = []
46              for i in range(num_cols):              for col in table.Columns():
47                  type, name, length, decc = table.field_info(i)                  names.append(col.name)
48                  names.append(name)              record = table.ReadRowAsDict(shape)
             record = table.read_record(shape)  
49    
50              for i in range(len(names)):              for i in range(len(names)):
51                  name = names[i]                  name = names[i]
# Line 60  class SelectableRecordListCtrl(RecordLis Line 64  class SelectableRecordListCtrl(RecordLis
64          # selected is the index of the selected record or -1 if none is          # selected is the index of the selected record or -1 if none is
65          # selected          # selected
66          self.selected = -1          self.selected = -1
67          EVT_LIST_ITEM_SELECTED(self, self.GetId(), self.OnItemSelected)          self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, id=self.GetId())
68    
69      def OnItemSelected(self, event):      def OnItemSelected(self, event):
70          """Event handler. Update the selected instvar"""          """Event handler. Update the selected instvar"""
# Line 72  class SelectableRecordListCtrl(RecordLis Line 76  class SelectableRecordListCtrl(RecordLis
76              return self.values[self.selected]              return self.values[self.selected]
77          else:          else:
78              return None              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)

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26