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@814: from types import TupleType Laurent@814: Laurent@814: import wx Laurent@814: import wx.lib.buttons Laurent@814: Laurent@814: from graphics import DebugDataConsumer, 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@814: def AppendMenu(parent, help, id, kind, text): Laurent@814: parent.Append(help=help, id=id, kind=kind, text=text) Laurent@814: Laurent@814: def GetDebugVariablesTableColnames(): Laurent@814: _ = lambda x : x Laurent@814: return [_("Variable"), _("Value")] Laurent@814: Laurent@814: class VariableTableItem(DebugDataConsumer): Laurent@814: Laurent@814: def __init__(self, parent, variable, value): Laurent@814: DebugDataConsumer.__init__(self) Laurent@814: self.Parent = parent Laurent@814: self.Variable = variable Laurent@814: self.Value = value Laurent@814: Laurent@814: def __del__(self): Laurent@814: self.Parent = None Laurent@814: Laurent@814: def SetVariable(self, variable): Laurent@814: if self.Parent and self.Variable != variable: Laurent@814: self.Variable = variable Laurent@814: self.Parent.RefreshGrid() Laurent@814: Laurent@814: def GetVariable(self): Laurent@814: return self.Variable Laurent@814: Laurent@814: def SetForced(self, forced): Laurent@814: if self.Forced != forced: Laurent@814: self.Forced = forced Laurent@814: self.Parent.HasNewData = True Laurent@814: Laurent@814: def SetValue(self, value): Laurent@814: if self.Value != value: Laurent@814: self.Value = value Laurent@814: self.Parent.HasNewData = True Laurent@814: Laurent@814: def GetValue(self): Laurent@814: return self.Value Laurent@814: 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@814: if self.GetColLabelValue(col, False) == "Value": Laurent@814: if self.IsForced(row): Laurent@814: grid.SetCellTextColour(row, col, wx.BLUE) Laurent@814: else: Laurent@814: grid.SetCellTextColour(row, col, wx.BLACK) Laurent@814: grid.SetReadOnly(row, col, True) Laurent@814: self.ResizeRow(grid, row) Laurent@814: Laurent@814: def AppendItem(self, data): Laurent@814: self.data.append(data) Laurent@814: Laurent@814: def InsertItem(self, idx, data): Laurent@814: self.data.insert(idx, data) Laurent@814: Laurent@814: def RemoveItem(self, idx): Laurent@814: self.data.pop(idx) 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@814: class DebugVariableDropTarget(wx.TextDropTarget): Laurent@814: Laurent@814: def __init__(self, parent): Laurent@814: wx.TextDropTarget.__init__(self) Laurent@814: self.ParentWindow = parent Laurent@814: Laurent@814: def OnDropText(self, x, y, data): Laurent@814: x, y = self.ParentWindow.VariablesGrid.CalcUnscrolledPosition(x, y) Laurent@814: row = self.ParentWindow.VariablesGrid.YToRow(y - self.ParentWindow.VariablesGrid.GetColLabelSize()) Laurent@814: if row == wx.NOT_FOUND: Laurent@814: row = self.ParentWindow.Table.GetNumberRows() Laurent@814: message = None Laurent@814: try: Laurent@814: values = eval(data) Laurent@814: except: Laurent@814: message = _("Invalid value \"%s\" for debug variable")%data Laurent@814: values = None Laurent@814: if not isinstance(values, TupleType): Laurent@814: message = _("Invalid value \"%s\" for debug variable")%data Laurent@814: values = None Laurent@814: if values is not None and values[1] == "debug": Laurent@814: self.ParentWindow.InsertValue(values[0], row) Laurent@814: if message is not None: Laurent@814: wx.CallAfter(self.ShowMessage, message) Laurent@814: Laurent@814: def ShowMessage(self, message): Laurent@814: dialog = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK|wx.ICON_ERROR) Laurent@814: dialog.ShowModal() Laurent@814: dialog.Destroy() Laurent@814: Laurent@814: class DebugVariablePanel(wx.Panel, DebugViewer): Laurent@814: Laurent@814: def __init__(self, parent, producer): Laurent@814: wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL) Laurent@814: DebugViewer.__init__(self, producer, True) Laurent@814: Laurent@814: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) Laurent@814: main_sizer.AddGrowableCol(0) Laurent@814: main_sizer.AddGrowableRow(1) Laurent@814: Laurent@814: button_sizer = wx.BoxSizer(wx.HORIZONTAL) Laurent@814: main_sizer.AddSizer(button_sizer, border=5, Laurent@814: flag=wx.ALIGN_RIGHT|wx.ALL) Laurent@814: Laurent@814: for name, bitmap, help in [ Laurent@814: ("DeleteButton", "remove_element", _("Remove debug variable")), Laurent@814: ("UpButton", "up", _("Move debug variable up")), Laurent@814: ("DownButton", "down", _("Move debug variable down"))]: Laurent@814: button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap), Laurent@814: size=wx.Size(28, 28), style=wx.NO_BORDER) Laurent@814: button.SetToolTipString(help) Laurent@814: setattr(self, name, button) Laurent@814: button_sizer.AddWindow(button, border=5, flag=wx.LEFT) Laurent@814: Laurent@814: self.VariablesGrid = CustomGrid(self, size=wx.Size(0, 150), style=wx.VSCROLL) Laurent@814: self.VariablesGrid.SetDropTarget(DebugVariableDropTarget(self)) Laurent@814: self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK, Laurent@814: self.OnVariablesGridCellRightClick) Laurent@814: main_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW) Laurent@814: Laurent@814: self.SetSizer(main_sizer) Laurent@814: Laurent@814: self.HasNewData = False Laurent@814: Laurent@814: self.Table = DebugVariableTable(self, [], GetDebugVariablesTableColnames()) Laurent@814: self.VariablesGrid.SetTable(self.Table) Laurent@814: self.VariablesGrid.SetButtons({"Delete": self.DeleteButton, Laurent@814: "Up": self.UpButton, Laurent@814: "Down": self.DownButton}) Laurent@814: Laurent@814: def _AddVariable(new_row): Laurent@814: return self.VariablesGrid.GetGridCursorRow() Laurent@814: setattr(self.VariablesGrid, "_AddRow", _AddVariable) Laurent@814: Laurent@814: def _DeleteVariable(row): Laurent@814: item = self.Table.GetItem(row) Laurent@814: self.RemoveDataConsumer(item) Laurent@814: self.Table.RemoveItem(row) Laurent@814: self.RefreshGrid() Laurent@814: setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable) Laurent@814: Laurent@814: def _MoveVariable(row, move): Laurent@814: new_row = max(0, min(row + move, self.Table.GetNumberRows() - 1)) Laurent@814: if new_row != row: Laurent@814: self.Table.MoveItem(row, new_row) Laurent@814: self.RefreshGrid() Laurent@814: return new_row Laurent@814: setattr(self.VariablesGrid, "_MoveRow", _MoveVariable) Laurent@814: Laurent@814: self.VariablesGrid.SetRowLabelSize(0) Laurent@814: Laurent@814: for col in range(self.Table.GetNumberCols()): Laurent@814: attr = wx.grid.GridCellAttr() Laurent@814: attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTER) Laurent@814: self.VariablesGrid.SetColAttr(col, attr) Laurent@814: self.VariablesGrid.SetColSize(col, 100) Laurent@814: Laurent@814: self.Table.ResetView(self.VariablesGrid) Laurent@814: self.VariablesGrid.RefreshButtons() Laurent@814: Laurent@814: def RefreshNewData(self): Laurent@814: if self.HasNewData: Laurent@814: self.HasNewData = False Laurent@814: self.RefreshGrid() Laurent@814: DebugViewer.RefreshNewData(self) Laurent@814: Laurent@814: def RefreshGrid(self): Laurent@814: self.Freeze() Laurent@814: self.Table.ResetView(self.VariablesGrid) Laurent@814: self.VariablesGrid.RefreshButtons() Laurent@814: self.Thaw() Laurent@814: Laurent@814: def UnregisterObsoleteData(self): Laurent@814: items = [(idx, item) for idx, item in enumerate(self.Table.GetData())] Laurent@814: items.reverse() Laurent@814: for idx, item in items: Laurent@814: iec_path = item.GetVariable().upper() Laurent@814: if self.GetDataType(iec_path) is None: Laurent@814: self.RemoveDataConsumer(item) Laurent@814: self.Table.RemoveItem(idx) Laurent@814: else: Laurent@814: self.AddDataConsumer(iec_path, item) Laurent@814: self.Freeze() Laurent@814: self.Table.ResetView(self.VariablesGrid) Laurent@814: self.VariablesGrid.RefreshButtons() Laurent@814: self.Thaw() Laurent@814: Laurent@814: def ResetGrid(self): Laurent@814: self.DeleteDataConsumers() Laurent@814: self.Table.Empty() Laurent@814: self.Freeze() Laurent@814: self.Table.ResetView(self.VariablesGrid) Laurent@814: self.VariablesGrid.RefreshButtons() Laurent@814: self.Thaw() Laurent@814: Laurent@814: def GetForceVariableMenuFunction(self, iec_path, item): Laurent@814: iec_type = self.GetDataType(iec_path) Laurent@814: def ForceVariableFunction(event): Laurent@814: if iec_type is not None: Laurent@814: dialog = ForceVariableDialog(self, iec_type, str(item.GetValue())) Laurent@814: if dialog.ShowModal() == wx.ID_OK: Laurent@814: self.ForceDataValue(iec_path, dialog.GetValue()) Laurent@814: return ForceVariableFunction Laurent@814: Laurent@814: def GetReleaseVariableMenuFunction(self, iec_path): Laurent@814: def ReleaseVariableFunction(event): Laurent@814: self.ReleaseDataValue(iec_path) Laurent@814: return ReleaseVariableFunction Laurent@814: Laurent@814: def OnVariablesGridCellRightClick(self, event): Laurent@814: row, col = event.GetRow(), event.GetCol() Laurent@814: if self.Table.GetColLabelValue(col, False) == "Value": Laurent@814: iec_path = self.Table.GetValueByName(row, "Variable").upper() Laurent@814: Laurent@814: menu = wx.Menu(title='') Laurent@814: Laurent@814: new_id = wx.NewId() Laurent@814: AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Force value")) Laurent@814: self.Bind(wx.EVT_MENU, self.GetForceVariableMenuFunction(iec_path.upper(), self.Table.GetItem(row)), id=new_id) Laurent@814: Laurent@814: new_id = wx.NewId() Laurent@814: AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Release value")) Laurent@814: self.Bind(wx.EVT_MENU, self.GetReleaseVariableMenuFunction(iec_path.upper()), id=new_id) Laurent@814: Laurent@814: if self.Table.IsForced(row): Laurent@814: menu.Enable(new_id, True) Laurent@814: else: Laurent@814: menu.Enable(new_id, False) Laurent@814: Laurent@814: self.PopupMenu(menu) Laurent@814: Laurent@814: menu.Destroy() Laurent@814: event.Skip() Laurent@814: Laurent@814: def InsertValue(self, iec_path, idx = None, force=False): Laurent@814: if idx is None: Laurent@814: idx = self.Table.GetNumberRows() Laurent@814: for item in self.Table.GetData(): Laurent@814: if iec_path == item.GetVariable(): Laurent@814: return Laurent@814: item = VariableTableItem(self, iec_path, "") Laurent@814: result = self.AddDataConsumer(iec_path.upper(), item) Laurent@814: if result is not None or force: Laurent@814: self.Table.InsertItem(idx, item) Laurent@814: self.RefreshGrid() Laurent@814: Laurent@814: def GetDebugVariables(self): Laurent@814: return [item.GetVariable() for item in self.Table.GetData()]