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@887: from types import TupleType, FloatType Laurent@887: from time import time as gettime Laurent@887: import numpy Laurent@814: Laurent@814: import wx Laurent@814: import wx.lib.buttons Laurent@888: Laurent@888: try: Laurent@888: import matplotlib Laurent@888: matplotlib.use('WX') Laurent@888: import matplotlib.pyplot Laurent@888: from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas Laurent@888: from mpl_toolkits.mplot3d import Axes3D Laurent@888: USE_MPL = True Laurent@888: except: Laurent@888: USE_MPL = False Laurent@887: Laurent@887: from graphics import DebugDataConsumer, DebugViewer, REFRESH_PERIOD 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@888: cols = [_("Variable"), _("Value")] Laurent@888: if USE_MPL: Laurent@888: cols.append(_("3DAxis")) Laurent@888: return cols Laurent@814: Laurent@814: class VariableTableItem(DebugDataConsumer): Laurent@814: Laurent@887: def __init__(self, parent, variable): Laurent@814: DebugDataConsumer.__init__(self) Laurent@814: self.Parent = parent Laurent@814: self.Variable = variable Laurent@887: self.RefreshVariableType() Laurent@887: self.Value = "" Laurent@887: self.Axis3D = False 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@887: self.RefreshVariableType() Laurent@814: self.Parent.RefreshGrid() Laurent@814: Laurent@814: def GetVariable(self): Laurent@814: return self.Variable Laurent@814: Laurent@887: def RefreshVariableType(self): Laurent@887: self.VariableType = self.Parent.GetDataType(self.Variable) Laurent@887: self.ResetData() Laurent@887: Laurent@887: def GetVariableType(self): Laurent@887: return self.VariableType Laurent@887: Laurent@887: def GetData(self): Laurent@887: return self.Data Laurent@887: Laurent@887: def ResetData(self): Laurent@887: if self.IsNumVariable(): Laurent@887: self.Data = numpy.array([]).reshape(0, 2) Laurent@887: else: Laurent@887: self.Data = None Laurent@887: Laurent@887: def IsNumVariable(self): Laurent@887: return self.Parent.IsNumType(self.VariableType) Laurent@887: Laurent@887: def NewValue(self, tick, value, forced=False): Laurent@887: if self.IsNumVariable(): Laurent@895: num_value = {True:1., False:0.}.get(value, float(value)) Laurent@895: self.Data = numpy.append(self.Data, [[float(tick), num_value]], axis=0) Laurent@887: self.Parent.HasNewData = True Laurent@887: DebugDataConsumer.NewValue(self, tick, value, forced) Laurent@887: 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@892: if (self.VariableType == "STRING" and value.startswith("'") and value.endswith("'") or Laurent@892: self.VariableType == "WSTRING" and value.startswith('"') and value.endswith('"')): Laurent@892: value = value[1:-1] 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@887: if self.VariableType == "STRING": Laurent@878: return "'%s'" % self.Value Laurent@887: elif self.VariableType == "WSTRING": Laurent@878: return "\"%s\"" % self.Value Laurent@887: elif isinstance(self.Value, FloatType): Laurent@887: return "%.6g" % self.Value Laurent@814: return self.Value Laurent@814: Laurent@887: def SetAxis3D(self, axis_3d): Laurent@887: if self.IsNumVariable(): Laurent@887: self.Axis3D = axis_3d Laurent@887: Laurent@887: def GetAxis3D(self): Laurent@887: if self.IsNumVariable(): Laurent@887: return self.Axis3D Laurent@887: return "" Laurent@887: 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@887: elif colname == "3DAxis": Laurent@887: return self.data[row].GetAxis3D() 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@887: elif colname == "3DAxis": Laurent@887: self.data[row].SetAxis3D(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@887: if colname == "3DAxis": Laurent@887: if self.IsNumVariable(row): Laurent@887: grid.SetCellRenderer(row, col, wx.grid.GridCellBoolRenderer()) Laurent@887: grid.SetCellEditor(row, col, wx.grid.GridCellBoolEditor()) Laurent@887: grid.SetReadOnly(row, col, False) Laurent@814: else: Laurent@887: grid.SetReadOnly(row, col, True) Laurent@887: else: Laurent@887: if colname == "Value": Laurent@887: if self.IsForced(row): Laurent@887: grid.SetCellTextColour(row, col, wx.BLUE) Laurent@887: else: Laurent@887: grid.SetCellTextColour(row, col, wx.BLACK) Laurent@887: 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@887: SCROLLBAR_UNIT = 10 Laurent@887: Laurent@887: class DebugVariablePanel(wx.SplitterWindow, DebugViewer): Laurent@814: Laurent@814: def __init__(self, parent, producer): Laurent@887: wx.SplitterWindow.__init__(self, parent, style=wx.SP_3D) Laurent@814: DebugViewer.__init__(self, producer, True) Laurent@814: Laurent@887: self.SetSashGravity(0.5) Laurent@887: self.SetNeedUpdating(True) Laurent@887: self.SetMinimumPaneSize(1) Laurent@887: Laurent@887: self.MainPanel = wx.Panel(self, style=wx.TAB_TRAVERSAL) Laurent@887: Laurent@887: main_panel_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) Laurent@887: main_panel_sizer.AddGrowableCol(0) Laurent@887: main_panel_sizer.AddGrowableRow(1) Laurent@814: Laurent@814: button_sizer = wx.BoxSizer(wx.HORIZONTAL) Laurent@887: main_panel_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@887: button = wx.lib.buttons.GenBitmapButton(self.MainPanel, 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@887: self.VariablesGrid = CustomGrid(self.MainPanel, size=wx.Size(-1, 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@892: self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, Laurent@892: self.OnVariablesGridCellLeftClick) Laurent@887: self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, Laurent@887: self.OnVariablesGridCellChange) Laurent@887: main_panel_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW) Laurent@887: Laurent@887: self.MainPanel.SetSizer(main_panel_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@887: self.ResetGraphics() 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@887: self.ResetGraphics() 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@887: if self.Table.GetColLabelValue(col, False) == "3DAxis": Laurent@887: attr.SetAlignment(wx.ALIGN_CENTER, wx.ALIGN_CENTER) Laurent@887: else: Laurent@887: 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@887: Laurent@888: if USE_MPL: Laurent@888: self.GraphicsPanel = wx.Panel(self, style=wx.TAB_TRAVERSAL) Laurent@888: Laurent@888: graphics_panel_sizer = wx.BoxSizer(wx.VERTICAL) Laurent@888: Laurent@888: self.GraphicsCanvasWindow = wx.ScrolledWindow(self.GraphicsPanel, style=wx.HSCROLL|wx.VSCROLL) Laurent@888: self.GraphicsCanvasWindow.Bind(wx.EVT_SIZE, self.OnGraphicsCanvasWindowResize) Laurent@888: graphics_panel_sizer.AddWindow(self.GraphicsCanvasWindow, 1, flag=wx.GROW) Laurent@888: Laurent@888: graphics_canvas_window_sizer = wx.BoxSizer(wx.VERTICAL) Laurent@888: Laurent@888: self.GraphicsFigure = matplotlib.figure.Figure() Laurent@888: self.GraphicsFigure.subplots_adjust(hspace=0) Laurent@888: self.GraphicsAxes = [] Laurent@888: Laurent@888: self.GraphicsCanvas = FigureCanvas(self.GraphicsCanvasWindow, -1, self.GraphicsFigure) Laurent@888: graphics_canvas_window_sizer.AddWindow(self.GraphicsCanvas, 1, flag=wx.GROW) Laurent@888: Laurent@888: self.GraphicsCanvasWindow.SetSizer(graphics_canvas_window_sizer) Laurent@888: Laurent@888: self.Graphics3DFigure = matplotlib.figure.Figure() Laurent@888: self.Graphics3DFigure.subplotpars.update(left=0.0, right=1.0, bottom=0.0, top=1.0) Laurent@888: Laurent@888: self.LastMotionTime = gettime() Laurent@888: self.Graphics3DAxes = self.Graphics3DFigure.gca(projection='3d') Laurent@888: self.Graphics3DAxes.set_color_cycle(['b']) Laurent@888: setattr(self.Graphics3DAxes, "_on_move", self.OnGraphics3DMotion) Laurent@888: Laurent@888: self.Graphics3DCanvas = FigureCanvas(self.GraphicsPanel, -1, self.Graphics3DFigure) Laurent@892: self.Graphics3DCanvas.SetMinSize(wx.Size(1, 1)) Laurent@888: graphics_panel_sizer.AddWindow(self.Graphics3DCanvas, 1, flag=wx.GROW) Laurent@888: Laurent@888: self.Graphics3DAxes.mouse_init() Laurent@888: Laurent@888: self.GraphicsPanel.SetSizer(graphics_panel_sizer) Laurent@888: Laurent@888: self.SplitHorizontally(self.MainPanel, self.GraphicsPanel, -200) Laurent@888: Laurent@888: else: Laurent@888: self.Initialize(self.MainPanel) Laurent@887: Laurent@887: self.ResetGraphics() Laurent@814: Laurent@814: def RefreshNewData(self): Laurent@814: if self.HasNewData: Laurent@814: self.HasNewData = False Laurent@887: self.RefreshGrid(only_values=True) Laurent@814: DebugViewer.RefreshNewData(self) Laurent@814: Laurent@887: def RefreshGrid(self, only_values=False): Laurent@814: self.Freeze() Laurent@887: if only_values: Laurent@887: for col in xrange(self.Table.GetNumberCols()): Laurent@887: if self.Table.GetColLabelValue(col, False) == "Value": Laurent@887: for row in xrange(self.Table.GetNumberRows()): Laurent@887: self.VariablesGrid.SetCellValue(row, col, str(self.Table.GetValueByName(row, "Value"))) Laurent@887: else: Laurent@887: self.Table.ResetView(self.VariablesGrid) Laurent@814: self.VariablesGrid.RefreshButtons() Laurent@887: Laurent@888: if USE_MPL: Laurent@888: # Refresh graphics Laurent@888: idx = 0 Laurent@888: for item in self.Table.GetData(): Laurent@888: data = item.GetData() Laurent@888: if data is not None: Laurent@888: self.GraphicsAxes[idx].clear() Laurent@888: self.GraphicsAxes[idx].plot(data[:, 0], data[:, 1]) Laurent@888: idx += 1 Laurent@888: self.GraphicsCanvas.draw() Laurent@888: Laurent@888: # Refresh 3D graphics Laurent@888: while len(self.Graphics3DAxes.lines) > 0: Laurent@888: self.Graphics3DAxes.lines.pop() Laurent@888: if self.Axis3DValues is not None: Laurent@888: self.Graphics3DAxes.plot( Laurent@888: self.Axis3DValues[0][1].GetData()[self.Axis3DValues[0][0]:, 1], Laurent@888: self.Axis3DValues[1][1].GetData()[self.Axis3DValues[1][0]:, 1], Laurent@888: zs = self.Axis3DValues[2][1].GetData()[self.Axis3DValues[2][0]:, 1]) Laurent@888: self.Graphics3DCanvas.draw() Laurent@887: Laurent@814: self.Thaw() Laurent@887: 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@887: item.RefreshVariableType() 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@887: self.ResetGraphics() 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@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@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@887: def OnVariablesGridCellChange(self, event): Laurent@887: row, col = event.GetRow(), event.GetCol() Laurent@887: if self.Table.GetColLabelValue(col, False) == "3DAxis": Laurent@887: wx.CallAfter(self.Reset3DGraphics) Laurent@887: event.Skip() Laurent@887: Laurent@887: def InsertValue(self, iec_path, idx = None, force=False, axis3D=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@887: 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@887: item.SetAxis3D(int(axis3D)) Laurent@887: self.ResetGraphics() Laurent@814: self.RefreshGrid() Laurent@887: Laurent@814: def GetDebugVariables(self): Laurent@814: return [item.GetVariable() for item in self.Table.GetData()] Laurent@887: Laurent@887: def GetAxis3D(self): Laurent@887: return [item.GetVariable() for item in self.Table.GetData() if item.GetAxis3D()] Laurent@887: Laurent@887: def ResetGraphicsValues(self): Laurent@887: for item in self.Table.GetData(): Laurent@887: item.ResetData() Laurent@887: Laurent@887: def ResetGraphics(self): Laurent@888: if USE_MPL: Laurent@888: self.GraphicsFigure.clear() Laurent@888: self.GraphicsAxes = [] Laurent@888: Laurent@888: axes_num = 0 Laurent@888: for item in self.Table.GetData(): Laurent@888: if item.IsNumVariable(): Laurent@888: axes_num += 1 Laurent@888: Laurent@888: for idx in xrange(axes_num): Laurent@888: if idx == 0: Laurent@888: axes = self.GraphicsFigure.add_subplot(axes_num, 1, idx + 1) Laurent@888: else: Laurent@888: axes = self.GraphicsFigure.add_subplot(axes_num, 1, idx + 1, sharex=self.GraphicsAxes[0]) Laurent@888: self.GraphicsAxes.append(axes) Laurent@888: Laurent@888: self.RefreshGraphicsCanvasWindowScrollbars() Laurent@888: self.GraphicsCanvas.draw() Laurent@888: Laurent@888: self.Reset3DGraphics() Laurent@887: Laurent@887: def Reset3DGraphics(self): Laurent@887: axis = [item for item in self.Table.GetData() if item.GetAxis3D()] Laurent@887: if len(axis) == 3: Laurent@887: max_tick = None Laurent@887: xaxis, yaxis, zaxis = [item.GetData() for item in axis] Laurent@887: if len(xaxis) > 0 and len(yaxis) > 0 and len(zaxis) > 0: Laurent@887: max_tick = max(xaxis[0, 0], yaxis[0, 0], zaxis[0, 0]) Laurent@887: if max_tick is not None: Laurent@887: self.Axis3DValues = [(numpy.argmin(abs(item.GetData()[:, 0] - max_tick)), item) Laurent@887: for item in axis] Laurent@887: else: Laurent@887: self.Axis3DValues = [(0, item) for item in axis] Laurent@887: else: Laurent@887: self.Axis3DValues = None Laurent@887: Laurent@887: def OnGraphics3DMotion(self, event): Laurent@887: current_time = gettime() Laurent@887: if current_time - self.LastMotionTime > REFRESH_PERIOD: Laurent@887: self.LastMotionTime = current_time Laurent@887: Axes3D._on_move(self.Graphics3DAxes, event) Laurent@887: Laurent@887: def RefreshGraphicsCanvasWindowScrollbars(self): Laurent@887: xstart, ystart = self.GraphicsCanvasWindow.GetViewStart() Laurent@887: window_size = self.GraphicsCanvasWindow.GetClientSize() Laurent@887: vwidth, vheight = (window_size[0], (len(self.GraphicsAxes) + 1) * 50) Laurent@887: self.GraphicsCanvas.SetMinSize(wx.Size(vwidth, vheight)) Laurent@887: posx = max(0, min(xstart, (vwidth - window_size[0]) / SCROLLBAR_UNIT)) Laurent@887: posy = max(0, min(ystart, (vheight - window_size[1]) / SCROLLBAR_UNIT)) Laurent@887: self.GraphicsCanvasWindow.Scroll(posx, posy) Laurent@887: self.GraphicsCanvasWindow.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, Laurent@887: vwidth / SCROLLBAR_UNIT, vheight / SCROLLBAR_UNIT, posx, posy) Laurent@887: Laurent@887: def OnGraphicsCanvasWindowResize(self, event): Laurent@887: self.RefreshGraphicsCanvasWindowScrollbars() Laurent@887: event.Skip()