RessourceEditor.py
changeset 604 5b42b4401e6b
parent 588 4876fedbe9df
child 606 d65122c61eaf
equal deleted inserted replaced
603:25c92309cdae 604:5b42b4401e6b
    24 
    24 
    25 import wx
    25 import wx
    26 import wx.grid
    26 import wx.grid
    27 
    27 
    28 from graphics.GraphicCommons import REFRESH_HIGHLIGHT_PERIOD
    28 from graphics.GraphicCommons import REFRESH_HIGHLIGHT_PERIOD
    29 from controls import CustomGrid, EditorPanel, DurationCellEditor
    29 from controls import CustomGrid, CustomTable, EditorPanel, DurationCellEditor
    30 
    30 
    31 #-------------------------------------------------------------------------------
    31 #-------------------------------------------------------------------------------
    32 #                          Configuration Editor class
    32 #                          Configuration Editor class
    33 #-------------------------------------------------------------------------------
    33 #-------------------------------------------------------------------------------
    34 
    34 
    72 
    72 
    73 def GetInstancesTableColnames():
    73 def GetInstancesTableColnames():
    74     _ = lambda x : x
    74     _ = lambda x : x
    75     return [_("Name"), _("Type"), _("Task")]
    75     return [_("Name"), _("Type"), _("Task")]
    76 
    76 
    77 class ResourceTable(wx.grid.PyGridTableBase):
    77 class ResourceTable(CustomTable):
    78     
    78     
    79     """
    79     """
    80     A custom wx.grid.Grid Table using user supplied data
    80     A custom wx.grid.Grid Table using user supplied data
    81     """
    81     """
    82     def __init__(self, parent, data, colnames):
    82     def __init__(self, parent, data, colnames):
    83         # The base class must be initialized *first*
    83         # The base class must be initialized *first*
    84         wx.grid.PyGridTableBase.__init__(self)
    84         CustomTable.__init__(self, parent, data, colnames)
    85         self.data = data
       
    86         self.colnames = colnames
       
    87         self.Highlights = {}
       
    88         self.Parent = parent
       
    89         
       
    90         self.ColAlignements = []
    85         self.ColAlignements = []
    91         self.ColSizes = []
    86         self.ColSizes = []
    92         # XXX
    87         
    93         # we need to store the row length and collength to
       
    94         # see if the table has changed size
       
    95         self._rows = self.GetNumberRows()
       
    96         self._cols = self.GetNumberCols()
       
    97     
       
    98     def GetColAlignements(self):
    88     def GetColAlignements(self):
    99         return self.ColAlignements
    89         return self.ColAlignements
   100     
    90     
   101     def SetColAlignements(self, list):
    91     def SetColAlignements(self, list):
   102         self.ColAlignements = list
    92         self.ColAlignements = list
   104     def GetColSizes(self):
    94     def GetColSizes(self):
   105         return self.ColSizes
    95         return self.ColSizes
   106     
    96     
   107     def SetColSizes(self, list):
    97     def SetColSizes(self, list):
   108         self.ColSizes = list
    98         self.ColSizes = list
   109 
       
   110     def GetNumberCols(self):
       
   111         return len(self.colnames)
       
   112         
       
   113     def GetNumberRows(self):
       
   114         return len(self.data)
       
   115 
       
   116     def GetColLabelValue(self, col, translate=True):
       
   117         if col < len(self.colnames):
       
   118             if translate:
       
   119                 return _(self.colnames[col])
       
   120             return self.colnames[col]
       
   121 
       
   122     def GetRowLabelValues(self, row, translate=True):
       
   123         return row
       
   124 
    99 
   125     def GetValue(self, row, col):
   100     def GetValue(self, row, col):
   126         if row < self.GetNumberRows():
   101         if row < self.GetNumberRows():
   127             colname = self.GetColLabelValue(col, False)
   102             colname = self.GetColLabelValue(col, False)
   128             value = str(self.data[row].get(colname, ""))
   103             value = str(self.data[row].get(colname, ""))
   129             if colname == "Triggering":
   104             if colname == "Triggering":
   130                 return _(value)
   105                 return _(value)
   131             return value
   106             return value
   132         
   107         
   133     def GetValueByName(self, row, colname):
       
   134         return self.data[row].get(colname)
       
   135 
       
   136     def SetValue(self, row, col, value):
   108     def SetValue(self, row, col, value):
   137         if col < len(self.colnames):
   109         if col < len(self.colnames):
   138             colname = self.GetColLabelValue(col, False)
   110             colname = self.GetColLabelValue(col, False)
   139             if colname == "Triggering":
   111             if colname == "Triggering":
   140                 value = TASKTRIGGERINGOPTIONS_DICT[value]
   112                 value = TASKTRIGGERINGOPTIONS_DICT[value]
   141             self.data[row][colname] = value
   113             self.data[row][colname] = value
   142     
   114     
   143     def SetValueByName(self, row, colname, value):
       
   144         if colname in self.colnames:
       
   145             self.data[row][colname] = value
       
   146     
       
   147     def ResetView(self, grid):
       
   148         """
       
   149         (wx.grid.Grid) -> Reset the grid view.   Call this to
       
   150         update the grid if rows and columns have been added or deleted
       
   151         """
       
   152         grid.BeginBatch()
       
   153         for current, new, delmsg, addmsg in [
       
   154             (self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
       
   155             (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED),
       
   156         ]:
       
   157             if new < current:
       
   158                 msg = wx.grid.GridTableMessage(self,delmsg,new,current-new)
       
   159                 grid.ProcessTableMessage(msg)
       
   160             elif new > current:
       
   161                 msg = wx.grid.GridTableMessage(self,addmsg,new-current)
       
   162                 grid.ProcessTableMessage(msg)
       
   163                 self.UpdateValues(grid)
       
   164         grid.EndBatch()
       
   165 
       
   166         self._rows = self.GetNumberRows()
       
   167         self._cols = self.GetNumberCols()
       
   168         # update the column rendering scheme
       
   169         self._updateColAttrs(grid)
       
   170 
       
   171         # update the scrollbars and the displayed part of the grid
       
   172         grid.AdjustScrollbars()
       
   173         grid.ForceRefresh()
       
   174 
       
   175     def UpdateValues(self, grid):
       
   176         """Update all displayed values"""
       
   177         # This sends an event to the grid table to update all of the values
       
   178         msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
       
   179         grid.ProcessTableMessage(msg)
       
   180 
       
   181     def _updateColAttrs(self, grid):
   115     def _updateColAttrs(self, grid):
   182         """
   116         """
   183         wx.grid.Grid -> update the column attributes to add the
   117         wx.grid.Grid -> update the column attributes to add the
   184         appropriate renderer given the column name.
   118         appropriate renderer given the column name.
   185 
   119 
   229                 grid.SetCellRenderer(row, col, renderer)
   163                 grid.SetCellRenderer(row, col, renderer)
   230                 
   164                 
   231                 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
   165                 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
   232                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
   166                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
   233                 grid.SetCellTextColour(row, col, highlight_colours[1])
   167                 grid.SetCellTextColour(row, col, highlight_colours[1])
   234             if wx.Platform == '__WXMSW__':
   168             self.ResizeRow(grid, row)
   235                 grid.SetRowMinimalHeight(row, 20)
   169 
   236             else:
       
   237                 grid.SetRowMinimalHeight(row, 28)
       
   238             grid.AutoSizeRow(row, False)
       
   239     
       
   240     def SetData(self, data):
       
   241         self.data = data
       
   242     
       
   243     def GetData(self):
       
   244         return self.data
       
   245     
       
   246     def GetCurrentIndex(self):
       
   247         return self.CurrentIndex
       
   248     
       
   249     def SetCurrentIndex(self, index):
       
   250         self.CurrentIndex = index
       
   251     
       
   252     def AppendRow(self, row_content):
       
   253         self.data.append(row_content)
       
   254 
       
   255     def InsertRow(self, index, row_content):
       
   256         self.data.insert(index, row_content)
       
   257 
       
   258     def RemoveRow(self, row_index):
       
   259         self.data.pop(row_index)
       
   260         
       
   261     def MoveRow(self, row_index, move):
       
   262         new_index = max(0, min(row_index + move, len(self.data) - 1))
       
   263         if new_index != row_index:
       
   264             self.data.insert(new_index, self.data.pop(row_index))
       
   265         return new_index
       
   266         
       
   267     def Empty(self):
       
   268         self.data = []
       
   269     
   170     
   270 #-------------------------------------------------------------------------------
   171 #-------------------------------------------------------------------------------
   271 #                        Highlights showing functions
   172 #                        Highlights showing functions
   272 #-------------------------------------------------------------------------------
   173 #-------------------------------------------------------------------------------
   273 
   174