etherlab/ConfigEditor.py
changeset 2038 6f78c4ac22f9
parent 2037 d54036f70390
child 2040 d676082c1d2f
equal deleted inserted replaced
2037:d54036f70390 2038:6f78c4ac22f9
     1 import wx
     1 import wx
     2 import wx.grid
     2 import wx.grid
       
     3 import wx.gizmos
     3 
     4 
     4 from controls import CustomGrid, CustomTable, EditorPanel
     5 from controls import CustomGrid, CustomTable, EditorPanel
     5 
     6 
     6 [ETHERCAT_VENDOR, ETHERCAT_GROUP, ETHERCAT_DEVICE] = range(3)
     7 [ETHERCAT_VENDOR, ETHERCAT_GROUP, ETHERCAT_DEVICE] = range(3)
     7 
     8 
   108                 return row
   109                 return row
   109             return self.data[row].get(self.GetColLabelValue(col, False), "")
   110             return self.data[row].get(self.GetColLabelValue(col, False), "")
   110 
   111 
   111 def GetVariablesTableColnames():
   112 def GetVariablesTableColnames():
   112     _ = lambda x : x
   113     _ = lambda x : x
   113     return ["#", _("Index"), _("SubIndex"), _("Name"), _("Type"), _("PDO index"), _("PDO name"), _("PDO type")]
   114     return ["#", _("Name"), _("Index"), _("SubIndex"), _("Type"), _("PDO index"), _("PDO name"), _("PDO type")]
   114 
       
   115 class VariablesTable(CustomTable):
       
   116     
       
   117     def GetValue(self, row, col):
       
   118         if row < self.GetNumberRows():
       
   119             if col == 0:
       
   120                 return row + 1
       
   121             return self.data[row].get(self.GetColLabelValue(col, False), "")
       
   122 
       
   123     def _updateColAttrs(self, grid):
       
   124         """
       
   125         wx.grid.Grid -> update the column attributes to add the
       
   126         appropriate renderer given the column name.
       
   127 
       
   128         Otherwise default to the default renderer.
       
   129         """
       
   130         for row in range(self.GetNumberRows()):
       
   131             row_highlights = self.Highlights.get(row, {})
       
   132             pdo_mapping = self.GetValueByName(row, "PDOMapping")
       
   133             for col in range(self.GetNumberCols()):
       
   134                 colname = self.GetColLabelValue(col, False)
       
   135                 
       
   136                 if colname in ["PDO index", "PDO name", "PDO type"] and pdo_mapping == "":
       
   137                     highlight_colours = (wx.LIGHT_GREY, wx.WHITE)
       
   138                 else:
       
   139                     highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
       
   140                 grid.SetReadOnly(row, col, True)
       
   141                 grid.SetCellEditor(row, col, None)
       
   142                 grid.SetCellRenderer(row, col, None)
       
   143                 
       
   144                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
       
   145                 grid.SetCellTextColour(row, col, highlight_colours[1])
       
   146             self.ResizeRow(grid, row)
       
   147 
   115 
   148 [ID_SLAVEINFOSPANEL, ID_SLAVEINFOSPANELVENDORLABEL, 
   116 [ID_SLAVEINFOSPANEL, ID_SLAVEINFOSPANELVENDORLABEL, 
   149  ID_SLAVEINFOSPANELVENDOR, ID_SLAVEINFOSPANELPRODUCTCODELABEL, 
   117  ID_SLAVEINFOSPANELVENDOR, ID_SLAVEINFOSPANELPRODUCTCODELABEL, 
   150  ID_SLAVEINFOSPANELPRODUCTCODE, ID_SLAVEINFOSPANELREVISIONNUMBERLABEL, 
   118  ID_SLAVEINFOSPANELPRODUCTCODE, ID_SLAVEINFOSPANELREVISIONNUMBERLABEL, 
   151  ID_SLAVEINFOSPANELREVISIONNUMBER, ID_SLAVEINFOSPANELPHYSICSLABEL, 
   119  ID_SLAVEINFOSPANELREVISIONNUMBER, ID_SLAVEINFOSPANELPHYSICSLABEL, 
   246         
   214         
   247         self.VariablesLabel =  wx.StaticText(id=ID_SLAVEINFOSPANELVARIABLESLABEL,
   215         self.VariablesLabel =  wx.StaticText(id=ID_SLAVEINFOSPANELVARIABLESLABEL,
   248               label=_('Variable entries:'), name='VariablesLabel', parent=self,
   216               label=_('Variable entries:'), name='VariablesLabel', parent=self,
   249               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
   217               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
   250         
   218         
   251         self.VariablesGrid = CustomGrid(id=ID_SLAVEINFOSPANELVARIABLESGRID,
   219         self.VariablesGrid = wx.gizmos.TreeListCtrl(id=ID_SLAVEINFOSPANELVARIABLESGRID,
   252               name='VariablesGrid', parent=self, pos=wx.Point(0, 0), 
   220               name='VariablesGrid', parent=self, pos=wx.Point(0, 0), 
   253               size=wx.Size(0, 0), style=wx.VSCROLL)
   221               size=wx.Size(0, 0), style=wx.TR_DEFAULT_STYLE |
   254         if wx.VERSION >= (2, 5, 0):
   222                                         wx.TR_ROW_LINES |
   255             self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.OnVariablesGridCellLeftClick)
   223                                         wx.TR_COLUMN_LINES |
   256         else:
   224                                         wx.TR_HIDE_ROOT |
   257             wx.grid.EVT_GRID_CELL_LEFT_CLICK(self.VariablesGrid, self.OnVariablesGridCellLeftClick)
   225                                         wx.TR_FULL_ROW_HIGHLIGHT)
   258         
   226         self.VariablesGrid.GetMainWindow().Bind(wx.EVT_LEFT_DOWN, self.OnVariablesGridLeftClick)
       
   227                 
   259         self._init_sizers()
   228         self._init_sizers()
   260         
   229         
   261     def __init__(self, parent, controler):
   230     def __init__(self, parent, controler):
   262         self._init_ctrls(parent)
   231         self._init_ctrls(parent)
   263         
   232         
   275             attr = wx.grid.GridCellAttr()
   244             attr = wx.grid.GridCellAttr()
   276             attr.SetAlignment(self.SyncManagersGridColAlignements[col], wx.ALIGN_CENTRE)
   245             attr.SetAlignment(self.SyncManagersGridColAlignements[col], wx.ALIGN_CENTRE)
   277             self.SyncManagersGrid.SetColAttr(col, attr)
   246             self.SyncManagersGrid.SetColAttr(col, attr)
   278             self.SyncManagersGrid.SetColMinimalWidth(col, self.SyncManagersGridColSizes[col])
   247             self.SyncManagersGrid.SetColMinimalWidth(col, self.SyncManagersGridColSizes[col])
   279             self.SyncManagersGrid.AutoSizeColumn(col, False)
   248             self.SyncManagersGrid.AutoSizeColumn(col, False)
   280         
   249             
   281         self.VariablesTable = VariablesTable(self, [], GetVariablesTableColnames())
   250         for colname, colsize, colalign in zip(GetVariablesTableColnames(),
   282         self.VariablesGrid.SetTable(self.VariablesTable)
   251                                               [40, 150, 100, 100, 150, 100, 150, 100],
   283         self.VariablesGridColAlignements = [wx.ALIGN_RIGHT, wx.ALIGN_RIGHT, wx.ALIGN_RIGHT, 
   252                                               [wx.ALIGN_RIGHT, wx.ALIGN_LEFT, wx.ALIGN_RIGHT, 
   284                                             wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_RIGHT, 
   253                                                wx.ALIGN_RIGHT, wx.ALIGN_LEFT, wx.ALIGN_RIGHT, 
   285                                             wx.ALIGN_LEFT, wx.ALIGN_LEFT]
   254                                                wx.ALIGN_LEFT, wx.ALIGN_LEFT]):
   286         self.VariablesGridColSizes = [40, 100, 100, 150, 150, 100, 150, 100]
   255             self.VariablesGrid.AddColumn(colname, colsize, colalign)
   287         self.VariablesGrid.SetRowLabelSize(0)
   256         self.VariablesGrid.SetMainColumn(1)
   288         for col in range(self.VariablesTable.GetNumberCols()):
   257         
   289             attr = wx.grid.GridCellAttr()
       
   290             attr.SetAlignment(self.VariablesGridColAlignements[col], wx.ALIGN_CENTRE)
       
   291             self.VariablesGrid.SetColAttr(col, attr)
       
   292             self.VariablesGrid.SetColMinimalWidth(col, self.VariablesGridColSizes[col])
       
   293             self.VariablesGrid.AutoSizeColumn(col, False)
       
   294     
       
   295     def SetSlaveInfos(self, slave_pos, slave_infos):
   258     def SetSlaveInfos(self, slave_pos, slave_infos):
   296         self.Slave = slave_pos
   259         self.Slave = slave_pos
   297         if slave_infos is not None:
   260         if slave_infos is not None:
   298             self.Type = slave_infos["device_type"]
   261             self.Type = slave_infos["device_type"]
   299             self.Vendor.SetValue(slave_infos["vendor"])
   262             self.Vendor.SetValue(slave_infos["vendor"])
   300             self.ProductCode.SetValue(slave_infos["product_code"])
   263             self.ProductCode.SetValue(slave_infos["product_code"])
   301             self.RevisionNumber.SetValue(slave_infos["revision_number"])
   264             self.RevisionNumber.SetValue(slave_infos["revision_number"])
   302             self.Physics.SetValue(slave_infos["physics"])
   265             self.Physics.SetValue(slave_infos["physics"])
   303             self.SyncManagersTable.SetData(slave_infos["sync_managers"])
   266             self.SyncManagersTable.SetData(slave_infos["sync_managers"])
   304             self.SyncManagersTable.ResetView(self.SyncManagersGrid)
   267             self.SyncManagersTable.ResetView(self.SyncManagersGrid)
   305             self.VariablesTable.SetData(slave_infos["entries"])
   268             self.RefreshVariablesGrid(slave_infos["entries"])
   306             self.VariablesTable.ResetView(self.VariablesGrid)
       
   307         else:
   269         else:
   308             self.Type = None
   270             self.Type = None
   309             self.Vendor.SetValue("")
   271             self.Vendor.SetValue("")
   310             self.ProductCode.SetValue("")
   272             self.ProductCode.SetValue("")
   311             self.RevisionNumber.SetValue("")
   273             self.RevisionNumber.SetValue("")
   312             self.Physics.SetValue("")
   274             self.Physics.SetValue("")
   313             self.SyncManagersTable.SetData([])
   275             self.SyncManagersTable.SetData([])
   314             self.SyncManagersTable.ResetView(self.SyncManagersGrid)
   276             self.SyncManagersTable.ResetView(self.SyncManagersGrid)
   315             self.VariablesTable.SetData([])
   277             self.RefreshVariablesGrid([])
   316             self.VariablesTable.ResetView(self.VariablesGrid)
       
   317 
       
   318     def OnVariablesGridCellLeftClick(self, event):
       
   319         row = event.GetRow()
       
   320         
       
   321         data_type = self.VariablesTable.GetValueByName(row, "Type")
       
   322         pdo_mapping = self.VariablesTable.GetValueByName(row, "PDOMapping")
       
   323         if (event.GetCol() == 0 and pdo_mapping != "" and
       
   324             self.Controler.GetSizeOfType(data_type) is not None):
       
   325             
   278             
   326             entry_index = self.Controler.ExtractHexDecValue(self.VariablesTable.GetValueByName(row, "Index"))
   279     def RefreshVariablesGrid(self, entries):
   327             entry_subindex = self.Controler.ExtractHexDecValue(self.VariablesTable.GetValueByName(row, "SubIndex"))
   280         root = self.VariablesGrid.GetRootItem()
   328             var_name = "%s_%4.4x_%2.2x" % (self.Type, entry_index, entry_subindex)
   281         if not root.IsOk():
   329             if pdo_mapping in ["R"]:
   282             root = self.VariablesGrid.AddRoot("Slave entries")
   330                 dir = "%I"
   283         self.GenerateVariablesGridBranch(root, entries, GetVariablesTableColnames())
   331             else:
   284         self.VariablesGrid.Expand(root)
   332                 dir = "%Q"
   285         
   333             location = "%s%s" % (dir, self.Controler.GetSizeOfType(data_type)) + \
   286     def GenerateVariablesGridBranch(self, root, entries, colnames, idx=0):
   334                        ".".join(map(lambda x:str(x), self.Controler.GetCurrentLocation() + self.Slave + (entry_index, entry_subindex)))
   287         if wx.VERSION >= (2, 6, 0):
       
   288             item, root_cookie = self.VariablesGrid.GetFirstChild(root)
       
   289         else:
       
   290             item, root_cookie = self.VariablesGrid.GetFirstChild(root, 0)
       
   291         
       
   292         for entry in entries:
       
   293             idx += 1
       
   294             if not item.IsOk():
       
   295                 item = self.VariablesGrid.AppendItem(root, "")
       
   296             for col, colname in enumerate(colnames):
       
   297                 if col == 0:
       
   298                     self.VariablesGrid.SetItemText(item, str(idx), 0)
       
   299                 else:
       
   300                     self.VariablesGrid.SetItemText(item, entry.get(colname, ""), col)
       
   301             if entry["PDOMapping"] == "":
       
   302                 self.VariablesGrid.SetItemBackgroundColour(item, wx.LIGHT_GREY)
       
   303             self.VariablesGrid.SetItemPyData(item, entry)
       
   304             idx = self.GenerateVariablesGridBranch(item, entry["children"], colnames, idx)
       
   305             if wx.Platform != '__WXMSW__':
       
   306                 item, root_cookie = self.VariablesGrid.GetNextChild(root, root_cookie)
       
   307             item, root_cookie = self.VariablesGrid.GetNextChild(root, root_cookie)
       
   308         
       
   309         to_delete = []
       
   310         while item.IsOk():
       
   311             to_delete.append(item)
       
   312             item, root_cookie = self.VariablesGrid.GetNextChild(root, root_cookie)
       
   313         for item in to_delete:
       
   314             self.VariablesGrid.Delete(item)
       
   315         
       
   316         return idx
       
   317 
       
   318     def OnVariablesGridLeftClick(self, event):
       
   319         item, flags, col = self.VariablesGrid.HitTest(event.GetPosition())
       
   320         if item.IsOk():
       
   321             entry = self.VariablesGrid.GetItemPyData(item)
       
   322             data_type = entry.get("Type", "")
       
   323             pdo_mapping = entry.get("PDOMapping", "")
   335             
   324             
   336             data = wx.TextDataObject(str((location, "location", data_type, var_name, "")))
   325             if (col == -1 and pdo_mapping != "" and
   337             dragSource = wx.DropSource(self.VariablesGrid)
   326                 self.Controler.GetSizeOfType(data_type) is not None):
   338             dragSource.SetData(data)
   327                 
   339             dragSource.DoDragDrop()
   328                 entry_index = self.Controler.ExtractHexDecValue(entry.get("Index", "0"))
   340         
   329                 entry_subindex = self.Controler.ExtractHexDecValue(entry.get("SubIndex", "0"))
       
   330                 var_name = "%s_%4.4x_%2.2x" % (self.Type, entry_index, entry_subindex)
       
   331                 if pdo_mapping == "R":
       
   332                     dir = "%I"
       
   333                 else:
       
   334                     dir = "%Q"
       
   335                 location = "%s%s" % (dir, self.Controler.GetSizeOfType(data_type)) + \
       
   336                            ".".join(map(lambda x:str(x), self.Controler.GetCurrentLocation() + self.Slave + (entry_index, entry_subindex)))
       
   337                 
       
   338                 data = wx.TextDataObject(str((location, "location", data_type, var_name, "")))
       
   339                 dragSource = wx.DropSource(self.VariablesGrid)
       
   340                 dragSource.SetData(data)
       
   341                 dragSource.DoDragDrop()
       
   342             
   341         event.Skip()
   343         event.Skip()
   342 
   344 
   343 [ID_SLAVEPANEL, ID_SLAVEPANELTYPELABEL,
   345 [ID_SLAVEPANEL, ID_SLAVEPANELTYPELABEL,
   344  ID_SLAVEPANELTYPE, ID_SLAVEPANELTYPEBROWSE, 
   346  ID_SLAVEPANELTYPE, ID_SLAVEPANELTYPEBROWSE, 
   345  ID_SLAVEPANELALIASLABEL, ID_SLAVEPANELALIAS, 
   347  ID_SLAVEPANELALIASLABEL, ID_SLAVEPANELALIAS,