controls/VariablePanel.py
changeset 604 5b42b4401e6b
parent 600 7db729686416
child 606 d65122c61eaf
equal deleted inserted replaced
603:25c92309cdae 604:5b42b4401e6b
    29 
    29 
    30 from plcopen.structures import LOCATIONDATATYPES, TestIdentifier, IEC_KEYWORDS
    30 from plcopen.structures import LOCATIONDATATYPES, TestIdentifier, IEC_KEYWORDS
    31 from graphics.GraphicCommons import REFRESH_HIGHLIGHT_PERIOD
    31 from graphics.GraphicCommons import REFRESH_HIGHLIGHT_PERIOD
    32 from dialogs.ArrayTypeDialog import ArrayTypeDialog
    32 from dialogs.ArrayTypeDialog import ArrayTypeDialog
    33 from CustomGrid import CustomGrid
    33 from CustomGrid import CustomGrid
       
    34 from CustomTable import CustomTable
    34 from LocationCellEditor import LocationCellEditor
    35 from LocationCellEditor import LocationCellEditor
    35 
    36 
    36 # Compatibility function for wx versions < 2.6
    37 # Compatibility function for wx versions < 2.6
    37 def AppendMenu(parent, help, id, kind, text):
    38 def AppendMenu(parent, help, id, kind, text):
    38     if wx.VERSION >= (2, 6, 0):
    39     if wx.VERSION >= (2, 6, 0):
    82                        "Output": lambda x: {"Retain": "Retain", "Non-Retain": "Non-Retain"}.get(x, ""),
    83                        "Output": lambda x: {"Retain": "Retain", "Non-Retain": "Non-Retain"}.get(x, ""),
    83                        "Global": lambda x: {"Constant": "Constant", "Retain": "Retain"}.get(x, ""),
    84                        "Global": lambda x: {"Constant": "Constant", "Retain": "Retain"}.get(x, ""),
    84                        "External": lambda x: {"Constant": "Constant"}.get(x, "")
    85                        "External": lambda x: {"Constant": "Constant"}.get(x, "")
    85                       }
    86                       }
    86 
    87 
    87 class VariableTable(wx.grid.PyGridTableBase):
    88 class VariableTable(CustomTable):
    88     
    89     
    89     """
    90     """
    90     A custom wx.grid.Grid Table using user supplied data
    91     A custom wx.grid.Grid Table using user supplied data
    91     """
    92     """
    92     def __init__(self, parent, data, colnames):
    93     def __init__(self, parent, data, colnames):
    93         # The base class must be initialized *first*
    94         # The base class must be initialized *first*
    94         wx.grid.PyGridTableBase.__init__(self)
    95         CustomTable.__init__(self, parent, data, colnames)
    95         self.data = data
       
    96         self.old_value = None
    96         self.old_value = None
    97         self.colnames = colnames
    97     
    98         self.Highlights = {}
       
    99         self.Parent = parent
       
   100         # XXX
       
   101         # we need to store the row length and collength to
       
   102         # see if the table has changed size
       
   103         self._rows = self.GetNumberRows()
       
   104         self._cols = self.GetNumberCols()
       
   105     
       
   106     def GetNumberCols(self):
       
   107         return len(self.colnames)
       
   108         
       
   109     def GetNumberRows(self):
       
   110         return len(self.data)
       
   111 
       
   112     def GetColLabelValue(self, col, translate=True):
       
   113         if col < len(self.colnames):
       
   114             if translate:
       
   115                 return _(self.colnames[col])
       
   116             return self.colnames[col]
       
   117 
       
   118     def GetRowLabelValues(self, row, translate=True):
       
   119         return row
       
   120 
       
   121     def GetValue(self, row, col):
    98     def GetValue(self, row, col):
   122         if row < self.GetNumberRows():
    99         if row < self.GetNumberRows():
   123             if col == 0:
   100             if col == 0:
   124                 return self.data[row]["Number"]
   101                 return self.data[row]["Number"]
   125             colname = self.GetColLabelValue(col, False)
   102             colname = self.GetColLabelValue(col, False)
   144                 if value == "External":
   121                 if value == "External":
   145                     self.SetValueByName(row, "Initial Value", "")
   122                     self.SetValueByName(row, "Initial Value", "")
   146             elif colname == "Option":
   123             elif colname == "Option":
   147                 value = OPTIONS_DICT[value]
   124                 value = OPTIONS_DICT[value]
   148             self.data[row][colname] = value
   125             self.data[row][colname] = value
   149     
       
   150     def GetValueByName(self, row, colname):
       
   151         if row < self.GetNumberRows():
       
   152             return self.data[row].get(colname)
       
   153 
       
   154     def SetValueByName(self, row, colname, value):
       
   155         if row < self.GetNumberRows():
       
   156             self.data[row][colname] = value
       
   157 
   126 
   158     def GetOldValue(self):
   127     def GetOldValue(self):
   159         return self.old_value
   128         return self.old_value
   160     
       
   161     def ResetView(self, grid):
       
   162         """
       
   163         (wx.grid.Grid) -> Reset the grid view.   Call this to
       
   164         update the grid if rows and columns have been added or deleted
       
   165         """
       
   166         grid.BeginBatch()
       
   167         for current, new, delmsg, addmsg in [
       
   168             (self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
       
   169             (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED),
       
   170         ]:
       
   171             if new < current:
       
   172                 msg = wx.grid.GridTableMessage(self,delmsg,new,current-new)
       
   173                 grid.ProcessTableMessage(msg)
       
   174             elif new > current:
       
   175                 msg = wx.grid.GridTableMessage(self,addmsg,new-current)
       
   176                 grid.ProcessTableMessage(msg)
       
   177                 self.UpdateValues(grid)
       
   178         grid.EndBatch()
       
   179 
       
   180         self._rows = self.GetNumberRows()
       
   181         self._cols = self.GetNumberCols()
       
   182         # update the column rendering scheme
       
   183         self._updateColAttrs(grid)
       
   184 
       
   185         # update the scrollbars and the displayed part of the grid
       
   186         grid.AdjustScrollbars()
       
   187         grid.ForceRefresh()
       
   188 
       
   189     def UpdateValues(self, grid):
       
   190         """Update all displayed values"""
       
   191         # This sends an event to the grid table to update all of the values
       
   192         msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
       
   193         grid.ProcessTableMessage(msg)
       
   194 
   129 
   195     def _updateColAttrs(self, grid):
   130     def _updateColAttrs(self, grid):
   196         """
   131         """
   197         wx.grid.Grid -> update the column attributes to add the
   132         wx.grid.Grid -> update the column attributes to add the
   198         appropriate renderer given the column name.
   133         appropriate renderer given the column name.
   261                 grid.SetCellRenderer(row, col, renderer)
   196                 grid.SetCellRenderer(row, col, renderer)
   262                 
   197                 
   263                 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
   198                 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
   264                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
   199                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
   265                 grid.SetCellTextColour(row, col, highlight_colours[1])
   200                 grid.SetCellTextColour(row, col, highlight_colours[1])
   266             if wx.Platform == '__WXMSW__':
   201             self.ResizeRow(grid, row)
   267                 grid.SetRowMinimalHeight(row, 20)
       
   268             else:
       
   269                 grid.SetRowMinimalHeight(row, 28)
       
   270             grid.AutoSizeRow(row, False)
       
   271     
       
   272     def SetData(self, data):
       
   273         self.data = data
       
   274     
       
   275     def GetData(self):
       
   276         return self.data
       
   277     
       
   278     def GetCurrentIndex(self):
       
   279         return self.CurrentIndex
       
   280     
       
   281     def SetCurrentIndex(self, index):
       
   282         self.CurrentIndex = index
       
   283     
       
   284     def AppendRow(self, row_content):
       
   285         self.data.append(row_content)
       
   286 
       
   287     def RemoveRow(self, row_index):
       
   288         self.data.pop(row_index)
       
   289 
       
   290     def GetRow(self, row_index):
       
   291         return self.data[row_index]
       
   292 
       
   293     def Empty(self):
       
   294         self.data = []
       
   295         self.editors = []
       
   296 
       
   297     def AddHighlight(self, infos, highlight_type):
       
   298         row_highlights = self.Highlights.setdefault(infos[0], {})
       
   299         col_highlights = row_highlights.setdefault(infos[1], [])
       
   300         col_highlights.append(highlight_type)
       
   301 
       
   302     def ClearHighlights(self, highlight_type=None):
       
   303         if highlight_type is None:
       
   304             self.Highlights = {}
       
   305         else:
       
   306             for row, row_highlights in self.Highlights.iteritems():
       
   307                 row_items = row_highlights.items()
       
   308                 for col, col_highlights in row_items:
       
   309                     if highlight_type in col_highlights:
       
   310                         col_highlights.remove(highlight_type)
       
   311                     if len(col_highlights) == 0:
       
   312                         row_highlights.pop(col)
       
   313                     
   202                     
   314 
   203 
   315 class VariableDropTarget(wx.TextDropTarget):
   204 class VariableDropTarget(wx.TextDropTarget):
   316     '''
   205     '''
   317     This allows dragging a variable location from somewhere to the Location
   206     This allows dragging a variable location from somewhere to the Location