Laurent@711: #!/usr/bin/env python Laurent@711: # -*- coding: utf-8 -*- Laurent@711: Laurent@711: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor Laurent@711: #based on the plcopen standard. Laurent@711: # Laurent@711: #Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD Laurent@711: # Laurent@711: #See COPYING file for copyrights details. Laurent@711: # Laurent@711: #This library is free software; you can redistribute it and/or Laurent@711: #modify it under the terms of the GNU General Public Laurent@711: #License as published by the Free Software Foundation; either Laurent@711: #version 2.1 of the License, or (at your option) any later version. Laurent@711: # Laurent@711: #This library is distributed in the hope that it will be useful, Laurent@711: #but WITHOUT ANY WARRANTY; without even the implied warranty of Laurent@711: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Laurent@711: #General Public License for more details. Laurent@711: # Laurent@711: #You should have received a copy of the GNU General Public Laurent@711: #License along with this library; if not, write to the Free Software Laurent@711: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Laurent@711: Laurent@715: from types import TupleType Laurent@715: Laurent@711: import wx Laurent@714: import wx.lib.buttons Laurent@711: Laurent@711: from graphics import DebugDataConsumer, DebugViewer Laurent@711: from controls import CustomGrid, CustomTable Laurent@715: from dialogs import ForceVariableDialog Laurent@714: from utils.BitmapLibrary import GetBitmap Laurent@711: Laurent@715: def AppendMenu(parent, help, id, kind, text): Laurent@715: parent.Append(help=help, id=id, kind=kind, text=text) Laurent@715: Laurent@711: def GetDebugVariablesTableColnames(): Laurent@711: _ = lambda x : x Laurent@711: return [_("Variable"), _("Value")] Laurent@711: Laurent@711: class VariableTableItem(DebugDataConsumer): Laurent@711: Laurent@711: def __init__(self, parent, variable, value): Laurent@711: DebugDataConsumer.__init__(self) Laurent@711: self.Parent = parent Laurent@711: self.Variable = variable Laurent@711: self.Value = value Laurent@711: Laurent@711: def __del__(self): Laurent@711: self.Parent = None Laurent@711: Laurent@711: def SetVariable(self, variable): Laurent@711: if self.Parent and self.Variable != variable: Laurent@711: self.Variable = variable Laurent@711: self.Parent.RefreshGrid() Laurent@711: Laurent@711: def GetVariable(self): Laurent@711: return self.Variable Laurent@711: Laurent@711: def SetForced(self, forced): Laurent@711: if self.Forced != forced: Laurent@711: self.Forced = forced Laurent@711: self.Parent.HasNewData = True Laurent@711: Laurent@711: def SetValue(self, value): Laurent@711: if self.Value != value: Laurent@711: self.Value = value Laurent@711: self.Parent.HasNewData = True Laurent@711: Laurent@711: def GetValue(self): Laurent@711: return self.Value Laurent@711: Laurent@711: class DebugVariableTable(CustomTable): Laurent@711: Laurent@711: def GetValue(self, row, col): Laurent@711: if row < self.GetNumberRows(): Laurent@711: return self.GetValueByName(row, self.GetColLabelValue(col, False)) Laurent@711: return "" Laurent@711: Laurent@711: def SetValue(self, row, col, value): Laurent@711: if col < len(self.colnames): Laurent@711: self.SetValueByName(row, self.GetColLabelValue(col, False), value) Laurent@711: Laurent@711: def GetValueByName(self, row, colname): Laurent@711: if row < self.GetNumberRows(): Laurent@711: if colname == "Variable": Laurent@711: return self.data[row].GetVariable() Laurent@711: elif colname == "Value": Laurent@711: return self.data[row].GetValue() Laurent@711: return "" Laurent@711: Laurent@711: def SetValueByName(self, row, colname, value): Laurent@711: if row < self.GetNumberRows(): Laurent@711: if colname == "Variable": Laurent@711: self.data[row].SetVariable(value) Laurent@711: elif colname == "Value": Laurent@711: self.data[row].SetValue(value) Laurent@711: Laurent@711: def IsForced(self, row): Laurent@711: if row < self.GetNumberRows(): Laurent@711: return self.data[row].IsForced() Laurent@711: return False Laurent@711: Laurent@711: def _updateColAttrs(self, grid): Laurent@711: """ Laurent@711: wx.grid.Grid -> update the column attributes to add the Laurent@711: appropriate renderer given the column name. Laurent@711: Laurent@711: Otherwise default to the default renderer. Laurent@711: """ Laurent@711: Laurent@711: for row in range(self.GetNumberRows()): Laurent@711: for col in range(self.GetNumberCols()): Laurent@711: if self.GetColLabelValue(col, False) == "Value": Laurent@711: if self.IsForced(row): Laurent@711: grid.SetCellTextColour(row, col, wx.BLUE) Laurent@711: else: Laurent@711: grid.SetCellTextColour(row, col, wx.BLACK) Laurent@711: grid.SetReadOnly(row, col, True) Laurent@711: self.ResizeRow(grid, row) Laurent@711: Laurent@711: def AppendItem(self, data): Laurent@711: self.data.append(data) Laurent@711: Laurent@711: def InsertItem(self, idx, data): Laurent@711: self.data.insert(idx, data) Laurent@711: Laurent@711: def RemoveItem(self, idx): Laurent@711: self.data.pop(idx) Laurent@711: Laurent@711: def MoveItem(self, idx, new_idx): Laurent@711: self.data.insert(new_idx, self.data.pop(idx)) Laurent@711: Laurent@711: def GetItem(self, idx): Laurent@711: return self.data[idx] Laurent@711: Laurent@711: class DebugVariableDropTarget(wx.TextDropTarget): Laurent@711: Laurent@711: def __init__(self, parent): Laurent@711: wx.TextDropTarget.__init__(self) Laurent@711: self.ParentWindow = parent Laurent@711: Laurent@711: def OnDropText(self, x, y, data): Laurent@711: x, y = self.ParentWindow.VariablesGrid.CalcUnscrolledPosition(x, y) Laurent@711: row = self.ParentWindow.VariablesGrid.YToRow(y - self.ParentWindow.VariablesGrid.GetColLabelSize()) Laurent@711: if row == wx.NOT_FOUND: Laurent@711: row = self.ParentWindow.Table.GetNumberRows() Laurent@711: message = None Laurent@711: try: Laurent@711: values = eval(data) Laurent@711: except: Laurent@711: message = _("Invalid value \"%s\" for debug variable")%data Laurent@711: values = None Laurent@711: if not isinstance(values, TupleType): Laurent@711: message = _("Invalid value \"%s\" for debug variable")%data Laurent@711: values = None Laurent@711: if values is not None and values[1] == "debug": Laurent@711: self.ParentWindow.InsertValue(values[0], row) Laurent@711: if message is not None: Laurent@711: wx.CallAfter(self.ShowMessage, message) Laurent@711: Laurent@711: def ShowMessage(self, message): Laurent@711: dialog = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK|wx.ICON_ERROR) Laurent@711: dialog.ShowModal() Laurent@711: dialog.Destroy() Laurent@711: Laurent@711: class DebugVariablePanel(wx.Panel, DebugViewer): Laurent@711: Laurent@714: def __init__(self, parent, producer): Laurent@711: wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL) Laurent@711: DebugViewer.__init__(self, producer, True) Laurent@711: Laurent@711: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) Laurent@711: main_sizer.AddGrowableCol(0) Laurent@711: main_sizer.AddGrowableRow(1) Laurent@711: Laurent@711: button_sizer = wx.BoxSizer(wx.HORIZONTAL) Laurent@711: main_sizer.AddSizer(button_sizer, border=5, Laurent@711: flag=wx.ALIGN_RIGHT|wx.ALL) Laurent@711: Laurent@714: for name, bitmap, help in [ Laurent@714: ("DeleteButton", "remove_element", _("Remove debug variable")), Laurent@714: ("UpButton", "up", _("Move debug variable up")), Laurent@714: ("DownButton", "down", _("Move debug variable down"))]: Laurent@714: button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap), Laurent@714: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@714: button.SetToolTipString(help) Laurent@714: setattr(self, name, button) Laurent@714: button_sizer.AddWindow(button, border=5, flag=wx.LEFT) Laurent@711: Laurent@711: self.VariablesGrid = CustomGrid(self, size=wx.Size(0, 150), style=wx.VSCROLL) Laurent@711: self.VariablesGrid.SetDropTarget(DebugVariableDropTarget(self)) Laurent@711: self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK, Laurent@711: self.OnVariablesGridCellRightClick) Laurent@711: main_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW) Laurent@711: Laurent@711: self.SetSizer(main_sizer) Laurent@711: Laurent@711: self.HasNewData = False Laurent@711: Laurent@711: self.Table = DebugVariableTable(self, [], GetDebugVariablesTableColnames()) Laurent@711: self.VariablesGrid.SetTable(self.Table) Laurent@714: self.VariablesGrid.SetButtons({"Delete": self.DeleteButton, Laurent@714: "Up": self.UpButton, Laurent@714: "Down": self.DownButton}) Laurent@711: Laurent@711: def _AddVariable(new_row): Laurent@711: return self.VariablesGrid.GetGridCursorRow() Laurent@711: setattr(self.VariablesGrid, "_AddRow", _AddVariable) Laurent@711: Laurent@711: def _DeleteVariable(row): Laurent@711: item = self.Table.GetItem(row) Laurent@711: self.RemoveDataConsumer(item) Laurent@711: self.Table.RemoveItem(row) Laurent@711: self.RefreshGrid() Laurent@711: setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable) Laurent@711: Laurent@711: def _MoveVariable(row, move): Laurent@711: new_row = max(0, min(row + move, self.Table.GetNumberRows() - 1)) Laurent@711: if new_row != row: Laurent@711: self.Table.MoveItem(row, new_row) Laurent@711: self.RefreshGrid() Laurent@711: return new_row Laurent@711: setattr(self.VariablesGrid, "_MoveRow", _MoveVariable) Laurent@711: Laurent@711: self.VariablesGrid.SetRowLabelSize(0) Laurent@711: Laurent@711: for col in range(self.Table.GetNumberCols()): Laurent@711: attr = wx.grid.GridCellAttr() Laurent@711: attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTER) Laurent@711: self.VariablesGrid.SetColAttr(col, attr) Laurent@711: self.VariablesGrid.SetColSize(col, 100) Laurent@711: Laurent@711: self.Table.ResetView(self.VariablesGrid) Laurent@711: self.VariablesGrid.RefreshButtons() Laurent@711: Laurent@711: def RefreshNewData(self): Laurent@711: if self.HasNewData: Laurent@711: self.HasNewData = False Laurent@711: self.RefreshGrid() Laurent@711: DebugViewer.RefreshNewData(self) Laurent@711: Laurent@711: def RefreshGrid(self): Laurent@711: self.Freeze() Laurent@711: self.Table.ResetView(self.VariablesGrid) Laurent@711: self.VariablesGrid.RefreshButtons() Laurent@711: self.Thaw() Laurent@711: Laurent@711: def UnregisterObsoleteData(self): Laurent@711: items = [(idx, item) for idx, item in enumerate(self.Table.GetData())] Laurent@711: items.reverse() Laurent@711: for idx, item in items: Laurent@711: iec_path = item.GetVariable().upper() Laurent@711: if self.GetDataType(iec_path) is None: Laurent@711: self.RemoveDataConsumer(item) Laurent@711: self.Table.RemoveItem(idx) Laurent@711: else: Laurent@711: self.AddDataConsumer(iec_path, item) Laurent@711: self.Freeze() Laurent@711: self.Table.ResetView(self.VariablesGrid) Laurent@711: self.VariablesGrid.RefreshButtons() Laurent@711: self.Thaw() Laurent@711: Laurent@711: def ResetGrid(self): Laurent@711: self.DeleteDataConsumers() Laurent@711: self.Table.Empty() Laurent@711: self.Freeze() Laurent@711: self.Table.ResetView(self.VariablesGrid) Laurent@711: self.VariablesGrid.RefreshButtons() Laurent@711: self.Thaw() Laurent@711: Laurent@711: def GetForceVariableMenuFunction(self, iec_path, item): Laurent@711: iec_type = self.GetDataType(iec_path) Laurent@711: def ForceVariableFunction(event): Laurent@711: if iec_type is not None: Laurent@711: dialog = ForceVariableDialog(self, iec_type, str(item.GetValue())) Laurent@711: if dialog.ShowModal() == wx.ID_OK: Laurent@711: self.ForceDataValue(iec_path, dialog.GetValue()) Laurent@711: return ForceVariableFunction Laurent@711: Laurent@711: def GetReleaseVariableMenuFunction(self, iec_path): Laurent@711: def ReleaseVariableFunction(event): Laurent@711: self.ReleaseDataValue(iec_path) Laurent@711: return ReleaseVariableFunction Laurent@711: Laurent@711: def OnVariablesGridCellRightClick(self, event): Laurent@711: row, col = event.GetRow(), event.GetCol() Laurent@711: if self.Table.GetColLabelValue(col, False) == "Value": Laurent@711: iec_path = self.Table.GetValueByName(row, "Variable").upper() Laurent@711: Laurent@711: menu = wx.Menu(title='') Laurent@711: Laurent@711: new_id = wx.NewId() Laurent@711: AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Force value")) Laurent@711: self.Bind(wx.EVT_MENU, self.GetForceVariableMenuFunction(iec_path.upper(), self.Table.GetItem(row)), id=new_id) Laurent@711: Laurent@711: new_id = wx.NewId() Laurent@711: AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Release value")) Laurent@711: self.Bind(wx.EVT_MENU, self.GetReleaseVariableMenuFunction(iec_path.upper()), id=new_id) Laurent@711: Laurent@711: if self.Table.IsForced(row): Laurent@711: menu.Enable(new_id, True) Laurent@711: else: Laurent@711: menu.Enable(new_id, False) Laurent@711: Laurent@711: self.PopupMenu(menu) Laurent@711: Laurent@711: menu.Destroy() Laurent@711: event.Skip() Laurent@711: Laurent@711: def InsertValue(self, iec_path, idx = None, force=False): Laurent@711: if idx is None: Laurent@711: idx = self.Table.GetNumberRows() Laurent@711: for item in self.Table.GetData(): Laurent@711: if iec_path == item.GetVariable(): Laurent@711: return Laurent@711: item = VariableTableItem(self, iec_path, "") Laurent@711: result = self.AddDataConsumer(iec_path.upper(), item) Laurent@711: if result is not None or force: Laurent@711: self.Table.InsertItem(idx, item) Laurent@711: self.RefreshGrid() Laurent@711: Laurent@711: def GetDebugVariables(self): Laurent@711: return [item.GetVariable() for item in self.Table.GetData()]