controls/CustomTable.py
changeset 604 5b42b4401e6b
child 606 d65122c61eaf
equal deleted inserted replaced
603:25c92309cdae 604:5b42b4401e6b
       
     1 #This library is free software; you can redistribute it and/or
       
     2 #modify it under the terms of the GNU General Public
       
     3 #License as published by the Free Software Foundation; either
       
     4 #version 2.1 of the License, or (at your option) any later version.
       
     5 #
       
     6 #This library is distributed in the hope that it will be useful,
       
     7 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
     8 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
     9 #General Public License for more details.
       
    10 #
       
    11 #You should have received a copy of the GNU General Public
       
    12 #License along with this library; if not, write to the Free Software
       
    13 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    14 
       
    15 import wx
       
    16 import wx.grid
       
    17 
       
    18 class CustomTable(wx.grid.PyGridTableBase):
       
    19     
       
    20     """
       
    21     A custom wx.grid.Grid Table using user supplied data
       
    22     """
       
    23     def __init__(self, parent, data, colnames):
       
    24         # The base class must be initialized *first*
       
    25         wx.grid.PyGridTableBase.__init__(self)
       
    26         self.data = data
       
    27         self.colnames = colnames
       
    28         self.Highlights = {}
       
    29         self.Parent = parent
       
    30         # XXX
       
    31         # we need to store the row length and collength to
       
    32         # see if the table has changed size
       
    33         self._rows = self.GetNumberRows()
       
    34         self._cols = self.GetNumberCols()
       
    35     
       
    36     def GetNumberCols(self):
       
    37         return len(self.colnames)
       
    38         
       
    39     def GetNumberRows(self):
       
    40         return len(self.data)
       
    41 
       
    42     def GetColLabelValue(self, col, translate=True):
       
    43         if col < len(self.colnames):
       
    44             if translate:
       
    45                 return _(self.colnames[col])
       
    46             return self.colnames[col]
       
    47 
       
    48     def GetRowLabelValue(self, row, translate=True):
       
    49         return row
       
    50 
       
    51     def GetValue(self, row, col):
       
    52         if row < self.GetNumberRows():
       
    53             return self.data[row].get(self.GetColLabelValue(col, False), "")
       
    54     
       
    55     def SetValue(self, row, col, value):
       
    56         if col < len(self.colnames):
       
    57             self.data[row][self.GetColLabelValue(col, False)] = value
       
    58     
       
    59     def GetValueByName(self, row, colname):
       
    60         if row < self.GetNumberRows():
       
    61             return self.data[row].get(colname)
       
    62 
       
    63     def SetValueByName(self, row, colname, value):
       
    64         if row < self.GetNumberRows():
       
    65             self.data[row][colname] = value
       
    66 
       
    67     def ResetView(self, grid):
       
    68         """
       
    69         (wx.grid.Grid) -> Reset the grid view.   Call this to
       
    70         update the grid if rows and columns have been added or deleted
       
    71         """
       
    72         grid.BeginBatch()
       
    73         for current, new, delmsg, addmsg in [
       
    74             (self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
       
    75             (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED),
       
    76         ]:
       
    77             if new < current:
       
    78                 msg = wx.grid.GridTableMessage(self,delmsg,new,current-new)
       
    79                 grid.ProcessTableMessage(msg)
       
    80             elif new > current:
       
    81                 msg = wx.grid.GridTableMessage(self,addmsg,new-current)
       
    82                 grid.ProcessTableMessage(msg)
       
    83                 self.UpdateValues(grid)
       
    84         grid.EndBatch()
       
    85 
       
    86         self._rows = self.GetNumberRows()
       
    87         self._cols = self.GetNumberCols()
       
    88         # update the column rendering scheme
       
    89         self._updateColAttrs(grid)
       
    90 
       
    91         # update the scrollbars and the displayed part of the grid
       
    92         grid.AdjustScrollbars()
       
    93         grid.ForceRefresh()
       
    94 
       
    95     def UpdateValues(self, grid):
       
    96         """Update all displayed values"""
       
    97         # This sends an event to the grid table to update all of the values
       
    98         msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
       
    99         grid.ProcessTableMessage(msg)
       
   100 
       
   101     def _updateColAttrs(self, grid):
       
   102         """
       
   103         wx.grid.Grid -> update the column attributes to add the
       
   104         appropriate renderer given the column name.
       
   105 
       
   106         Otherwise default to the default renderer.
       
   107         """
       
   108         for row in range(self.GetNumberRows()):
       
   109             row_highlights = self.Highlights.get(row, {})
       
   110             for col in range(self.GetNumberCols()):
       
   111                 grid.SetReadOnly(row, col, True)
       
   112                 grid.SetCellEditor(row, col, None)
       
   113                 grid.SetCellRenderer(row, col, None)
       
   114                 
       
   115                 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
       
   116                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
       
   117                 grid.SetCellTextColour(row, col, highlight_colours[1])
       
   118             self.ResizeRow(grid, row)
       
   119     
       
   120     def ResizeRow(self, grid, row):
       
   121         if wx.Platform == '__WXMSW__':
       
   122             grid.SetRowMinimalHeight(row, 20)
       
   123         else:
       
   124             grid.SetRowMinimalHeight(row, 28)
       
   125         grid.AutoSizeRow(row, False)
       
   126     
       
   127     def SetData(self, data):
       
   128         self.data = data
       
   129     
       
   130     def GetData(self):
       
   131         return self.data
       
   132     
       
   133     def GetCurrentIndex(self):
       
   134         return self.CurrentIndex
       
   135     
       
   136     def SetCurrentIndex(self, index):
       
   137         self.CurrentIndex = index
       
   138     
       
   139     def AppendRow(self, row_content):
       
   140         self.data.append(row_content)
       
   141 
       
   142     def InsertRow(self, index, row_content):
       
   143         self.data.insert(index, row_content)
       
   144 
       
   145     def MoveRow(self, row_index, move):
       
   146         new_index = max(0, min(row_index + move, len(self.data) - 1))
       
   147         if new_index != row_index:
       
   148             self.data.insert(new_index, self.data.pop(row_index))
       
   149         return new_index
       
   150 
       
   151     def RemoveRow(self, row_index):
       
   152         self.data.pop(row_index)
       
   153 
       
   154     def GetRow(self, row_index):
       
   155         return self.data[row_index]
       
   156 
       
   157     def Empty(self):
       
   158         self.data = []
       
   159 
       
   160     def AddHighlight(self, infos, highlight_type):
       
   161         row_highlights = self.Highlights.setdefault(infos[0], {})
       
   162         col_highlights = row_highlights.setdefault(infos[1], [])
       
   163         col_highlights.append(highlight_type)
       
   164 
       
   165     def ClearHighlights(self, highlight_type=None):
       
   166         if highlight_type is None:
       
   167             self.Highlights = {}
       
   168         else:
       
   169             for row, row_highlights in self.Highlights.iteritems():
       
   170                 row_items = row_highlights.items()
       
   171                 for col, col_highlights in row_items:
       
   172                     if highlight_type in col_highlights:
       
   173                         col_highlights.remove(highlight_type)
       
   174                     if len(col_highlights) == 0:
       
   175                         row_highlights.pop(col)