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@814: _ = lambda x : x Laurent@909: return [_("Variable"), _("Value")] Laurent@924: 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@887: def IsNumVariable(self, row): Laurent@887: if row < self.GetNumberRows(): Laurent@887: return self.data[row].IsNumVariable() Laurent@887: return False Laurent@887: 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@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@1198: class DebugVariableTableDropTarget(wx.TextDropTarget): Laurent@1198: Laurent@1198: def __init__(self, parent): Laurent@814: wx.TextDropTarget.__init__(self) Laurent@814: self.ParentWindow = parent Laurent@1198: Laurent@916: def __del__(self): Laurent@916: self.ParentWindow = None Laurent@928: Laurent@814: def OnDropText(self, x, y, data): 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@909: Laurent@814: if message is not None: Laurent@814: wx.CallAfter(self.ShowMessage, message) Laurent@909: elif values is not None and values[1] == "debug": Laurent@1198: grid = self.ParentWindow.VariablesGrid Laurent@1198: x, y = grid.CalcUnscrolledPosition(x, y) Laurent@1198: row = grid.YToRow(y - grid.GetColLabelSize()) Laurent@1198: if row == wx.NOT_FOUND: Laurent@1198: row = self.ParentWindow.Table.GetNumberRows() Laurent@1198: self.ParentWindow.InsertValue(values[0], row, force=True) Laurent@1198: 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@1198: Laurent@1198: class DebugVariableTablePanel(wx.Panel, DebugViewer): Laurent@814: Laurent@902: def __init__(self, parent, producer, window): Laurent@916: wx.Panel.__init__(self, parent, style=wx.SP_3D|wx.TAB_TRAVERSAL) Laurent@902: Laurent@902: self.ParentWindow = window Laurent@902: Laurent@814: self.HasNewData = False Laurent@1198: Laurent@1198: DebugViewer.__init__(self, producer, True) Laurent@1198: 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@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@1198: button = wx.lib.buttons.GenBitmapButton(self, 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@1198: self.VariablesGrid = CustomGrid(self, size=wx.Size(-1, 150), style=wx.VSCROLL) 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@1198: self.Table = DebugVariableTable(self, [], 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@1198: 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@1198: self.Table.RemoveItem(row) 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@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@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@1198: self.Freeze() Laurent@1198: Laurent@1198: if only_values: Laurent@1198: for col in xrange(self.Table.GetNumberCols()): Laurent@1198: if self.Table.GetColLabelValue(col, False) == "Value": Laurent@1198: for row in xrange(self.Table.GetNumberRows()): Laurent@1198: self.VariablesGrid.SetCellValue(row, col, str(self.Table.GetValueByName(row, "Value"))) Laurent@1198: if self.Table.IsForced(row): Laurent@1198: self.VariablesGrid.SetCellTextColour(row, col, wx.BLUE) Laurent@1198: else: Laurent@1198: self.VariablesGrid.SetCellTextColour(row, col, wx.BLACK) Laurent@929: else: Laurent@1198: self.Table.ResetView(self.VariablesGrid) Laurent@1198: self.VariablesGrid.RefreshButtons() Laurent@1198: Laurent@1198: self.Thaw() Laurent@916: Laurent@814: def UnregisterObsoleteData(self): Laurent@1176: self.SubscribeAllDataConsumers() Laurent@1198: Laurent@1198: items = [(idx, item) for idx, item in enumerate(self.Table.GetData())] Laurent@1198: items.reverse() Laurent@1198: for idx, item in items: 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@1198: self.Freeze() Laurent@1198: self.Table.ResetView(self.VariablesGrid) Laurent@1198: self.VariablesGrid.RefreshButtons() Laurent@1198: self.Thaw() Laurent@916: Laurent@916: def ResetView(self): Laurent@1176: self.UnsubscribeAllDataConsumers() Laurent@1198: Laurent@1198: self.Table.Empty() Laurent@1198: self.Freeze() Laurent@1198: self.Table.ResetView(self.VariablesGrid) Laurent@1198: self.VariablesGrid.RefreshButtons() Laurent@1198: self.Thaw() Laurent@902: 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@1102: self.ForceDataValue(iec_path.upper(), 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@892: def OnVariablesGridCellLeftClick(self, event): Laurent@892: if event.GetCol() == 0: Laurent@892: row = event.GetRow() Laurent@898: data = wx.TextDataObject(str((self.Table.GetValueByName(row, "Variable"), "debug"))) Laurent@892: dragSource = wx.DropSource(self.VariablesGrid) Laurent@892: dragSource.SetData(data) Laurent@892: dragSource.DoDragDrop() Laurent@892: event.Skip() Laurent@892: 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@1198: menu.Append(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@1198: menu.Append(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@1198: def InsertValue(self, iec_path, idx = None, force=False): Laurent@1198: for item in self.Table.GetData(): Laurent@1198: if iec_path == item.GetVariable(): Laurent@1198: return Laurent@934: if idx is None: Laurent@1198: idx = self.Table.GetNumberRows() Laurent@1198: item = DebugVariableItem(self, iec_path) Laurent@814: result = self.AddDataConsumer(iec_path.upper(), item) Laurent@814: if result is not None or force: Laurent@1198: self.Table.InsertItem(idx, item) Laurent@1198: self.RefreshView() Laurent@916: Laurent@887: def ResetGraphicsValues(self): Laurent@988: pass Laurent@1198: