/[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 1092 by bh, Wed May 28 17:10:04 2003 UTC revision 1219 by bh, Mon Jun 16 17:42:54 2003 UTC
# Line 18  from Thuban.Lib.connector import Publish Line 18  from Thuban.Lib.connector import Publish
18  from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \  from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \
19       FIELDTYPE_STRING, table_to_dbf, table_to_csv       FIELDTYPE_STRING, table_to_dbf, table_to_csv
20  import view  import view
21  from dialogs import NonModalNonParentDialog  from dialogs import ThubanFrame
22    
23  from messages import SHAPES_SELECTED, SESSION_REPLACED  from messages import SHAPES_SELECTED, SESSION_REPLACED
24  from Thuban.Model.messages import TABLE_REMOVED  from Thuban.Model.messages import TABLE_REMOVED, MAP_LAYERS_REMOVED
25    
26  wx_value_type_map = {FIELDTYPE_INT: wxGRID_VALUE_NUMBER,  wx_value_type_map = {FIELDTYPE_INT: wxGRID_VALUE_NUMBER,
27                       FIELDTYPE_DOUBLE: wxGRID_VALUE_FLOAT,                       FIELDTYPE_DOUBLE: wxGRID_VALUE_FLOAT,
# Line 150  class TableGrid(wxGrid, Publisher): Line 150  class TableGrid(wxGrid, Publisher):
150    
151          self.allow_messages_count = 0          self.allow_messages_count = 0
152    
153            # keep track of which rows are selected.
154            self.rows = {}
155    
156          self.table = DataTable(table)          self.table = DataTable(table)
157    
158          # The second parameter means that the grid is to take ownership          # The second parameter means that the grid is to take ownership
# Line 169  class TableGrid(wxGrid, Publisher): Line 172  class TableGrid(wxGrid, Publisher):
172          self.SetSelectionMode(wxGrid.wxGridSelectRows)          self.SetSelectionMode(wxGrid.wxGridSelectRows)
173    
174          self.ToggleEventListeners(True)          self.ToggleEventListeners(True)
175            EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect)
176            EVT_GRID_SELECT_CELL(self, self.OnSelectCell)
177    
178          # Replace the normal renderers with our own versions which          # Replace the normal renderers with our own versions which
179          # render NULL/None values specially          # render NULL/None values specially
# Line 183  class TableGrid(wxGrid, Publisher): Line 188  class TableGrid(wxGrid, Publisher):
188          self.table.SetTable(table)          self.table.SetTable(table)
189    
190      def OnRangeSelect(self, event):      def OnRangeSelect(self, event):
191          rows = dict([(i, 0) for i in self.GetSelectedRows()])          if self.handleSelectEvents:
192                self.rows = dict([(i, 0) for i in self.GetSelectedRows()])
193    
194          # if we're selecting we need to include the selected range and              # if we're selecting we need to include the selected range and
195          # make sure that the current row is also included, which may              # make sure that the current row is also included, which may
196          # not be the case if you just click on a single row!              # not be the case if you just click on a single row!
197          if event.Selecting():              if event.Selecting():
198              for i in range(event.GetTopRow(), event.GetBottomRow() + 1):                  for i in range(event.GetTopRow(), event.GetBottomRow() + 1):
199                  rows[i] = 0                      self.rows[i] = 0
200              rows[event.GetTopLeftCoords().GetRow()] = 0                  self.rows[event.GetTopLeftCoords().GetRow()] = 0
201        
202                self.issue(ROW_SELECTED, self.rows.keys())
203    
         self.issue(ROW_SELECTED, rows.keys())  
204          event.Skip()          event.Skip()
205    
206      def OnSelectCell(self, event):      def OnSelectCell(self, event):
207          self.issue(ROW_SELECTED, self.GetSelectedRows())          if self.handleSelectEvents:
208                self.issue(ROW_SELECTED, self.GetSelectedRows())
209          event.Skip()          event.Skip()
210    
211      def ToggleEventListeners(self, on):      def ToggleEventListeners(self, on):
212          if on:          self.handleSelectEvents = on
             EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect)  
             EVT_GRID_SELECT_CELL(self, self.OnSelectCell)  
         else:  
             EVT_GRID_RANGE_SELECT(self, None)  
             EVT_GRID_SELECT_CELL(self, None)  
213                            
214        def GetNumberSelected(self):
215            return len(self.rows)
216    
217      def disallow_messages(self):      def disallow_messages(self):
218          """Disallow messages to be send.          """Disallow messages to be send.
219    
# Line 250  class LayerTableGrid(TableGrid): Line 256  class LayerTableGrid(TableGrid):
256          nothing. If shape or layer is None also do nothing.          nothing. If shape or layer is None also do nothing.
257          """          """
258          if layer is not None \          if layer is not None \
259              and layer.table is self.table.table:              and layer.ShapeStore().Table() is self.table.table:
260    
261              self.disallow_messages()              self.disallow_messages()
262              try:              try:
# Line 273  class LayerTableGrid(TableGrid): Line 279  class LayerTableGrid(TableGrid):
279                  self.allow_messages()                  self.allow_messages()
280    
281    
282  class TableFrame(NonModalNonParentDialog):  class TableFrame(ThubanFrame):
283    
284      """Frame that displays a Thuban table in a grid view"""      """Frame that displays a Thuban table in a grid view"""
285    
286      def __init__(self, parent, name, title, table):      def __init__(self, parent, name, title, table):
287          NonModalNonParentDialog.__init__(self, parent, name, title)          ThubanFrame.__init__(self, parent, name, title)
288            self.panel = wxPanel(self, -1)
289    
290          self.table = table          self.table = table
291          self.grid = self.make_grid(self.table)          self.grid = self.make_grid(self.table)
292          self.app = self.parent.application          self.app = self.parent.application
# Line 286  class TableFrame(NonModalNonParentDialog Line 294  class TableFrame(NonModalNonParentDialog
294          self.session = self.app.Session()          self.session = self.app.Session()
295          self.session.Subscribe(TABLE_REMOVED, self.close_on_table_removed)          self.session.Subscribe(TABLE_REMOVED, self.close_on_table_removed)
296    
297    
298      def make_grid(self, table):      def make_grid(self, table):
299          """Return the table grid to use in the frame.          """Return the table grid to use in the frame.
300    
# Line 297  class TableFrame(NonModalNonParentDialog Line 306  class TableFrame(NonModalNonParentDialog
306      def OnClose(self, event):      def OnClose(self, event):
307          self.app.Unsubscribe(SESSION_REPLACED, self.close_on_session_replaced)          self.app.Unsubscribe(SESSION_REPLACED, self.close_on_session_replaced)
308          self.session.Unsubscribe(TABLE_REMOVED, self.close_on_table_removed)          self.session.Unsubscribe(TABLE_REMOVED, self.close_on_table_removed)
309          NonModalNonParentDialog.OnClose(self, event)          ThubanFrame.OnClose(self, event)
310    
311      def close_on_session_replaced(self, *args):      def close_on_session_replaced(self, *args):
312          """Subscriber for the SESSION_REPLACED messages.          """Subscriber for the SESSION_REPLACED messages.
# Line 319  class TableFrame(NonModalNonParentDialog Line 328  class TableFrame(NonModalNonParentDialog
328    
329  ID_QUERY = 4001  ID_QUERY = 4001
330  ID_EXPORT = 4002  ID_EXPORT = 4002
331    ID_COMBOVALUE = 4003
332    
333  class QueryTableFrame(TableFrame):  class QueryTableFrame(TableFrame):
334    
335      """Frame that displays a table in a grid view and offers user actions      """Frame that displays a table in a grid view and offers user actions
336      selection and export      selection and export
337    
338      A LayerTableFrame is TableFrame whose selection is connected to the      A QueryTableFrame is TableFrame whose selection is connected to the
339      selected object in a map.      selected object in a map.
340      """      """
341    
342      def __init__(self, parent, name, title, table):      def __init__(self, parent, name, title, table):
343          TableFrame.__init__(self, parent, name, title, table)          TableFrame.__init__(self, parent, name, title, table)
344    
345          self.combo_fields = wxComboBox(self, -1, style=wxCB_READONLY)          self.combo_fields = wxComboBox(self.panel, -1, style=wxCB_READONLY)
346          self.choice_comp = wxChoice(self, -1,          self.choice_comp = wxChoice(self.panel, -1,
347                                choices=["<", "<=", "==", "!=", ">=", ">"])                                choices=["<", "<=", "==", "!=", ">=", ">"])
348          self.combo_value = wxComboBox(self, -1)          self.combo_value = wxComboBox(self.panel, ID_COMBOVALUE)
349          self.choice_action = wxChoice(self, -1,          self.choice_action = wxChoice(self.panel, -1,
350                                  choices=[_("Replace Selection"),                                  choices=[_("Replace Selection"),
351                                          _("Refine Selection"),                                          _("Refine Selection"),
352                                          _("Add to Selection")])                                          _("Add to Selection")])
353    
354          button_query = wxButton(self, ID_QUERY, _("Query"))          button_query = wxButton(self.panel, ID_QUERY, _("Query"))
355          button_saveas = wxButton(self, ID_EXPORT, _("Export"))          button_saveas = wxButton(self.panel, ID_EXPORT, _("Export"))
356    
357            self.CreateStatusBar()
358    
359          self.grid.SetSize((400, 200))          self.grid.SetSize((400, 200))
360    
# Line 355  class QueryTableFrame(TableFrame): Line 367  class QueryTableFrame(TableFrame):
367          # assume at least one field?          # assume at least one field?
368          self.combo_fields.SetSelection(0)          self.combo_fields.SetSelection(0)
369          self.combo_value.SetSelection(0)          self.combo_value.SetSelection(0)
370            self.choice_action.SetSelection(0)
371            self.choice_comp.SetSelection(0)
372    
373            self.grid.Reparent(self.panel)
374    
375            self.UpdateStatusText()
376    
377          topBox = wxBoxSizer(wxVERTICAL)          topBox = wxBoxSizer(wxVERTICAL)
378    
379          sizer = wxStaticBoxSizer(wxStaticBox(self, -1,          sizer = wxStaticBoxSizer(wxStaticBox(self.panel, -1,
380                                    _("Selection")),                                    _("Selection")),
381                                    wxHORIZONTAL)                                    wxHORIZONTAL)
382          sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4)          sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4)
# Line 372  class QueryTableFrame(TableFrame): Line 390  class QueryTableFrame(TableFrame):
390          topBox.Add(sizer, 0, wxEXPAND|wxALL, 4)          topBox.Add(sizer, 0, wxEXPAND|wxALL, 4)
391          topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0)          topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0)
392    
393            self.panel.SetAutoLayout(True)
394            self.panel.SetSizer(topBox)
395            topBox.Fit(self.panel)
396            topBox.SetSizeHints(self.panel)
397    
398            panelSizer = wxBoxSizer(wxVERTICAL)
399            panelSizer.Add(self.panel, 1, wxEXPAND, 0)
400          self.SetAutoLayout(True)          self.SetAutoLayout(True)
401          self.SetSizer(topBox)          self.SetSizer(panelSizer)
402          topBox.Fit(self)          panelSizer.Fit(self)
403          topBox.SetSizeHints(self)          panelSizer.SetSizeHints(self)
404    
405          self.grid.SetFocus()          self.grid.SetFocus()
406    
407          EVT_BUTTON(self, ID_QUERY, self.OnQuery)          EVT_BUTTON(self, ID_QUERY, self.OnQuery)
408          EVT_BUTTON(self, ID_EXPORT, self.OnSaveAs)          EVT_BUTTON(self, ID_EXPORT, self.OnSaveAs)
409          EVT_KEY_DOWN(self.grid, self.OnKeyDown)          EVT_KEY_DOWN(self.grid, self.OnKeyDown)
410            EVT_GRID_RANGE_SELECT(self.grid, self.OnGridSelectRange)
411            EVT_GRID_SELECT_CELL(self.grid, self.OnGridSelectCell)
412    
413        def UpdateStatusText(self):
414            self.SetStatusText(_("%i rows (%i selected), %i columns")
415                % (self.grid.GetNumberRows(),
416                   self.grid.GetNumberSelected(),
417                   self.grid.GetNumberCols()))
418    
419        def OnGridSelectRange(self, event):
420            self.UpdateStatusText()
421            event.Skip()
422    
423        def OnGridSelectCell(self, event):
424            self.UpdateStatusText()
425            event.Skip()
426            
427      def OnKeyDown(self, event):      def OnKeyDown(self, event):
428          """Catch query key from grid"""          """Catch query key from grid"""
429          if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY):          if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY):
# Line 390  class QueryTableFrame(TableFrame): Line 432  class QueryTableFrame(TableFrame):
432          else:          else:
433              event.Skip()              event.Skip()
434    
   
435      def OnQuery(self, event):      def OnQuery(self, event):
436          wxBeginBusyCursor()          wxBeginBusyCursor()
437            try:
438    
439          if self.combo_value.GetSelection() < 1:              text = self.combo_value.GetValue()
440              value = self.combo_value.GetValue()              if self.combo_value.GetSelection() < 1 \
441          else:                  or self.combo_value.FindString(text) == -1:
442              value = self.table.Column(self.combo_value.GetValue())                  value = text
   
         ids = self.table.SimpleQuery(  
                 self.table.Column(self.combo_fields.GetStringSelection()),  
                 self.choice_comp.GetStringSelection(),  
                 value)  
   
         choice = self.choice_action.GetSelection()  
               
         #  
         # what used to be nice code got became a bit ugly because  
         # each time we select a row a message is sent to the grid  
         # which we are listening for and then we send further  
         # messages.  
         #  
         # now, we disable those listeners select everything but  
         # the first item, reenable the listeners, and select  
         # the first element, which causes everything to be  
         # updated properly.  
         #  
         self.grid.ToggleEventListeners(False)  
   
         if choice == 0:  
             # Replace Selection  
             self.grid.ClearSelection()  
         elif choice == 1:  
             # Refine Selection  
             sel = self.get_selected()  
             self.grid.ClearSelection()  
             ids = filter(sel.has_key, ids)  
         elif choice == 2:  
             # Add to Selection  
             pass  
   
         #  
         # select the rows (all but the first)  
         #  
         firsttime = True  
         for id in ids:  
             if firsttime:  
                 firsttime = False  
443              else:              else:
444                  self.grid.SelectRow(id, True)                  value = self.table.Column(text)
445    
446          self.grid.ToggleEventListeners(True)              ids = self.table.SimpleQuery(
447                        self.table.Column(self.combo_fields.GetStringSelection()),
448                        self.choice_comp.GetStringSelection(),
449                        value)
450    
451          #              choice = self.choice_action.GetSelection()
452          # select the first row              
453          #              #
454          if ids:              # what used to be nice code got became a bit ugly because
455              self.grid.SelectRow(ids[0], True)              # each time we select a row a message is sent to the grid
456                # which we are listening for and then we send further
457                # messages.
458                #
459                # now, we disable those listeners select everything but
460                # the first item, reenable the listeners, and select
461                # the first element, which causes everything to be
462                # updated properly.
463                #
464                if ids:
465                    self.grid.ToggleEventListeners(False)
466    
467                if choice == 0:
468                    # Replace Selection
469                    self.grid.ClearSelection()
470                elif choice == 1:
471                    # Refine Selection
472                    sel = self.get_selected()
473                    self.grid.ClearSelection()
474                    ids = filter(sel.has_key, ids)
475                elif choice == 2:
476                    # Add to Selection
477                    pass
478    
479                #
480                # select the rows (all but the first)
481                #
482                firsttime = True
483                for id in ids:
484                    if firsttime:
485                        firsttime = False
486                    else:
487                        self.grid.SelectRow(id, True)
488    
489                self.grid.ToggleEventListeners(True)
490    
491                #
492                # select the first row
493                #
494                if ids:
495                    self.grid.SelectRow(ids[0], True)
496    
497          wxEndBusyCursor()          finally:
498                wxEndBusyCursor()
499                    
500      def OnSaveAs(self, event):      def OnSaveAs(self, event):
501          dlg = wxFileDialog(self, _("Export Table To"), ".", "",          dlg = wxFileDialog(self, _("Export Table To"), ".", "",
# Line 495  class LayerTableFrame(QueryTableFrame): Line 541  class LayerTableFrame(QueryTableFrame):
541          self.layer = layer          self.layer = layer
542          self.grid.Subscribe(ROW_SELECTED, self.rows_selected)          self.grid.Subscribe(ROW_SELECTED, self.rows_selected)
543          self.parent.Subscribe(SHAPES_SELECTED, self.select_shapes)          self.parent.Subscribe(SHAPES_SELECTED, self.select_shapes)
544            self.map = self.parent.Map()
545            self.map.Subscribe(MAP_LAYERS_REMOVED, self.map_layers_removed)
546    
547          # if there is already a selection present, update the grid          # if there is already a selection present, update the grid
548          # accordingly          # accordingly
# Line 516  class LayerTableFrame(QueryTableFrame): Line 564  class LayerTableFrame(QueryTableFrame):
564      def OnClose(self, event):      def OnClose(self, event):
565          """Override the derived method to first unsubscribed."""          """Override the derived method to first unsubscribed."""
566          self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes)          self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes)
567            self.grid.Unsubscribe(ROW_SELECTED, self.rows_selected)
568            self.map.Unsubscribe(MAP_LAYERS_REMOVED, self.map_layers_removed)
569          QueryTableFrame.OnClose(self, event)          QueryTableFrame.OnClose(self, event)
570    
571      def select_shapes(self, layer, shapes):      def select_shapes(self, layer, shapes):
# Line 532  class LayerTableFrame(QueryTableFrame): Line 582  class LayerTableFrame(QueryTableFrame):
582          """          """
583          if self.layer is not None:          if self.layer is not None:
584              self.parent.SelectShapes(self.layer, rows)              self.parent.SelectShapes(self.layer, rows)
585    
586        def map_layers_removed(self, *args):
587            """Receiver for the map's MAP_LAYERS_REMOVED message
588    
589            Close the dialog if the layer whose table we're showing is not
590            in the map anymore.
591            """
592            if self.layer not in self.map.Layers():
593                self.Close()
594    

Legend:
Removed from v.1092  
changed lines
  Added in v.1219

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26