Laurent@814: #!/usr/bin/env python Laurent@814: # -*- coding: utf-8 -*- Laurent@814: Laurent@814: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor Laurent@814: #based on the plcopen standard. Laurent@814: # Laurent@814: #Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD Laurent@814: # Laurent@814: #See COPYING file for copyrights details. Laurent@814: # Laurent@814: #This library is free software; you can redistribute it and/or Laurent@814: #modify it under the terms of the GNU General Public Laurent@814: #License as published by the Free Software Foundation; either Laurent@814: #version 2.1 of the License, or (at your option) any later version. Laurent@814: # Laurent@814: #This library is distributed in the hope that it will be useful, Laurent@814: #but WITHOUT ANY WARRANTY; without even the implied warranty of Laurent@814: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Laurent@814: #General Public License for more details. Laurent@814: # Laurent@814: #You should have received a copy of the GNU General Public Laurent@814: #License along with this library; if not, write to the Free Software Laurent@814: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Laurent@814: Laurent@1198: from types import TupleType Laurent@814: Laurent@814: import wx Laurent@814: import wx.lib.buttons Laurent@888: Laurent@1198: from editors.DebugViewer import DebugViewer Laurent@814: from controls import CustomGrid, CustomTable Laurent@814: from dialogs.ForceVariableDialog import ForceVariableDialog Laurent@814: from util.BitmapLibrary import GetBitmap Laurent@814: Laurent@1199: from DebugVariableItem import DebugVariableItem Laurent@1199: Laurent@814: def GetDebugVariablesTableColnames(): Laurent@1207: """ Laurent@1207: Function returning table column header labels Laurent@1207: @return: List of labels [col_label,...] Laurent@1207: """ Laurent@814: _ = lambda x : x Laurent@909: return [_("Variable"), _("Value")] Laurent@924: Laurent@1207: #------------------------------------------------------------------------------- Laurent@1207: # Debug Variable Table Panel Laurent@1207: #------------------------------------------------------------------------------- Laurent@1207: Laurent@1207: """ Laurent@1207: Class that implements a custom table storing value to display in Debug Variable Laurent@1207: Table Panel grid Laurent@1207: """ Laurent@1207: Laurent@814: class DebugVariableTable(CustomTable): Laurent@814: Laurent@814: def GetValue(self, row, col): Laurent@814: if row < self.GetNumberRows(): Laurent@814: return self.GetValueByName(row, self.GetColLabelValue(col, False)) Laurent@814: return "" Laurent@814: Laurent@814: def SetValue(self, row, col, value): Laurent@814: if col < len(self.colnames): Laurent@814: self.SetValueByName(row, self.GetColLabelValue(col, False), value) Laurent@814: Laurent@814: def GetValueByName(self, row, colname): Laurent@814: if row < self.GetNumberRows(): Laurent@814: if colname == "Variable": Laurent@814: return self.data[row].GetVariable() Laurent@814: elif colname == "Value": Laurent@814: return self.data[row].GetValue() Laurent@814: return "" Laurent@814: Laurent@814: def SetValueByName(self, row, colname, value): Laurent@814: if row < self.GetNumberRows(): Laurent@814: if colname == "Variable": Laurent@814: self.data[row].SetVariable(value) Laurent@814: elif colname == "Value": Laurent@814: self.data[row].SetValue(value) Laurent@814: Laurent@814: def IsForced(self, row): Laurent@814: if row < self.GetNumberRows(): Laurent@814: return self.data[row].IsForced() Laurent@814: return False Laurent@814: Laurent@814: def _updateColAttrs(self, grid): Laurent@814: """ Laurent@814: wx.grid.Grid -> update the column attributes to add the Laurent@814: appropriate renderer given the column name. Laurent@814: Laurent@814: Otherwise default to the default renderer. Laurent@814: """ Laurent@814: Laurent@814: for row in range(self.GetNumberRows()): Laurent@814: for col in range(self.GetNumberCols()): Laurent@887: colname = self.GetColLabelValue(col, False) Laurent@909: if colname == "Value": Laurent@909: if self.IsForced(row): Laurent@909: grid.SetCellTextColour(row, col, wx.BLUE) Laurent@814: else: Laurent@909: grid.SetCellTextColour(row, col, wx.BLACK) Laurent@909: grid.SetReadOnly(row, col, True) Laurent@814: self.ResizeRow(grid, row) Laurent@1207: Laurent@1207: def RefreshValues(self, grid): Laurent@1207: for col in xrange(self.GetNumberCols()): Laurent@1207: colname = self.GetColLabelValue(col, False) Laurent@1207: if colname == "Value": Laurent@1207: for row in xrange(self.GetNumberRows()): Laurent@1207: grid.SetCellValue(row, col, str(self.data[row].GetValue())) Laurent@1207: if self.IsForced(row): Laurent@1207: grid.SetCellTextColour(row, col, wx.BLUE) Laurent@1207: else: Laurent@1207: grid.SetCellTextColour(row, col, wx.BLACK) Laurent@1207: Laurent@1207: def AppendItem(self, item): Laurent@1207: self.data.append(item) Laurent@1207: Laurent@1207: def InsertItem(self, idx, item): Laurent@1207: self.data.insert(idx, item) Laurent@1207: Laurent@1207: def RemoveItem(self, item): Laurent@1207: self.data.remove(item) Laurent@814: Laurent@814: def MoveItem(self, idx, new_idx): Laurent@814: self.data.insert(new_idx, self.data.pop(idx)) Laurent@814: Laurent@814: def GetItem(self, idx): Laurent@814: return self.data[idx] Laurent@814: Laurent@1207: Laurent@1207: #------------------------------------------------------------------------------- Laurent@1207: # Debug Variable Table Panel Drop Target Laurent@1207: #------------------------------------------------------------------------------- Laurent@1207: Laurent@1207: """ Laurent@1207: Class that implements a custom drop target class for Debug Variable Table Panel Laurent@1207: """ Laurent@1207: Laurent@1198: class DebugVariableTableDropTarget(wx.TextDropTarget): Laurent@1198: Laurent@1198: def __init__(self, parent): Laurent@1207: """ Laurent@1207: Constructor Laurent@1207: @param window: Reference to the Debug Variable Panel Laurent@1207: """ Laurent@814: wx.TextDropTarget.__init__(self) Laurent@814: self.ParentWindow = parent Laurent@1198: Laurent@916: def __del__(self): Laurent@1207: """ Laurent@1207: Destructor Laurent@1207: """ Laurent@1207: # Remove reference to Debug Variable Panel Laurent@916: self.ParentWindow = None Laurent@928: Laurent@814: def OnDropText(self, x, y, data): Laurent@1207: """ Laurent@1207: Function called when mouse is dragged over Drop Target Laurent@1207: @param x: X coordinate of mouse pointer Laurent@1207: @param y: Y coordinate of mouse pointer Laurent@1207: @param data: Text associated to drag'n drop Laurent@1207: """ Laurent@814: message = None Laurent@1209: Laurent@1209: # Check that data is valid regarding DebugVariablePanel Laurent@814: try: Laurent@814: values = eval(data) Laurent@1207: if not isinstance(values, TupleType): Laurent@1207: raise ValueError Laurent@814: except: Laurent@1207: message = _("Invalid value \"%s\" for debug variable") % data Laurent@814: values = None Laurent@909: Laurent@1209: # Display message if data is invalid Laurent@814: if message is not None: Laurent@814: wx.CallAfter(self.ShowMessage, message) Laurent@1207: Laurent@1209: # Data contain a reference to a variable to debug Laurent@1207: elif values[1] == "debug": Laurent@1198: grid = self.ParentWindow.VariablesGrid Laurent@1207: Laurent@1207: # Get row where variable was dropped Laurent@1198: x, y = grid.CalcUnscrolledPosition(x, y) Laurent@1198: row = grid.YToRow(y - grid.GetColLabelSize()) Laurent@1207: Laurent@1207: # If no row found add variable at table end Laurent@1198: if row == wx.NOT_FOUND: Laurent@1198: row = self.ParentWindow.Table.GetNumberRows() Laurent@1207: Laurent@1207: # Add variable to table Laurent@1198: self.ParentWindow.InsertValue(values[0], row, force=True) Laurent@1198: Laurent@814: def ShowMessage(self, message): Laurent@1207: """ Laurent@1207: Show error message in Error Dialog Laurent@1207: @param message: Error message to display Laurent@1207: """ Laurent@1207: dialog = wx.MessageDialog(self.ParentWindow, Laurent@1207: message, Laurent@1207: _("Error"), Laurent@1207: wx.OK|wx.ICON_ERROR) Laurent@814: dialog.ShowModal() Laurent@814: dialog.Destroy() Laurent@1207: Laurent@1207: Laurent@1207: #------------------------------------------------------------------------------- Laurent@1207: # Debug Variable Table Panel Laurent@1207: #------------------------------------------------------------------------------- Laurent@1207: Laurent@1207: """ Laurent@1207: Class that implements a panel displaying debug variable values in a table Laurent@1207: """ Laurent@1207: Laurent@1198: class DebugVariableTablePanel(wx.Panel, DebugViewer): Laurent@814: Laurent@902: def __init__(self, parent, producer, window): Laurent@1207: """ Laurent@1207: Constructor Laurent@1207: @param parent: Reference to the parent wx.Window Laurent@1207: @param producer: Object receiving debug value and dispatching them to Laurent@1207: consumers Laurent@1207: @param window: Reference to Beremiz frame Laurent@1207: """ Laurent@916: wx.Panel.__init__(self, parent, style=wx.SP_3D|wx.TAB_TRAVERSAL) Laurent@902: Laurent@1207: # Save Reference to Beremiz frame Laurent@902: self.ParentWindow = window Laurent@902: Laurent@1207: # Variable storing flag indicating that variable displayed in table Laurent@1207: # received new value and then table need to be refreshed Laurent@814: self.HasNewData = False Laurent@1198: Laurent@1198: DebugViewer.__init__(self, producer, True) Laurent@1198: Laurent@1207: # Construction of window layout by creating controls and sizers Laurent@1207: Laurent@1198: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) Laurent@1198: main_sizer.AddGrowableCol(0) Laurent@1198: main_sizer.AddGrowableRow(1) Laurent@1198: Laurent@1198: button_sizer = wx.BoxSizer(wx.HORIZONTAL) Laurent@1198: main_sizer.AddSizer(button_sizer, border=5, Laurent@1198: flag=wx.ALIGN_RIGHT|wx.ALL) Laurent@1198: Laurent@1207: # Creation of buttons for navigating in table Laurent@1207: Laurent@1198: for name, bitmap, help in [ Laurent@1198: ("DeleteButton", "remove_element", _("Remove debug variable")), Laurent@1198: ("UpButton", "up", _("Move debug variable up")), Laurent@1198: ("DownButton", "down", _("Move debug variable down"))]: Laurent@1207: button = wx.lib.buttons.GenBitmapButton(self, Laurent@1207: bitmap=GetBitmap(bitmap), Laurent@1198: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@1198: button.SetToolTipString(help) Laurent@1198: setattr(self, name, button) Laurent@1198: button_sizer.AddWindow(button, border=5, flag=wx.LEFT) Laurent@1198: Laurent@1207: # Creation of grid and associated table Laurent@1207: Laurent@1207: self.VariablesGrid = CustomGrid(self, Laurent@1207: size=wx.Size(-1, 150), style=wx.VSCROLL) Laurent@1207: # Define grid drop target Laurent@1198: self.VariablesGrid.SetDropTarget(DebugVariableTableDropTarget(self)) Laurent@1198: self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK, Laurent@1198: self.OnVariablesGridCellRightClick) Laurent@1198: self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, Laurent@1198: self.OnVariablesGridCellLeftClick) Laurent@1198: main_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW) Laurent@1198: Laurent@1207: self.Table = DebugVariableTable(self, [], Laurent@1207: GetDebugVariablesTableColnames()) Laurent@1198: self.VariablesGrid.SetTable(self.Table) Laurent@1198: self.VariablesGrid.SetButtons({"Delete": self.DeleteButton, Laurent@1198: "Up": self.UpButton, Laurent@1198: "Down": self.DownButton}) Laurent@1207: Laurent@1207: # Definition of function associated to navigation buttons Laurent@1207: Laurent@1198: def _AddVariable(new_row): Laurent@1198: return self.VariablesGrid.GetGridCursorRow() Laurent@1198: setattr(self.VariablesGrid, "_AddRow", _AddVariable) Laurent@1198: Laurent@1198: def _DeleteVariable(row): Laurent@1198: item = self.Table.GetItem(row) Laurent@1198: self.RemoveDataConsumer(item) Laurent@1207: self.Table.RemoveItem(item) Laurent@1198: self.RefreshView() Laurent@1198: setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable) Laurent@1198: Laurent@1198: def _MoveVariable(row, move): Laurent@1198: new_row = max(0, min(row + move, self.Table.GetNumberRows() - 1)) Laurent@1198: if new_row != row: Laurent@1198: self.Table.MoveItem(row, new_row) Laurent@916: self.RefreshView() Laurent@1198: return new_row Laurent@1198: setattr(self.VariablesGrid, "_MoveRow", _MoveVariable) Laurent@1198: Laurent@1207: # Initialization of grid layout Laurent@1207: Laurent@1198: self.VariablesGrid.SetRowLabelSize(0) Laurent@1198: Laurent@1198: self.GridColSizes = [200, 100] Laurent@1198: Laurent@1198: for col in range(self.Table.GetNumberCols()): Laurent@1198: attr = wx.grid.GridCellAttr() Laurent@1198: attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTER) Laurent@1198: self.VariablesGrid.SetColAttr(col, attr) Laurent@1198: self.VariablesGrid.SetColSize(col, self.GridColSizes[col]) Laurent@1198: Laurent@1198: self.Table.ResetView(self.VariablesGrid) Laurent@1198: self.VariablesGrid.RefreshButtons() Laurent@1192: Laurent@916: self.SetSizer(main_sizer) Laurent@1192: Laurent@902: def RefreshNewData(self, *args, **kwargs): Laurent@1207: """ Laurent@1207: Called to refresh Table according to values received by variables Laurent@1207: Can receive any parameters (not used here) Laurent@1207: """ Laurent@1207: # Refresh 'Value' column of table if new data have been received since Laurent@1207: # last refresh Laurent@1198: if self.HasNewData: Laurent@814: self.HasNewData = False Laurent@916: self.RefreshView(only_values=True) Laurent@902: DebugViewer.RefreshNewData(self, *args, **kwargs) Laurent@902: Laurent@1198: def RefreshView(self, only_values=False): Laurent@1207: """ Laurent@1207: Function refreshing table layout and values Laurent@1207: @param only_values: True if only 'Value' column need to be updated Laurent@1207: """ Laurent@1207: # Block refresh until table layout and values are completely updated Laurent@1198: self.Freeze() Laurent@1198: Laurent@1207: # Update only 'value' column from table Laurent@1198: if only_values: Laurent@1207: self.Table.RefreshValues(self.VariablesGrid) Laurent@1207: Laurent@1207: # Update complete table layout refreshing table navigation buttons Laurent@1207: # state according to Laurent@929: else: Laurent@1198: self.Table.ResetView(self.VariablesGrid) Laurent@1207: self.VariablesGrid.RefreshButtons() Laurent@1207: Laurent@1207: self.Thaw() Laurent@1207: Laurent@1207: def ResetView(self): Laurent@1207: """ Laurent@1207: Function removing all variables denugged from table Laurent@1207: @param only_values: True if only 'Value' column need to be updated Laurent@1207: """ Laurent@1207: # Unsubscribe all variables debugged Laurent@1207: self.UnsubscribeAllDataConsumers() Laurent@1207: Laurent@1207: # Clear table content Laurent@1207: self.Table.Empty() Laurent@1207: Laurent@1207: # Update table layout Laurent@1207: self.Freeze() Laurent@1207: self.Table.ResetView(self.VariablesGrid) Laurent@1198: self.VariablesGrid.RefreshButtons() Laurent@1198: self.Thaw() Laurent@1207: Laurent@1207: def SubscribeAllDataConsumers(self): Laurent@1207: """ Laurent@1207: Function refreshing table layout and values Laurent@1207: @param only_values: True if only 'Value' column need to be updated Laurent@1207: """ Laurent@1207: DebugViewer.SubscribeAllDataConsumers(self) Laurent@1207: Laurent@1207: # Navigate through variable displayed in table, removing those that Laurent@1207: # doesn't exist anymore in PLC Laurent@1207: for item in self.Table.GetData()[:]: Laurent@1198: iec_path = item.GetVariable() Laurent@1198: if self.GetDataType(iec_path) is None: Laurent@1198: self.RemoveDataConsumer(item) Laurent@1198: self.Table.RemoveItem(idx) Laurent@1198: else: Laurent@1198: self.AddDataConsumer(iec_path.upper(), item) Laurent@1198: item.RefreshVariableType() Laurent@1207: Laurent@1207: # Update table layout Laurent@1198: self.Freeze() Laurent@1198: self.Table.ResetView(self.VariablesGrid) Laurent@1198: self.VariablesGrid.RefreshButtons() Laurent@1198: self.Thaw() Laurent@916: Laurent@1207: def GetForceVariableMenuFunction(self, item): Laurent@1207: """ Laurent@1207: Function returning callback function for contextual menu 'Force' item Laurent@1207: @param item: Debug Variable item where contextual menu was opened Laurent@1207: @return: Callback function Laurent@1207: """ Laurent@814: def ForceVariableFunction(event): Laurent@1207: # Get variable path and data type Laurent@1207: iec_path = item.GetVariable() Laurent@1207: iec_type = self.GetDataType(iec_path) Laurent@1207: Laurent@1207: # Return immediately if not data type found Laurent@1207: if iec_type is None: Laurent@1207: return Laurent@1207: Laurent@1207: # Open dialog for entering value to force variable Laurent@1207: dialog = ForceVariableDialog(self, iec_type, str(item.GetValue())) Laurent@1207: Laurent@1207: # If valid value entered, force variable Laurent@1207: if dialog.ShowModal() == wx.ID_OK: Laurent@1207: self.ForceDataValue(iec_path.upper(), dialog.GetValue()) Laurent@1207: Laurent@814: return ForceVariableFunction Laurent@814: Laurent@814: def GetReleaseVariableMenuFunction(self, iec_path): Laurent@1207: """ Laurent@1207: Function returning callback function for contextual menu 'Release' item Laurent@1207: @param iec_path: Debug Variable path where contextual menu was opened Laurent@1207: @return: Callback function Laurent@1207: """ Laurent@814: def ReleaseVariableFunction(event): Laurent@1207: # Release variable Laurent@814: self.ReleaseDataValue(iec_path) Laurent@814: return ReleaseVariableFunction Laurent@814: Laurent@892: def OnVariablesGridCellLeftClick(self, event): Laurent@1207: """ Laurent@1207: Called when left mouse button is pressed on a table cell Laurent@1207: @param event: wx.grid.GridEvent Laurent@1207: """ Laurent@1207: # Initiate a drag and drop if the cell clicked was in 'Variable' column Laurent@1207: if self.Table.GetColLabelValue(event.GetCol(), False) == "Variable": Laurent@1207: item = self.Table.GetItem(event.GetRow()) Laurent@1207: data = wx.TextDataObject(str((item.GetVariable(), "debug"))) Laurent@892: dragSource = wx.DropSource(self.VariablesGrid) Laurent@892: dragSource.SetData(data) Laurent@892: dragSource.DoDragDrop() Laurent@1207: Laurent@892: event.Skip() Laurent@892: Laurent@814: def OnVariablesGridCellRightClick(self, event): Laurent@1207: """ Laurent@1207: Called when right mouse button is pressed on a table cell Laurent@1207: @param event: wx.grid.GridEvent Laurent@1207: """ Laurent@1207: # Open a contextual menu if the cell clicked was in 'Value' column Laurent@1207: if self.Table.GetColLabelValue(event.GetCol(), False) == "Value": Laurent@1207: row = event.GetRow() Laurent@1207: Laurent@1207: # Get variable path Laurent@1207: item = self.Table.GetItem(row) Laurent@1207: iec_path = item.GetVariable().upper() Laurent@1207: Laurent@1207: # Create contextual menu Laurent@814: menu = wx.Menu(title='') Laurent@814: Laurent@1207: # Add menu items Laurent@1207: for text, enable, callback in [ Laurent@1207: (_("Force value"), True, Laurent@1207: self.GetForceVariableMenuFunction(item)), Laurent@1207: # Release menu item is enabled only if variable is forced Laurent@1207: (_("Release value"), self.Table.IsForced(row), Laurent@1207: self.GetReleaseVariableMenuFunction(iec_path))]: Laurent@1207: Laurent@1207: new_id = wx.NewId() Laurent@1207: menu.Append(help='', id=new_id, kind=wx.ITEM_NORMAL, text=text) Laurent@1207: menu.Enable(new_id, enable) Laurent@1207: self.Bind(wx.EVT_MENU, callback, id=new_id) Laurent@1207: Laurent@1207: # Popup contextual menu Laurent@814: self.PopupMenu(menu) Laurent@814: Laurent@814: menu.Destroy() Laurent@814: event.Skip() Laurent@814: Laurent@1214: def InsertValue(self, iec_path, index=None, force=False, graph=False): Laurent@1207: """ Laurent@1207: Insert a new variable to debug in table Laurent@1207: @param iec_path: Variable path to debug Laurent@1207: @param index: Row where insert the variable in table (default None, Laurent@1207: insert at last position) Laurent@1207: @param force: Force insertion of variable even if not defined in Laurent@1207: producer side Laurent@1214: @param graph: Values must be displayed in graph canvas (Do nothing, Laurent@1214: here for compatibility with Debug Variable Graphic Panel) Laurent@1207: """ Laurent@1207: # Return immediately if variable is already debugged Laurent@1198: for item in self.Table.GetData(): Laurent@1198: if iec_path == item.GetVariable(): Laurent@1198: return Laurent@1207: Laurent@1207: # Insert at last position if index not defined Laurent@1207: if index is None: Laurent@1207: index = self.Table.GetNumberRows() Laurent@1207: Laurent@1207: # Subscribe variable to producer Laurent@1198: item = DebugVariableItem(self, iec_path) Laurent@814: result = self.AddDataConsumer(iec_path.upper(), item) Laurent@1207: Laurent@1207: # Insert variable in table if subscription done or insertion forced Laurent@814: if result is not None or force: Laurent@1207: self.Table.InsertItem(index, item) Laurent@1198: self.RefreshView() Laurent@916: Laurent@887: def ResetGraphicsValues(self): Laurent@1207: """ Laurent@1207: Called to reset graphics values when PLC is started Laurent@1207: (Nothing to do because no graphic values here. Defined for Laurent@1207: compatibility with Debug Variable Graphic Panel) Laurent@1207: """ Laurent@988: pass Laurent@1198: