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