edouard@2165: #!/usr/bin/env python edouard@2165: # -*- coding: utf-8 -*- edouard@2165: edouard@2165: # This file is part of Beremiz edouard@2165: # edouard@2165: # Copyright (C) 2011-2014: Laurent BESSARD, Edouard TISSERANT edouard@2165: # RTES Lab : CRKim, JBLee, youcu edouard@2165: # Higen Motor : Donggu Kang edouard@2165: # edouard@2165: # See COPYING file for copyrights details. edouard@2165: andrej@2405: from __future__ import absolute_import andrej@2437: from __future__ import division Laurent@2097: import os Laurent@2098: import re Laurent@2097: laurent@2022: import wx laurent@2026: import wx.grid edouard@3303: import wx.adv 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 andrej@2392: from controls.CustomStyledTextCtrl import NAVIGATION_KEYS Laurent@2071: from editors.ConfTreeNodeEditor import ConfTreeNodeEditor, SCROLLBAR_UNIT Laurent@2097: from util.BitmapLibrary import GetBitmap andrej@2392: from util.TranslationCatalogs import NoTranslate laurent@2022: Edouard@2152: # ----------------------------------------------------------------------- andrej@2405: from etherlab.EtherCATManagementEditor import EtherCATManagementTreebook, MasterStatePanelClass edouard@2641: # ----------------------------------------------------------------------- edouard@2641: laurent@2022: [ETHERCAT_VENDOR, ETHERCAT_GROUP, ETHERCAT_DEVICE] = range(3) laurent@2022: andrej@2360: Laurent@2098: def GetVariablesTableColnames(position=False): andrej@2375: _ = NoTranslate Laurent@2098: colname = ["#"] Laurent@2098: if position: Laurent@2098: colname.append(_("Position")) Laurent@2098: return colname + [_("Name"), _("Index"), _("SubIndex"), _("Type"), _("Access")] Laurent@2097: andrej@2370: Laurent@2097: ACCESS_TYPES = { Laurent@2097: 'ro': 'R', Laurent@2097: 'wo': 'W', Laurent@2097: 'rw': 'R/W'} Laurent@2097: andrej@2360: Laurent@2097: def GetAccessValue(access, pdo_mapping): Laurent@2129: value = "SDO: %s" % ACCESS_TYPES.get(access, "") Laurent@2097: if pdo_mapping != "": Laurent@2129: value += ", PDO: %s" % pdo_mapping Laurent@2097: return value laurent@2034: andrej@2370: 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@2129: VARIABLE_INDEX_FILTER_FORMAT = _("Variable Index: #x%4.4X") Laurent@2129: 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})$") andrej@2439: LOCATION_MODEL = re.compile(r"(?:%[IQM](?:[XBWLD]?([0-9]+(?:\.[0-9]+)*)))$") Laurent@2098: Laurent@2098: Laurent@2098: class NodeVariablesSizer(wx.FlexGridSizer): andrej@2355: 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) andrej@2355: Laurent@2098: self.Controler = controler Laurent@2098: self.PositionColumn = position_column andrej@2355: 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@2129: self.VariablesFilter.Bind(wx.EVT_CHAR, self.OnVariablesFilterKeyDown) edouard@3303: self.Add(self.VariablesFilter, flag=wx.GROW) edouard@3303: edouard@3303: self.VariablesGrid = wx.adv.TreeListCtrl(parent, andrej@2381: style=wx.TR_DEFAULT_STYLE | andrej@2381: wx.TR_ROW_LINES | andrej@2381: wx.TR_COLUMN_LINES | andrej@2381: wx.TR_HIDE_ROOT | andrej@2381: wx.TR_FULL_ROW_HIGHLIGHT) Laurent@2098: self.VariablesGrid.GetMainWindow().Bind(wx.EVT_LEFT_DOWN, andrej@2381: self.OnVariablesGridLeftClick) edouard@3303: self.Add(self.VariablesGrid, flag=wx.GROW) andrej@2355: Laurent@2098: self.Filters = [] Laurent@2098: for desc, value in VARIABLES_FILTERS: Laurent@2098: self.VariablesFilter.Append(desc) Laurent@2098: self.Filters.append(value) andrej@2355: Laurent@2098: self.VariablesFilter.SetSelection(0) Laurent@2098: self.CurrentFilter = self.Filters[0] Laurent@2129: self.VariablesFilterFirstCharacter = True andrej@2355: Laurent@2098: if position_column: Laurent@2098: for colname, colsize, colalign in zip(GetVariablesTableColnames(position_column), Laurent@2129: [40, 80, 350, 80, 100, 80, 150], andrej@2355: [wx.ALIGN_RIGHT, wx.ALIGN_RIGHT, wx.ALIGN_LEFT, andrej@2355: 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@2129: [40, 350, 80, 100, 80, 150], andrej@2355: [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) andrej@2355: laurent@2041: def RefreshView(self): Laurent@2098: entries = self.Controler.GetSlaveVariables(self.CurrentFilter) Laurent@2098: self.RefreshVariablesGrid(entries) andrej@2355: 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) andrej@2355: laurent@2038: def GenerateVariablesGridBranch(self, root, entries, colnames, idx=0): Laurent@2097: item, root_cookie = self.VariablesGrid.GetFirstChild(root) andrej@2355: 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, "") edouard@2641: # add jblee edouard@2641: if value is None: edouard@2641: value = "" 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() andrej@2355: 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) andrej@2355: laurent@2038: return idx andrej@2355: 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@2129: if value == "": Laurent@2129: self.CurrentFilter = self.Filters[0] Laurent@2129: self.VariablesFilter.SetSelection(0) Laurent@2129: else: Laurent@2129: result = ETHERCAT_INDEX_MODEL.match(value) Laurent@2129: if result is not None: Laurent@2129: value = result.group(1) Laurent@2129: index = int(value, 16) Laurent@2129: self.CurrentFilter = (index, index) Laurent@2129: self.VariablesFilter.SetValue(VARIABLE_INDEX_FILTER_FORMAT % index) Laurent@2098: self.RefreshView() andrej@2353: except Exception: Laurent@2129: if self.CurrentFilter in self.Filters: Laurent@2129: self.VariablesFilter.SetSelection(self.Filters.index(self.CurrentFilter)) Laurent@2129: else: Laurent@2129: self.VariablesFilter.SetValue(VARIABLE_INDEX_FILTER_FORMAT % self.CurrentFilter[0]) Laurent@2129: self.VariablesFilterFirstCharacter = True Laurent@2098: event.Skip() andrej@2355: Laurent@2129: def OnVariablesFilterKeyDown(self, event): Laurent@2129: if self.VariablesFilterFirstCharacter: Laurent@2129: keycode = event.GetKeyCode() andrej@2355: if keycode not in [wx.WXK_RETURN, Laurent@2130: wx.WXK_NUMPAD_ENTER]: Laurent@2130: self.VariablesFilterFirstCharacter = False Laurent@2130: if keycode not in NAVIGATION_KEYS: Laurent@2130: self.VariablesFilter.SetValue("") andrej@2355: if keycode not in [wx.WXK_DELETE, andrej@2355: wx.WXK_NUMPAD_DELETE, Laurent@2129: wx.WXK_BACK]: Laurent@2129: event.Skip() Laurent@2129: else: Laurent@2129: event.Skip() andrej@2355: laurent@2038: def OnVariablesGridLeftClick(self, event): andrej@2406: 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) andrej@2355: 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@2124: location = self.Controler.GetCurrentLocation() Laurent@2098: if self.PositionColumn: Laurent@2098: slave_pos = self.Controler.ExtractHexDecValue(entry.get("Position", "0")) Laurent@2124: location += (slave_pos,) Laurent@2124: node_name = self.Controler.GetSlaveName(slave_pos) laurent@2038: else: Laurent@2124: node_name = self.Controler.CTNName() andrej@2355: Laurent@2098: if pdo_mapping != "": Laurent@2124: var_name = "%s_%4.4x_%2.2x" % (node_name, entry_index, entry_subindex) Laurent@2124: if pdo_mapping == "T": Laurent@2098: dir = "%I" Laurent@2098: else: Laurent@2098: dir = "%Q" Laurent@2098: location = "%s%s" % (dir, data_size) + \ andrej@2392: ".".join(map(str, location + (entry_index, entry_subindex))) andrej@2355: 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 andrej@2355: Laurent@2124: elif self.PositionColumn: 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 andrej@2355: Laurent@2098: event.Skip() Laurent@2098: andrej@2360: Laurent@2098: class NodeEditor(ConfTreeNodeEditor): andrej@2355: Laurent@2098: CONFNODEEDITOR_TABS = [ Edouard@2152: (_("Ethercat node"), "_create_EthercatNodeEditor"), Edouard@2152: # Add Notebook Tab for EtherCAT Management Treebook Edouard@2152: (_("EtherCAT Management"), "_create_EtherCATManagementEditor") Edouard@2152: ] andrej@2355: Laurent@2098: def _create_EthercatNodeEditor(self, prnt): Laurent@2098: self.EthercatNodeEditor = wx.Panel(prnt, style=wx.TAB_TRAVERSAL) andrej@2355: 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) andrej@2355: Laurent@2098: variables_label = wx.StaticText(self.EthercatNodeEditor, andrej@2381: label=_('Variable entries:')) edouard@3303: main_sizer.Add(variables_label, border=10, flag=wx.TOP | wx.LEFT | wx.RIGHT) andrej@2355: Laurent@2098: self.NodeVariables = NodeVariablesSizer(self.EthercatNodeEditor, self.Controler) edouard@3303: main_sizer.Add(self.NodeVariables, border=10, andrej@2381: flag=wx.GROW | wx.BOTTOM | wx.LEFT | wx.RIGHT) andrej@2355: Laurent@2098: self.EthercatNodeEditor.SetSizer(main_sizer) Laurent@2098: Laurent@2098: return self.EthercatNodeEditor andrej@2355: Laurent@2098: def __init__(self, parent, controler, window): Laurent@2098: ConfTreeNodeEditor.__init__(self, parent, controler, window) andrej@2355: Edouard@2152: # add Contoler for use EthercatSlave.py Method Edouard@2152: self.Controler = controler edouard@2641: Laurent@2098: def GetBufferState(self): Laurent@2098: return False, False andrej@2355: Laurent@2098: def RefreshView(self): Laurent@2098: ConfTreeNodeEditor.RefreshView(self) andrej@2355: Laurent@2098: self.NodeVariables.RefreshView() Laurent@2098: andrej@2355: # -------------------For EtherCAT Management ---------------------------------------------- Edouard@2152: def _create_EtherCATManagementEditor(self, prnt): Edouard@2152: self.EtherCATManagementEditor = wx.ScrolledWindow(prnt, andrej@2381: style=wx.TAB_TRAVERSAL | wx.HSCROLL | wx.VSCROLL) Edouard@2152: self.EtherCATManagementEditor.Bind(wx.EVT_SIZE, self.OnResize) Edouard@2152: Edouard@2152: self.EtherCATManagermentEditor_Main_Sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) Edouard@2152: self.EtherCATManagermentEditor_Main_Sizer.AddGrowableCol(0) Edouard@2152: self.EtherCATManagermentEditor_Main_Sizer.AddGrowableRow(0) andrej@2355: Edouard@2152: self.EtherCATManagementTreebook = EtherCATManagementTreebook(self.EtherCATManagementEditor, self.Controler, self) edouard@2641: edouard@3303: self.EtherCATManagermentEditor_Main_Sizer.Add(self.EtherCATManagementTreebook, border=10, flag=wx.GROW) Edouard@2152: Edouard@2152: self.EtherCATManagementEditor.SetSizer(self.EtherCATManagermentEditor_Main_Sizer) Edouard@2152: return self.EtherCATManagementEditor edouard@2641: Edouard@2152: def OnResize(self, event): Edouard@2152: self.EtherCATManagementEditor.GetBestSize() Edouard@2152: xstart, ystart = self.EtherCATManagementEditor.GetViewStart() Edouard@2152: window_size = self.EtherCATManagementEditor.GetClientSize() Edouard@2152: maxx, maxy = self.EtherCATManagementEditor.GetMinSize() andrej@2437: posx = max(0, min(xstart, (maxx - window_size[0]) // SCROLLBAR_UNIT)) andrej@2437: posy = max(0, min(ystart, (maxy - window_size[1]) // SCROLLBAR_UNIT)) Edouard@2152: self.EtherCATManagementEditor.Scroll(posx, posy) andrej@2355: self.EtherCATManagementEditor.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, andrej@2437: maxx // SCROLLBAR_UNIT, andrej@2437: maxy // SCROLLBAR_UNIT, andrej@2381: posx, posy) Edouard@2152: event.Skip() Edouard@2152: # ------------------------------------------------------------------------------------------------------- Edouard@2152: andrej@2370: Laurent@2098: CIA402NodeEditor = NodeEditor Laurent@2098: Edouard@2152: Laurent@2098: def GetProcessVariablesTableColnames(): andrej@2375: _ = NoTranslate andrej@2355: return ["#", _("Name"), andrej@2355: _("Read from (nodeid, index, subindex)"), Laurent@2098: _("Write to (nodeid, index, subindex)"), Laurent@2098: _("Description")] Laurent@2098: andrej@2360: Laurent@2098: class ProcessVariablesTable(CustomTable): andrej@2355: 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, "") andrej@2355: 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 andrej@2355: 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) andrej@2355: Laurent@2098: grid.SetCellEditor(row, col, editor) Laurent@2098: grid.SetCellRenderer(row, col, renderer) andrej@2355: Laurent@2098: self.ResizeRow(grid, row) Laurent@2098: andrej@2360: Laurent@2098: class ProcessVariableDropTarget(wx.TextDropTarget): andrej@2355: Laurent@2098: def __init__(self, parent): Laurent@2098: wx.TextDropTarget.__init__(self) Laurent@2098: self.ParentWindow = parent andrej@2355: 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) andrej@2353: except Exception: andrej@2358: message = _("Invalid value \"%s\" for process variable") % data Laurent@2098: values = None andrej@2450: if not isinstance(values, tuple): andrej@2358: 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() andrej@2379: if master_location == tuple(location[:len(master_location)]) and \ andrej@2379: 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: andrej@2358: message = _("Invalid value \"%s\" for process variable") % data andrej@2355: Laurent@2098: if message is not None: Laurent@2098: wx.CallAfter(self.ShowMessage, message) andrej@2355: Laurent@2098: def ShowMessage(self, message): andrej@2367: message = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK | wx.ICON_ERROR) Laurent@2098: message.ShowModal() Laurent@2098: message.Destroy() Laurent@2098: andrej@2360: Laurent@2098: def GetStartupCommandsTableColnames(): andrej@2375: _ = NoTranslate Laurent@2098: return [_("Position"), _("Index"), _("Subindex"), _("Value"), _("Description")] Laurent@2098: andrej@2360: Laurent@2098: class StartupCommandDropTarget(wx.TextDropTarget): andrej@2355: Laurent@2098: def __init__(self, parent): Laurent@2098: wx.TextDropTarget.__init__(self) Laurent@2098: self.ParentWindow = parent andrej@2355: 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) andrej@2353: except Exception: andrej@2358: message = _("Invalid value \"%s\" for startup command") % data Laurent@2098: values = None andrej@2450: if not isinstance(values, tuple): andrej@2358: 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() andrej@2379: if master_location == tuple(location[:len(master_location)]) and \ andrej@2379: 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: andrej@2358: message = _("Invalid value \"%s\" for startup command") % data andrej@2355: Laurent@2098: if message is not None: Laurent@2098: wx.CallAfter(self.ShowMessage, message) andrej@2355: Laurent@2098: def ShowMessage(self, message): andrej@2367: message = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK | wx.ICON_ERROR) Laurent@2098: message.ShowModal() Laurent@2098: message.Destroy() Laurent@2098: andrej@2360: 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 andrej@2355: 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 andrej@2355: Laurent@2098: def GetOldValue(self): Laurent@2098: return self.old_value andrej@2355: 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() andrej@2355: Laurent@2098: grid.SetCellEditor(row, col, editor) Laurent@2098: grid.SetCellRenderer(row, col, renderer) Laurent@2098: grid.SetReadOnly(row, col, False) andrej@2355: Laurent@2098: self.ResizeRow(grid, row) andrej@2355: 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: andrej@2360: Laurent@2098: class MasterNodesVariablesSizer(NodeVariablesSizer): andrej@2355: Laurent@2098: def __init__(self, parent, controler): Laurent@2098: NodeVariablesSizer.__init__(self, parent, controler, True) andrej@2355: Laurent@2098: self.CurrentNodesFilter = {} andrej@2355: Laurent@2098: def SetCurrentNodesFilter(self, nodes_filter): Laurent@2098: self.CurrentNodesFilter = nodes_filter andrej@2355: 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: andrej@2370: Laurent@2129: NODE_POSITION_FILTER_FORMAT = _("Node Position: %d") Laurent@2129: andrej@2360: Laurent@2098: class MasterEditor(ConfTreeNodeEditor): andrej@2355: Laurent@2098: CONFNODEEDITOR_TABS = [ Edouard@2152: (_("Network"), "_create_EthercatMasterEditor"), Edouard@2152: (_("Master State"), "_create_MasterStateEditor") Edouard@2152: ] andrej@2355: Edouard@2152: def _create_MasterStateEditor(self, prnt): andrej@2367: self.MasterStateEditor = wx.ScrolledWindow(prnt, style=wx.TAB_TRAVERSAL | wx.HSCROLL | wx.VSCROLL) Edouard@2152: self.MasterStateEditor.Bind(wx.EVT_SIZE, self.OnResize) andrej@2355: edouard@2641: self.MasterStateEditor_Panel_Main_Sizer = wx.BoxSizer(wx.VERTICAL) edouard@2641: Edouard@2152: self.MasterStateEditor_Panel = MasterStatePanelClass(self.MasterStateEditor, self.Controler) andrej@2355: edouard@3303: self.MasterStateEditor_Panel_Main_Sizer.Add(self.MasterStateEditor_Panel, border=10, flag=wx.GROW) andrej@2355: Edouard@2152: self.MasterStateEditor.SetSizer(self.MasterStateEditor_Panel_Main_Sizer) Edouard@2152: return self.MasterStateEditor andrej@2355: edouard@2641: def OnResize2(self, event): Edouard@2152: self.MasterStateEditor.GetBestSize() Edouard@2152: xstart, ystart = self.MasterStateEditor.GetViewStart() Edouard@2152: window_size = self.MasterStateEditor.GetClientSize() Edouard@2152: maxx, maxy = self.MasterStateEditor.GetMinSize() Edouard@2152: posx = max(0, min(xstart, (maxx - window_size[0]) / SCROLLBAR_UNIT)) Edouard@2152: posy = max(0, min(ystart, (maxy - window_size[1]) / SCROLLBAR_UNIT)) Edouard@2152: self.MasterStateEditor.Scroll(posx, posy) Edouard@2152: self.MasterStateEditor.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, Edouard@2152: maxx / SCROLLBAR_UNIT, maxy / SCROLLBAR_UNIT, posx, posy) Edouard@2152: event.Skip() edouard@2643: Laurent@2098: def _create_EthercatMasterEditor(self, prnt): andrej@2355: self.EthercatMasterEditor = wx.ScrolledWindow(prnt, andrej@2381: style=wx.TAB_TRAVERSAL | wx.HSCROLL | wx.VSCROLL) Laurent@2098: self.EthercatMasterEditor.Bind(wx.EVT_SIZE, self.OnResize) andrej@2355: Laurent@2134: self.EthercatMasterEditorSizer = wx.BoxSizer(wx.VERTICAL) andrej@2355: Laurent@2098: self.NodesFilter = wx.ComboBox(self.EthercatMasterEditor, andrej@2381: 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@2129: self.NodesFilter.Bind(wx.EVT_CHAR, self.OnNodesFilterKeyDown) andrej@2355: Laurent@2098: process_variables_header = wx.BoxSizer(wx.HORIZONTAL) andrej@2355: Laurent@2098: process_variables_label = wx.StaticText(self.EthercatMasterEditor, andrej@2381: label=_("Process variables mapped between nodes:")) edouard@3303: process_variables_header.Add(process_variables_label, 1, andrej@2381: flag=wx.ALIGN_CENTER_VERTICAL) andrej@2355: 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"))]: andrej@2355: button = wx.lib.buttons.GenBitmapButton(self.EthercatMasterEditor, bitmap=GetBitmap(bitmap), andrej@2381: size=wx.Size(28, 28), style=wx.NO_BORDER) edouard@3303: button.SetToolTip(help) Laurent@2098: setattr(self, name, button) edouard@3303: process_variables_header.Add(button, border=5, flag=wx.LEFT) andrej@2355: 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)) edouard@3303: self.ProcessVariablesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGING, andrej@2381: self.OnProcessVariablesGridCellChange) andrej@2355: self.ProcessVariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, andrej@2381: self.OnProcessVariablesGridCellLeftClick) Laurent@2099: self.ProcessVariablesGrid.Bind(wx.EVT_KEY_DOWN, self.OnProcessVariablesGridKeyDown) andrej@2355: Laurent@2098: startup_commands_header = wx.BoxSizer(wx.HORIZONTAL) andrej@2355: Laurent@2098: startup_commands_label = wx.StaticText(self.EthercatMasterEditor, andrej@2381: label=_("Startup service variables assignments:")) edouard@3303: startup_commands_header.Add(startup_commands_label, 1, andrej@2381: flag=wx.ALIGN_CENTER_VERTICAL) andrej@2355: 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"))]: andrej@2355: button = wx.lib.buttons.GenBitmapButton(self.EthercatMasterEditor, bitmap=GetBitmap(bitmap), andrej@2381: size=wx.Size(28, 28), style=wx.NO_BORDER) edouard@3303: button.SetToolTip(help) Laurent@2098: setattr(self, name, button) edouard@3303: startup_commands_header.Add(button, border=5, flag=wx.LEFT) andrej@2355: 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)) edouard@3303: self.StartupCommandsGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGING, andrej@2381: self.OnStartupCommandsGridCellChange) andrej@2355: self.StartupCommandsGrid.Bind(wx.grid.EVT_GRID_EDITOR_SHOWN, andrej@2381: self.OnStartupCommandsGridEditorShow) andrej@2355: Laurent@2098: self.NodesVariables = MasterNodesVariablesSizer(self.EthercatMasterEditor, self.Controler) andrej@2355: Laurent@2098: main_staticbox = wx.StaticBox(self.EthercatMasterEditor, label=_("Node filter:")) Laurent@2098: staticbox_sizer = wx.StaticBoxSizer(main_staticbox, wx.VERTICAL) edouard@3303: self.EthercatMasterEditorSizer.Add(staticbox_sizer, 0, border=10, flag=wx.GROW | wx.ALL) andrej@2355: 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) edouard@3303: staticbox_sizer.Add(main_staticbox_sizer, 1, flag=wx.GROW) edouard@3303: main_staticbox_sizer.Add(self.NodesFilter, border=5, flag=wx.GROW | wx.ALL) edouard@3303: main_staticbox_sizer.Add(process_variables_header, border=5, andrej@2381: flag=wx.GROW | wx.LEFT | wx.RIGHT | wx.BOTTOM) edouard@3303: main_staticbox_sizer.Add(self.ProcessVariablesGrid, 1, andrej@2381: border=5, flag=wx.GROW | wx.LEFT | wx.RIGHT | wx.BOTTOM) edouard@3303: main_staticbox_sizer.Add(startup_commands_header, andrej@2381: border=5, flag=wx.GROW | wx.LEFT | wx.RIGHT | wx.BOTTOM) edouard@3303: main_staticbox_sizer.Add(self.StartupCommandsGrid, 1, andrej@2381: border=5, flag=wx.GROW | wx.LEFT | wx.RIGHT | wx.BOTTOM) andrej@2355: Laurent@2134: second_staticbox = wx.StaticBox(self.EthercatMasterEditor, label=_("Nodes variables filter:")) Laurent@2134: second_staticbox_sizer = wx.StaticBoxSizer(second_staticbox, wx.VERTICAL) edouard@3303: second_staticbox_sizer.Add(self.NodesVariables, 1, border=5, flag=wx.GROW | wx.ALL) edouard@3303: edouard@3303: main_staticbox_sizer.Add(second_staticbox_sizer, 1, andrej@2381: border=5, flag=wx.GROW | wx.LEFT | wx.RIGHT | wx.BOTTOM) andrej@2355: Laurent@2134: self.EthercatMasterEditor.SetSizer(self.EthercatMasterEditorSizer) andrej@2355: Laurent@2098: return self.EthercatMasterEditor Laurent@2098: Laurent@2098: def __init__(self, parent, controler, window): Laurent@2098: ConfTreeNodeEditor.__init__(self, parent, controler, window) andrej@2355: Edouard@2152: # ------------------------------------------------------------------ Edouard@2152: self.Controler = controler Edouard@2152: # ------------------------------------------------------------------ andrej@2355: Laurent@2099: self.ProcessVariables = [] Laurent@2100: self.CellShown = None Laurent@2129: self.NodesFilterFirstCharacter = True andrej@2355: 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] andrej@2355: 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}) andrej@2355: 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) andrej@2355: 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) andrej@2355: 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) andrej@2355: Laurent@2099: _refresh_buttons = getattr(self.ProcessVariablesGrid, "RefreshButtons") andrej@2371: 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) andrej@2355: 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() andrej@2355: 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] andrej@2355: Laurent@2098: self.StartupCommandsGrid.SetTable(self.StartupCommandsTable) Laurent@2098: self.StartupCommandsGrid.SetButtons({"Add": self.AddCommandButton, Laurent@2098: "Delete": self.DeleteCommandButton}) andrej@2355: 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) andrej@2355: 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) andrej@2355: 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() andrej@2355: 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() andrej@2355: Laurent@2099: def GetBufferState(self): Laurent@2099: return self.Controler.GetBufferState() andrej@2355: Laurent@2099: def Undo(self): Laurent@2099: self.Controler.LoadPrevious() Laurent@2099: self.RefreshView() andrej@2355: Laurent@2099: def Redo(self): Laurent@2099: self.Controler.LoadNext() Laurent@2099: self.RefreshView() andrej@2355: Laurent@2098: def RefreshView(self): Laurent@2098: ConfTreeNodeEditor.RefreshView(self) andrej@2355: Laurent@2098: self.RefreshNodesFilter() Laurent@2098: self.RefreshProcessVariables() Laurent@2098: self.RefreshStartupCommands() Laurent@2098: self.NodesVariables.RefreshView() andrej@2355: 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) andrej@2353: except Exception: Laurent@2098: self.NodesFilter.SetSelection(0) Laurent@2098: self.RefreshCurrentNodesFilter() andrej@2355: 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@2129: value = self.NodesFilter.GetValue() Laurent@2129: if value == "": Laurent@2129: self.CurrentNodesFilter = self.NodesFilterValues[0] Laurent@2129: self.NodesFilter.SetSelection(0) Laurent@2129: else: Laurent@2129: position = int(self.NodesFilter.GetValue()) Laurent@2129: self.CurrentNodesFilter = {"slave_pos": position} Laurent@2129: self.NodesFilter.SetValue(NODE_POSITION_FILTER_FORMAT % position) andrej@2353: except Exception: Laurent@2129: if self.CurrentNodesFilter in self.NodesFilterValues: Laurent@2129: self.NodesFilter.SetSelection(self.NodesFilterValues.index(self.CurrentNodesFilter)) Laurent@2129: else: Laurent@2129: self.NodesFilter.SetValue(NODE_POSITION_FILTER_FORMAT % self.CurrentNodesFilter["slave_pos"]) Laurent@2129: self.NodesFilterFirstCharacter = True Laurent@2098: self.NodesVariables.SetCurrentNodesFilter(self.CurrentNodesFilter) andrej@2355: 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: andrej@2379: if variable["ReadFrom"] == "" or variable["ReadFrom"][0] in slaves or \ andrej@2379: 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() andrej@2355: Laurent@2098: def SaveProcessVariables(self): Laurent@2110: if self.CurrentNodesFilter is not None: Laurent@2110: if len(self.CurrentNodesFilter) > 0: Laurent@2110: self.Controler.SetProcessVariables(self.ProcessVariables) Laurent@2110: else: Laurent@2110: self.Controler.SetProcessVariables(self.ProcessVariablesTable.GetData()) Laurent@2110: self.RefreshBuffer() andrej@2355: Laurent@2099: def RefreshStartupCommands(self, position=None, command_idx=None): Laurent@2098: if self.CurrentNodesFilter is not None: Laurent@2151: col = max(self.StartupCommandsGrid.GetGridCursorCol(), 0) 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@2151: self.SelectStartupCommand(position, command_idx, col) andrej@2355: Laurent@2151: def SelectStartupCommand(self, position, command_idx, col): Laurent@2151: self.StartupCommandsGrid.SetSelectedCell( Laurent@2151: self.StartupCommandsTable.GetCommandIndex(position, command_idx), Laurent@2151: col) andrej@2355: Laurent@2098: def GetMasterLocation(self): Laurent@2098: return self.Controler.GetCurrentLocation() andrej@2355: Laurent@2098: def AddStartupCommand(self, position, index, subindex): Laurent@2151: col = max(self.StartupCommandsGrid.GetGridCursorCol(), 0) 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@2151: self.SelectStartupCommand(position, command_idx, col) andrej@2355: Laurent@2098: def OnNodesFilterChanged(self, event): Laurent@2098: self.RefreshCurrentNodesFilter() Laurent@2098: if self.CurrentNodesFilter is not None: Laurent@2110: self.RefreshProcessVariables() Laurent@2098: self.RefreshStartupCommands() Laurent@2098: self.NodesVariables.RefreshView() laurent@2022: event.Skip() andrej@2355: Laurent@2129: def OnNodesFilterKeyDown(self, event): Laurent@2129: if self.NodesFilterFirstCharacter: Laurent@2129: keycode = event.GetKeyCode() andrej@2355: if keycode not in [wx.WXK_RETURN, Laurent@2130: wx.WXK_NUMPAD_ENTER]: Laurent@2130: self.NodesFilterFirstCharacter = False Laurent@2130: if keycode not in NAVIGATION_KEYS: Laurent@2130: self.NodesFilter.SetValue("") andrej@2355: if keycode not in [wx.WXK_DELETE, andrej@2355: wx.WXK_NUMPAD_DELETE, Laurent@2129: wx.WXK_BACK]: Laurent@2129: event.Skip() Laurent@2129: else: Laurent@2129: event.Skip() andrej@2355: 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: andrej@2367: dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR) Laurent@2098: dialog.ShowModal() Laurent@2098: dialog.Destroy() Laurent@2098: event.Veto() andrej@2355: 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 + \ andrej@2392: ".".join(map(str, self.Controler.GetCurrentLocation() + (number,))) andrej@2355: 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() andrej@2355: 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) andrej@2379: if keycode in (wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE) and \ andrej@2379: (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() andrej@2355: Laurent@2100: def OnStartupCommandsGridEditorShow(self, event): Laurent@2100: self.CellShown = event.GetRow(), event.GetCol() Laurent@2100: event.Skip() andrej@2355: 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) andrej@2355: 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: andrej@2367: 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() andrej@2355: 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@2134: maxx, maxy = self.EthercatMasterEditorSizer.GetMinSize() andrej@2437: posx = max(0, min(xstart, (maxx - window_size[0]) // SCROLLBAR_UNIT)) andrej@2437: posy = max(0, min(ystart, (maxy - window_size[1]) // SCROLLBAR_UNIT)) Laurent@2098: self.EthercatMasterEditor.Scroll(posx, posy) andrej@2355: self.EthercatMasterEditor.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, andrej@2437: maxx // SCROLLBAR_UNIT, andrej@2437: maxy // SCROLLBAR_UNIT, andrej@2381: posx, posy) Laurent@2098: event.Skip() andrej@2355: andrej@2356: # def OnButtonClick(self, event): Edouard@2152: # self.MasterState = self.Controler.getMasterState() Edouard@2152: # if self.MasterState: Edouard@2152: # self.Phase.SetValue(self.MasterState["phase"]) Edouard@2152: # self.Active.SetValue(self.MasterState["active"]) Edouard@2152: # self.SlaveCount.SetValue(self.MasterState["slave"]) Edouard@2152: # self.MacAddress.SetValue(self.MasterState["MAC"]) Edouard@2152: # self.LinkState.SetValue(self.MasterState["link"]) Edouard@2152: # self.TxFrames.SetValue(self.MasterState["TXframe"]) Edouard@2152: # self.RxFrames.SetValue(self.MasterState["RXframe"]) Edouard@2152: # self.TxByte.SetValue(self.MasterState["TXbyte"]) Edouard@2152: # self.TxError.SetValue(self.MasterState["TXerror"]) Edouard@2152: # self.LostFrames.SetValue(self.MasterState["lost"]) andrej@2355: Edouard@2152: # self.TxFrameRate1.SetValue(self.MasterState["TXframerate1"]) Edouard@2152: # self.TxFrameRate2.SetValue(self.MasterState["TXframerate2"]) Edouard@2152: # self.TxFrameRate3.SetValue(self.MasterState["TXframerate3"]) Edouard@2152: # self.TxRate1.SetValue(self.MasterState["TXrate1"]) Edouard@2152: # self.TxRate2.SetValue(self.MasterState["TXrate2"]) Edouard@2152: # self.TxRate3.SetValue(self.MasterState["TXrate3"]) Edouard@2152: # self.LossRate1.SetValue(self.MasterState["loss1"]) Edouard@2152: # self.LossRate2.SetValue(self.MasterState["loss2"]) Edouard@2152: # self.LossRate3.SetValue(self.MasterState["loss3"]) Edouard@2152: # self.FrameLoss1.SetValue(self.MasterState["frameloss1"]) Edouard@2152: # self.FrameLoss2.SetValue(self.MasterState["frameloss2"]) Edouard@2152: # self.FrameLoss3.SetValue(self.MasterState["frameloss3"]) andrej@2355: andrej@2360: Laurent@2098: class LibraryEditorSizer(wx.FlexGridSizer): andrej@2355: Laurent@2097: def __init__(self, parent, module_library, buttons): Laurent@2098: wx.FlexGridSizer.__init__(self, cols=1, hgap=0, rows=4, vgap=5) andrej@2355: Laurent@2097: self.ModuleLibrary = module_library Laurent@2105: self.ParentWindow = parent andrej@2355: Laurent@2098: self.AddGrowableCol(0) Laurent@2098: self.AddGrowableRow(1) Laurent@2098: self.AddGrowableRow(3) andrej@2355: andrej@2355: ESI_files_label = wx.StaticText(parent, andrej@2381: label=_("ESI Files:")) edouard@3303: self.Add(ESI_files_label, border=10, andrej@2381: flag=wx.TOP | wx.LEFT | wx.RIGHT) andrej@2355: 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) edouard@3303: self.Add(folder_tree_sizer, border=10, andrej@2381: flag=wx.GROW | wx.LEFT | wx.RIGHT) andrej@2355: Laurent@2098: self.ESIFiles = FolderTree(parent, self.GetPath(), editable=False) Laurent@2097: self.ESIFiles.SetFilter(".xml") edouard@3303: folder_tree_sizer.Add(self.ESIFiles, flag=wx.GROW) andrej@2355: Laurent@2097: buttons_sizer = wx.BoxSizer(wx.VERTICAL) edouard@3303: folder_tree_sizer.Add(buttons_sizer, andrej@2381: flag=wx.ALIGN_CENTER_VERTICAL) andrej@2355: Laurent@2097: for idx, (name, bitmap, help, callback) in enumerate(buttons): andrej@2355: button = wx.lib.buttons.GenBitmapButton(parent, andrej@2381: bitmap=GetBitmap(bitmap), andrej@2381: size=wx.Size(28, 28), andrej@2381: style=wx.NO_BORDER) edouard@3303: button.SetToolTip(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) edouard@3303: buttons_sizer.Add(button, border=10, flag=flag) andrej@2355: andrej@2355: modules_label = wx.StaticText(parent, andrej@2381: label=_("Modules library:")) edouard@3303: self.Add(modules_label, border=10, andrej@2381: flag=wx.LEFT | wx.RIGHT) andrej@2355: edouard@3303: self.ModulesGrid = wx.adv.TreeListCtrl(parent, andrej@2381: style=wx.TR_DEFAULT_STYLE | andrej@2381: wx.TR_ROW_LINES | andrej@2381: wx.TR_COLUMN_LINES | andrej@2381: wx.TR_HIDE_ROOT | andrej@2381: wx.TR_FULL_ROW_HIGHLIGHT) Laurent@2138: self.ModulesGrid.GetMainWindow().Bind(wx.EVT_LEFT_DOWN, andrej@2381: self.OnModulesGridLeftDown) Laurent@2138: self.ModulesGrid.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, andrej@2381: self.OnModulesGridBeginLabelEdit) Laurent@2138: self.ModulesGrid.Bind(wx.EVT_TREE_END_LABEL_EDIT, andrej@2381: self.OnModulesGridEndLabelEdit) andrej@2355: self.ModulesGrid.GetHeaderWindow().Bind(wx.EVT_MOTION, andrej@2381: self.OnModulesGridHeaderMotion) edouard@3303: self.Add(self.ModulesGrid, border=10, andrej@2381: flag=wx.GROW | wx.BOTTOM | wx.LEFT | wx.RIGHT) andrej@2355: Laurent@2137: for colname, colsize, colalign in zip( andrej@2355: [_("Name")] + [param_infos["column_label"] andrej@2406: for _param, param_infos in Laurent@2137: self.ModuleLibrary.MODULES_EXTRA_PARAMS], andrej@2355: [400] + [param_infos["column_size"] andrej@2406: for _param, param_infos in Laurent@2138: self.ModuleLibrary.MODULES_EXTRA_PARAMS], Laurent@2137: [wx.ALIGN_LEFT] + [wx.ALIGN_RIGHT] * len(self.ModuleLibrary.MODULES_EXTRA_PARAMS)): Laurent@2138: self.ModulesGrid.AddColumn(_(colname), colsize, colalign, edit=True) Laurent@2097: self.ModulesGrid.SetMainColumn(0) andrej@2355: Laurent@2138: self.CurrentSelectedCol = None Laurent@2138: self.LastToolTipCol = None andrej@2355: Laurent@2097: def GetPath(self): Laurent@2097: return self.ModuleLibrary.GetPath() andrej@2355: Laurent@2098: def SetControlMinSize(self, size): Laurent@2098: self.ESIFiles.SetMinSize(size) Laurent@2098: self.ModulesGrid.SetMinSize(size) andrej@2355: Laurent@2097: def GetSelectedFilePath(self): Laurent@2097: return self.ESIFiles.GetPath() andrej@2355: Laurent@2097: def RefreshView(self): Laurent@2097: self.ESIFiles.RefreshTree() Laurent@2097: self.RefreshModulesGrid() andrej@2355: Laurent@2097: def RefreshModulesGrid(self): Laurent@2097: root = self.ModulesGrid.GetRootItem() Laurent@2097: if not root.IsOk(): Laurent@2097: root = self.ModulesGrid.AddRoot("Modules") andrej@2355: self.GenerateModulesGridBranch(root, andrej@2381: self.ModuleLibrary.GetModulesLibrary(), andrej@2381: GetVariablesTableColnames()) Laurent@2097: self.ModulesGrid.Expand(root) andrej@2355: Laurent@2097: def GenerateModulesGridBranch(self, root, modules, colnames): Laurent@2097: item, root_cookie = self.ModulesGrid.GetFirstChild(root) andrej@2355: 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: andrej@2406: for param_idx, (param, _param_infos) in enumerate(self.ModuleLibrary.MODULES_EXTRA_PARAMS): andrej@2355: self.ModulesGrid.SetItemText(item, andrej@2355: str(module["infos"][param]), Laurent@2137: param_idx + 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() andrej@2355: 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) andrej@2355: Laurent@2097: def OnImportButton(self, event): Laurent@2105: dialog = wx.FileDialog(self.ParentWindow, andrej@2381: _("Choose an XML file"), andrej@2381: os.getcwd(), "", andrej@2381: _("XML files (*.xml)|*.xml|All files|*.*"), andrej@2381: wx.OPEN) andrej@2355: 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: andrej@2355: message = wx.MessageDialog(self, andrej@2381: _("No such XML file: %s\n") % filepath, andrej@2381: _("Error"), andrej@2381: wx.OK | wx.ICON_ERROR) Laurent@2097: message.ShowModal() Laurent@2097: message.Destroy() Laurent@2097: dialog.Destroy() andrej@2355: Laurent@2097: event.Skip() andrej@2355: Laurent@2097: def OnDeleteButton(self, event): Laurent@2097: filepath = self.GetSelectedFilePath() Laurent@2097: if os.path.isfile(filepath): andrej@2406: _folder, filename = os.path.split(filepath) andrej@2355: andrej@2355: dialog = wx.MessageDialog(self.ParentWindow, andrej@2381: _("Do you really want to delete the file '%s'?") % filename, andrej@2381: _("Delete File"), andrej@2381: wx.YES_NO | wx.ICON_QUESTION) Laurent@2097: remove = dialog.ShowModal() == wx.ID_YES Laurent@2097: dialog.Destroy() andrej@2355: Laurent@2097: if remove: Laurent@2097: os.remove(filepath) Laurent@2097: self.ModuleLibrary.LoadModules() Laurent@2097: wx.CallAfter(self.RefreshView) Laurent@2097: event.Skip() andrej@2355: Laurent@2138: def OnModulesGridLeftDown(self, event): andrej@2406: item, _flags, col = self.ModulesGrid.HitTest(event.GetPosition()) Laurent@2097: if item.IsOk(): Laurent@2097: entry_infos = self.ModulesGrid.GetItemPyData(item) Laurent@2137: if entry_infos is not None and col > 0: Laurent@2138: self.CurrentSelectedCol = col Laurent@2138: else: Laurent@2138: self.CurrentSelectedCol = None Laurent@2138: else: Laurent@2138: self.CurrentSelectedCol = None Laurent@2138: event.Skip() Laurent@2138: Laurent@2138: def OnModulesGridBeginLabelEdit(self, event): Laurent@2138: item = event.GetItem() Laurent@2138: if item.IsOk(): Laurent@2138: entry_infos = self.ModulesGrid.GetItemPyData(item) Laurent@2138: if entry_infos is not None: Laurent@2138: event.Skip() Laurent@2138: else: Laurent@2138: event.Veto() Laurent@2138: else: Laurent@2138: event.Veto() Laurent@2138: Laurent@2138: def OnModulesGridEndLabelEdit(self, event): Laurent@2138: item = event.GetItem() Laurent@2138: if item.IsOk() and self.CurrentSelectedCol is not None: Laurent@2138: entry_infos = self.ModulesGrid.GetItemPyData(item) Laurent@2138: if entry_infos is not None and self.CurrentSelectedCol > 0: Laurent@2138: param, param_infos = self.ModuleLibrary.MODULES_EXTRA_PARAMS[self.CurrentSelectedCol - 1] Laurent@2138: stripped_column_label = param_infos["column_label"].split('(')[0].strip() Laurent@2138: try: Laurent@2138: self.ModuleLibrary.SetModuleExtraParam( Laurent@2138: entry_infos["vendor"], Laurent@2138: entry_infos["product_code"], Laurent@2138: entry_infos["revision_number"], Laurent@2138: param, Laurent@2138: int(event.GetLabel())) Laurent@2138: wx.CallAfter(self.RefreshModulesGrid) Laurent@2138: event.Skip() Laurent@2138: except ValueError: andrej@2355: message = wx.MessageDialog(self, andrej@2381: _("Module %s must be an integer!") % stripped_column_label, andrej@2381: _("Error"), andrej@2381: wx.OK | wx.ICON_ERROR) Laurent@2138: message.ShowModal() Laurent@2138: message.Destroy() Laurent@2138: event.Veto() Laurent@2138: else: Laurent@2138: event.Veto() Laurent@2138: else: Laurent@2138: event.Veto() andrej@2355: Laurent@2138: def OnModulesGridHeaderMotion(self, event): andrej@2406: _item, _flags, col = self.ModulesGrid.HitTest(event.GetPosition()) Laurent@2138: if col != self.LastToolTipCol and self.LastToolTipCol is not None: Laurent@2143: self.ModulesGrid.GetHeaderWindow().SetToolTip(None) Laurent@2138: self.LastToolTipCol = None Laurent@2143: if col > 0 and self.LastToolTipCol != col: Laurent@2138: self.LastToolTipCol = col andrej@2406: _param, param_infos = self.ModuleLibrary.MODULES_EXTRA_PARAMS[col - 1] edouard@3303: wx.CallAfter(self.ModulesGrid.GetHeaderWindow().SetToolTip, Laurent@2143: param_infos["description"]) Laurent@2097: event.Skip() Laurent@2058: andrej@2360: Laurent@2097: class DatabaseManagementDialog(wx.Dialog): andrej@2355: Laurent@2097: def __init__(self, parent, database): Laurent@2097: wx.Dialog.__init__(self, parent, andrej@2381: size=wx.Size(700, 500), andrej@2381: title=_('ESI Files Database management'), andrej@2381: style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER) andrej@2355: 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) andrej@2355: andrej@2381: self.DatabaseSizer = LibraryEditorSizer( andrej@2381: self, database, andrej@2381: [ andrej@2381: ("ImportButton", "ImportESI", _("Import file to ESI files database"), None), andrej@2381: ("DeleteButton", "remove_element", _("Remove file from database"), None) andrej@2381: ]) Laurent@2098: self.DatabaseSizer.SetControlMinSize(wx.Size(0, 0)) edouard@3303: main_sizer.Add(self.DatabaseSizer, border=10, andrej@2381: flag=wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT) andrej@2367: andrej@2367: button_sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE) edouard@3303: # FIXME: find a way to change buttons label compatible with wxPython 4.x edouard@3303: # button_sizer.GetAffirmativeButton().SetLabel(_("Add file to project")) edouard@3303: # button_sizer.GetCancelButton().SetLabel(_("Close")) edouard@3303: main_sizer.Add(button_sizer, border=10, andrej@2381: flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT) andrej@2355: Laurent@2097: self.SetSizer(main_sizer) andrej@2355: Laurent@2098: self.DatabaseSizer.RefreshView() andrej@2355: Laurent@2097: def GetValue(self): Laurent@2098: return self.DatabaseSizer.GetSelectedFilePath() Laurent@2097: andrej@2360: Laurent@2097: class LibraryEditor(ConfTreeNodeEditor): andrej@2355: Laurent@2097: CONFNODEEDITOR_TABS = [ Laurent@2097: (_("Modules Library"), "_create_ModuleLibraryEditor")] andrej@2355: Laurent@2097: def _create_ModuleLibraryEditor(self, prnt): Laurent@2098: self.ModuleLibraryEditor = wx.ScrolledWindow(prnt, andrej@2381: style=wx.TAB_TRAVERSAL | wx.HSCROLL | wx.VSCROLL) Laurent@2098: self.ModuleLibraryEditor.Bind(wx.EVT_SIZE, self.OnResize) andrej@2355: andrej@2381: self.ModuleLibrarySizer = LibraryEditorSizer( andrej@2381: self.ModuleLibraryEditor, Laurent@2097: self.Controler.GetModulesLibraryInstance(), andrej@2381: [ andrej@2381: ("ImportButton", "ImportESI", _("Import ESI file"), None), andrej@2381: ("AddButton", "ImportDatabase", _("Add file from ESI files database"), self.OnAddButton), andrej@2381: ("DeleteButton", "remove_element", _("Remove file from library"), None) andrej@2381: ]) Laurent@2098: self.ModuleLibrarySizer.SetControlMinSize(wx.Size(0, 200)) Laurent@2098: self.ModuleLibraryEditor.SetSizer(self.ModuleLibrarySizer) andrej@2355: Laurent@2097: return self.ModuleLibraryEditor Laurent@2097: Laurent@2097: def __init__(self, parent, controler, window): Laurent@2097: ConfTreeNodeEditor.__init__(self, parent, controler, window) andrej@2355: Laurent@2097: self.RefreshView() andrej@2355: Laurent@2097: def RefreshView(self): Laurent@2097: ConfTreeNodeEditor.RefreshView(self) Laurent@2098: self.ModuleLibrarySizer.RefreshView() Laurent@2097: Laurent@2097: def OnAddButton(self, event): andrej@2355: dialog = DatabaseManagementDialog(self, andrej@2381: self.Controler.GetModulesDatabaseInstance()) andrej@2355: Laurent@2097: if dialog.ShowModal() == wx.ID_OK: Laurent@2097: module_library = self.Controler.GetModulesLibraryInstance() Laurent@2097: module_library.ImportModuleLibrary(dialog.GetValue()) andrej@2355: Laurent@2097: dialog.Destroy() andrej@2355: Laurent@2098: wx.CallAfter(self.ModuleLibrarySizer.RefreshView) andrej@2355: 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() andrej@2437: posx = max(0, min(xstart, (maxx - window_size[0]) // SCROLLBAR_UNIT)) andrej@2437: posy = max(0, min(ystart, (maxy - window_size[1]) // SCROLLBAR_UNIT)) Laurent@2098: self.ModuleLibraryEditor.Scroll(posx, posy) andrej@2355: self.ModuleLibraryEditor.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, andrej@2437: maxx // SCROLLBAR_UNIT, andrej@2437: maxy // SCROLLBAR_UNIT, andrej@2381: posx, posy) Laurent@2098: event.Skip()