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

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

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

revision 77 by bh, Mon Feb 4 19:27:13 2002 UTC revision 808 by bh, Mon May 5 10:33:47 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001, 2002 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 15  from Thuban.Model.table import FIELDTYPE Line 15  from Thuban.Model.table import FIELDTYPE
15       FIELDTYPE_STRING       FIELDTYPE_STRING
16  import view  import view
17  from dialogs import NonModalDialog  from dialogs import NonModalDialog
18  from messages import SELECTED_SHAPE  from messages import SHAPES_SELECTED
19    
20  wx_value_type_map = {FIELDTYPE_INT: wxGRID_VALUE_NUMBER,  wx_value_type_map = {FIELDTYPE_INT: wxGRID_VALUE_NUMBER,
21                       FIELDTYPE_DOUBLE: wxGRID_VALUE_FLOAT,                       FIELDTYPE_DOUBLE: wxGRID_VALUE_FLOAT,
# Line 97  class DataTable(wxPyGridTableBase): Line 97  class DataTable(wxPyGridTableBase):
97    
98  class TableGrid(wxGrid, Publisher):  class TableGrid(wxGrid, Publisher):
99    
100      """A grid view for a Thuban table"""      """A grid view for a Thuban table
101    
102        When rows are selected by the user the table issues ROW_SELECTED
103        messages. wx sends selection events even when the selection is
104        manipulated by code (instead of by the user) which usually lead to
105        ROW_SELECTED messages being sent in turn. Therefore sending messages
106        can be switched on and off with the allow_messages and
107        disallow_messages methods.
108        """
109    
110      def __init__(self, parent, table = None):      def __init__(self, parent, table = None):
111          wxGrid.__init__(self, parent, -1)          wxGrid.__init__(self, parent, -1)
112    
113            self.allow_messages_count = 0
114    
115          self.table = DataTable(table)          self.table = DataTable(table)
116    
117          # The second parameter means that the grid is to take ownership          # The second parameter means that the grid is to take ownership
# Line 111  class TableGrid(wxGrid, Publisher): Line 121  class TableGrid(wxGrid, Publisher):
121          self.SetTable(self.table, true)          self.SetTable(self.table, true)
122    
123          #self.SetMargins(0,0)          #self.SetMargins(0,0)
124          self.AutoSizeColumns(false)  
125            # AutoSizeColumns would allow us to make the grid have optimal
126            # column widths automatically but it would cause a traversal of
127            # the entire table which for large .dbf files can take a very
128            # long time.
129            #self.AutoSizeColumns(false)
130    
131          self.SetSelectionMode(wxGrid.wxGridSelectRows)          self.SetSelectionMode(wxGrid.wxGridSelectRows)
132            
133          EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect)          EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect)
134          EVT_GRID_SELECT_CELL(self, self.OnSelectCell)          EVT_GRID_SELECT_CELL(self, self.OnSelectCell)
135    
# Line 128  class TableGrid(wxGrid, Publisher): Line 143  class TableGrid(wxGrid, Publisher):
143      def OnSelectCell(self, event):      def OnSelectCell(self, event):
144          self.issue(ROW_SELECTED, event.GetRow())          self.issue(ROW_SELECTED, event.GetRow())
145    
146        def disallow_messages(self):
147            """Disallow messages to be send.
148    
149            This method only increases a counter so that calls to
150            disallow_messages and allow_messages can be nested. Only the
151            outermost calls will actually switch message sending on and off.
152            """
153            self.allow_messages_count += 1
154    
155        def allow_messages(self):
156            """Allow messages to be send.
157    
158            This method only decreases a counter so that calls to
159            disallow_messages and allow_messages can be nested. Only the
160            outermost calls will actually switch message sending on and off.
161            """
162            self.allow_messages_count -= 1
163    
164        def issue(self, *args):
165            """Issue a message unless disallowed.
166    
167            See the allow_messages and disallow_messages methods.
168            """
169            if self.allow_messages_count == 0:
170                Publisher.issue(self, *args)
171    
172    
173    class LayerTableGrid(TableGrid):
174    
175        """Table grid for the layer tables.
176    
177        The LayerTableGrid is basically the same as a TableGrid but it's
178        selection is usually coupled to the selected object in the map.
179        """
180    
181      def select_shape(self, layer, shape):      def select_shape(self, layer, shape):
182            """Select the row corresponding to the specified shape and layer
183    
184            If layer is not the layer the table is associated with do
185            nothing. If shape or layer is None also do nothing.
186            """
187          if layer is not None and layer.table is self.table.table \          if layer is not None and layer.table is self.table.table \
188             and shape is not None:             and shape is not None:
189              self.SelectRow(shape)              self.disallow_messages()
190              self.SetGridCursor(shape, 0)              try:
191              self.MakeCellVisible(shape, 0)                  self.SelectRow(shape)
192                    self.SetGridCursor(shape, 0)
193                    self.MakeCellVisible(shape, 0)
194                finally:
195                    self.allow_messages()
196    
197    
198  class TableFrame(NonModalDialog):  class TableFrame(NonModalDialog):
199    
200      """Frame that displays a Thuban table in a grid view"""      """Frame that displays a Thuban table in a grid view"""
201    
202      def __init__(self, parent, interactor, name, title, layer = None,      def __init__(self, parent, name, title, table):
203                   table = None):          NonModalDialog.__init__(self, parent, name, title)
         NonModalDialog.__init__(self, parent, interactor, name, title)  
         self.layer = layer  
204          self.table = table          self.table = table
205          self.grid = TableGrid(self, table)          self.grid = self.make_grid(self.table)
206    
207        def make_grid(self, table):
208            """Return the table grid to use in the frame.
209    
210            The default implementation returns a TableGrid instance.
211            Override in derived classes to use different grid classes.
212            """
213            return TableGrid(self, table)
214    
215    
216    class LayerTableFrame(TableFrame):
217    
218        """Frame that displays a layer table in a grid view
219    
220        A LayerTableFrame is TableFrame whose selection is connected to the
221        selected object in a map.
222        """
223    
224        def __init__(self, parent, name, title, layer, table):
225            TableFrame.__init__(self, parent, name, title, table)
226            self.layer = layer
227          self.grid.Subscribe(ROW_SELECTED, self.row_selected)          self.grid.Subscribe(ROW_SELECTED, self.row_selected)
228          self.interactor.Subscribe(SELECTED_SHAPE, self.select_shape)          self.parent.Subscribe(SHAPES_SELECTED, self.select_shape)
229    
230        def make_grid(self, table):
231            """Override the derived method to return a LayerTableGrid.
232            """
233            return LayerTableGrid(self, table)
234    
235      def OnClose(self, event):      def OnClose(self, event):
236          self.interactor.Unsubscribe(SELECTED_SHAPE, self.select_shape)          self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shape)
237          NonModalDialog.OnClose(self, event)          TableFrame.OnClose(self, event)
238    
239      def select_shape(self, layer, shape):      def select_shape(self, layer, shapes):
240            """Subscribed to the SHAPES_SELECTED message.
241    
242            If shapes contains exactly one shape id, select that shape in
243            the grid. Otherwise deselect all.
244            """
245            if len(shapes):
246                shape = shapes[0]
247            else:
248                shape = None
249          self.grid.select_shape(layer, shape)          self.grid.select_shape(layer, shape)
250    
251      def row_selected(self, row):      def row_selected(self, row):
252          if self.layer is not None:          if self.layer is not None:
253              self.interactor.SelectLayerAndShape(self.layer, row)              self.parent.SelectShapes(self.layer, [row])

Legend:
Removed from v.77  
changed lines
  Added in v.808

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26