Laurent@2097: import os Laurent@2098: import re Laurent@2098: from types import TupleType Laurent@2097: laurent@2022: import wx laurent@2026: import wx.grid laurent@2038: import wx.gizmos Laurent@2097: import wx.lib.buttons Laurent@2097: Laurent@2098: from plcopen.structures import IEC_KEYWORDS, TestIdentifier Laurent@2097: from controls import CustomGrid, CustomTable, FolderTree Laurent@2071: from editors.ConfTreeNodeEditor import ConfTreeNodeEditor, SCROLLBAR_UNIT Laurent@2097: from util.BitmapLibrary import GetBitmap laurent@2022: laurent@2022: [ETHERCAT_VENDOR, ETHERCAT_GROUP, ETHERCAT_DEVICE] = range(3) laurent@2022: laurent@2022: def AppendMenu(parent, help, id, kind, text): laurent@2022: if wx.VERSION >= (2, 6, 0): laurent@2022: parent.Append(help=help, id=id, kind=kind, text=text) laurent@2022: else: laurent@2022: parent.Append(helpString=help, id=id, kind=kind, item=text) laurent@2022: Laurent@2098: def GetVariablesTableColnames(position=False): laurent@2022: _ = lambda x : x Laurent@2098: colname = ["#"] Laurent@2098: if position: Laurent@2098: colname.append(_("Position")) Laurent@2098: return colname + [_("Name"), _("Index"), _("SubIndex"), _("Type"), _("Access")] Laurent@2097: Laurent@2097: ACCESS_TYPES = { Laurent@2097: 'ro': 'R', Laurent@2097: 'wo': 'W', Laurent@2097: 'rw': 'R/W'} Laurent@2097: Laurent@2097: def GetAccessValue(access, pdo_mapping): Laurent@2098: value = ACCESS_TYPES.get(access, "") Laurent@2097: if pdo_mapping != "": Laurent@2097: value += "/P" Laurent@2097: return value laurent@2034: Laurent@2098: VARIABLES_FILTERS = [ Laurent@2098: (_("All"), (0x0000, 0xffff)), Laurent@2098: (_("Communication Parameters"), (0x1000, 0x1fff)), Laurent@2098: (_("Manufacturer Specific"), (0x2000, 0x5fff)), Laurent@2098: (_("Standardized Device Profile"), (0x6000, 0x9fff))] Laurent@2098: Laurent@2098: ETHERCAT_INDEX_MODEL = re.compile("#x([0-9a-fA-F]{0,4})$") Laurent@2098: ETHERCAT_SUBINDEX_MODEL = re.compile("#x([0-9a-fA-F]{0,2})$") Laurent@2098: LOCATION_MODEL = re.compile("(?:%[IQM](?:[XBWLD]?([0-9]+(?:\.[0-9]+)*)))$") Laurent@2098: Laurent@2098: class NodeVariablesSizer(wx.FlexGridSizer): Laurent@2098: Laurent@2098: def __init__(self, parent, controler, position_column=False): Laurent@2098: wx.FlexGridSizer.__init__(self, cols=1, hgap=0, rows=2, vgap=5) Laurent@2098: self.AddGrowableCol(0) Laurent@2098: self.AddGrowableRow(1) Laurent@2098: Laurent@2098: self.Controler = controler Laurent@2098: self.PositionColumn = position_column Laurent@2098: Laurent@2105: self.VariablesFilter = wx.ComboBox(parent, style=wx.TE_PROCESS_ENTER) Laurent@2098: self.VariablesFilter.Bind(wx.EVT_COMBOBOX, self.OnVariablesFilterChanged) Laurent@2105: self.VariablesFilter.Bind(wx.EVT_TEXT_ENTER, self.OnVariablesFilterChanged) Laurent@2098: self.AddWindow(self.VariablesFilter, flag=wx.GROW) Laurent@2098: Laurent@2098: self.VariablesGrid = wx.gizmos.TreeListCtrl(parent, Laurent@2098: style=wx.TR_DEFAULT_STYLE | Laurent@2098: wx.TR_ROW_LINES | Laurent@2098: wx.TR_COLUMN_LINES | Laurent@2098: wx.TR_HIDE_ROOT | Laurent@2098: wx.TR_FULL_ROW_HIGHLIGHT) Laurent@2098: self.VariablesGrid.GetMainWindow().Bind(wx.EVT_LEFT_DOWN, Laurent@2097: self.OnVariablesGridLeftClick) Laurent@2098: self.AddWindow(self.VariablesGrid, flag=wx.GROW) Laurent@2098: Laurent@2098: self.Filters = [] Laurent@2098: for desc, value in VARIABLES_FILTERS: Laurent@2098: self.VariablesFilter.Append(desc) Laurent@2098: self.Filters.append(value) Laurent@2098: Laurent@2098: self.VariablesFilter.SetSelection(0) Laurent@2098: self.CurrentFilter = self.Filters[0] Laurent@2098: Laurent@2098: if position_column: Laurent@2098: for colname, colsize, colalign in zip(GetVariablesTableColnames(position_column), Laurent@2098: [40, 80, 350, 80, 100, 80, 80], Laurent@2098: [wx.ALIGN_RIGHT, wx.ALIGN_RIGHT, wx.ALIGN_LEFT, Laurent@2098: wx.ALIGN_RIGHT, wx.ALIGN_RIGHT, wx.ALIGN_LEFT, Laurent@2098: wx.ALIGN_LEFT]): Laurent@2098: self.VariablesGrid.AddColumn(_(colname), colsize, colalign) Laurent@2098: self.VariablesGrid.SetMainColumn(2) Laurent@2098: else: Laurent@2098: for colname, colsize, colalign in zip(GetVariablesTableColnames(), Laurent@2098: [40, 350, 80, 100, 80, 80], Laurent@2098: [wx.ALIGN_RIGHT, wx.ALIGN_LEFT, wx.ALIGN_RIGHT, Laurent@2098: wx.ALIGN_RIGHT, wx.ALIGN_LEFT, wx.ALIGN_LEFT]): Laurent@2098: self.VariablesGrid.AddColumn(_(colname), colsize, colalign) Laurent@2098: self.VariablesGrid.SetMainColumn(1) Laurent@2098: laurent@2041: def RefreshView(self): Laurent@2098: entries = self.Controler.GetSlaveVariables(self.CurrentFilter) Laurent@2098: self.RefreshVariablesGrid(entries) laurent@2041: laurent@2038: def RefreshVariablesGrid(self, entries): laurent@2038: root = self.VariablesGrid.GetRootItem() laurent@2038: if not root.IsOk(): Laurent@2098: root = self.VariablesGrid.AddRoot(_("Slave entries")) Laurent@2098: self.GenerateVariablesGridBranch(root, entries, GetVariablesTableColnames(self.PositionColumn)) laurent@2038: self.VariablesGrid.Expand(root) laurent@2038: laurent@2038: def GenerateVariablesGridBranch(self, root, entries, colnames, idx=0): Laurent@2097: item, root_cookie = self.VariablesGrid.GetFirstChild(root) laurent@2038: laurent@2056: no_more_items = not item.IsOk() laurent@2038: for entry in entries: laurent@2038: idx += 1 laurent@2056: if no_more_items: laurent@2038: item = self.VariablesGrid.AppendItem(root, "") laurent@2038: for col, colname in enumerate(colnames): laurent@2038: if col == 0: laurent@2038: self.VariablesGrid.SetItemText(item, str(idx), 0) laurent@2038: else: Laurent@2097: value = entry.get(colname, "") Laurent@2097: if colname == "Access": Laurent@2097: value = GetAccessValue(value, entry.get("PDOMapping", "")) Laurent@2097: self.VariablesGrid.SetItemText(item, value, col) laurent@2038: if entry["PDOMapping"] == "": laurent@2038: self.VariablesGrid.SetItemBackgroundColour(item, wx.LIGHT_GREY) Laurent@2098: else: Laurent@2098: self.VariablesGrid.SetItemBackgroundColour(item, wx.WHITE) laurent@2038: self.VariablesGrid.SetItemPyData(item, entry) laurent@2056: idx = self.GenerateVariablesGridBranch(item, entry["children"], colnames, idx) laurent@2056: if not no_more_items: laurent@2041: item, root_cookie = self.VariablesGrid.GetNextChild(root, root_cookie) laurent@2056: no_more_items = not item.IsOk() laurent@2056: laurent@2056: if not no_more_items: laurent@2056: to_delete = [] laurent@2056: while item.IsOk(): laurent@2056: to_delete.append(item) laurent@2056: item, root_cookie = self.VariablesGrid.GetNextChild(root, root_cookie) laurent@2056: for item in to_delete: laurent@2056: self.VariablesGrid.Delete(item) laurent@2038: laurent@2038: return idx Laurent@2098: Laurent@2098: def OnVariablesFilterChanged(self, event): Laurent@2098: filter = self.VariablesFilter.GetSelection() Laurent@2098: if filter != -1: Laurent@2098: self.CurrentFilter = self.Filters[filter] Laurent@2098: self.RefreshView() Laurent@2098: else: Laurent@2098: try: Laurent@2098: value = self.VariablesFilter.GetValue() Laurent@2098: result = ETHERCAT_INDEX_MODEL.match(value) Laurent@2098: if result is not None: Laurent@2098: value = result.group(1) Laurent@2105: index = int(value, 16) Laurent@2098: self.CurrentFilter = (index, index) Laurent@2098: self.RefreshView() Laurent@2098: except: Laurent@2098: pass Laurent@2098: event.Skip() Laurent@2098: laurent@2038: def OnVariablesGridLeftClick(self, event): laurent@2038: item, flags, col = self.VariablesGrid.HitTest(event.GetPosition()) laurent@2038: if item.IsOk(): laurent@2038: entry = self.VariablesGrid.GetItemPyData(item) laurent@2038: data_type = entry.get("Type", "") Laurent@2098: data_size = self.Controler.GetSizeOfType(data_type) laurent@2029: Laurent@2098: if col == -1 and data_size is not None: Laurent@2098: pdo_mapping = entry.get("PDOMapping", "") Laurent@2098: access = entry.get("Access", "") laurent@2038: entry_index = self.Controler.ExtractHexDecValue(entry.get("Index", "0")) laurent@2038: entry_subindex = self.Controler.ExtractHexDecValue(entry.get("SubIndex", "0")) Laurent@2098: if self.PositionColumn: Laurent@2098: slave_pos = self.Controler.ExtractHexDecValue(entry.get("Position", "0")) laurent@2038: else: Laurent@2098: slave_pos = self.Controler.GetSlavePos() laurent@2038: Laurent@2098: if pdo_mapping != "": Laurent@2098: var_name = "%s_%4.4x_%2.2x" % (self.Controler.CTNName(), entry_index, entry_subindex) Laurent@2098: if pdo_mapping == "R": Laurent@2098: dir = "%I" Laurent@2098: else: Laurent@2098: dir = "%Q" Laurent@2098: location = "%s%s" % (dir, data_size) + \ Laurent@2098: ".".join(map(lambda x:str(x), self.Controler.GetCurrentLocation() + Laurent@2098: (slave_pos, entry_index, entry_subindex))) Laurent@2098: Laurent@2098: data = wx.TextDataObject(str((location, "location", data_type, var_name, "", access))) Laurent@2098: dragSource = wx.DropSource(self.VariablesGrid) Laurent@2098: dragSource.SetData(data) Laurent@2098: dragSource.DoDragDrop() Laurent@2098: return Laurent@2098: Laurent@2098: elif self.ColumnPosition: Laurent@2098: location = self.Controler.GetCurrentLocation() +\ Laurent@2098: (slave_pos, entry_index, entry_subindex) Laurent@2098: data = wx.TextDataObject(str((location, "variable", access))) Laurent@2098: dragSource = wx.DropSource(self.VariablesGrid) Laurent@2098: dragSource.SetData(data) Laurent@2098: dragSource.DoDragDrop() Laurent@2098: return Laurent@2098: Laurent@2098: event.Skip() Laurent@2098: Laurent@2098: class NodeEditor(ConfTreeNodeEditor): Laurent@2098: Laurent@2098: CONFNODEEDITOR_TABS = [ Laurent@2098: (_("Ethercat node"), "_create_EthercatNodeEditor")] Laurent@2098: Laurent@2098: def _create_EthercatNodeEditor(self, prnt): Laurent@2098: self.EthercatNodeEditor = wx.Panel(prnt, style=wx.TAB_TRAVERSAL) Laurent@2098: Laurent@2098: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) Laurent@2098: main_sizer.AddGrowableCol(0) Laurent@2098: main_sizer.AddGrowableRow(1) Laurent@2098: Laurent@2098: variables_label = wx.StaticText(self.EthercatNodeEditor, Laurent@2098: label=_('Variable entries:')) Laurent@2098: main_sizer.AddWindow(variables_label, border=10, flag=wx.TOP|wx.LEFT|wx.RIGHT) Laurent@2098: Laurent@2098: self.NodeVariables = NodeVariablesSizer(self.EthercatNodeEditor, self.Controler) Laurent@2098: main_sizer.AddSizer(self.NodeVariables, border=10, Laurent@2098: flag=wx.GROW|wx.BOTTOM|wx.LEFT|wx.RIGHT) Laurent@2098: Laurent@2098: self.EthercatNodeEditor.SetSizer(main_sizer) Laurent@2098: Laurent@2098: return self.EthercatNodeEditor Laurent@2098: Laurent@2098: def __init__(self, parent, controler, window): Laurent@2098: ConfTreeNodeEditor.__init__(self, parent, controler, window) Laurent@2098: Laurent@2098: def GetBufferState(self): Laurent@2098: return False, False Laurent@2098: Laurent@2098: def RefreshView(self): Laurent@2098: ConfTreeNodeEditor.RefreshView(self) Laurent@2098: Laurent@2098: self.NodeVariables.RefreshView() Laurent@2098: Laurent@2098: CIA402NodeEditor = NodeEditor Laurent@2098: Laurent@2098: def GetProcessVariablesTableColnames(): Laurent@2098: _ = lambda x : x Laurent@2098: return ["#", _("Name"), Laurent@2098: _("Read from (nodeid, index, subindex)"), Laurent@2098: _("Write to (nodeid, index, subindex)"), Laurent@2098: _("Description")] Laurent@2098: Laurent@2098: class ProcessVariablesTable(CustomTable): Laurent@2098: Laurent@2098: def GetValue(self, row, col): Laurent@2098: if row < self.GetNumberRows(): Laurent@2098: if col == 0: Laurent@2098: return row + 1 Laurent@2098: colname = self.GetColLabelValue(col, False) Laurent@2098: if colname.startswith("Read from"): Laurent@2098: value = self.data[row].get("ReadFrom", "") Laurent@2098: if value == "": Laurent@2098: return value Laurent@2100: return "%d, #x%0.4X, #x%0.2X" % value Laurent@2098: elif colname.startswith("Write to"): Laurent@2098: value = self.data[row].get("WriteTo", "") Laurent@2098: if value == "": Laurent@2098: return value Laurent@2100: return "%d, #x%0.4X, #x%0.2X" % value Laurent@2098: return self.data[row].get(colname, "") Laurent@2098: Laurent@2098: def SetValue(self, row, col, value): Laurent@2098: if col < len(self.colnames): Laurent@2098: colname = self.GetColLabelValue(col, False) Laurent@2098: if colname.startswith("Read from"): Laurent@2098: self.data[row]["ReadFrom"] = value Laurent@2098: elif colname.startswith("Write to"): Laurent@2098: self.data[row]["WriteTo"] = value Laurent@2098: else: Laurent@2098: self.data[row][colname] = value Laurent@2098: Laurent@2098: def _updateColAttrs(self, grid): Laurent@2098: """ Laurent@2098: wx.grid.Grid -> update the column attributes to add the Laurent@2098: appropriate renderer given the column name. Laurent@2098: Laurent@2098: Otherwise default to the default renderer. Laurent@2098: """ Laurent@2098: for row in range(self.GetNumberRows()): Laurent@2098: for col in range(self.GetNumberCols()): Laurent@2098: editor = None Laurent@2098: renderer = None Laurent@2098: colname = self.GetColLabelValue(col, False) Laurent@2098: if colname in ["Name", "Description"]: Laurent@2098: editor = wx.grid.GridCellTextEditor() Laurent@2098: renderer = wx.grid.GridCellStringRenderer() Laurent@2098: grid.SetReadOnly(row, col, False) Laurent@2098: else: Laurent@2098: grid.SetReadOnly(row, col, True) Laurent@2098: Laurent@2098: grid.SetCellEditor(row, col, editor) Laurent@2098: grid.SetCellRenderer(row, col, renderer) Laurent@2098: Laurent@2098: self.ResizeRow(grid, row) Laurent@2098: Laurent@2098: class ProcessVariableDropTarget(wx.TextDropTarget): Laurent@2098: Laurent@2098: def __init__(self, parent): Laurent@2098: wx.TextDropTarget.__init__(self) Laurent@2098: self.ParentWindow = parent Laurent@2098: Laurent@2098: def OnDropText(self, x, y, data): Laurent@2098: self.ParentWindow.Select() Laurent@2098: x, y = self.ParentWindow.ProcessVariablesGrid.CalcUnscrolledPosition(x, y) Laurent@2098: col = self.ParentWindow.ProcessVariablesGrid.XToCol(x) Laurent@2098: row = self.ParentWindow.ProcessVariablesGrid.YToRow(y - self.ParentWindow.ProcessVariablesGrid.GetColLabelSize()) Laurent@2098: message = None Laurent@2098: try: Laurent@2098: values = eval(data) Laurent@2098: except: Laurent@2098: message = _("Invalid value \"%s\" for process variable")%data Laurent@2098: values = None Laurent@2098: if not isinstance(values, TupleType): Laurent@2098: message = _("Invalid value \"%s\" for process variable")%data Laurent@2098: values = None Laurent@2105: if values is not None and col != wx.NOT_FOUND and row != wx.NOT_FOUND and 2 <= col <= 3: Laurent@2098: location = None Laurent@2098: if values[1] == "location": Laurent@2098: result = LOCATION_MODEL.match(values[0]) Laurent@2098: if result is not None: Laurent@2098: location = map(int, result.group(1).split('.')) Laurent@2098: master_location = self.ParentWindow.GetMasterLocation() Laurent@2098: if (master_location == tuple(location[:len(master_location)]) and Laurent@2098: len(location) - len(master_location) == 3): Laurent@2099: values = tuple(location[len(master_location):]) Laurent@2099: var_type = self.ParentWindow.Controler.GetSlaveVariableDataType(*values) Laurent@2098: if col == 2: Laurent@2099: other_values = self.ParentWindow.ProcessVariablesTable.GetValueByName(row, "WriteTo") Laurent@2098: else: Laurent@2099: other_values = self.ParentWindow.ProcessVariablesTable.GetValueByName(row, "ReadFrom") Laurent@2099: if other_values != "": Laurent@2099: other_type = self.ParentWindow.Controler.GetSlaveVariableDataType(*other_values) Laurent@2099: else: Laurent@2099: other_type = None Laurent@2099: if other_type is None or var_type == other_type: Laurent@2099: if col == 2: Laurent@2099: self.ParentWindow.ProcessVariablesTable.SetValueByName(row, "ReadFrom", values) Laurent@2099: else: Laurent@2099: self.ParentWindow.ProcessVariablesTable.SetValueByName(row, "WriteTo", values) Laurent@2099: self.ParentWindow.SaveProcessVariables() Laurent@2099: self.ParentWindow.RefreshProcessVariables() Laurent@2099: else: Laurent@2099: message = _("'Read from' and 'Write to' variables types are not compatible") Laurent@2098: else: Laurent@2098: message = _("Invalid value \"%s\" for process variable")%data Laurent@2098: Laurent@2098: if message is not None: Laurent@2098: wx.CallAfter(self.ShowMessage, message) Laurent@2098: Laurent@2098: def ShowMessage(self, message): Laurent@2098: message = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK|wx.ICON_ERROR) Laurent@2098: message.ShowModal() Laurent@2098: message.Destroy() Laurent@2098: Laurent@2098: def GetStartupCommandsTableColnames(): Laurent@2098: _ = lambda x : x Laurent@2098: return [_("Position"), _("Index"), _("Subindex"), _("Value"), _("Description")] Laurent@2098: Laurent@2098: class StartupCommandDropTarget(wx.TextDropTarget): Laurent@2098: Laurent@2098: def __init__(self, parent): Laurent@2098: wx.TextDropTarget.__init__(self) Laurent@2098: self.ParentWindow = parent Laurent@2098: Laurent@2098: def OnDropText(self, x, y, data): Laurent@2098: self.ParentWindow.Select() Laurent@2098: message = None Laurent@2098: try: Laurent@2098: values = eval(data) Laurent@2098: except: Laurent@2098: message = _("Invalid value \"%s\" for startup command")%data Laurent@2098: values = None Laurent@2098: if not isinstance(values, TupleType): Laurent@2098: message = _("Invalid value \"%s\" for startup command")%data Laurent@2098: values = None Laurent@2098: if values is not None: Laurent@2098: location = None Laurent@2098: if values[1] == "location": Laurent@2098: result = LOCATION_MODEL.match(values[0]) Laurent@2099: if result is not None and len(values) > 5: Laurent@2098: location = map(int, result.group(1).split('.')) Laurent@2098: access = values[5] Laurent@2098: elif values[1] == "variable": Laurent@2098: location = values[0] Laurent@2098: access = values[2] Laurent@2098: if location is not None: Laurent@2098: master_location = self.ParentWindow.GetMasterLocation() Laurent@2098: if (master_location == tuple(location[:len(master_location)]) and Laurent@2098: len(location) - len(master_location) == 3): Laurent@2098: if access in ["wo", "rw"]: Laurent@2098: self.ParentWindow.AddStartupCommand(*location[len(master_location):]) Laurent@2098: else: Laurent@2098: message = _("Entry can't be write through SDO") Laurent@2098: else: Laurent@2098: message = _("Invalid value \"%s\" for startup command")%data Laurent@2098: Laurent@2098: if message is not None: Laurent@2098: wx.CallAfter(self.ShowMessage, message) Laurent@2098: Laurent@2098: def ShowMessage(self, message): Laurent@2098: message = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK|wx.ICON_ERROR) Laurent@2098: message.ShowModal() Laurent@2098: message.Destroy() Laurent@2098: Laurent@2098: class StartupCommandsTable(CustomTable): Laurent@2098: Laurent@2098: """ Laurent@2098: A custom wx.grid.Grid Table using user supplied data Laurent@2098: """ Laurent@2098: def __init__(self, parent, data, colnames): Laurent@2098: # The base class must be initialized *first* Laurent@2098: CustomTable.__init__(self, parent, data, colnames) Laurent@2098: self.old_value = None Laurent@2098: Laurent@2098: def GetValue(self, row, col): Laurent@2098: if row < self.GetNumberRows(): Laurent@2098: colname = self.GetColLabelValue(col, False) Laurent@2098: value = self.data[row].get(colname, "") Laurent@2098: if colname == "Index": Laurent@2100: return "#x%0.4X" % value Laurent@2098: elif colname == "Subindex": Laurent@2100: return "#x%0.2X" % value Laurent@2098: return value Laurent@2098: Laurent@2098: def SetValue(self, row, col, value): Laurent@2098: if col < len(self.colnames): Laurent@2098: colname = self.GetColLabelValue(col, False) Laurent@2098: if colname in ["Index", "Subindex"]: Laurent@2098: if colname == "Index": Laurent@2098: result = ETHERCAT_INDEX_MODEL.match(value) Laurent@2098: else: Laurent@2098: result = ETHERCAT_SUBINDEX_MODEL.match(value) Laurent@2098: if result is None: Laurent@2098: return Laurent@2098: value = int(result.group(1), 16) Laurent@2098: elif colname == "Value": Laurent@2098: value = int(value) Laurent@2098: elif colname == "Position": Laurent@2098: self.old_value = self.data[row][colname] Laurent@2098: value = int(value) Laurent@2098: self.data[row][colname] = value Laurent@2098: Laurent@2098: def GetOldValue(self): Laurent@2098: return self.old_value Laurent@2098: Laurent@2098: def _updateColAttrs(self, grid): Laurent@2098: """ Laurent@2098: wx.grid.Grid -> update the column attributes to add the Laurent@2098: appropriate renderer given the column name. Laurent@2098: Laurent@2098: Otherwise default to the default renderer. Laurent@2098: """ Laurent@2098: for row in range(self.GetNumberRows()): Laurent@2098: for col in range(self.GetNumberCols()): Laurent@2098: editor = None Laurent@2098: renderer = None Laurent@2098: colname = self.GetColLabelValue(col, False) Laurent@2098: if colname in ["Position", "Value"]: Laurent@2098: editor = wx.grid.GridCellNumberEditor() Laurent@2098: renderer = wx.grid.GridCellNumberRenderer() Laurent@2098: else: Laurent@2098: editor = wx.grid.GridCellTextEditor() Laurent@2098: renderer = wx.grid.GridCellStringRenderer() Laurent@2098: Laurent@2098: grid.SetCellEditor(row, col, editor) Laurent@2098: grid.SetCellRenderer(row, col, renderer) Laurent@2098: grid.SetReadOnly(row, col, False) Laurent@2098: Laurent@2098: self.ResizeRow(grid, row) Laurent@2098: Laurent@2098: def GetCommandIndex(self, position, command_idx): Laurent@2098: for row, command in enumerate(self.data): Laurent@2098: if command["Position"] == position and command["command_idx"] == command_idx: Laurent@2098: return row Laurent@2098: return None Laurent@2098: Laurent@2098: class MasterNodesVariablesSizer(NodeVariablesSizer): Laurent@2098: Laurent@2098: def __init__(self, parent, controler): Laurent@2098: NodeVariablesSizer.__init__(self, parent, controler, True) Laurent@2098: Laurent@2098: self.CurrentNodesFilter = {} Laurent@2098: Laurent@2098: def SetCurrentNodesFilter(self, nodes_filter): Laurent@2098: self.CurrentNodesFilter = nodes_filter Laurent@2098: Laurent@2098: def RefreshView(self): Laurent@2098: if self.CurrentNodesFilter is not None: Laurent@2098: args = self.CurrentNodesFilter.copy() Laurent@2098: args["limits"] = self.CurrentFilter Laurent@2098: entries = self.Controler.GetNodesVariables(**args) Laurent@2098: self.RefreshVariablesGrid(entries) Laurent@2098: Laurent@2098: class MasterEditor(ConfTreeNodeEditor): Laurent@2098: Laurent@2098: CONFNODEEDITOR_TABS = [ Laurent@2098: (_("Network"), "_create_EthercatMasterEditor")] Laurent@2098: Laurent@2098: def _create_EthercatMasterEditor(self, prnt): Laurent@2098: self.EthercatMasterEditor = wx.ScrolledWindow(prnt, Laurent@2098: style=wx.TAB_TRAVERSAL|wx.HSCROLL|wx.VSCROLL) Laurent@2098: self.EthercatMasterEditor.Bind(wx.EVT_SIZE, self.OnResize) Laurent@2098: Laurent@2098: main_sizer = wx.BoxSizer(wx.VERTICAL) Laurent@2098: Laurent@2098: self.NodesFilter = wx.ComboBox(self.EthercatMasterEditor, Laurent@2098: style=wx.TE_PROCESS_ENTER) Laurent@2098: self.Bind(wx.EVT_COMBOBOX, self.OnNodesFilterChanged, self.NodesFilter) Laurent@2098: self.Bind(wx.EVT_TEXT_ENTER, self.OnNodesFilterChanged, self.NodesFilter) Laurent@2098: Laurent@2098: process_variables_header = wx.BoxSizer(wx.HORIZONTAL) Laurent@2098: Laurent@2098: process_variables_label = wx.StaticText(self.EthercatMasterEditor, Laurent@2098: label=_("Process variables mapped between nodes:")) Laurent@2098: process_variables_header.AddWindow(process_variables_label, 1, Laurent@2098: flag=wx.ALIGN_CENTER_VERTICAL) Laurent@2098: Laurent@2098: for name, bitmap, help in [ Laurent@2098: ("AddVariableButton", "add_element", _("Add process variable")), Laurent@2098: ("DeleteVariableButton", "remove_element", _("Remove process variable")), Laurent@2098: ("UpVariableButton", "up", _("Move process variable up")), Laurent@2098: ("DownVariableButton", "down", _("Move process variable down"))]: Laurent@2098: button = wx.lib.buttons.GenBitmapButton(self.EthercatMasterEditor, bitmap=GetBitmap(bitmap), Laurent@2098: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@2098: button.SetToolTipString(help) Laurent@2098: setattr(self, name, button) Laurent@2098: process_variables_header.AddWindow(button, border=5, flag=wx.LEFT) Laurent@2098: Laurent@2098: self.ProcessVariablesGrid = CustomGrid(self.EthercatMasterEditor, style=wx.VSCROLL) Laurent@2098: self.ProcessVariablesGrid.SetMinSize(wx.Size(0, 150)) Laurent@2098: self.ProcessVariablesGrid.SetDropTarget(ProcessVariableDropTarget(self)) Laurent@2098: self.ProcessVariablesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, Laurent@2098: self.OnProcessVariablesGridCellChange) Laurent@2098: self.ProcessVariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, Laurent@2098: self.OnProcessVariablesGridCellLeftClick) Laurent@2099: self.ProcessVariablesGrid.Bind(wx.EVT_KEY_DOWN, self.OnProcessVariablesGridKeyDown) Laurent@2098: Laurent@2098: startup_commands_header = wx.BoxSizer(wx.HORIZONTAL) Laurent@2098: Laurent@2098: startup_commands_label = wx.StaticText(self.EthercatMasterEditor, Laurent@2098: label=_("Startup service variables assignments:")) Laurent@2098: startup_commands_header.AddWindow(startup_commands_label, 1, Laurent@2098: flag=wx.ALIGN_CENTER_VERTICAL) Laurent@2098: Laurent@2098: for name, bitmap, help in [ Laurent@2098: ("AddCommandButton", "add_element", _("Add startup service variable")), Laurent@2098: ("DeleteCommandButton", "remove_element", _("Remove startup service variable"))]: Laurent@2098: button = wx.lib.buttons.GenBitmapButton(self.EthercatMasterEditor, bitmap=GetBitmap(bitmap), Laurent@2098: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@2098: button.SetToolTipString(help) Laurent@2098: setattr(self, name, button) Laurent@2098: startup_commands_header.AddWindow(button, border=5, flag=wx.LEFT) Laurent@2098: Laurent@2098: self.StartupCommandsGrid = CustomGrid(self.EthercatMasterEditor, style=wx.VSCROLL) Laurent@2098: self.StartupCommandsGrid.SetDropTarget(StartupCommandDropTarget(self)) Laurent@2098: self.StartupCommandsGrid.SetMinSize(wx.Size(0, 150)) Laurent@2098: self.StartupCommandsGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, Laurent@2098: self.OnStartupCommandsGridCellChange) Laurent@2100: self.StartupCommandsGrid.Bind(wx.grid.EVT_GRID_EDITOR_SHOWN, Laurent@2100: self.OnStartupCommandsGridEditorShow) Laurent@2098: Laurent@2098: second_staticbox = wx.StaticBox(self.EthercatMasterEditor, label=_("Nodes variables filter:")) Laurent@2098: second_staticbox_sizer = wx.StaticBoxSizer(second_staticbox, wx.VERTICAL) Laurent@2098: Laurent@2098: self.NodesVariables = MasterNodesVariablesSizer(self.EthercatMasterEditor, self.Controler) Laurent@2098: second_staticbox_sizer.AddSizer(self.NodesVariables, 1, border=5, flag=wx.GROW|wx.ALL) Laurent@2098: Laurent@2098: main_staticbox = wx.StaticBox(self.EthercatMasterEditor, label=_("Node filter:")) Laurent@2098: staticbox_sizer = wx.StaticBoxSizer(main_staticbox, wx.VERTICAL) Laurent@2105: main_sizer.AddSizer(staticbox_sizer, 1, border=10, flag=wx.GROW|wx.ALL) Laurent@2105: main_staticbox_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=6, vgap=0) Laurent@2098: main_staticbox_sizer.AddGrowableCol(0) Laurent@2105: main_staticbox_sizer.AddGrowableRow(2) Laurent@2105: main_staticbox_sizer.AddGrowableRow(4) Laurent@2105: main_staticbox_sizer.AddGrowableRow(5) Laurent@2098: staticbox_sizer.AddSizer(main_staticbox_sizer, 1, flag=wx.GROW) Laurent@2098: main_staticbox_sizer.AddWindow(self.NodesFilter, border=5, flag=wx.GROW|wx.ALL) Laurent@2098: main_staticbox_sizer.AddSizer(process_variables_header, border=5, Laurent@2098: flag=wx.GROW|wx.LEFT|wx.RIGHT|wx.BOTTOM) Laurent@2098: main_staticbox_sizer.AddWindow(self.ProcessVariablesGrid, 1, Laurent@2098: border=5, flag=wx.GROW|wx.LEFT|wx.RIGHT|wx.BOTTOM) Laurent@2098: main_staticbox_sizer.AddSizer(startup_commands_header, Laurent@2098: border=5, flag=wx.GROW|wx.LEFT|wx.RIGHT|wx.BOTTOM) Laurent@2098: main_staticbox_sizer.AddWindow(self.StartupCommandsGrid, 1, Laurent@2098: border=5, flag=wx.GROW|wx.LEFT|wx.RIGHT|wx.BOTTOM) Laurent@2098: main_staticbox_sizer.AddSizer(second_staticbox_sizer, 1, Laurent@2098: border=5, flag=wx.GROW|wx.LEFT|wx.RIGHT|wx.BOTTOM) Laurent@2098: Laurent@2098: self.EthercatMasterEditor.SetSizer(main_sizer) Laurent@2098: Laurent@2098: return self.EthercatMasterEditor Laurent@2098: Laurent@2098: def __init__(self, parent, controler, window): Laurent@2098: ConfTreeNodeEditor.__init__(self, parent, controler, window) Laurent@2099: Laurent@2099: self.ProcessVariables = [] Laurent@2100: self.CellShown = None Laurent@2099: Laurent@2098: self.ProcessVariablesDefaultValue = {"Name": "", "ReadFrom": "", "WriteTo": "", "Description": ""} Laurent@2098: self.ProcessVariablesTable = ProcessVariablesTable(self, [], GetProcessVariablesTableColnames()) Laurent@2098: self.ProcessVariablesColSizes = [40, 100, 150, 150, 200] Laurent@2098: self.ProcessVariablesColAlignements = [wx.ALIGN_CENTER, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT] Laurent@2098: Laurent@2098: self.ProcessVariablesGrid.SetTable(self.ProcessVariablesTable) Laurent@2098: self.ProcessVariablesGrid.SetButtons({"Add": self.AddVariableButton, Laurent@2098: "Delete": self.DeleteVariableButton, Laurent@2098: "Up": self.UpVariableButton, Laurent@2098: "Down": self.DownVariableButton}) Laurent@2098: Laurent@2098: def _AddVariablesElement(new_row): Laurent@2098: self.ProcessVariablesTable.InsertRow(new_row, self.ProcessVariablesDefaultValue.copy()) Laurent@2098: self.SaveProcessVariables() Laurent@2098: self.ProcessVariablesTable.ResetView(self.ProcessVariablesGrid) Laurent@2098: return new_row Laurent@2098: setattr(self.ProcessVariablesGrid, "_AddRow", _AddVariablesElement) Laurent@2098: Laurent@2098: def _DeleteVariablesElement(row): Laurent@2098: self.ProcessVariablesTable.RemoveRow(row) Laurent@2098: self.SaveProcessVariables() Laurent@2098: self.ProcessVariablesTable.ResetView(self.ProcessVariablesGrid) Laurent@2098: setattr(self.ProcessVariablesGrid, "_DeleteRow", _DeleteVariablesElement) laurent@2038: Laurent@2098: def _MoveVariablesElement(row, move): Laurent@2098: new_row = self.ProcessVariablesTable.MoveRow(row, move) Laurent@2098: if new_row != row: Laurent@2098: self.SaveProcessVariables() Laurent@2098: self.ProcessVariablesTable.ResetView(self.ProcessVariablesGrid) Laurent@2098: return new_row Laurent@2098: setattr(self.ProcessVariablesGrid, "_MoveRow", _MoveVariablesElement) Laurent@2098: Laurent@2099: _refresh_buttons = getattr(self.ProcessVariablesGrid, "RefreshButtons") Laurent@2099: def _RefreshButtons(): Laurent@2099: if self.NodesFilter.GetSelection() == 0: Laurent@2099: _refresh_buttons() Laurent@2099: else: Laurent@2099: self.AddVariableButton.Enable(False) Laurent@2099: self.DeleteVariableButton.Enable(False) Laurent@2099: self.UpVariableButton.Enable(False) Laurent@2099: self.DownVariableButton.Enable(False) Laurent@2099: setattr(self.ProcessVariablesGrid, "RefreshButtons", _RefreshButtons) Laurent@2099: Laurent@2098: self.ProcessVariablesGrid.SetRowLabelSize(0) Laurent@2098: for col in range(self.ProcessVariablesTable.GetNumberCols()): Laurent@2098: attr = wx.grid.GridCellAttr() Laurent@2098: attr.SetAlignment(self.ProcessVariablesColAlignements[col], wx.ALIGN_CENTRE) Laurent@2098: self.ProcessVariablesGrid.SetColAttr(col, attr) Laurent@2098: self.ProcessVariablesGrid.SetColMinimalWidth(col, self.ProcessVariablesColSizes[col]) Laurent@2098: self.ProcessVariablesGrid.AutoSizeColumn(col, False) Laurent@2098: self.ProcessVariablesGrid.RefreshButtons() Laurent@2098: Laurent@2098: self.StartupCommandsDefaultValue = {"Position": 0, "Index": 0, "Subindex": 0, "Value": 0, "Description": ""} Laurent@2098: self.StartupCommandsTable = StartupCommandsTable(self, [], GetStartupCommandsTableColnames()) Laurent@2098: self.StartupCommandsColSizes = [100, 100, 50, 100, 200] Laurent@2098: self.StartupCommandsColAlignements = [wx.ALIGN_CENTER, wx.ALIGN_RIGHT, wx.ALIGN_RIGHT, wx.ALIGN_RIGHT, wx.ALIGN_LEFT] Laurent@2098: Laurent@2098: self.StartupCommandsGrid.SetTable(self.StartupCommandsTable) Laurent@2098: self.StartupCommandsGrid.SetButtons({"Add": self.AddCommandButton, Laurent@2098: "Delete": self.DeleteCommandButton}) Laurent@2098: Laurent@2098: def _AddCommandsElement(new_row): Laurent@2098: command = self.StartupCommandsDefaultValue.copy() Laurent@2098: command_idx = self.Controler.AppendStartupCommand(command) Laurent@2098: self.RefreshStartupCommands() Laurent@2098: self.RefreshBuffer() Laurent@2098: return self.StartupCommandsTable.GetCommandIndex(command["Position"], command_idx) Laurent@2098: setattr(self.StartupCommandsGrid, "_AddRow", _AddCommandsElement) Laurent@2098: Laurent@2098: def _DeleteCommandsElement(row): Laurent@2098: command = self.StartupCommandsTable.GetRow(row) Laurent@2098: self.Controler.RemoveStartupCommand(command["Position"], command["command_idx"]) Laurent@2098: self.RefreshStartupCommands() Laurent@2098: self.RefreshBuffer() Laurent@2098: setattr(self.StartupCommandsGrid, "_DeleteRow", _DeleteCommandsElement) Laurent@2099: Laurent@2098: self.StartupCommandsGrid.SetRowLabelSize(0) Laurent@2098: for col in range(self.StartupCommandsTable.GetNumberCols()): Laurent@2098: attr = wx.grid.GridCellAttr() Laurent@2098: attr.SetAlignment(self.StartupCommandsColAlignements[col], wx.ALIGN_CENTRE) Laurent@2098: self.StartupCommandsGrid.SetColAttr(col, attr) Laurent@2098: self.StartupCommandsGrid.SetColMinimalWidth(col, self.StartupCommandsColSizes[col]) Laurent@2098: self.StartupCommandsGrid.AutoSizeColumn(col, False) Laurent@2098: self.StartupCommandsGrid.RefreshButtons() Laurent@2098: Laurent@2098: def RefreshBuffer(self): Laurent@2098: self.ParentWindow.RefreshTitle() Laurent@2098: self.ParentWindow.RefreshFileMenu() Laurent@2098: self.ParentWindow.RefreshEditMenu() Laurent@2098: self.ParentWindow.RefreshPageTitles() Laurent@2098: Laurent@2099: def GetBufferState(self): Laurent@2099: return self.Controler.GetBufferState() Laurent@2099: Laurent@2099: def Undo(self): Laurent@2099: self.Controler.LoadPrevious() Laurent@2099: self.RefreshView() Laurent@2099: Laurent@2099: def Redo(self): Laurent@2099: self.Controler.LoadNext() Laurent@2099: self.RefreshView() Laurent@2099: Laurent@2098: def RefreshView(self): Laurent@2098: ConfTreeNodeEditor.RefreshView(self) Laurent@2098: Laurent@2098: self.RefreshNodesFilter() Laurent@2098: self.RefreshProcessVariables() Laurent@2098: self.RefreshStartupCommands() Laurent@2098: self.NodesVariables.RefreshView() Laurent@2098: Laurent@2098: def RefreshNodesFilter(self): Laurent@2098: value = self.NodesFilter.GetValue() Laurent@2098: self.NodesFilter.Clear() Laurent@2098: self.NodesFilter.Append(_("All")) Laurent@2098: self.NodesFilterValues = [{}] Laurent@2098: for vendor_id, vendor_name in self.Controler.GetLibraryVendors(): Laurent@2098: self.NodesFilter.Append(_("%s's nodes") % vendor_name) Laurent@2098: self.NodesFilterValues.append({"vendor": vendor_id}) Laurent@2098: self.NodesFilter.Append(_("CIA402 nodes")) Laurent@2098: self.NodesFilterValues.append({"slave_profile": 402}) Laurent@2098: if value in self.NodesFilter.GetStrings(): Laurent@2098: self.NodesFilter.SetStringSelection(value) Laurent@2098: else: Laurent@2098: try: Laurent@2098: int(value) Laurent@2098: self.NodesFilter.SetValue(value) Laurent@2098: except: Laurent@2098: self.NodesFilter.SetSelection(0) Laurent@2098: self.RefreshCurrentNodesFilter() Laurent@2098: Laurent@2098: def RefreshCurrentNodesFilter(self): Laurent@2098: filter = self.NodesFilter.GetSelection() Laurent@2098: if filter != -1: Laurent@2098: self.CurrentNodesFilter = self.NodesFilterValues[filter] Laurent@2098: else: Laurent@2098: try: Laurent@2098: self.CurrentNodesFilter = {"slave_pos": int(self.NodesFilter.GetValue())} Laurent@2098: except: Laurent@2098: self.CurrentNodesFilter = None Laurent@2098: self.NodesVariables.SetCurrentNodesFilter(self.CurrentNodesFilter) Laurent@2098: Laurent@2098: def RefreshProcessVariables(self): Laurent@2099: if self.CurrentNodesFilter is not None: Laurent@2099: self.ProcessVariables = self.Controler.GetProcessVariables() Laurent@2099: slaves = self.Controler.GetSlaves(**self.CurrentNodesFilter) Laurent@2099: data = [] Laurent@2099: for variable in self.ProcessVariables: Laurent@2099: if (variable["ReadFrom"] == "" or variable["ReadFrom"][0] in slaves or Laurent@2099: variable["WriteTo"] == "" or variable["WriteTo"][0] in slaves): Laurent@2099: data.append(variable) Laurent@2099: self.ProcessVariablesTable.SetData(data) Laurent@2099: self.ProcessVariablesTable.ResetView(self.ProcessVariablesGrid) Laurent@2099: self.ProcessVariablesGrid.RefreshButtons() Laurent@2098: Laurent@2098: def SaveProcessVariables(self): Laurent@2098: self.Controler.SetProcessVariables( Laurent@2098: self.ProcessVariablesTable.GetData()) Laurent@2098: self.RefreshBuffer() Laurent@2098: Laurent@2099: def RefreshStartupCommands(self, position=None, command_idx=None): Laurent@2098: if self.CurrentNodesFilter is not None: Laurent@2098: self.StartupCommandsTable.SetData( Laurent@2098: self.Controler.GetStartupCommands(**self.CurrentNodesFilter)) Laurent@2098: self.StartupCommandsTable.ResetView(self.StartupCommandsGrid) Laurent@2099: if position is not None and command_idx is not None: Laurent@2100: wx.CallAfter(self.SelectStartupCommand, position, command_idx) Laurent@2098: Laurent@2098: def SelectStartupCommand(self, position, command_idx): Laurent@2098: self.StartupCommandsGrid.SetSelectedRow( Laurent@2098: self.StartupCommandsTable.GetCommandIndex(position, command_idx)) Laurent@2098: Laurent@2098: def GetMasterLocation(self): Laurent@2098: return self.Controler.GetCurrentLocation() Laurent@2098: Laurent@2098: def AddStartupCommand(self, position, index, subindex): Laurent@2098: command = self.StartupCommandsDefaultValue.copy() Laurent@2098: command["Position"] = position Laurent@2098: command["Index"] = index Laurent@2098: command["Subindex"] = subindex Laurent@2098: command_idx = self.Controler.AppendStartupCommand(command) Laurent@2098: self.RefreshStartupCommands() Laurent@2098: self.RefreshBuffer() Laurent@2098: self.StartupCommandsGrid.SetSelectedRow( Laurent@2098: self.StartupCommandsTable.GetCommandIndex(position, command_idx)) Laurent@2098: Laurent@2098: def OnNodesFilterChanged(self, event): Laurent@2098: self.RefreshCurrentNodesFilter() Laurent@2098: if self.CurrentNodesFilter is not None: Laurent@2098: self.RefreshStartupCommands() Laurent@2098: self.NodesVariables.RefreshView() laurent@2022: event.Skip() Laurent@2098: Laurent@2098: def OnProcessVariablesGridCellChange(self, event): Laurent@2098: row, col = event.GetRow(), event.GetCol() Laurent@2098: colname = self.ProcessVariablesTable.GetColLabelValue(col, False) Laurent@2098: value = self.ProcessVariablesTable.GetValue(row, col) Laurent@2098: message = None Laurent@2098: if colname == "Name": Laurent@2098: if not TestIdentifier(value): Laurent@2098: message = _("\"%s\" is not a valid identifier!") % value Laurent@2098: elif value.upper() in IEC_KEYWORDS: Laurent@2098: message = _("\"%s\" is a keyword. It can't be used!") % value Laurent@2098: elif value.upper() in [var["Name"].upper() for idx, var in enumerate(self.ProcessVariablesTable.GetData()) if idx != row]: Laurent@2098: message = _("An variable named \"%s\" already exists!") % value Laurent@2098: if message is None: Laurent@2098: self.SaveProcessVariables() Laurent@2098: wx.CallAfter(self.ProcessVariablesTable.ResetView, self.ProcessVariablesGrid) Laurent@2098: event.Skip() Laurent@2098: else: Laurent@2098: dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) Laurent@2098: dialog.ShowModal() Laurent@2098: dialog.Destroy() Laurent@2098: event.Veto() Laurent@2099: Laurent@2098: def OnProcessVariablesGridCellLeftClick(self, event): Laurent@2099: row = event.GetRow() Laurent@2099: if event.GetCol() == 0: Laurent@2099: var_name = self.ProcessVariablesTable.GetValueByName(row, "Name") Laurent@2099: var_type = self.Controler.GetSlaveVariableDataType( Laurent@2099: *self.ProcessVariablesTable.GetValueByName(row, "ReadFrom")) Laurent@2099: data_size = self.Controler.GetSizeOfType(var_type) Laurent@2099: number = self.ProcessVariablesTable.GetValueByName(row, "Number") Laurent@2099: location = "%%M%s" % data_size + \ Laurent@2099: ".".join(map(lambda x:str(x), self.Controler.GetCurrentLocation() + (number,))) Laurent@2099: Laurent@2099: data = wx.TextDataObject(str((location, "location", var_type, var_name, ""))) Laurent@2099: dragSource = wx.DropSource(self.ProcessVariablesGrid) Laurent@2099: dragSource.SetData(data) Laurent@2099: dragSource.DoDragDrop() Laurent@2098: event.Skip() Laurent@2098: Laurent@2099: def OnProcessVariablesGridKeyDown(self, event): Laurent@2099: keycode = event.GetKeyCode() Laurent@2099: col = self.ProcessVariablesGrid.GetGridCursorCol() Laurent@2099: row = self.ProcessVariablesGrid.GetGridCursorRow() Laurent@2099: colname = self.ProcessVariablesTable.GetColLabelValue(col, False) Laurent@2099: if (keycode in (wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE) and Laurent@2099: (colname.startswith("Read from") or colname.startswith("Write to"))): Laurent@2099: self.ProcessVariablesTable.SetValue(row, col, "") Laurent@2099: self.SaveProcessVariables() Laurent@2099: wx.CallAfter(self.ProcessVariablesTable.ResetView, self.ProcessVariablesGrid) Laurent@2099: else: Laurent@2099: event.Skip() Laurent@2099: Laurent@2100: def OnStartupCommandsGridEditorShow(self, event): Laurent@2100: self.CellShown = event.GetRow(), event.GetCol() Laurent@2100: event.Skip() Laurent@2100: Laurent@2098: def OnStartupCommandsGridCellChange(self, event): Laurent@2098: row, col = event.GetRow(), event.GetCol() Laurent@2100: if self.CellShown == (row, col): Laurent@2100: self.CellShown = None Laurent@2100: colname = self.StartupCommandsTable.GetColLabelValue(col, False) Laurent@2100: value = self.StartupCommandsTable.GetValue(row, col) Laurent@2100: message = None Laurent@2100: if colname == "Position": Laurent@2100: if value not in self.Controler.GetSlaves(): Laurent@2100: message = _("No slave defined at position %d!") % value Laurent@2100: old_value = self.StartupCommandsTable.GetOldValue() Laurent@2098: command = self.StartupCommandsTable.GetRow(row) Laurent@2100: if message is None and old_value != command["Position"]: Laurent@2100: self.Controler.RemoveStartupCommand( Laurent@2100: self.StartupCommandsTable.GetOldValue(), Laurent@2100: command["command_idx"], False) Laurent@2100: command_idx = self.Controler.AppendStartupCommand(command) Laurent@2100: wx.CallAfter(self.RefreshStartupCommands, command["Position"], command_idx) Laurent@2100: else: Laurent@2100: command = self.StartupCommandsTable.GetRow(row) Laurent@2099: self.Controler.SetStartupCommandInfos(command) Laurent@2099: if colname in ["Index", "SubIndex"]: Laurent@2099: wx.CallAfter(self.RefreshStartupCommands, command["Position"], command["command_idx"]) Laurent@2100: if message is None: Laurent@2100: self.RefreshBuffer() Laurent@2100: event.Skip() Laurent@2100: else: Laurent@2100: dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) Laurent@2100: dialog.ShowModal() Laurent@2100: dialog.Destroy() Laurent@2099: event.Veto() Laurent@2098: else: Laurent@2098: event.Veto() Laurent@2100: Laurent@2098: def OnResize(self, event): Laurent@2098: self.EthercatMasterEditor.GetBestSize() Laurent@2098: xstart, ystart = self.EthercatMasterEditor.GetViewStart() Laurent@2098: window_size = self.EthercatMasterEditor.GetClientSize() Laurent@2098: maxx, maxy = self.EthercatMasterEditor.GetMinSize() Laurent@2098: posx = max(0, min(xstart, (maxx - window_size[0]) / SCROLLBAR_UNIT)) Laurent@2098: posy = max(0, min(ystart, (maxy - window_size[1]) / SCROLLBAR_UNIT)) Laurent@2098: self.EthercatMasterEditor.Scroll(posx, posy) Laurent@2098: self.EthercatMasterEditor.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, Laurent@2098: maxx / SCROLLBAR_UNIT, maxy / SCROLLBAR_UNIT, posx, posy) Laurent@2098: event.Skip() Laurent@2098: Laurent@2097: def GetModulesTableColnames(): Laurent@2097: _ = lambda x : x Laurent@2097: return [_("Name"), _("PDO alignment (bits)")] Laurent@2097: Laurent@2098: class LibraryEditorSizer(wx.FlexGridSizer): Laurent@2097: Laurent@2097: def __init__(self, parent, module_library, buttons): Laurent@2098: wx.FlexGridSizer.__init__(self, cols=1, hgap=0, rows=4, vgap=5) Laurent@2097: Laurent@2097: self.ModuleLibrary = module_library Laurent@2105: self.ParentWindow = parent Laurent@2105: Laurent@2098: self.AddGrowableCol(0) Laurent@2098: self.AddGrowableRow(1) Laurent@2098: self.AddGrowableRow(3) Laurent@2098: Laurent@2098: ESI_files_label = wx.StaticText(parent, Laurent@2097: label=_("ESI Files:")) Laurent@2098: self.AddWindow(ESI_files_label, border=10, Laurent@2097: flag=wx.TOP|wx.LEFT|wx.RIGHT) Laurent@2097: Laurent@2097: folder_tree_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=1, vgap=0) Laurent@2097: folder_tree_sizer.AddGrowableCol(0) Laurent@2097: folder_tree_sizer.AddGrowableRow(0) Laurent@2098: self.AddSizer(folder_tree_sizer, border=10, Laurent@2097: flag=wx.GROW|wx.LEFT|wx.RIGHT) Laurent@2097: Laurent@2098: self.ESIFiles = FolderTree(parent, self.GetPath(), editable=False) Laurent@2097: self.ESIFiles.SetFilter(".xml") Laurent@2097: folder_tree_sizer.AddWindow(self.ESIFiles, flag=wx.GROW) Laurent@2097: Laurent@2097: buttons_sizer = wx.BoxSizer(wx.VERTICAL) Laurent@2097: folder_tree_sizer.AddSizer(buttons_sizer, Laurent@2097: flag=wx.ALIGN_CENTER_VERTICAL) Laurent@2097: Laurent@2097: for idx, (name, bitmap, help, callback) in enumerate(buttons): Laurent@2098: button = wx.lib.buttons.GenBitmapButton(parent, Laurent@2097: bitmap=GetBitmap(bitmap), Laurent@2097: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@2097: button.SetToolTipString(help) Laurent@2097: setattr(self, name, button) Laurent@2097: if idx > 0: Laurent@2097: flag = wx.TOP Laurent@2097: else: Laurent@2097: flag = 0 Laurent@2097: if callback is None: Laurent@2097: callback = getattr(self, "On" + name, None) Laurent@2097: if callback is not None: Laurent@2098: parent.Bind(wx.EVT_BUTTON, callback, button) Laurent@2097: buttons_sizer.AddWindow(button, border=10, flag=flag) Laurent@2097: Laurent@2098: modules_label = wx.StaticText(parent, Laurent@2097: label=_("Modules library:")) Laurent@2098: self.AddSizer(modules_label, border=10, Laurent@2097: flag=wx.LEFT|wx.RIGHT) Laurent@2097: Laurent@2098: self.ModulesGrid = wx.gizmos.TreeListCtrl(parent, Laurent@2097: style=wx.TR_DEFAULT_STYLE | Laurent@2097: wx.TR_ROW_LINES | Laurent@2097: wx.TR_COLUMN_LINES | Laurent@2097: wx.TR_HIDE_ROOT | Laurent@2097: wx.TR_FULL_ROW_HIGHLIGHT) Laurent@2097: self.ModulesGrid.GetMainWindow().Bind(wx.EVT_LEFT_DCLICK, Laurent@2097: self.OnModulesGridLeftDClick) Laurent@2098: self.AddWindow(self.ModulesGrid, border=10, Laurent@2097: flag=wx.GROW|wx.BOTTOM|wx.LEFT|wx.RIGHT) Laurent@2097: Laurent@2097: for colname, colsize, colalign in zip(GetModulesTableColnames(), Laurent@2097: [400, 150], Laurent@2097: [wx.ALIGN_LEFT, wx.ALIGN_RIGHT]): Laurent@2097: self.ModulesGrid.AddColumn(_(colname), colsize, colalign) Laurent@2097: self.ModulesGrid.SetMainColumn(0) Laurent@2097: Laurent@2097: def GetPath(self): Laurent@2097: return self.ModuleLibrary.GetPath() Laurent@2097: Laurent@2098: def SetControlMinSize(self, size): Laurent@2098: self.ESIFiles.SetMinSize(size) Laurent@2098: self.ModulesGrid.SetMinSize(size) Laurent@2098: Laurent@2097: def GetSelectedFilePath(self): Laurent@2097: return self.ESIFiles.GetPath() Laurent@2097: Laurent@2097: def RefreshView(self): Laurent@2097: self.ESIFiles.RefreshTree() Laurent@2097: self.RefreshModulesGrid() Laurent@2097: Laurent@2097: def RefreshModulesGrid(self): Laurent@2097: root = self.ModulesGrid.GetRootItem() Laurent@2097: if not root.IsOk(): Laurent@2097: root = self.ModulesGrid.AddRoot("Modules") Laurent@2097: self.GenerateModulesGridBranch(root, Laurent@2097: self.ModuleLibrary.GetModulesLibrary(), Laurent@2097: GetVariablesTableColnames()) Laurent@2097: self.ModulesGrid.Expand(root) Laurent@2097: Laurent@2097: def GenerateModulesGridBranch(self, root, modules, colnames): Laurent@2097: item, root_cookie = self.ModulesGrid.GetFirstChild(root) Laurent@2097: Laurent@2097: no_more_items = not item.IsOk() Laurent@2097: for module in modules: Laurent@2097: if no_more_items: Laurent@2097: item = self.ModulesGrid.AppendItem(root, "") Laurent@2097: self.ModulesGrid.SetItemText(item, module["name"], 0) Laurent@2097: if module["infos"] is not None: Laurent@2097: self.ModulesGrid.SetItemText(item, str(module["infos"]["alignment"]), 1) Laurent@2097: else: Laurent@2097: self.ModulesGrid.SetItemBackgroundColour(item, wx.LIGHT_GREY) Laurent@2097: self.ModulesGrid.SetItemPyData(item, module["infos"]) Laurent@2097: self.GenerateModulesGridBranch(item, module["children"], colnames) Laurent@2097: if not no_more_items: Laurent@2097: item, root_cookie = self.ModulesGrid.GetNextChild(root, root_cookie) Laurent@2097: no_more_items = not item.IsOk() Laurent@2097: Laurent@2097: if not no_more_items: Laurent@2097: to_delete = [] Laurent@2097: while item.IsOk(): Laurent@2097: to_delete.append(item) Laurent@2097: item, root_cookie = self.ModulesGrid.GetNextChild(root, root_cookie) Laurent@2097: for item in to_delete: Laurent@2097: self.ModulesGrid.Delete(item) Laurent@2097: Laurent@2097: def OnImportButton(self, event): Laurent@2105: dialog = wx.FileDialog(self.ParentWindow, Laurent@2097: _("Choose an XML file"), Laurent@2097: os.getcwd(), "", Laurent@2097: _("XML files (*.xml)|*.xml|All files|*.*"), wx.OPEN) Laurent@2097: Laurent@2097: if dialog.ShowModal() == wx.ID_OK: Laurent@2097: filepath = dialog.GetPath() Laurent@2097: if self.ModuleLibrary.ImportModuleLibrary(filepath): Laurent@2097: wx.CallAfter(self.RefreshView) Laurent@2097: else: Laurent@2097: message = wx.MessageDialog(self, Laurent@2097: _("No such XML file: %s\n") % filepath, Laurent@2097: _("Error"), wx.OK|wx.ICON_ERROR) Laurent@2097: message.ShowModal() Laurent@2097: message.Destroy() Laurent@2097: dialog.Destroy() Laurent@2097: Laurent@2097: event.Skip() Laurent@2097: Laurent@2097: def OnDeleteButton(self, event): Laurent@2097: filepath = self.GetSelectedFilePath() Laurent@2097: if os.path.isfile(filepath): Laurent@2097: folder, filename = os.path.split(filepath) Laurent@2097: Laurent@2105: dialog = wx.MessageDialog(self.ParentWindow, Laurent@2097: _("Do you really want to delete the file '%s'?") % filename, Laurent@2097: _("Delete File"), wx.YES_NO|wx.ICON_QUESTION) Laurent@2097: remove = dialog.ShowModal() == wx.ID_YES Laurent@2097: dialog.Destroy() Laurent@2097: Laurent@2097: if remove: Laurent@2097: os.remove(filepath) Laurent@2097: self.ModuleLibrary.LoadModules() Laurent@2097: wx.CallAfter(self.RefreshView) Laurent@2097: event.Skip() Laurent@2097: Laurent@2097: def OnModulesGridLeftDClick(self, event): Laurent@2097: item, flags, col = self.ModulesGrid.HitTest(event.GetPosition()) Laurent@2097: if item.IsOk(): Laurent@2097: entry_infos = self.ModulesGrid.GetItemPyData(item) Laurent@2097: if entry_infos is not None and col == 1: Laurent@2105: dialog = wx.TextEntryDialog(self.ParentWindow, Laurent@2097: _("Set PDO alignment (bits):"), Laurent@2097: _("%s PDO alignment") % self.ModulesGrid.GetItemText(item), Laurent@2097: str(entry_infos["alignment"])) Laurent@2097: Laurent@2097: if dialog.ShowModal() == wx.ID_OK: Laurent@2097: try: Laurent@2097: self.ModuleLibrary.SetAlignment( Laurent@2097: entry_infos["vendor"], Laurent@2097: entry_infos["product_code"], Laurent@2097: entry_infos["revision_number"], Laurent@2097: int(dialog.GetValue())) Laurent@2097: wx.CallAfter(self.RefreshModulesGrid) Laurent@2097: except ValueError: Laurent@2097: message = wx.MessageDialog(self, Laurent@2097: _("Module PDO alignment must be an integer!"), Laurent@2097: _("Error"), wx.OK|wx.ICON_ERROR) Laurent@2097: message.ShowModal() Laurent@2097: message.Destroy() Laurent@2097: Laurent@2097: dialog.Destroy() Laurent@2097: Laurent@2097: event.Skip() Laurent@2058: Laurent@2097: class DatabaseManagementDialog(wx.Dialog): Laurent@2097: Laurent@2097: def __init__(self, parent, database): Laurent@2097: wx.Dialog.__init__(self, parent, Laurent@2097: size=wx.Size(700, 500), title=_('ESI Files Database management'), Laurent@2097: style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER) Laurent@2097: Laurent@2097: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) Laurent@2097: main_sizer.AddGrowableCol(0) Laurent@2097: main_sizer.AddGrowableRow(0) Laurent@2097: Laurent@2098: self.DatabaseSizer = LibraryEditorSizer(self, database, Laurent@2097: [("ImportButton", "ImportESI", _("Import file to ESI files database"), None), Laurent@2097: ("DeleteButton", "remove_element", _("Remove file from database"), None)]) Laurent@2098: self.DatabaseSizer.SetControlMinSize(wx.Size(0, 0)) Laurent@2098: main_sizer.AddSizer(self.DatabaseSizer, border=10, Laurent@2097: flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) Laurent@2097: Laurent@2097: button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) Laurent@2097: button_sizer.GetAffirmativeButton().SetLabel(_("Add file to project")) Laurent@2097: button_sizer.GetCancelButton().SetLabel(_("Close")) Laurent@2097: main_sizer.AddSizer(button_sizer, border=10, Laurent@2097: flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) Laurent@2097: Laurent@2097: self.SetSizer(main_sizer) Laurent@2097: Laurent@2098: self.DatabaseSizer.RefreshView() Laurent@2097: Laurent@2097: def GetValue(self): Laurent@2098: return self.DatabaseSizer.GetSelectedFilePath() Laurent@2097: Laurent@2097: class LibraryEditor(ConfTreeNodeEditor): Laurent@2097: Laurent@2097: CONFNODEEDITOR_TABS = [ Laurent@2097: (_("Modules Library"), "_create_ModuleLibraryEditor")] Laurent@2097: Laurent@2097: def _create_ModuleLibraryEditor(self, prnt): Laurent@2098: self.ModuleLibraryEditor = wx.ScrolledWindow(prnt, Laurent@2098: style=wx.TAB_TRAVERSAL|wx.HSCROLL|wx.VSCROLL) Laurent@2098: self.ModuleLibraryEditor.Bind(wx.EVT_SIZE, self.OnResize) Laurent@2098: Laurent@2098: self.ModuleLibrarySizer = LibraryEditorSizer(self.ModuleLibraryEditor, Laurent@2097: self.Controler.GetModulesLibraryInstance(), Laurent@2097: [("ImportButton", "ImportESI", _("Import ESI file"), None), Laurent@2097: ("AddButton", "ImportDatabase", _("Add file from ESI files database"), self.OnAddButton), Laurent@2097: ("DeleteButton", "remove_element", _("Remove file from library"), None)]) Laurent@2098: self.ModuleLibrarySizer.SetControlMinSize(wx.Size(0, 200)) Laurent@2098: self.ModuleLibraryEditor.SetSizer(self.ModuleLibrarySizer) Laurent@2097: Laurent@2097: return self.ModuleLibraryEditor Laurent@2097: Laurent@2097: def __init__(self, parent, controler, window): Laurent@2097: ConfTreeNodeEditor.__init__(self, parent, controler, window) Laurent@2097: Laurent@2097: self.RefreshView() Laurent@2097: Laurent@2097: def RefreshView(self): Laurent@2097: ConfTreeNodeEditor.RefreshView(self) Laurent@2098: self.ModuleLibrarySizer.RefreshView() Laurent@2097: Laurent@2097: def OnAddButton(self, event): Laurent@2097: dialog = DatabaseManagementDialog(self, Laurent@2097: self.Controler.GetModulesDatabaseInstance()) Laurent@2097: Laurent@2097: if dialog.ShowModal() == wx.ID_OK: Laurent@2097: module_library = self.Controler.GetModulesLibraryInstance() Laurent@2097: module_library.ImportModuleLibrary(dialog.GetValue()) Laurent@2097: Laurent@2097: dialog.Destroy() Laurent@2097: Laurent@2098: wx.CallAfter(self.ModuleLibrarySizer.RefreshView) Laurent@2097: Laurent@2097: event.Skip() Laurent@2097: Laurent@2098: def OnResize(self, event): Laurent@2098: self.ModuleLibraryEditor.GetBestSize() Laurent@2098: xstart, ystart = self.ModuleLibraryEditor.GetViewStart() Laurent@2098: window_size = self.ModuleLibraryEditor.GetClientSize() Laurent@2098: maxx, maxy = self.ModuleLibraryEditor.GetMinSize() Laurent@2098: posx = max(0, min(xstart, (maxx - window_size[0]) / SCROLLBAR_UNIT)) Laurent@2098: posy = max(0, min(ystart, (maxy - window_size[1]) / SCROLLBAR_UNIT)) Laurent@2098: self.ModuleLibraryEditor.Scroll(posx, posy) Laurent@2098: self.ModuleLibraryEditor.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, Laurent@2098: maxx / SCROLLBAR_UNIT, maxy / SCROLLBAR_UNIT, posx, posy) Laurent@2098: event.Skip() Laurent@2098: Laurent@2098: