controls/CustomTable.py
changeset 1784 64beb9e9c749
parent 1740 b789b695b5c6
child 1847 6198190bc121
equal deleted inserted replaced
1729:31e63e25b4cc 1784:64beb9e9c749
    28 if wx.Platform == '__WXMSW__':
    28 if wx.Platform == '__WXMSW__':
    29     ROW_HEIGHT = 20
    29     ROW_HEIGHT = 20
    30 else:
    30 else:
    31     ROW_HEIGHT = 28
    31     ROW_HEIGHT = 28
    32 
    32 
       
    33 
    33 class CustomTable(wx.grid.PyGridTableBase):
    34 class CustomTable(wx.grid.PyGridTableBase):
    34     
    35 
    35     """
    36     """
    36     A custom wx.grid.Grid Table using user supplied data
    37     A custom wx.grid.Grid Table using user supplied data
    37     """
    38     """
    38     def __init__(self, parent, data, colnames):
    39     def __init__(self, parent, data, colnames):
    39         # The base class must be initialized *first*
    40         # The base class must be initialized *first*
    45         # XXX
    46         # XXX
    46         # we need to store the row length and collength to
    47         # we need to store the row length and collength to
    47         # see if the table has changed size
    48         # see if the table has changed size
    48         self._rows = self.GetNumberRows()
    49         self._rows = self.GetNumberRows()
    49         self._cols = self.GetNumberCols()
    50         self._cols = self.GetNumberCols()
    50     
    51 
    51     def GetNumberCols(self):
    52     def GetNumberCols(self):
    52         return len(self.colnames)
    53         return len(self.colnames)
    53         
    54 
    54     def GetNumberRows(self):
    55     def GetNumberRows(self):
    55         return len(self.data)
    56         return len(self.data)
    56 
    57 
    57     def GetColLabelValue(self, col, translate=True):
    58     def GetColLabelValue(self, col, translate=True):
    58         if col < len(self.colnames):
    59         if col < len(self.colnames):
    64         return row
    65         return row
    65 
    66 
    66     def GetValue(self, row, col):
    67     def GetValue(self, row, col):
    67         if row < self.GetNumberRows():
    68         if row < self.GetNumberRows():
    68             return self.data[row].get(self.GetColLabelValue(col, False), "")
    69             return self.data[row].get(self.GetColLabelValue(col, False), "")
    69     
    70 
    70     def SetValue(self, row, col, value):
    71     def SetValue(self, row, col, value):
    71         if col < len(self.colnames):
    72         if col < len(self.colnames):
    72             self.data[row][self.GetColLabelValue(col, False)] = value
    73             self.data[row][self.GetColLabelValue(col, False)] = value
    73     
    74 
    74     def GetValueByName(self, row, colname):
    75     def GetValueByName(self, row, colname):
    75         if row < self.GetNumberRows():
    76         if row < self.GetNumberRows():
    76             return self.data[row].get(colname)
    77             return self.data[row].get(colname)
    77 
    78 
    78     def SetValueByName(self, row, colname, value):
    79     def SetValueByName(self, row, colname, value):
    89         for current, new, delmsg, addmsg in [
    90         for current, new, delmsg, addmsg in [
    90             (self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
    91             (self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
    91             (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED),
    92             (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED),
    92         ]:
    93         ]:
    93             if new < current:
    94             if new < current:
    94                 msg = wx.grid.GridTableMessage(self,delmsg,new,current-new)
    95                 msg = wx.grid.GridTableMessage(self, delmsg, new, current-new)
    95                 grid.ProcessTableMessage(msg)
    96                 grid.ProcessTableMessage(msg)
    96             elif new > current:
    97             elif new > current:
    97                 msg = wx.grid.GridTableMessage(self,addmsg,new-current)
    98                 msg = wx.grid.GridTableMessage(self, addmsg, new-current)
    98                 grid.ProcessTableMessage(msg)
    99                 grid.ProcessTableMessage(msg)
    99                 self.UpdateValues(grid)
   100                 self.UpdateValues(grid)
   100         grid.EndBatch()
   101         grid.EndBatch()
   101 
   102 
   102         self._rows = self.GetNumberRows()
   103         self._rows = self.GetNumberRows()
   123         """
   124         """
   124         for row in range(self.GetNumberRows()):
   125         for row in range(self.GetNumberRows()):
   125             row_highlights = self.Highlights.get(row, {})
   126             row_highlights = self.Highlights.get(row, {})
   126             for col in range(self.GetNumberCols()):
   127             for col in range(self.GetNumberCols()):
   127                 colname = self.GetColLabelValue(col, False)
   128                 colname = self.GetColLabelValue(col, False)
   128                 
   129 
   129                 grid.SetReadOnly(row, col, True)
   130                 grid.SetReadOnly(row, col, True)
   130                 grid.SetCellEditor(row, col, None)
   131                 grid.SetCellEditor(row, col, None)
   131                 grid.SetCellRenderer(row, col, None)
   132                 grid.SetCellRenderer(row, col, None)
   132                 
   133 
   133                 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
   134                 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
   134                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
   135                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
   135                 grid.SetCellTextColour(row, col, highlight_colours[1])
   136                 grid.SetCellTextColour(row, col, highlight_colours[1])
   136             self.ResizeRow(grid, row)
   137             self.ResizeRow(grid, row)
   137     
   138 
   138     def ResizeRow(self, grid, row):
   139     def ResizeRow(self, grid, row):
   139         if grid.GetRowSize(row) < ROW_HEIGHT:
   140         if grid.GetRowSize(row) < ROW_HEIGHT:
   140             grid.SetRowMinimalHeight(row, ROW_HEIGHT)
   141             grid.SetRowMinimalHeight(row, ROW_HEIGHT)
   141             grid.AutoSizeRow(row, False)
   142             grid.AutoSizeRow(row, False)
   142     
   143 
   143     def SetData(self, data):
   144     def SetData(self, data):
   144         self.data = data
   145         self.data = data
   145     
   146 
   146     def GetData(self):
   147     def GetData(self):
   147         return self.data
   148         return self.data
   148     
   149 
   149     def GetCurrentIndex(self):
   150     def GetCurrentIndex(self):
   150         return self.CurrentIndex
   151         return self.CurrentIndex
   151     
   152 
   152     def SetCurrentIndex(self, index):
   153     def SetCurrentIndex(self, index):
   153         self.CurrentIndex = index
   154         self.CurrentIndex = index
   154     
   155 
   155     def AppendRow(self, row_content):
   156     def AppendRow(self, row_content):
   156         self.data.append(row_content)
   157         self.data.append(row_content)
   157 
   158 
   158     def InsertRow(self, index, row_content):
   159     def InsertRow(self, index, row_content):
   159         self.data.insert(index, row_content)
   160         self.data.insert(index, row_content)