b@418: # -*- coding: utf-8 -*- b@418: b@418: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor b@418: #based on the plcopen standard. b@418: # b@418: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD b@418: # b@418: #See COPYING file for copyrights details. b@418: # b@418: #This library is free software; you can redistribute it and/or b@418: #modify it under the terms of the GNU General Public b@418: #License as published by the Free Software Foundation; either b@418: #version 2.1 of the License, or (at your option) any later version. b@418: # b@418: #This library is distributed in the hope that it will be useful, b@418: #but WITHOUT ANY WARRANTY; without even the implied warranty of b@418: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU b@418: #General Public License for more details. b@418: # b@418: #You should have received a copy of the GNU General Public b@418: #License along with this library; if not, write to the Free Software b@418: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA b@418: b@418: import wx, wx.grid b@418: b@419: from types import TupleType b@419: b@418: from PLCOpenEditor import AppendMenu b@418: from plcopen.structures import LOCATIONDATATYPES, TestIdentifier, IEC_KEYWORDS b@418: b@418: #------------------------------------------------------------------------------- b@418: # Variables Editor Panel b@418: #------------------------------------------------------------------------------- b@418: b@418: def GetVariableTableColnames(location): b@418: _ = lambda x : x b@418: if location: b@418: return ["#", _("Name"), _("Class"), _("Type"), _("Location"), _("Initial Value"), _("Retain"), _("Constant")] b@418: return ["#", _("Name"), _("Class"), _("Type"), _("Initial Value"), _("Retain"), _("Constant")] b@418: b@418: def GetAlternativeOptions(): b@418: _ = lambda x : x b@418: return [_("Yes"), _("No")] b@418: ALTERNATIVE_OPTIONS_DICT = dict([(_(option), option) for option in GetAlternativeOptions()]) b@418: b@418: def GetFilterChoiceTransfer(): b@418: _ = lambda x : x b@418: return {_("All"): _("All"), _("Interface"): _("Interface"), b@418: _(" Input"): _("Input"), _(" Output"): _("Output"), _(" InOut"): _("InOut"), b@418: _(" External"): _("External"), _("Variables"): _("Variables"), _(" Local"): _("Local"), b@418: _(" Temp"): _("Temp"), _("Global"): _("Global")}#, _("Access") : _("Access")} b@418: VARIABLE_CLASSES_DICT = dict([(_(_class), _class) for _class in GetFilterChoiceTransfer().itervalues()]) b@418: b@418: class VariableTable(wx.grid.PyGridTableBase): b@418: b@418: """ b@418: A custom wx.grid.Grid Table using user supplied data b@418: """ b@418: def __init__(self, parent, data, colnames): b@418: # The base class must be initialized *first* b@418: wx.grid.PyGridTableBase.__init__(self) b@418: self.data = data b@418: self.old_value = None b@418: self.colnames = colnames b@418: self.Errors = {} b@418: self.Parent = parent b@418: # XXX b@418: # we need to store the row length and collength to b@418: # see if the table has changed size b@418: self._rows = self.GetNumberRows() b@418: self._cols = self.GetNumberCols() b@418: b@418: def GetNumberCols(self): b@418: return len(self.colnames) b@418: b@418: def GetNumberRows(self): b@418: return len(self.data) b@418: b@418: def GetColLabelValue(self, col, translate=True): b@418: if col < len(self.colnames): b@418: if translate: b@418: return _(self.colnames[col]) b@418: return self.colnames[col] b@418: b@418: def GetRowLabelValues(self, row, translate=True): b@418: return row b@418: b@418: def GetValue(self, row, col): b@418: if row < self.GetNumberRows(): b@418: if col == 0: b@418: return self.data[row]["Number"] b@418: colname = self.GetColLabelValue(col, False) b@418: value = str(self.data[row].get(colname, "")) b@418: if colname in ["Class", "Retain", "Constant"]: b@418: return _(value) b@418: return value b@418: b@418: def SetValue(self, row, col, value): b@418: if col < len(self.colnames): b@418: colname = self.GetColLabelValue(col, False) b@418: if colname == "Name": b@418: self.old_value = self.data[row][colname] b@418: elif colname == "Class": b@418: value = VARIABLE_CLASSES_DICT[value] b@418: elif colname in ["Retain", "Constant"]: b@418: value = ALTERNATIVE_OPTIONS_DICT[value] b@418: self.data[row][colname] = value b@418: b@418: def GetValueByName(self, row, colname): b@418: if row < self.GetNumberRows(): b@418: return self.data[row].get(colname) b@418: b@418: def SetValueByName(self, row, colname, value): b@418: if row < self.GetNumberRows(): b@418: self.data[row][colname] = value b@418: b@418: def GetOldValue(self): b@418: return self.old_value b@418: b@418: def ResetView(self, grid): b@418: """ b@418: (wx.grid.Grid) -> Reset the grid view. Call this to b@418: update the grid if rows and columns have been added or deleted b@418: """ b@418: grid.BeginBatch() b@418: for current, new, delmsg, addmsg in [ b@418: (self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED), b@418: (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED), b@418: ]: b@418: if new < current: b@418: msg = wx.grid.GridTableMessage(self,delmsg,new,current-new) b@418: grid.ProcessTableMessage(msg) b@418: elif new > current: b@418: msg = wx.grid.GridTableMessage(self,addmsg,new-current) b@418: grid.ProcessTableMessage(msg) b@418: self.UpdateValues(grid) b@418: grid.EndBatch() b@418: b@418: self._rows = self.GetNumberRows() b@418: self._cols = self.GetNumberCols() b@418: # update the column rendering scheme b@418: self._updateColAttrs(grid) b@418: b@418: # update the scrollbars and the displayed part of the grid b@418: grid.AdjustScrollbars() b@418: grid.ForceRefresh() b@418: b@418: def UpdateValues(self, grid): b@418: """Update all displayed values""" b@418: # This sends an event to the grid table to update all of the values b@418: msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES) b@418: grid.ProcessTableMessage(msg) b@418: b@418: def _updateColAttrs(self, grid): b@418: """ b@418: wx.grid.Grid -> update the column attributes to add the b@418: appropriate renderer given the column name. b@418: b@418: Otherwise default to the default renderer. b@418: """ b@418: b@418: for row in range(self.GetNumberRows()): b@418: for col in range(self.GetNumberCols()): b@418: editor = None b@418: renderer = None b@418: colname = self.GetColLabelValue(col, False) b@418: if col != 0 and self.GetValueByName(row, "Edit"): b@418: grid.SetReadOnly(row, col, False) b@418: if colname == "Name": b@418: if self.Parent.PouIsUsed and self.GetValueByName(row, "Class") in ["Input", "Output", "InOut"]: b@418: grid.SetReadOnly(row, col, True) b@418: else: b@418: editor = wx.grid.GridCellTextEditor() b@418: renderer = wx.grid.GridCellStringRenderer() b@418: elif colname == "Initial Value": b@418: editor = wx.grid.GridCellTextEditor() b@418: renderer = wx.grid.GridCellStringRenderer() b@418: elif colname == "Location": b@418: if self.GetValueByName(row, "Class") in ["Local", "Global"]: b@418: editor = wx.grid.GridCellTextEditor() b@418: renderer = wx.grid.GridCellStringRenderer() b@418: else: b@418: grid.SetReadOnly(row, col, True) b@418: elif colname == "Class": b@418: if len(self.Parent.ClassList) == 1 or self.Parent.PouIsUsed and self.GetValueByName(row, "Class") in ["Input", "Output", "InOut"]: b@418: grid.SetReadOnly(row, col, True) b@418: else: b@418: editor = wx.grid.GridCellChoiceEditor() b@418: excluded = [] b@418: if self.Parent.PouIsUsed: b@418: excluded.extend(["Input","Output","InOut"]) b@418: if self.Parent.IsFunctionBlockType(self.data[row]["Type"]): b@418: excluded.extend(["Local","Temp"]) b@418: editor.SetParameters(",".join([_(choice) for choice in self.Parent.ClassList if choice not in excluded])) b@418: elif colname in ["Retain", "Constant"]: b@418: editor = wx.grid.GridCellChoiceEditor() b@418: editor.SetParameters(",".join(map(_, self.Parent.OptionList))) b@418: elif colname == "Type": b@418: editor = wx.grid.GridCellTextEditor() b@418: else: b@418: grid.SetReadOnly(row, col, True) b@418: b@418: grid.SetCellEditor(row, col, editor) b@418: grid.SetCellRenderer(row, col, renderer) b@418: b@418: if row in self.Errors and self.Errors[row][0] == colname.lower(): b@418: grid.SetCellBackgroundColour(row, col, wx.Colour(255, 255, 0)) b@418: grid.SetCellTextColour(row, col, wx.RED) b@418: grid.MakeCellVisible(row, col) b@418: else: b@418: grid.SetCellTextColour(row, col, wx.BLACK) b@418: grid.SetCellBackgroundColour(row, col, wx.WHITE) b@418: b@418: def SetData(self, data): b@418: self.data = data b@418: b@418: def GetData(self): b@418: return self.data b@418: b@418: def GetCurrentIndex(self): b@418: return self.CurrentIndex b@418: b@418: def SetCurrentIndex(self, index): b@418: self.CurrentIndex = index b@418: b@418: def AppendRow(self, row_content): b@418: self.data.append(row_content) b@418: b@418: def RemoveRow(self, row_index): b@418: self.data.pop(row_index) b@418: b@418: def GetRow(self, row_index): b@418: return self.data[row_index] b@418: b@418: def Empty(self): b@418: self.data = [] b@418: self.editors = [] b@418: b@418: def AddError(self, infos): b@418: self.Errors[infos[0]] = infos[1:] b@418: b@418: def ClearErrors(self): b@418: self.Errors = {} b@418: b@418: class VariableDropTarget(wx.TextDropTarget): b@419: ''' b@419: This allows dragging a variable location from somewhere to the Location b@419: column of a variable row. b@419: b@419: The drag source should be a TextDataObject containing a Python tuple like: b@419: ('%ID0.0.0', 'location', 'REAL') b@419: b@419: c_ext/CFileEditor.py has an example of this (you can drag a C extension b@419: variable to the Location column of the variable panel). b@419: ''' b@418: def __init__(self, parent): b@418: wx.TextDropTarget.__init__(self) b@418: self.ParentWindow = parent b@418: b@418: def OnDropText(self, x, y, data): b@418: x, y = self.ParentWindow.VariablesGrid.CalcUnscrolledPosition(x, y) b@418: col = self.ParentWindow.VariablesGrid.XToCol(x) b@418: row = self.ParentWindow.VariablesGrid.YToRow(y - self.ParentWindow.VariablesGrid.GetColLabelSize()) b@418: if col != wx.NOT_FOUND and row != wx.NOT_FOUND: b@418: if self.ParentWindow.Table.GetColLabelValue(col, False) != "Location": b@418: return b@418: message = None b@418: if not self.ParentWindow.Table.GetValueByName(row, "Edit"): b@418: message = _("Can't affect a location to a function block instance") b@418: elif self.ParentWindow.Table.GetValueByName(row, "Class") not in ["Local", "Global"]: b@418: message = _("Can affect a location only to local or global variables") b@418: else: b@418: try: b@418: values = eval(data) b@418: except: b@418: message = _("Invalid value \"%s\" for location")%data b@418: values = None b@418: if not isinstance(values, TupleType): b@418: message = _("Invalid value \"%s\" for location")%data b@418: values = None b@418: if values is not None and values[1] == "location": b@418: location = values[0] b@418: variable_type = self.ParentWindow.Table.GetValueByName(row, "Type") b@418: base_type = self.ParentWindow.Controler.GetBaseType(variable_type) b@418: message = None b@418: if location.startswith("%"): b@418: if base_type != values[2]: b@418: message = _("Incompatible data types between \"%s\" and \"%s\"")%(values[2], variable_type) b@418: else: b@418: self.ParentWindow.Table.SetValue(row, col, location) b@418: self.ParentWindow.Table.ResetView(self.ParentWindow.VariablesGrid) b@418: self.ParentWindow.SaveValues() b@418: else: b@418: if location[0].isdigit() and base_type != "BOOL": b@418: message = _("Incompatible size of data between \"%s\" and \"BOOL\"")%location b@418: elif location[0] not in LOCATIONDATATYPES: b@418: message = _("Unrecognized data size \"%s\"")%location[0] b@418: elif base_type not in LOCATIONDATATYPES[location[0]]: b@418: message = _("Incompatible size of data between \"%s\" and \"%s\"")%(location, variable_type) b@418: else: b@418: dialog = wx.SingleChoiceDialog(self.ParentWindow, _("Select a variable class:"), _("Variable class"), ["Input", "Output", "Memory"], wx.OK|wx.CANCEL) b@418: if dialog.ShowModal() == wx.ID_OK: b@418: selected = dialog.GetSelection() b@418: if selected == 0: b@418: location = "%I" + location b@418: elif selected == 1: b@418: location = "%Q" + location b@418: else: b@418: location = "%M" + location b@418: self.ParentWindow.Table.SetValue(row, col, location) b@418: self.ParentWindow.Table.ResetView(self.ParentWindow.VariablesGrid) b@418: self.ParentWindow.SaveValues() b@418: dialog.Destroy() b@418: if message is not None: b@418: wx.CallAfter(self.ShowMessage, message) b@418: b@418: def ShowMessage(self, message): b@418: message = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK|wx.ICON_ERROR) b@418: message.ShowModal() b@418: message.Destroy() b@418: b@418: [ID_VARIABLEEDITORPANEL, ID_VARIABLEEDITORPANELVARIABLESGRID, b@418: ID_VARIABLEEDITORCONTROLPANEL, ID_VARIABLEEDITORPANELRETURNTYPE, b@418: ID_VARIABLEEDITORPANELCLASSFILTER, ID_VARIABLEEDITORPANELADDBUTTON, b@418: ID_VARIABLEEDITORPANELDELETEBUTTON, ID_VARIABLEEDITORPANELUPBUTTON, b@418: ID_VARIABLEEDITORPANELDOWNBUTTON, ID_VARIABLEEDITORPANELSTATICTEXT1, b@418: ID_VARIABLEEDITORPANELSTATICTEXT2, ID_VARIABLEEDITORPANELSTATICTEXT3, b@418: ] = [wx.NewId() for _init_ctrls in range(12)] b@418: b@418: class VariablePanel(wx.Panel): b@418: b@418: if wx.VERSION < (2, 6, 0): b@418: def Bind(self, event, function, id = None): b@418: if id is not None: b@418: event(self, id, function) b@418: else: b@418: event(self, function) b@418: b@418: def _init_coll_MainSizer_Items(self, parent): b@418: parent.AddWindow(self.VariablesGrid, 0, border=0, flag=wx.GROW) b@418: parent.AddWindow(self.ControlPanel, 0, border=5, flag=wx.GROW|wx.ALL) b@418: b@418: def _init_coll_MainSizer_Growables(self, parent): b@418: parent.AddGrowableCol(0) b@418: parent.AddGrowableRow(0) b@418: b@418: def _init_coll_ControlPanelSizer_Items(self, parent): b@418: parent.AddSizer(self.ChoicePanelSizer, 0, border=0, flag=wx.GROW) b@418: parent.AddSizer(self.ButtonPanelSizer, 0, border=0, flag=wx.ALIGN_CENTER) b@418: b@418: def _init_coll_ControlPanelSizer_Growables(self, parent): b@418: parent.AddGrowableCol(0) b@418: parent.AddGrowableRow(0) b@418: parent.AddGrowableRow(1) b@418: b@418: def _init_coll_ChoicePanelSizer_Items(self, parent): b@418: parent.AddWindow(self.staticText1, 0, border=0, flag=wx.ALIGN_BOTTOM) b@418: parent.AddWindow(self.ReturnType, 0, border=0, flag=0) b@418: parent.AddWindow(self.staticText2, 0, border=0, flag=wx.ALIGN_BOTTOM) b@418: parent.AddWindow(self.ClassFilter, 0, border=0, flag=0) b@418: b@418: def _init_coll_ButtonPanelSizer_Items(self, parent): b@418: parent.AddWindow(self.UpButton, 0, border=0, flag=0) b@418: parent.AddWindow(self.AddButton, 0, border=0, flag=0) b@418: parent.AddWindow(self.DownButton, 0, border=0, flag=0) b@418: parent.AddWindow(self.DeleteButton, 0, border=0, flag=0) b@418: b@418: def _init_coll_ButtonPanelSizer_Growables(self, parent): b@418: parent.AddGrowableCol(0) b@418: parent.AddGrowableCol(1) b@418: parent.AddGrowableCol(2) b@418: parent.AddGrowableCol(3) b@418: parent.AddGrowableRow(0) b@418: b@418: def _init_sizers(self): b@418: self.MainSizer = wx.FlexGridSizer(cols=2, hgap=10, rows=1, vgap=0) b@418: self.ControlPanelSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) b@418: self.ChoicePanelSizer = wx.GridSizer(cols=1, hgap=5, rows=4, vgap=5) b@418: self.ButtonPanelSizer = wx.FlexGridSizer(cols=2, hgap=5, rows=2, vgap=5) b@418: b@418: self._init_coll_MainSizer_Items(self.MainSizer) b@418: self._init_coll_MainSizer_Growables(self.MainSizer) b@418: self._init_coll_ControlPanelSizer_Items(self.ControlPanelSizer) b@418: self._init_coll_ControlPanelSizer_Growables(self.ControlPanelSizer) b@418: self._init_coll_ChoicePanelSizer_Items(self.ChoicePanelSizer) b@418: self._init_coll_ButtonPanelSizer_Items(self.ButtonPanelSizer) b@418: self._init_coll_ButtonPanelSizer_Growables(self.ButtonPanelSizer) b@418: b@418: self.SetSizer(self.MainSizer) b@418: self.ControlPanel.SetSizer(self.ControlPanelSizer) b@418: b@418: def _init_ctrls(self, prnt): b@418: wx.Panel.__init__(self, id=ID_VARIABLEEDITORPANEL, b@418: name='VariableEditorPanel', parent=prnt, pos=wx.Point(0, 0), b@418: size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL) b@418: b@418: self.VariablesGrid = wx.grid.Grid(id=ID_VARIABLEEDITORPANELVARIABLESGRID, b@418: name='VariablesGrid', parent=self, pos=wx.Point(0, 0), b@418: size=wx.Size(0, 0), style=wx.VSCROLL) b@418: self.VariablesGrid.SetFont(wx.Font(12, 77, wx.NORMAL, wx.NORMAL, False, b@418: 'Sans')) b@418: self.VariablesGrid.SetLabelFont(wx.Font(10, 77, wx.NORMAL, wx.NORMAL, b@418: False, 'Sans')) b@418: self.VariablesGrid.SetSelectionBackground(wx.WHITE) b@418: self.VariablesGrid.SetSelectionForeground(wx.BLACK) b@418: if wx.VERSION >= (2, 6, 0): b@418: self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnVariablesGridCellChange) b@418: self.VariablesGrid.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnVariablesGridSelectCell) b@418: self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.OnVariablesGridCellLeftClick) b@418: self.VariablesGrid.Bind(wx.grid.EVT_GRID_EDITOR_SHOWN, self.OnVariablesGridEditorShown) b@418: #self.VariablesGrid.Bind(wx.EVT_KEY_DOWN, self.OnChar) b@418: else: b@418: wx.grid.EVT_GRID_CELL_CHANGE(self.VariablesGrid, self.OnVariablesGridCellChange) b@418: wx.grid.EVT_GRID_SELECT_CELL(self.VariablesGrid, self.OnVariablesGridSelectCell) b@418: wx.grid.EVT_GRID_CELL_LEFT_CLICK(self.VariablesGrid, self.OnVariablesGridCellLeftClick) b@418: wx.grid.EVT_GRID_EDITOR_SHOWN(self.VariablesGrid, self.OnVariablesGridEditorShown) b@418: #wx.EVT_KEY_DOWN(self.VariablesGrid, self.OnChar) b@418: self.VariablesGrid.SetDropTarget(VariableDropTarget(self)) b@418: b@418: self.ControlPanel = wx.ScrolledWindow(id=ID_VARIABLEEDITORCONTROLPANEL, b@418: name='ControlPanel', parent=self, pos=wx.Point(0, 0), b@418: size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL) b@418: self.ControlPanel.SetScrollRate(0, 10) b@418: b@418: self.staticText1 = wx.StaticText(id=ID_VARIABLEEDITORPANELSTATICTEXT1, b@418: label=_('Return Type:'), name='staticText1', parent=self.ControlPanel, b@418: pos=wx.Point(0, 0), size=wx.Size(145, 17), style=0) b@418: b@418: self.ReturnType = wx.ComboBox(id=ID_VARIABLEEDITORPANELRETURNTYPE, b@418: name='ReturnType', parent=self.ControlPanel, pos=wx.Point(0, 0), b@418: size=wx.Size(145, 28), style=wx.CB_READONLY) b@418: self.Bind(wx.EVT_COMBOBOX, self.OnReturnTypeChanged, id=ID_VARIABLEEDITORPANELRETURNTYPE) b@418: b@418: self.staticText2 = wx.StaticText(id=ID_VARIABLEEDITORPANELSTATICTEXT2, b@418: label=_('Class Filter:'), name='staticText2', parent=self.ControlPanel, b@418: pos=wx.Point(0, 0), size=wx.Size(145, 17), style=0) b@418: b@418: self.ClassFilter = wx.ComboBox(id=ID_VARIABLEEDITORPANELCLASSFILTER, b@418: name='ClassFilter', parent=self.ControlPanel, pos=wx.Point(0, 0), b@418: size=wx.Size(145, 28), style=wx.CB_READONLY) b@418: self.Bind(wx.EVT_COMBOBOX, self.OnClassFilter, id=ID_VARIABLEEDITORPANELCLASSFILTER) b@418: b@418: self.AddButton = wx.Button(id=ID_VARIABLEEDITORPANELADDBUTTON, label=_('Add'), b@418: name='AddButton', parent=self.ControlPanel, pos=wx.Point(0, 0), b@418: size=wx.DefaultSize, style=0) b@418: self.Bind(wx.EVT_BUTTON, self.OnAddButton, id=ID_VARIABLEEDITORPANELADDBUTTON) b@418: b@418: self.DeleteButton = wx.Button(id=ID_VARIABLEEDITORPANELDELETEBUTTON, label=_('Delete'), b@418: name='DeleteButton', parent=self.ControlPanel, pos=wx.Point(0, 0), b@418: size=wx.DefaultSize, style=0) b@418: self.Bind(wx.EVT_BUTTON, self.OnDeleteButton, id=ID_VARIABLEEDITORPANELDELETEBUTTON) b@418: b@418: self.UpButton = wx.Button(id=ID_VARIABLEEDITORPANELUPBUTTON, label='^', b@418: name='UpButton', parent=self.ControlPanel, pos=wx.Point(0, 0), b@418: size=wx.Size(32, 32), style=0) b@418: self.Bind(wx.EVT_BUTTON, self.OnUpButton, id=ID_VARIABLEEDITORPANELUPBUTTON) b@418: b@418: self.DownButton = wx.Button(id=ID_VARIABLEEDITORPANELDOWNBUTTON, label='v', b@418: name='DownButton', parent=self.ControlPanel, pos=wx.Point(0, 0), b@418: size=wx.Size(32, 32), style=0) b@418: self.Bind(wx.EVT_BUTTON, self.OnDownButton, id=ID_VARIABLEEDITORPANELDOWNBUTTON) b@418: b@418: self._init_sizers() b@418: b@418: def __init__(self, parent, window, controler, element_type): b@418: self._init_ctrls(parent) b@418: self.ParentWindow = window b@418: self.Controler = controler b@418: self.ElementType = element_type b@418: b@418: self.Filter = "All" b@418: self.FilterChoices = [] b@418: self.FilterChoiceTransfer = GetFilterChoiceTransfer() b@418: b@418: if element_type in ["config", "resource"]: b@418: self.DefaultTypes = {"All" : "Global"} b@418: self.DefaultValue = {"Name" : "", "Class" : "", "Type" : "INT", "Location" : "", "Initial Value" : "", "Retain" : "No", "Constant" : "No", "Edit" : True} b@418: else: b@418: self.DefaultTypes = {"All" : "Local", "Interface" : "Input", "Variables" : "Local"} b@418: self.DefaultValue = {"Name" : "", "Class" : "", "Type" : "INT", "Location" : "", "Initial Value" : "", "Retain" : "No", "Constant" : "No", "Edit" : True} b@418: if element_type in ["config", "resource"] or element_type in ["program", "transition", "action"]: b@418: self.Table = VariableTable(self, [], GetVariableTableColnames(True)) b@418: if element_type not in ["config", "resource"]: b@418: self.FilterChoices = ["All", "Interface", " Input", " Output", " InOut", " External", "Variables", " Local", " Temp"]#,"Access"] b@418: else: b@418: self.FilterChoices = ["All", "Global"]#,"Access"] b@418: self.ColSizes = [40, 80, 70, 80, 80, 80, 60, 70] b@418: self.ColAlignements = [wx.ALIGN_CENTER, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_CENTER, wx.ALIGN_CENTER] b@418: else: b@418: self.Table = VariableTable(self, [], GetVariableTableColnames(False)) b@418: if element_type == "function": b@418: self.FilterChoices = ["All", "Interface", " Input", " Output", " InOut", "Variables", " Local", " Temp"] b@418: else: b@418: self.FilterChoices = ["All", "Interface", " Input", " Output", " InOut", " External", "Variables", " Local", " Temp"] b@418: self.ColSizes = [40, 120, 70, 80, 120, 60, 70] b@418: self.ColAlignements = [wx.ALIGN_CENTER, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_CENTER, wx.ALIGN_CENTER] b@418: for choice in self.FilterChoices: b@418: self.ClassFilter.Append(_(choice)) b@418: reverse_transfer = {} b@418: for filter, choice in self.FilterChoiceTransfer.items(): b@418: reverse_transfer[choice] = filter b@418: self.ClassFilter.SetStringSelection(_(reverse_transfer[self.Filter])) b@418: self.RefreshTypeList() b@418: b@418: self.OptionList = GetAlternativeOptions() b@418: b@418: if element_type == "function": b@418: for base_type in self.Controler.GetBaseTypes(): b@418: self.ReturnType.Append(base_type) b@418: self.ReturnType.Enable(True) b@418: else: b@418: self.ReturnType.Enable(False) b@418: self.staticText1.Hide() b@418: self.ReturnType.Hide() b@418: b@418: self.VariablesGrid.SetTable(self.Table) b@418: self.VariablesGrid.SetRowLabelSize(0) b@418: for col in range(self.Table.GetNumberCols()): b@418: attr = wx.grid.GridCellAttr() b@418: attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE) b@418: self.VariablesGrid.SetColAttr(col, attr) b@418: self.VariablesGrid.SetColMinimalWidth(col, self.ColSizes[col]) b@418: self.VariablesGrid.AutoSizeColumn(col, False) b@418: b@418: def SetTagName(self, tagname): b@418: self.TagName = tagname b@418: b@418: def IsFunctionBlockType(self, name): b@418: bodytype = self.Controler.GetEditedElementBodyType(self.TagName, self.ParentWindow.Debug) b@418: pouname, poutype = self.Controler.GetEditedElementType(self.TagName, self.ParentWindow.Debug) b@418: if poutype != "function" and bodytype in ["ST", "IL"]: b@418: return False b@418: else: b@418: return name in self.Controler.GetFunctionBlockTypes(self.TagName, self.ParentWindow.Debug) b@418: b@418: def RefreshView(self): b@418: self.PouNames = self.Controler.GetProjectPouNames(self.ParentWindow.Debug) b@418: b@418: words = self.TagName.split("::") b@418: if self.ElementType == "config": b@418: self.PouIsUsed = False b@418: returnType = None b@418: self.Values = self.Controler.GetConfigurationGlobalVars(words[1], self.ParentWindow.Debug) b@418: elif self.ElementType == "resource": b@418: self.PouIsUsed = False b@418: returnType = None b@418: self.Values = self.Controler.GetConfigurationResourceGlobalVars(words[1], words[2], self.ParentWindow.Debug) b@418: else: b@418: self.PouIsUsed = self.Controler.PouIsUsed(words[1], self.ParentWindow.Debug) b@418: returnType = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.ParentWindow.Debug) b@418: self.Values = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.ParentWindow.Debug) b@418: b@418: if returnType and self.ReturnType.IsEnabled(): b@418: self.ReturnType.SetStringSelection(returnType) b@418: b@418: self.RefreshValues() b@418: self.RefreshButtons() b@418: b@418: def OnReturnTypeChanged(self, event): b@418: words = self.TagName.split("::") b@418: self.Controler.SetPouInterfaceReturnType(words[1], self.ReturnType.GetStringSelection()) b@418: self.Controler.BufferProject() b@418: self.ParentWindow.RefreshEditor(variablepanel = False) b@418: self.ParentWindow.RefreshTitle() b@418: self.ParentWindow.RefreshEditMenu() b@418: self.ParentWindow.RefreshInstancesTree() b@418: self.ParentWindow.RefreshLibraryTree() b@418: event.Skip() b@418: b@418: def OnClassFilter(self, event): b@418: self.Filter = self.FilterChoiceTransfer[self.ClassFilter.GetStringSelection()] b@418: self.RefreshTypeList() b@418: self.RefreshValues() b@418: self.RefreshButtons() b@418: event.Skip() b@418: b@418: def RefreshTypeList(self): b@418: if self.Filter == "All": b@418: self.ClassList = [self.FilterChoiceTransfer[choice] for choice in self.FilterChoices if self.FilterChoiceTransfer[choice] not in ["All","Interface","Variables"]] b@418: elif self.Filter == "Interface": b@418: self.ClassList = ["Input","Output","InOut","External"] b@418: elif self.Filter == "Variables": b@418: self.ClassList = ["Local","Temp"] b@418: else: b@418: self.ClassList = [self.Filter] b@418: b@418: def RefreshButtons(self): b@418: if getattr(self, "Table", None): b@418: table_length = len(self.Table.data) b@418: row_class = None b@418: row_edit = True b@418: if table_length > 0: b@418: row = self.VariablesGrid.GetGridCursorRow() b@418: row_edit = self.Table.GetValueByName(row, "Edit") b@418: if self.PouIsUsed: b@418: row_class = self.Table.GetValueByName(row, "Class") b@418: self.AddButton.Enable(not self.PouIsUsed or self.Filter not in ["Interface", "Input", "Output", "InOut"]) b@418: self.DeleteButton.Enable(table_length > 0 and row_edit and row_class not in ["Input", "Output", "InOut"]) b@418: self.UpButton.Enable(table_length > 0 and self.Filter == "All" and row_class not in ["Input", "Output", "InOut"]) b@418: self.DownButton.Enable(table_length > 0 and self.Filter == "All" and row_class not in ["Input", "Output", "InOut"]) b@418: b@418: def OnAddButton(self, event): b@418: new_row = self.DefaultValue.copy() b@418: if self.Filter in self.DefaultTypes: b@418: new_row["Class"] = self.DefaultTypes[self.Filter] b@418: else: b@418: new_row["Class"] = self.Filter b@418: if self.Filter == "All" and len(self.Values) > 0: b@418: row_index = self.VariablesGrid.GetGridCursorRow() + 1 b@418: self.Values.insert(row_index, new_row) b@418: else: b@418: row_index = -1 b@418: self.Values.append(new_row) b@418: self.SaveValues() b@418: self.RefreshValues(row_index) b@418: self.RefreshButtons() b@418: event.Skip() b@418: b@418: def OnDeleteButton(self, event): b@418: row = self.Table.GetRow(self.VariablesGrid.GetGridCursorRow()) b@418: self.Values.remove(row) b@418: self.SaveValues() b@418: self.RefreshValues() b@418: self.RefreshButtons() b@418: event.Skip() b@418: b@418: def OnUpButton(self, event): b@418: self.MoveValue(self.VariablesGrid.GetGridCursorRow(), -1) b@418: self.RefreshButtons() b@418: event.Skip() b@418: b@418: def OnDownButton(self, event): b@418: self.MoveValue(self.VariablesGrid.GetGridCursorRow(), 1) b@418: self.RefreshButtons() b@418: event.Skip() b@418: b@418: def OnVariablesGridCellChange(self, event): b@418: row, col = event.GetRow(), event.GetCol() b@418: colname = self.Table.GetColLabelValue(col) b@418: value = self.Table.GetValue(row, col) b@418: if colname == "Name" and value != "": b@418: if not TestIdentifier(value): b@418: message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%value, _("Error"), wx.OK|wx.ICON_ERROR) b@418: message.ShowModal() b@418: message.Destroy() b@418: event.Veto() b@418: elif value.upper() in IEC_KEYWORDS: b@418: message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%value, _("Error"), wx.OK|wx.ICON_ERROR) b@418: message.ShowModal() b@418: message.Destroy() b@418: event.Veto() b@418: elif value.upper() in self.PouNames: b@418: message = wx.MessageDialog(self, _("A pou with \"%s\" as name exists!")%value, _("Error"), wx.OK|wx.ICON_ERROR) b@418: message.ShowModal() b@418: message.Destroy() b@418: event.Veto() b@418: elif value.upper() in [var["Name"].upper() for var in self.Values if var != self.Table.data[row]]: b@418: message = wx.MessageDialog(self, _("A variable with \"%s\" as name already exists in this pou!")%value, _("Error"), wx.OK|wx.ICON_ERROR) b@418: message.ShowModal() b@418: message.Destroy() b@418: event.Veto() b@418: else: b@418: self.SaveValues(False) b@418: old_value = self.Table.GetOldValue() b@418: if old_value != "": b@418: self.Controler.UpdateEditedElementUsedVariable(self.TagName, old_value, value) b@418: self.Controler.BufferProject() b@418: self.ParentWindow.RefreshEditor(variablepanel = False) b@418: self.ParentWindow.RefreshTitle() b@418: self.ParentWindow.RefreshEditMenu() b@418: self.ParentWindow.RefreshInstancesTree() b@418: self.ParentWindow.RefreshLibraryTree() b@418: event.Skip() b@418: else: b@418: self.SaveValues() b@418: if colname == "Class": b@418: self.Table.ResetView(self.VariablesGrid) b@418: event.Skip() b@418: b@418: def OnVariablesGridEditorShown(self, event): b@418: row, col = event.GetRow(), event.GetCol() b@418: classtype = self.Table.GetValueByName(row, "Class") b@418: if self.Table.GetColLabelValue(col) == "Type": b@418: type_menu = wx.Menu(title='') b@418: base_menu = wx.Menu(title='') b@418: for base_type in self.Controler.GetBaseTypes(): b@418: new_id = wx.NewId() b@418: AppendMenu(base_menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=base_type) b@418: self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(base_type), id=new_id) b@418: type_menu.AppendMenu(wx.NewId(), _("Base Types"), base_menu) b@418: datatype_menu = wx.Menu(title='') b@418: for datatype in self.Controler.GetDataTypes(basetypes = False, debug = self.ParentWindow.Debug): b@418: new_id = wx.NewId() b@418: AppendMenu(datatype_menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=datatype) b@418: self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(datatype), id=new_id) b@418: type_menu.AppendMenu(wx.NewId(), _("User Data Types"), datatype_menu) b@418: functionblock_menu = wx.Menu(title='') b@418: bodytype = self.Controler.GetEditedElementBodyType(self.TagName, self.ParentWindow.Debug) b@418: pouname, poutype = self.Controler.GetEditedElementType(self.TagName, self.ParentWindow.Debug) b@418: if classtype in ["Input","Output","InOut","External","Global"] or poutype != "function" and bodytype in ["ST", "IL"]: b@418: for functionblock_type in self.Controler.GetFunctionBlockTypes(self.TagName, self.ParentWindow.Debug): b@418: new_id = wx.NewId() b@418: AppendMenu(functionblock_menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=functionblock_type) b@418: self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(functionblock_type), id=new_id) b@418: type_menu.AppendMenu(wx.NewId(), _("Function Block Types"), functionblock_menu) b@418: rect = self.VariablesGrid.BlockToDeviceRect((row, col), (row, col)) b@418: self.VariablesGrid.PopupMenuXY(type_menu, rect.x + rect.width, rect.y + self.VariablesGrid.GetColLabelSize()) b@418: event.Veto() b@418: else: b@418: event.Skip() b@418: b@418: def GetVariableTypeFunction(self, base_type): b@418: def VariableTypeFunction(event): b@418: row = self.VariablesGrid.GetGridCursorRow() b@418: self.Table.SetValueByName(row, "Type", base_type) b@418: self.Table.ResetView(self.VariablesGrid) b@418: self.SaveValues(False) b@418: self.ParentWindow.RefreshEditor(variablepanel = False) b@418: self.Controler.BufferProject() b@418: self.ParentWindow.RefreshTitle() b@418: self.ParentWindow.RefreshEditMenu() b@418: self.ParentWindow.RefreshInstancesTree() b@418: self.ParentWindow.RefreshLibraryTree() b@418: event.Skip() b@418: return VariableTypeFunction b@418: b@418: def OnVariablesGridCellLeftClick(self, event): b@418: row = event.GetRow() b@418: if event.GetCol() == 0 and self.Table.GetValueByName(row, "Edit"): b@418: row = event.GetRow() b@418: var_name = self.Table.GetValueByName(row, "Name") b@418: var_class = self.Table.GetValueByName(row, "Class") b@418: var_type = self.Table.GetValueByName(row, "Type") b@418: data = wx.TextDataObject(str((var_name, var_class, var_type, self.TagName))) b@418: dragSource = wx.DropSource(self.VariablesGrid) b@418: dragSource.SetData(data) b@418: dragSource.DoDragDrop() b@418: event.Skip() b@418: b@418: def OnVariablesGridSelectCell(self, event): b@418: wx.CallAfter(self.RefreshButtons) b@418: event.Skip() b@418: b@418: def OnChar(self, event): b@418: keycode = event.GetKeyCode() b@418: if keycode == wx.WXK_DELETE: b@418: row = self.Table.GetRow(self.VariablesGrid.GetGridCursorRow()) b@418: self.Values.remove(row) b@418: self.SaveValues() b@418: self.RefreshValues() b@418: self.RefreshButtons() b@418: event.Skip() b@418: b@418: def MoveValue(self, value_index, move): b@418: new_index = max(0, min(value_index + move, len(self.Values) - 1)) b@418: if new_index != value_index: b@418: self.Values.insert(new_index, self.Values.pop(value_index)) b@418: self.SaveValues() b@418: self.RefreshValues() b@418: self.VariablesGrid.SetGridCursor(new_index, self.VariablesGrid.GetGridCursorCol()) b@418: b@418: def RefreshValues(self, select=0): b@418: if len(self.Table.data) > 0: b@418: self.VariablesGrid.SetGridCursor(0, 1) b@418: data = [] b@418: for num, variable in enumerate(self.Values): b@418: if variable["Class"] in self.ClassList: b@418: variable["Number"] = num + 1 b@418: data.append(variable) b@418: self.Table.SetData(data) b@418: if len(self.Table.data) > 0: b@418: if select == -1: b@418: select = len(self.Table.data) - 1 b@418: self.VariablesGrid.SetGridCursor(select, 1) b@418: self.VariablesGrid.MakeCellVisible(select, 1) b@418: self.Table.ResetView(self.VariablesGrid) b@418: b@418: def SaveValues(self, buffer = True): b@418: words = self.TagName.split("::") b@418: if self.ElementType == "config": b@418: self.Controler.SetConfigurationGlobalVars(words[1], self.Values) b@418: elif self.ElementType == "resource": b@418: self.Controler.SetConfigurationResourceGlobalVars(words[1], words[2], self.Values) b@418: else: b@418: if self.ReturnType.IsEnabled(): b@418: self.Controler.SetPouInterfaceReturnType(words[1], self.ReturnType.GetStringSelection()) b@418: self.Controler.SetPouInterfaceVars(words[1], self.Values) b@418: if buffer: b@418: self.Controler.BufferProject() b@418: self.ParentWindow.RefreshTitle() b@418: self.ParentWindow.RefreshEditMenu() b@418: self.ParentWindow.RefreshInstancesTree() b@418: self.ParentWindow.RefreshLibraryTree() b@418: b@418: def AddVariableError(self, infos): b@418: if isinstance(infos[0], TupleType): b@418: for i in xrange(*infos[0]): b@418: self.Table.AddError((i,) + infos[1:]) b@418: else: b@418: self.Table.AddError(infos) b@418: self.Table.ResetView(self.VariablesGrid) b@418: b@418: def ClearErrors(self): b@418: self.Table.ClearErrors() b@418: self.Table.ResetView(self.VariablesGrid)