#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard.
#Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
#See COPYING file for copyrights details.
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#General Public License for more details.
#You should have received a copy of the GNU General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from types import TupleType, ListType, FloatType
from time import time as gettime
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wxagg import _convert_agg_to_wx_bitmap
from matplotlib.backends.backend_agg import FigureCanvasAgg
from mpl_toolkits.mplot3d import Axes3D
color_cycle = ['r', 'b', 'g', 'm', 'y', 'k']
from graphics.DebugDataConsumer import DebugDataConsumer
from editors.DebugViewer import DebugViewer, REFRESH_PERIOD
from controls import CustomGrid, CustomTable
from controls.DebugVariablePanel.DebugVariableItem import DebugVariableItem
from dialogs.ForceVariableDialog import ForceVariableDialog
from util.BitmapLibrary import GetBitmap
def AppendMenu(parent, help, id, kind, text):
parent.Append(help=help, id=id, kind=kind, text=text)
def GetDebugVariablesTableColnames():
return [_("Variable"), _("Value")]
class DebugVariableTable(CustomTable):
def GetValue(self, row, col):
if row < self.GetNumberRows():
return self.GetValueByName(row, self.GetColLabelValue(col, False))
def SetValue(self, row, col, value):
if col < len(self.colnames):
self.SetValueByName(row, self.GetColLabelValue(col, False), value)
def GetValueByName(self, row, colname):
if row < self.GetNumberRows():
if colname == "Variable":
return self.data[row].GetVariable()
return self.data[row].GetValue()
def SetValueByName(self, row, colname, value):
if row < self.GetNumberRows():
if colname == "Variable":
self.data[row].SetVariable(value)
self.data[row].SetValue(value)
if row < self.GetNumberRows():
return self.data[row].IsForced()
def IsNumVariable(self, row):
if row < self.GetNumberRows():
return self.data[row].IsNumVariable()
def _updateColAttrs(self, grid):
wx.grid.Grid -> update the column attributes to add the
appropriate renderer given the column name.
Otherwise default to the default renderer.
for row in range(self.GetNumberRows()):
for col in range(self.GetNumberCols()):
colname = self.GetColLabelValue(col, False)
grid.SetCellTextColour(row, col, wx.BLUE)
grid.SetCellTextColour(row, col, wx.BLACK)
grid.SetReadOnly(row, col, True)
self.ResizeRow(grid, row)
def AppendItem(self, data):
def InsertItem(self, idx, data):
self.data.insert(idx, data)
def RemoveItem(self, idx):
def MoveItem(self, idx, new_idx):
self.data.insert(new_idx, self.data.pop(idx))
class DebugVariableDropTarget(wx.TextDropTarget):
def __init__(self, parent, control=None):
wx.TextDropTarget.__init__(self)
self.ParentWindow = parent
self.ParentControl = control
self.ParentControl = None
def OnDragOver(self, x, y, d):
if not isinstance(self.ParentControl, CustomGrid):
if self.ParentControl is not None:
self.ParentControl.OnMouseDragging(x, y)
self.ParentWindow.RefreshHighlight(x, y)
return wx.TextDropTarget.OnDragOver(self, x, y, d)
def OnDropText(self, x, y, data):
message = _("Invalid value \"%s\" for debug variable")%data
if not isinstance(values, TupleType):
message = _("Invalid value \"%s\" for debug variable")%data
wx.CallAfter(self.ShowMessage, message)
elif values is not None and values[1] == "debug":
if isinstance(self.ParentControl, CustomGrid):
x, y = self.ParentControl.CalcUnscrolledPosition(x, y)
row = self.ParentControl.YToRow(y - self.ParentControl.GetColLabelSize())
row = self.ParentWindow.Table.GetNumberRows()
self.ParentWindow.InsertValue(values[0], row, force=True)
elif self.ParentControl is not None:
width, height = self.ParentControl.GetSize()
target_idx = self.ParentControl.GetIndex()
merge_type = GRAPH_PARALLEL
if isinstance(self.ParentControl, DebugVariableGraphic):
if self.ParentControl.Is3DCanvas():
if len(values) > 1 and values[2] == "move":
self.ParentWindow.MoveValue(values[0], target_idx)
self.ParentWindow.InsertValue(values[0], target_idx, force=True)
rect = self.ParentControl.GetAxesBoundingBox()
merge_rect = wx.Rect(rect.x, rect.y, rect.width / 2., rect.height)
if merge_rect.InsideXY(x, y):
merge_type = GRAPH_ORTHOGONAL
wx.CallAfter(self.ParentWindow.MergeGraphs, values[0], target_idx, merge_type, force=True)
if len(values) > 2 and values[2] == "move":
self.ParentWindow.MoveValue(values[0], target_idx)
self.ParentWindow.InsertValue(values[0], target_idx, force=True)
if len(values) > 2 and values[2] == "move":
self.ParentWindow.MoveValue(values[0], target_idx)
self.ParentWindow.InsertValue(values[0], target_idx, force=True)
elif len(values) > 2 and values[2] == "move":
self.ParentWindow.MoveValue(values[0])
self.ParentWindow.InsertValue(values[0], force=True)
if not isinstance(self.ParentControl, CustomGrid):
self.ParentWindow.ResetHighlight()
return wx.TextDropTarget.OnLeave(self)
def ShowMessage(self, message):
dialog = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK|wx.ICON_ERROR)
SECOND = 1000 * MILLISECOND
ZOOM_VALUES = map(lambda x:("x %.1f" % x, x), [math.sqrt(2) ** i for i in xrange(8)])
[("%dms" % i, i * MILLISECOND) for i in (10, 20, 50, 100, 200, 500)] + \
[("%ds" % i, i * SECOND) for i in (1, 2, 5, 10, 20, 30)] + \
[("%dm" % i, i * MINUTE) for i in (1, 2, 5, 10, 20, 30)] + \
[("%dh" % i, i * HOUR) for i in (1, 2, 3, 6, 12, 24)]
GRAPH_PARALLEL, GRAPH_ORTHOGONAL = range(2)
HIGHLIGHT_RESIZE] = range(6)
HIGHLIGHT_DROP_PEN = wx.Pen(wx.Colour(0, 128, 255))
HIGHLIGHT_DROP_BRUSH = wx.Brush(wx.Colour(0, 128, 255, 128))
HIGHLIGHT_RESIZE_PEN = wx.Pen(wx.Colour(200, 200, 200))
HIGHLIGHT_RESIZE_BRUSH = wx.Brush(wx.Colour(200, 200, 200))
[SIZE_MINI, SIZE_MIDDLE, SIZE_MAXI] = [0, 100, 200]
DEFAULT_CANVAS_HEIGHT = 200.
CANVAS_BORDER = (20., 10.)
AXES_LABEL_HEIGHT = 12.75
for item, data in variables:
next_tick = min(next_tick, data[0][0])
def OrthogonalData(item, start_tick, end_tick):
data = item.GetData(start_tick, end_tick)
min_value, max_value = item.GetValueRange()
if min_value is not None and max_value is not None:
center = (min_value + max_value) / 2.
range = max(1.0, max_value - min_value)
return data, center - range * 0.55, center + range * 0.55
def __init__(self, x, y, bitmap, callback):
self.Position = wx.Point(x, y)
return self.Bitmap.GetSize()
def SetPosition(self, x, y):
self.Position = wx.Point(x, y)
if self.Shown and self.Enabled:
w, h = self.Bitmap.GetSize()
rect = wx.Rect(self.Position.x, self.Position.y, w, h)
def ProcessCallback(self):
if self.Callback is not None:
wx.CallAfter(self.Callback)
if self.Shown and self.Enabled:
dc.DrawBitmap(self.Bitmap, self.Position.x, self.Position.y, True)
class DebugVariableViewer:
def __init__(self, window, items=[]):
self.ParentWindow = window
self.Highlight = HIGHLIGHT_NONE
return self.ParentWindow.GetViewerIndex(self)
def GetItem(self, variable):
if item.GetVariable() == variable:
variables = [item.GetVariable() for item in self.Items]
if self.GraphType == GRAPH_ORTHOGONAL:
return self.Items[0].GetVariable()
def RemoveItem(self, item):
self.ParentWindow.RemoveDataConsumer(item)
return len(self.Items) == 0
def UnregisterObsoleteData(self):
for item in self.Items[:]:
iec_path = item.GetVariable()
if self.ParentWindow.GetDataType(iec_path) is None:
self.ParentWindow.RemoveDataConsumer(item)
self.ParentWindow.AddDataConsumer(iec_path.upper(), item)
item.RefreshVariableType()
def SetHighlight(self, highlight):
if self.Highlight != highlight:
self.Highlight = highlight
def HandleButtons(self, x, y):
for button in self.GetButtons():
def IsOverButton(self, x, y):
for button in self.GetButtons():
def ShowButtons(self, show):
for button in self.Buttons:
self.RefreshButtonsState()
self.ParentWindow.ForceRefresh()
def RefreshButtonsState(self, refresh_positions=False):
width, height = self.GetSize()
buttons = self.Buttons[:]
button.SetPosition(width - 5 - w - offset, 5)
self.ParentWindow.ForceRefresh()
def DrawCommonElements(self, dc, buttons=None):
width, height = self.GetSize()
dc.SetPen(HIGHLIGHT_DROP_PEN)
dc.SetBrush(HIGHLIGHT_DROP_BRUSH)
if self.Highlight in [HIGHLIGHT_BEFORE]:
dc.DrawLine(0, 1, width - 1, 1)
elif self.Highlight in [HIGHLIGHT_AFTER]:
dc.DrawLine(0, height - 1, width - 1, height - 1)
if self.ParentWindow.IsDragging():
destBBox = self.ParentWindow.GetDraggingAxesClippingRegion(self)
srcPos = self.ParentWindow.GetDraggingAxesPosition(self)
if destBBox.width > 0 and destBBox.height > 0:
srcPanel = self.ParentWindow.DraggingAxesPanel
srcBBox = srcPanel.GetAxesBoundingBox()
srcX = srcBBox.x - srcPos.x
srcY = srcBBox.y - srcPos.y
srcBmp = _convert_agg_to_wx_bitmap(srcPanel.get_renderer(), None)
srcDC.SelectObject(srcBmp)
dc.Blit(destBBox.x, destBBox.y,
int(destBBox.width), int(destBBox.height),
def OnEnter(self, event):
def OnLeave(self, event):
if self.Highlight != HIGHLIGHT_RESIZE or self.CanvasStartSize is None:
x, y = event.GetPosition()
width, height = self.GetSize()
if (x <= 0 or x >= width - 1 or
y <= 0 or y >= height - 1):
wx.CallAfter(self.ParentWindow.DeleteValue, self)
wx.CallAfter(self.ForceValue, self.Items[0])
def OnReleaseButton(self):
wx.CallAfter(self.ReleaseValue, self.Items[0])
def OnResizeWindow(self, event):
wx.CallAfter(self.RefreshButtonsState, True)
def OnMouseDragging(self, x, y):
xw, yw = self.GetPosition()
self.ParentWindow.RefreshHighlight(x + xw, y + yw)
def OnDragging(self, x, y):
width, height = self.GetSize()
if self.ParentWindow.IsViewerFirst(self):
self.SetHighlight(HIGHLIGHT_BEFORE)
self.SetHighlight(HIGHLIGHT_NONE)
self.ParentWindow.HighlightPreviousViewer(self)
self.SetHighlight(HIGHLIGHT_AFTER)
def OnEraseBackground(self, event):
def OnResize(self, event):
wx.CallAfter(self.RefreshButtonsState, True)
def ForceValue(self, item):
iec_path = item.GetVariable()
iec_type = self.ParentWindow.GetDataType(iec_path)
dialog = ForceVariableDialog(self, iec_type, str(item.GetValue()))
if dialog.ShowModal() == wx.ID_OK:
self.ParentWindow.ForceDataValue(iec_path.upper(), dialog.GetValue())
def ReleaseValue(self, item):
iec_path = item.GetVariable().upper()
self.ParentWindow.ReleaseDataValue(iec_path)
class DebugVariableText(DebugVariableViewer, wx.Panel):
def __init__(self, parent, window, items=[]):
DebugVariableViewer.__init__(self, window, items)
wx.Panel.__init__(self, parent)
self.SetBackgroundColour(wx.WHITE)
self.SetDropTarget(DebugVariableDropTarget(window, self))
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_ENTER_WINDOW, self.OnEnter)
self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
self.Bind(wx.EVT_SIZE, self.OnResize)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.SetMinSize(wx.Size(0, 25))
GraphButton(0, 0, GetBitmap("force"), self.OnForceButton))
GraphButton(0, 0, GetBitmap("release"), self.OnReleaseButton))
GraphButton(0, 0, GetBitmap("delete_graph"), self.OnCloseButton))
width, height = self.GetSize()
bitmap = wx.EmptyBitmap(width, height)
dc = wx.BufferedDC(wx.ClientDC(self), bitmap)
item_name = self.Items[0].GetVariable(self.ParentWindow.GetVariableNameMask())
w, h = gc.GetTextExtent(item_name)
gc.DrawText(item_name, 20, (height - h) / 2)
if self.Items[0].IsForced():
gc.SetTextForeground(wx.BLUE)
self.Buttons[0].Disable()
self.Buttons[1].Disable()
self.RefreshButtonsState(True)
item_value = self.Items[0].GetValue()
w, h = gc.GetTextExtent(item_value)
gc.DrawText(item_value, width - 40 - w, (height - h) / 2)
self.DrawCommonElements(gc)
def OnLeftDown(self, event):
width, height = self.GetSize()
item_name = self.Items[0].GetVariable(self.ParentWindow.GetVariableNameMask())
w, h = self.GetTextExtent(item_name)
x, y = event.GetPosition()
rect = wx.Rect(20, (height - h) / 2, w, h)
data = wx.TextDataObject(str((self.Items[0].GetVariable(), "debug", "move")))
dragSource = wx.DropSource(self)
def OnLeftUp(self, event):
x, y = event.GetPosition()
wx.CallAfter(self.HandleButtons, x, y)
def OnPaint(self, event):
class DebugVariableGraphic(DebugVariableViewer, FigureCanvas):
def __init__(self, parent, window, items, graph_type):
DebugVariableViewer.__init__(self, window, items)
self.CanvasSize = SIZE_MINI
self.GraphType = graph_type
self.MouseStartPos = None
self.StartCursorTick = None
self.CanvasStartSize = None
self.ContextualButtons = []
self.ContextualButtonsItem = None
self.Figure = matplotlib.figure.Figure(facecolor='w')
self.Figure.subplotpars.update(top=0.95, left=0.1, bottom=0.1, right=0.95)
FigureCanvas.__init__(self, parent, -1, self.Figure)
self.SetWindowStyle(wx.WANTS_CHARS)
self.SetBackgroundColour(wx.WHITE)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_ENTER_WINDOW, self.OnEnter)
self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Bind(wx.EVT_SIZE, self.OnResize)
canvas_size = self.GetCanvasMinSize()
self.SetMinSize(canvas_size)
self.SetDropTarget(DebugVariableDropTarget(self.ParentWindow, self))
self.mpl_connect('button_press_event', self.OnCanvasButtonPressed)
self.mpl_connect('motion_notify_event', self.OnCanvasMotion)
self.mpl_connect('button_release_event', self.OnCanvasButtonReleased)
self.mpl_connect('scroll_event', self.OnCanvasScroll)
for size, bitmap in zip([SIZE_MINI, SIZE_MIDDLE, SIZE_MAXI],
["minimize_graph", "middle_graph", "maximize_graph"]):
self.Buttons.append(GraphButton(0, 0, GetBitmap(bitmap), self.GetOnChangeSizeButton(size)))
GraphButton(0, 0, GetBitmap("export_graph_mini"), self.OnExportGraphButton))
GraphButton(0, 0, GetBitmap("delete_graph"), self.OnCloseButton))
self.RefreshLabelsPosition(canvas_size.height)
def draw(self, drawDC=None):
FigureCanvasAgg.draw(self)
self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
width, height = self.GetSize()
bbox = self.GetAxesBoundingBox()
destDC.SelectObject(self.bitmap)
if self.Highlight == HIGHLIGHT_RESIZE:
destGC.SetPen(HIGHLIGHT_RESIZE_PEN)
destGC.SetBrush(HIGHLIGHT_RESIZE_BRUSH)
destGC.DrawRectangle(0, height - 5, width, 5)
destGC.SetPen(HIGHLIGHT_DROP_PEN)
destGC.SetBrush(HIGHLIGHT_DROP_BRUSH)
if self.Highlight == HIGHLIGHT_LEFT:
destGC.DrawRectangle(bbox.x, bbox.y,
bbox.width / 2, bbox.height)
elif self.Highlight == HIGHLIGHT_RIGHT:
destGC.DrawRectangle(bbox.x + bbox.width / 2, bbox.y,
bbox.width / 2, bbox.height)
self.DrawCommonElements(destGC, self.Buttons + self.ContextualButtons)
self.gui_repaint(drawDC=drawDC)
return self.Buttons + self.ContextualButtons
def PopupContextualButtons(self, item, rect, style=wx.RIGHT):
if self.ContextualButtonsItem is not None and item != self.ContextualButtonsItem:
self.DismissContextualButtons()
if self.ContextualButtonsItem is None:
self.ContextualButtonsItem = item
if self.ContextualButtonsItem.IsForced():
self.ContextualButtons.append(
GraphButton(0, 0, GetBitmap("release"), self.OnReleaseButton))
self.ContextualButtons.append(
GraphButton(0, 0, GetBitmap("force"), self.OnForceButton))
self.ContextualButtons.append(
GraphButton(0, 0, GetBitmap("export_graph_mini"), self.OnExportItemGraphButton))
self.ContextualButtons.append(
GraphButton(0, 0, GetBitmap("delete_graph"), self.OnRemoveItemButton))
buttons = self.ContextualButtons[:]
if style in [wx.TOP, wx.LEFT]:
if style in [wx.LEFT, wx.RIGHT]:
x = rect.x + rect.width + offset
y = rect.y + (rect.height - h) / 2
x = rect.x + (rect.width - w ) / 2
y = rect.y + rect.height + offset
self.ParentWindow.ForceRefresh()
def DismissContextualButtons(self):
if self.ContextualButtonsItem is not None:
self.ContextualButtonsItem = None
self.ContextualButtons = []
self.ParentWindow.ForceRefresh()
def IsOverContextualButton(self, x, y):
for button in self.ContextualButtons:
def SetMinSize(self, size):
wx.Window.SetMinSize(self, size)
wx.CallAfter(self.RefreshButtonsState)
def GetOnChangeSizeButton(self, size):
def OnChangeSizeButton():
self.SetCanvasSize(200, self.CanvasSize)
return OnChangeSizeButton
def OnExportGraphButton(self):
wx.CallAfter(self.ForceValue,
self.ContextualButtonsItem)
self.DismissContextualButtons()
def OnReleaseButton(self):
wx.CallAfter(self.ReleaseValue,
self.ContextualButtonsItem)
self.DismissContextualButtons()
def OnExportItemGraphButton(self):
wx.CallAfter(self.ExportGraph,
self.ContextualButtonsItem)
self.DismissContextualButtons()
def OnRemoveItemButton(self):
wx.CallAfter(self.ParentWindow.DeleteValue, self,
self.ContextualButtonsItem)
self.DismissContextualButtons()
def RefreshLabelsPosition(self, height):
canvas_ratio = 1. / height
graph_ratio = 1. / ((1.0 - (CANVAS_BORDER[0] + CANVAS_BORDER[1]) * canvas_ratio) * height)
self.Figure.subplotpars.update(
top= 1.0 - CANVAS_BORDER[1] * canvas_ratio,
bottom= CANVAS_BORDER[0] * canvas_ratio)
if self.GraphType == GRAPH_PARALLEL or self.Is3DCanvas():
num_item = len(self.Items)
for idx in xrange(num_item):
if not self.Is3DCanvas():
self.AxesLabels[idx].set_position(
1.0 - (CANVAS_PADDING + AXES_LABEL_HEIGHT * idx) * graph_ratio))
self.Labels[idx].set_position(
CANVAS_PADDING * graph_ratio +
(num_item - idx - 1) * VALUE_LABEL_HEIGHT * graph_ratio))
self.AxesLabels[0].set_position((0.1, CANVAS_PADDING * graph_ratio))
self.Labels[0].set_position((0.95, CANVAS_PADDING * graph_ratio))
self.AxesLabels[1].set_position((0.05, 2 * CANVAS_PADDING * graph_ratio))
self.Labels[1].set_position((0.05, 1.0 - CANVAS_PADDING * graph_ratio))
self.Figure.subplots_adjust()
def GetCanvasMinSize(self):
CANVAS_BORDER[0] + CANVAS_BORDER[1] +
2 * CANVAS_PADDING + VALUE_LABEL_HEIGHT * len(self.Items))
def SetCanvasSize(self, width, height):
height = max(height, self.GetCanvasMinSize()[1])
self.SetMinSize(wx.Size(width, height))
self.RefreshLabelsPosition(height)
self.RefreshButtonsState()
self.ParentWindow.RefreshGraphicsSizer()
def GetAxesBoundingBox(self, absolute=False):
width, height = self.GetSize()
ax, ay, aw, ah = self.figure.gca().get_position().bounds
bbox = wx.Rect(ax * width, height - (ay + ah) * height - 1,
aw * width + 2, ah * height + 1)
xw, yw = self.GetPosition()
def OnCanvasButtonPressed(self, event):
width, height = self.GetSize()
x, y = event.x, height - event.y
if not self.IsOverButton(x, y):
if event.inaxes == self.Axes:
for i, t in ([pair for pair in enumerate(self.AxesLabels)] +
[pair for pair in enumerate(self.Labels)]):
(x0, y0), (x1, y1) = t.get_window_extent().get_points()
rect = wx.Rect(x0, height - y1, x1 - x0, y1 - y0)
self.DismissContextualButtons()
xw, yw = self.GetPosition()
self.ParentWindow.StartDragNDrop(self,
self.Items[item_idx], x + xw, y + yw, x + xw, y + yw)
elif not self.Is3DCanvas():
self.MouseStartPos = wx.Point(x, y)
if event.button == 1 and event.inaxes == self.Axes:
self.StartCursorTick = self.CursorTick
self.HandleCursorMove(event)
elif event.button == 2 and self.GraphType == GRAPH_PARALLEL:
width, height = self.GetSize()
start_tick, end_tick = self.ParentWindow.GetRange()
self.StartCursorTick = start_tick
elif event.button == 1 and event.y <= 5:
self.MouseStartPos = wx.Point(x, y)
self.CanvasStartSize = self.GetSize()
def OnCanvasButtonReleased(self, event):
if self.ParentWindow.IsDragging():
width, height = self.GetSize()
xw, yw = self.GetPosition()
self.ParentWindow.StopDragNDrop(
self.ParentWindow.DraggingAxesPanel.Items[0].GetVariable(),
self.MouseStartPos = None
self.StartCursorTick = None
self.CanvasStartSize = None
width, height = self.GetSize()
self.HandleButtons(event.x, height - event.y)
if event.y > 5 and self.SetHighlight(HIGHLIGHT_NONE):
self.SetCursor(wx.NullCursor)
self.ParentWindow.ForceRefresh()
def OnCanvasMotion(self, event):
width, height = self.GetSize()
if self.ParentWindow.IsDragging():
xw, yw = self.GetPosition()
self.ParentWindow.MoveDragNDrop(
if not self.Is3DCanvas():
if event.button == 1 and self.CanvasStartSize is None:
if event.inaxes == self.Axes:
if self.MouseStartPos is not None:
self.HandleCursorMove(event)
elif self.MouseStartPos is not None and len(self.Items) == 1:
xw, yw = self.GetPosition()
self.ParentWindow.SetCursorTick(self.StartCursorTick)
self.ParentWindow.StartDragNDrop(self,
event.x + xw, height - event.y + yw,
self.MouseStartPos.x + xw, self.MouseStartPos.y + yw)
elif event.button == 2 and self.GraphType == GRAPH_PARALLEL:
start_tick, end_tick = self.ParentWindow.GetRange()
rect = self.GetAxesBoundingBox()
self.ParentWindow.SetCanvasPosition(
self.StartCursorTick + (self.MouseStartPos.x - event.x) *
(end_tick - start_tick) / rect.width)
if event.button == 1 and self.CanvasStartSize is not None:
width, height = self.GetSize()
self.SetCanvasSize(width,
self.CanvasStartSize.height + height - event.y - self.MouseStartPos.y)
elif event.button in [None, "up", "down"]:
if self.GraphType == GRAPH_PARALLEL:
orientation = [wx.RIGHT] * len(self.AxesLabels) + [wx.LEFT] * len(self.Labels)
elif len(self.AxesLabels) > 0:
orientation = [wx.RIGHT, wx.TOP, wx.LEFT, wx.BOTTOM]
orientation = [wx.LEFT] * len(self.Labels)
for (i, t), style in zip([pair for pair in enumerate(self.AxesLabels)] +
[pair for pair in enumerate(self.Labels)],
(x0, y0), (x1, y1) = t.get_window_extent().get_points()
rect = wx.Rect(x0, height - y1, x1 - x0, y1 - y0)
if rect.InsideXY(event.x, height - event.y):
self.PopupContextualButtons(self.Items[item_idx], rect, item_style)
if not self.IsOverContextualButton(event.x, height - event.y):
self.DismissContextualButtons()
if self.SetHighlight(HIGHLIGHT_RESIZE):
self.SetCursor(wx.StockCursor(wx.CURSOR_SIZENS))
self.ParentWindow.ForceRefresh()
if self.SetHighlight(HIGHLIGHT_NONE):
self.SetCursor(wx.NullCursor)
self.ParentWindow.ForceRefresh()
def OnCanvasScroll(self, event):
if event.inaxes is not None and event.guiEvent.ControlDown():
if self.GraphType == GRAPH_ORTHOGONAL:
start_tick, end_tick = self.ParentWindow.GetRange()
tick = (start_tick + end_tick) / 2.
self.ParentWindow.ChangeRange(int(-event.step) / 3, tick)
self.ParentWindow.VetoScrollEvent = True
def OnDragging(self, x, y):
width, height = self.GetSize()
bbox = self.GetAxesBoundingBox()
if bbox.InsideXY(x, y) and not self.Is3DCanvas():
rect = wx.Rect(bbox.x, bbox.y, bbox.width / 2, bbox.height)
self.SetHighlight(HIGHLIGHT_LEFT)
self.SetHighlight(HIGHLIGHT_RIGHT)
if self.ParentWindow.IsViewerFirst(self):
self.SetHighlight(HIGHLIGHT_BEFORE)
self.SetHighlight(HIGHLIGHT_NONE)
self.ParentWindow.HighlightPreviousViewer(self)
self.SetHighlight(HIGHLIGHT_AFTER)
def OnLeave(self, event):
if self.CanvasStartSize is None and self.SetHighlight(HIGHLIGHT_NONE):
self.SetCursor(wx.NullCursor)
self.ParentWindow.ForceRefresh()
DebugVariableViewer.OnLeave(self, event)
def OnKeyDown(self, event):
if self.CursorTick is not None:
move = self.KEY_CURSOR_INCREMENT.get(event.GetKeyCode(), None)
self.ParentWindow.MoveCursorTick(move)
def HandleCursorMove(self, event):
start_tick, end_tick = self.ParentWindow.GetRange()
if self.GraphType == GRAPH_ORTHOGONAL:
x_data = self.Items[0].GetData(start_tick, end_tick)
y_data = self.Items[1].GetData(start_tick, end_tick)
if len(x_data) > 0 and len(y_data) > 0:
length = min(len(x_data), len(y_data))
d = numpy.sqrt((x_data[:length,1]-event.xdata) ** 2 + (y_data[:length,1]-event.ydata) ** 2)
cursor_tick = x_data[numpy.argmin(d), 0]
data = self.Items[0].GetData(start_tick, end_tick)
cursor_tick = data[numpy.argmin(numpy.abs(data[:,0] - event.xdata)), 0]
if cursor_tick is not None:
self.ParentWindow.SetCursorTick(cursor_tick)
def OnAxesMotion(self, event):
if current_time - self.LastMotionTime > REFRESH_PERIOD:
self.LastMotionTime = current_time
Axes3D._on_move(self.Axes, event)
self.Axes = self.Figure.gca(projection='3d')
self.Axes.set_color_cycle(['b'])
self.LastMotionTime = gettime()
setattr(self.Axes, "_on_move", self.OnAxesMotion)
self.Axes.tick_params(axis='z', labelsize='small')
self.Axes = self.Figure.gca()
self.Axes.set_color_cycle(color_cycle)
self.Axes.tick_params(axis='x', labelsize='small')
self.Axes.tick_params(axis='y', labelsize='small')
if not self.Is3DCanvas():
text_func = self.Axes.text
text_func = self.Axes.text2D
if self.GraphType == GRAPH_PARALLEL or self.Is3DCanvas():
num_item = len(self.Items)
for idx in xrange(num_item):
color = color_cycle[idx % len(color_cycle)]
if not self.Is3DCanvas():
text_func(0, 0, "", size='small',
transform=self.Axes.transAxes))
text_func(0, 0, "", size='large',
horizontalalignment='right',
transform=self.Axes.transAxes))
self.Axes.text(0, 0, "", size='small',
transform=self.Axes.transAxes))
self.Axes.text(0, 0, "", size='large',
horizontalalignment='right',
transform=self.Axes.transAxes))
self.Axes.text(0, 0, "", size='small',
verticalalignment='bottom',
transform=self.Axes.transAxes))
self.Axes.text(0, 0, "", size='large',
transform=self.Axes.transAxes))
width, height = self.GetSize()
self.RefreshLabelsPosition(height)
DebugVariableViewer.AddItem(self, item)
def RemoveItem(self, item):
DebugVariableViewer.RemoveItem(self, item)
self.GraphType = GRAPH_PARALLEL
def UnregisterObsoleteData(self):
DebugVariableViewer.UnregisterObsoleteData(self)
return self.GraphType == GRAPH_ORTHOGONAL and len(self.Items) == 3
def SetCursorTick(self, cursor_tick):
self.CursorTick = cursor_tick
def RefreshViewer(self, refresh_graphics=True):
start_tick, end_tick = self.ParentWindow.GetRange()
if self.GraphType == GRAPH_PARALLEL:
min_value = max_value = None
for idx, item in enumerate(self.Items):
data = item.GetData(start_tick, end_tick)
item_min_value, item_max_value = item.GetValueRange()
min_value = item_min_value
elif item_min_value is not None:
min_value = min(min_value, item_min_value)
max_value = item_max_value
elif item_max_value is not None:
max_value = max(max_value, item_max_value)
if len(self.Plots) <= idx:
self.Axes.plot(data[:, 0], data[:, 1])[0])
self.Plots[idx].set_data(data[:, 0], data[:, 1])
if min_value is not None and max_value is not None:
y_center = (min_value + max_value) / 2.
y_range = max(1.0, max_value - min_value)
x_min, x_max = start_tick, end_tick
y_min, y_max = y_center - y_range * 0.55, y_center + y_range * 0.55
if self.CursorTick is not None and start_tick <= self.CursorTick <= end_tick:
self.VLine = self.Axes.axvline(self.CursorTick, color=cursor_color)
self.VLine.set_xdata((self.CursorTick, self.CursorTick))
self.VLine.set_visible(True)
if self.VLine is not None:
self.VLine.set_visible(False)
min_start_tick = reduce(max, [item.GetData()[0, 0]
if len(item.GetData()) > 0], 0)
start_tick = max(start_tick, min_start_tick)
end_tick = max(end_tick, min_start_tick)
x_data, x_min, x_max = OrthogonalData(self.Items[0], start_tick, end_tick)
y_data, y_min, y_max = OrthogonalData(self.Items[1], start_tick, end_tick)
if self.CursorTick is not None:
x_cursor, x_forced = self.Items[0].GetValue(self.CursorTick, raw=True)
y_cursor, y_forced = self.Items[1].GetValue(self.CursorTick, raw=True)
if x_data is not None and y_data is not None:
length = min(len(x_data), len(y_data))
if x_data is not None and y_data is not None:
self.Axes.plot(x_data[:, 1][:length],
y_data[:, 1][:length])[0])
if self.CursorTick is not None and start_tick <= self.CursorTick <= end_tick:
self.VLine = self.Axes.axvline(x_cursor, color=cursor_color)
self.VLine.set_xdata((x_cursor, x_cursor))
self.HLine = self.Axes.axhline(y_cursor, color=cursor_color)
self.HLine.set_ydata((y_cursor, y_cursor))
self.VLine.set_visible(True)
self.HLine.set_visible(True)
if self.VLine is not None:
self.VLine.set_visible(False)
if self.HLine is not None:
self.HLine.set_visible(False)
while len(self.Axes.lines) > 0:
z_data, z_min, z_max = OrthogonalData(self.Items[2], start_tick, end_tick)
if self.CursorTick is not None:
z_cursor, z_forced = self.Items[2].GetValue(self.CursorTick, raw=True)
if x_data is not None and y_data is not None and z_data is not None:
length = min(length, len(z_data))
self.Axes.plot(x_data[:, 1][:length],
zs = z_data[:, 1][:length])
self.Axes.set_zlim(z_min, z_max)
if self.CursorTick is not None and start_tick <= self.CursorTick <= end_tick:
for kwargs in [{"xs": numpy.array([x_min, x_max])},
{"ys": numpy.array([y_min, y_max])},
{"zs": numpy.array([z_min, z_max])}]:
for param, value in [("xs", numpy.array([x_cursor, x_cursor])),
("ys", numpy.array([y_cursor, y_cursor])),
("zs", numpy.array([z_cursor, z_cursor]))]:
kwargs.setdefault(param, value)
kwargs["color"] = cursor_color
self.Axes.set_xlim(x_min, x_max)
self.Axes.set_ylim(y_min, y_max)
variable_name_mask = self.ParentWindow.GetVariableNameMask()
if self.CursorTick is not None:
values, forced = apply(zip, [item.GetValue(self.CursorTick) for item in self.Items])
values, forced = apply(zip, [(item.GetValue(), item.IsForced()) for item in self.Items])
labels = [item.GetVariable(variable_name_mask) for item in self.Items]
styles = map(lambda x: {True: 'italic', False: 'normal'}[x], forced)
for idx, label_func in enumerate([self.Axes.set_xlabel,
label_func(labels[idx], fontdict={'size': 'small','color': color_cycle[idx]})
for label, text in zip(self.AxesLabels, labels):
for label, value, style in zip(self.Labels, values, styles):
def ExportGraph(self, item=None):
variables = [(item, [entry for entry in item.GetData()])]
variables = [(item, [entry for entry in item.GetData()])
self.ParentWindow.CopyDataToClipboard(variables)
class DebugVariablePanel(wx.Panel, DebugViewer):
def __init__(self, parent, producer, window):
wx.Panel.__init__(self, parent, style=wx.SP_3D|wx.TAB_TRAVERSAL)
self.ParentWindow = window
self.SetBackgroundColour(wx.WHITE)
main_sizer = wx.BoxSizer(wx.VERTICAL)
self.Ticks = numpy.array([])
self.DraggingAxesPanel = None
self.DraggingAxesBoundingBox = None
self.DraggingAxesMousePos = None
self.VetoScrollEvent = False
self.VariableNameMask = []
graphics_button_sizer = wx.BoxSizer(wx.HORIZONTAL)
main_sizer.AddSizer(graphics_button_sizer, border=5, flag=wx.GROW|wx.ALL)
range_label = wx.StaticText(self, label=_('Range:'))
graphics_button_sizer.AddWindow(range_label, flag=wx.ALIGN_CENTER_VERTICAL)
self.CanvasRange = wx.ComboBox(self, style=wx.CB_READONLY)
self.Bind(wx.EVT_COMBOBOX, self.OnRangeChanged, self.CanvasRange)
graphics_button_sizer.AddWindow(self.CanvasRange, 1,
border=5, flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL)
for idx, (text, value) in enumerate(RANGE_VALUES):
self.CanvasRange.Append(text)
self.CanvasRange.SetSelection(default_range_idx)
for name, bitmap, help in [
("CurrentButton", "current", _("Go to current value")),
("ExportGraphButton", "export_graph", _("Export graph values to clipboard"))]:
button = wx.lib.buttons.GenBitmapButton(self,
bitmap=GetBitmap(bitmap),
size=wx.Size(28, 28), style=wx.NO_BORDER)
button.SetToolTipString(help)
setattr(self, name, button)
self.Bind(wx.EVT_BUTTON, getattr(self, "On" + name), button)
graphics_button_sizer.AddWindow(button, border=5, flag=wx.LEFT)
self.CanvasPosition = wx.ScrollBar(self,
size=wx.Size(0, 16), style=wx.SB_HORIZONTAL)
self.CanvasPosition.Bind(wx.EVT_SCROLL_THUMBTRACK,
self.OnPositionChanging, self.CanvasPosition)
self.CanvasPosition.Bind(wx.EVT_SCROLL_LINEUP,
self.OnPositionChanging, self.CanvasPosition)
self.CanvasPosition.Bind(wx.EVT_SCROLL_LINEDOWN,
self.OnPositionChanging, self.CanvasPosition)
self.CanvasPosition.Bind(wx.EVT_SCROLL_PAGEUP,
self.OnPositionChanging, self.CanvasPosition)
self.CanvasPosition.Bind(wx.EVT_SCROLL_PAGEDOWN,
self.OnPositionChanging, self.CanvasPosition)
main_sizer.AddWindow(self.CanvasPosition, border=5, flag=wx.GROW|wx.LEFT|wx.RIGHT|wx.BOTTOM)
self.TickSizer = wx.BoxSizer(wx.HORIZONTAL)
main_sizer.AddSizer(self.TickSizer, border=5, flag=wx.ALL|wx.GROW)
self.TickLabel = wx.StaticText(self)
self.TickSizer.AddWindow(self.TickLabel, border=5, flag=wx.RIGHT)
self.MaskLabel = wx.TextCtrl(self, style=wx.TE_READONLY|wx.TE_CENTER|wx.NO_BORDER)
self.TickSizer.AddWindow(self.MaskLabel, 1, border=5, flag=wx.RIGHT|wx.GROW)
self.TickTimeLabel = wx.StaticText(self)
self.TickSizer.AddWindow(self.TickTimeLabel)
self.GraphicsWindow = wx.ScrolledWindow(self, style=wx.HSCROLL|wx.VSCROLL)
self.GraphicsWindow.SetBackgroundColour(wx.WHITE)
self.GraphicsWindow.SetDropTarget(DebugVariableDropTarget(self))
self.GraphicsWindow.Bind(wx.EVT_ERASE_BACKGROUND, self.OnGraphicsWindowEraseBackground)
self.GraphicsWindow.Bind(wx.EVT_PAINT, self.OnGraphicsWindowPaint)
self.GraphicsWindow.Bind(wx.EVT_SIZE, self.OnGraphicsWindowResize)
self.GraphicsWindow.Bind(wx.EVT_MOUSEWHEEL, self.OnGraphicsWindowMouseWheel)
main_sizer.AddWindow(self.GraphicsWindow, 1, flag=wx.GROW)
self.GraphicsSizer = wx.BoxSizer(wx.VERTICAL)
self.GraphicsWindow.SetSizer(self.GraphicsSizer)
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
main_sizer.AddGrowableCol(0)
main_sizer.AddGrowableRow(1)
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
main_sizer.AddSizer(button_sizer, border=5,
flag=wx.ALIGN_RIGHT|wx.ALL)
for name, bitmap, help in [
("DeleteButton", "remove_element", _("Remove debug variable")),
("UpButton", "up", _("Move debug variable up")),
("DownButton", "down", _("Move debug variable down"))]:
button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap),
size=wx.Size(28, 28), style=wx.NO_BORDER)
button.SetToolTipString(help)
setattr(self, name, button)
button_sizer.AddWindow(button, border=5, flag=wx.LEFT)
self.VariablesGrid = CustomGrid(self, size=wx.Size(-1, 150), style=wx.VSCROLL)
self.VariablesGrid.SetDropTarget(DebugVariableDropTarget(self, self.VariablesGrid))
self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,
self.OnVariablesGridCellRightClick)
self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK,
self.OnVariablesGridCellLeftClick)
main_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW)
self.Table = DebugVariableTable(self, [], GetDebugVariablesTableColnames())
self.VariablesGrid.SetTable(self.Table)
self.VariablesGrid.SetButtons({"Delete": self.DeleteButton,
"Down": self.DownButton})
def _AddVariable(new_row):
return self.VariablesGrid.GetGridCursorRow()
setattr(self.VariablesGrid, "_AddRow", _AddVariable)
def _DeleteVariable(row):
item = self.Table.GetItem(row)
self.RemoveDataConsumer(item)
self.Table.RemoveItem(row)
setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable)
def _MoveVariable(row, move):
new_row = max(0, min(row + move, self.Table.GetNumberRows() - 1))
self.Table.MoveItem(row, new_row)
setattr(self.VariablesGrid, "_MoveRow", _MoveVariable)
self.VariablesGrid.SetRowLabelSize(0)
self.GridColSizes = [200, 100]
for col in range(self.Table.GetNumberCols()):
attr = wx.grid.GridCellAttr()
attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTER)
self.VariablesGrid.SetColAttr(col, attr)
self.VariablesGrid.SetColSize(col, self.GridColSizes[col])
self.Table.ResetView(self.VariablesGrid)
self.VariablesGrid.RefreshButtons()
DebugViewer.__init__(self, producer, True)
self.SetSizer(main_sizer)
def SetTickTime(self, ticktime=0):
self.Ticktime = MILLISECOND
self.CurrentRange = RANGE_VALUES[
self.CanvasRange.GetSelection()][1] / self.Ticktime
def SetDataProducer(self, producer):
DebugViewer.SetDataProducer(self, producer)
if self.DataProducer is not None:
self.SetTickTime(self.DataProducer.GetTicktime())
def RefreshNewData(self, *args, **kwargs):
if self.HasNewData or self.Force:
self.RefreshView(only_values=True)
DebugViewer.RefreshNewData(self, *args, **kwargs)
def NewDataAvailable(self, tick, *args, **kwargs):
if USE_MPL and tick is not None:
self.Ticks = numpy.append(self.Ticks, [tick])
if not self.Fixed or tick < self.StartTick + self.CurrentRange:
self.StartTick = max(self.StartTick, tick - self.CurrentRange)
if self.Fixed and self.Ticks[-1] - self.Ticks[0] < self.CurrentRange:
DebugViewer.NewDataAvailable(self, tick, *args, **kwargs)
wx.CallAfter(self.NewDataAvailable, None, True)
def RefreshGraphicsSizer(self):
self.GraphicsSizer.Clear()
for panel in self.GraphicPanels:
self.GraphicsSizer.AddWindow(panel, flag=wx.GROW)
self.GraphicsSizer.Layout()
self.RefreshGraphicsWindowScrollbars()
def SetCanvasPosition(self, tick):
tick = max(self.Ticks[0], min(tick, self.Ticks[-1] - self.CurrentRange))
self.StartTick = self.Ticks[numpy.argmin(numpy.abs(self.Ticks - tick))]
self.RefreshCanvasPosition()
def SetCursorTick(self, cursor_tick):
self.CursorTick = cursor_tick
def MoveCursorTick(self, move):
if self.CursorTick is not None:
cursor_tick = max(self.Ticks[0],
min(self.CursorTick + move,
cursor_tick_idx = numpy.argmin(numpy.abs(self.Ticks - cursor_tick))
if self.Ticks[cursor_tick_idx] == self.CursorTick:
min(cursor_tick_idx + abs(move) / move,
self.CursorTick = self.Ticks[cursor_tick_idx]
self.StartTick = max(self.Ticks[
numpy.argmin(numpy.abs(self.Ticks -
self.CursorTick + self.CurrentRange))],
min(self.StartTick, self.CursorTick))
self.RefreshCanvasPosition()
def ResetCursorTick(self):
def UpdateCursorTick(self):
for panel in self.GraphicPanels:
if isinstance(panel, DebugVariableGraphic):
panel.SetCursorTick(self.CursorTick)
def StartDragNDrop(self, panel, item, x_mouse, y_mouse, x_mouse_start, y_mouse_start):
if len(panel.GetItems()) > 1:
self.DraggingAxesPanel = DebugVariableGraphic(self.GraphicsWindow, self, [item], GRAPH_PARALLEL)
self.DraggingAxesPanel.SetCursorTick(self.CursorTick)
width, height = panel.GetSize()
self.DraggingAxesPanel.SetSize(wx.Size(width, height))
self.DraggingAxesPanel.ResetGraphics()
self.DraggingAxesPanel.SetPosition(wx.Point(0, -height))
self.DraggingAxesPanel = panel
self.DraggingAxesBoundingBox = panel.GetAxesBoundingBox(absolute=True)
self.DraggingAxesMousePos = wx.Point(
x_mouse_start - self.DraggingAxesBoundingBox.x,
y_mouse_start - self.DraggingAxesBoundingBox.y)
self.MoveDragNDrop(x_mouse, y_mouse)
def MoveDragNDrop(self, x_mouse, y_mouse):
self.DraggingAxesBoundingBox.x = x_mouse - self.DraggingAxesMousePos.x
self.DraggingAxesBoundingBox.y = y_mouse - self.DraggingAxesMousePos.y
self.RefreshHighlight(x_mouse, y_mouse)
def RefreshHighlight(self, x_mouse, y_mouse):
for idx, panel in enumerate(self.GraphicPanels):
x, y = panel.GetPosition()
width, height = panel.GetSize()
rect = wx.Rect(x, y, width, height)
if (rect.InsideXY(x_mouse, y_mouse) or
idx == 0 and y_mouse < 0 or
idx == len(self.GraphicPanels) - 1 and y_mouse > panel.GetPosition()[1]):
panel.OnDragging(x_mouse - x, y_mouse - y)
panel.SetHighlight(HIGHLIGHT_NONE)
if wx.Platform == "__WXMSW__":
def ResetHighlight(self):
for panel in self.GraphicPanels:
panel.SetHighlight(HIGHLIGHT_NONE)
if wx.Platform == "__WXMSW__":
return self.DraggingAxesPanel is not None
def GetDraggingAxesClippingRegion(self, panel):
x, y = panel.GetPosition()
width, height = panel.GetSize()
bbox = wx.Rect(x, y, width, height)
bbox = bbox.Intersect(self.DraggingAxesBoundingBox)
def GetDraggingAxesPosition(self, panel):
x, y = panel.GetPosition()
return wx.Point(self.DraggingAxesBoundingBox.x - x,
self.DraggingAxesBoundingBox.y - y)
def StopDragNDrop(self, variable, x_mouse, y_mouse):
if self.DraggingAxesPanel not in self.GraphicPanels:
self.DraggingAxesPanel.Destroy()
self.DraggingAxesPanel = None
self.DraggingAxesBoundingBox = None
self.DraggingAxesMousePos = None
for idx, panel in enumerate(self.GraphicPanels):
panel.SetHighlight(HIGHLIGHT_NONE)
xw, yw = panel.GetPosition()
width, height = panel.GetSize()
bbox = wx.Rect(xw, yw, width, height)
if bbox.InsideXY(x_mouse, y_mouse):
merge_type = GRAPH_PARALLEL
if isinstance(panel, DebugVariableText) or panel.Is3DCanvas():
if y_mouse > yw + height / 2:
wx.CallAfter(self.MoveValue, variable, idx)
rect = panel.GetAxesBoundingBox(True)
if rect.InsideXY(x_mouse, y_mouse):
merge_rect = wx.Rect(rect.x, rect.y, rect.width / 2., rect.height)
if merge_rect.InsideXY(x_mouse, y_mouse):
merge_type = GRAPH_ORTHOGONAL
wx.CallAfter(self.MergeGraphs, variable, idx, merge_type, force=True)
if y_mouse > yw + height / 2:
wx.CallAfter(self.MoveValue, variable, idx)
width, height = self.GraphicsWindow.GetVirtualSize()
rect = wx.Rect(0, 0, width, height)
if rect.InsideXY(x_mouse, y_mouse):
wx.CallAfter(self.MoveValue, variable, len(self.GraphicPanels))
def RefreshView(self, only_values=False):
self.RefreshCanvasPosition()
width, height = self.GraphicsWindow.GetVirtualSize()
bitmap = wx.EmptyBitmap(width, height)
dc = wx.BufferedDC(wx.ClientDC(self.GraphicsWindow), bitmap)
if self.DraggingAxesPanel is not None:
destBBox = self.DraggingAxesBoundingBox
srcBBox = self.DraggingAxesPanel.GetAxesBoundingBox()
srcBmp = _convert_agg_to_wx_bitmap(self.DraggingAxesPanel.get_renderer(), None)
srcDC.SelectObject(srcBmp)
dc.Blit(destBBox.x, destBBox.y,
int(destBBox.width), int(destBBox.height),
srcDC, srcBBox.x, srcBBox.y)
if not self.Fixed or self.Force:
if self.DraggingAxesPanel is not None and self.DraggingAxesPanel not in self.GraphicPanels:
self.DraggingAxesPanel.RefreshViewer(refresh_graphics)
for panel in self.GraphicPanels:
if isinstance(panel, DebugVariableGraphic):
panel.RefreshViewer(refresh_graphics)
if self.CursorTick is not None:
elif len(self.Ticks) > 0:
self.TickLabel.SetLabel("Tick: %d" % tick)
tick_duration = int(tick * self.Ticktime)
for value, format in [(tick_duration / DAY, "%dd"),
((tick_duration % DAY) / HOUR, "%dh"),
((tick_duration % HOUR) / MINUTE, "%dm"),
((tick_duration % MINUTE) / SECOND, "%ds")]:
if value > 0 or not_null:
duration += format % value
duration += "%gms" % (float(tick_duration % SECOND) / MILLISECOND)
self.TickTimeLabel.SetLabel("t: %s" % duration)
self.TickLabel.SetLabel("")
self.TickTimeLabel.SetLabel("")
for col in xrange(self.Table.GetNumberCols()):
if self.Table.GetColLabelValue(col, False) == "Value":
for row in xrange(self.Table.GetNumberRows()):
self.VariablesGrid.SetCellValue(row, col, str(self.Table.GetValueByName(row, "Value")))
self.Table.ResetView(self.VariablesGrid)
self.VariablesGrid.RefreshButtons()
def UnregisterObsoleteData(self):
self.SubscribeAllDataConsumers()
if self.DataProducer is not None:
if self.DataProducer is not None:
self.SetTickTime(self.DataProducer.GetTicktime())
for panel in self.GraphicPanels:
panel.UnregisterObsoleteData()
self.GraphicPanels.remove(panel)
self.ResetVariableNameMask()
self.RefreshGraphicsSizer()
items = [(idx, item) for idx, item in enumerate(self.Table.GetData())]
iec_path = item.GetVariable()
if self.GetDataType(iec_path) is None:
self.RemoveDataConsumer(item)
self.Table.RemoveItem(idx)
self.AddDataConsumer(iec_path.upper(), item)
item.RefreshVariableType()
self.Table.ResetView(self.VariablesGrid)
self.VariablesGrid.RefreshButtons()
self.UnsubscribeAllDataConsumers()
for panel in self.GraphicPanels:
self.ResetVariableNameMask()
self.RefreshGraphicsSizer()
self.Table.ResetView(self.VariablesGrid)
self.VariablesGrid.RefreshButtons()
def RefreshCanvasPosition(self):
pos = int(self.StartTick - self.Ticks[0])
range = int(self.Ticks[-1] - self.Ticks[0])
self.CanvasPosition.SetScrollbar(pos, self.CurrentRange, range, self.CurrentRange)
def GetForceVariableMenuFunction(self, iec_path, item):
iec_type = self.GetDataType(iec_path)
def ForceVariableFunction(event):
dialog = ForceVariableDialog(self, iec_type, str(item.GetValue()))
if dialog.ShowModal() == wx.ID_OK:
self.ForceDataValue(iec_path.upper(), dialog.GetValue())
return ForceVariableFunction
def GetReleaseVariableMenuFunction(self, iec_path):
def ReleaseVariableFunction(event):
self.ReleaseDataValue(iec_path)
return ReleaseVariableFunction
def OnVariablesGridCellLeftClick(self, event):
data = wx.TextDataObject(str((self.Table.GetValueByName(row, "Variable"), "debug")))
dragSource = wx.DropSource(self.VariablesGrid)
def OnVariablesGridCellRightClick(self, event):
row, col = event.GetRow(), event.GetCol()
if self.Table.GetColLabelValue(col, False) == "Value":
iec_path = self.Table.GetValueByName(row, "Variable").upper()
AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Force value"))
self.Bind(wx.EVT_MENU, self.GetForceVariableMenuFunction(iec_path.upper(), self.Table.GetItem(row)), id=new_id)
AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Release value"))
self.Bind(wx.EVT_MENU, self.GetReleaseVariableMenuFunction(iec_path.upper()), id=new_id)
if self.Table.IsForced(row):
menu.Enable(new_id, True)
menu.Enable(new_id, False)
def ChangeRange(self, dir, tick=None):
current_range = self.CurrentRange
current_range_idx = self.CanvasRange.GetSelection()
new_range_idx = max(0, min(current_range_idx + dir, len(RANGE_VALUES) - 1))
if new_range_idx != current_range_idx:
self.CanvasRange.SetSelection(new_range_idx)
self.CurrentRange = RANGE_VALUES[new_range_idx][1] / self.Ticktime
tick = self.StartTick + self.CurrentRange / 2.
new_start_tick = tick - (tick - self.StartTick) * self.CurrentRange / current_range
self.StartTick = self.Ticks[numpy.argmin(numpy.abs(self.Ticks - new_start_tick))]
self.Fixed = self.StartTick < self.Ticks[-1] - self.CurrentRange
if self.Fixed and self.Ticks[-1] - self.Ticks[0] < self.CurrentRange:
self.StartTick = min(self.StartTick, self.Ticks[-1] - self.CurrentRange)
self.StartTick = max(self.Ticks[0], self.Ticks[-1] - self.CurrentRange)
def OnRangeChanged(self, event):
self.CurrentRange = RANGE_VALUES[self.CanvasRange.GetSelection()][1] / self.Ticktime
self.CanvasRange.SetValue(str(self.CurrentRange))
wx.CallAfter(self.RefreshRange)
def OnCurrentButton(self, event):
self.StartTick = max(self.Ticks[0], self.Ticks[-1] - self.CurrentRange)
def CopyDataToClipboard(self, variables):
text = "tick;%s;\n" % ";".join([item.GetVariable() for item, data in variables])
next_tick = NextTick(variables)
while next_tick is not None:
for item, data in variables:
if next_tick == data[0][0]:
var_type = item.GetVariableType()
if var_type in ["STRING", "WSTRING"]:
value = item.GetRawValue(int(data.pop(0)[2]))
values.append("'%s'" % value)
values.append('"%s"' % value)
values.append("%.3f" % data.pop(0)[1])
text += "%d;%s;\n" % (next_tick, ";".join(values))
next_tick = NextTick(variables)
self.ParentWindow.SetCopyBuffer(text)
def OnExportGraphButton(self, event):
for panel in self.GraphicPanels:
items.extend(panel.GetItems())
items = self.Table.GetData()
variables.append((item, [entry for entry in item.GetData()]))
wx.CallAfter(self.CopyDataToClipboard, variables)
def OnPositionChanging(self, event):
self.StartTick = self.Ticks[0] + event.GetPosition()
return self.StartTick, self.StartTick + self.CurrentRange
def GetViewerIndex(self, viewer):
if viewer in self.GraphicPanels:
return self.GraphicPanels.index(viewer)
def IsViewerFirst(self, viewer):
return viewer == self.GraphicPanels[0]
def IsViewerLast(self, viewer):
return viewer == self.GraphicPanels[-1]
def HighlightPreviousViewer(self, viewer):
if self.IsViewerFirst(viewer):
idx = self.GetViewerIndex(viewer)
self.GraphicPanels[idx-1].SetHighlight(HIGHLIGHT_AFTER)
def ResetVariableNameMask(self):
for panel in self.GraphicPanels:
items.extend(panel.GetItems())
self.VariableNameMask = reduce(compute_mask,
[item.GetVariable().split('.') for item in items])
self.VariableNameMask = items[0].GetVariable().split('.')[:-1] + ['*']
self.VariableNameMask = []
self.MaskLabel.ChangeValue(".".join(self.VariableNameMask))
self.MaskLabel.SetInsertionPoint(self.MaskLabel.GetLastPosition())
def GetVariableNameMask(self):
return self.VariableNameMask
def InsertValue(self, iec_path, idx = None, force=False):
for panel in self.GraphicPanels:
if panel.GetItem(iec_path) is not None:
idx = len(self.GraphicPanels)
for item in self.Table.GetData():
if iec_path == item.GetVariable():
idx = self.Table.GetNumberRows()
item = DebugVariableItem(self, iec_path, USE_MPL)
result = self.AddDataConsumer(iec_path.upper(), item)
if result is not None or force:
panel = DebugVariableGraphic(self.GraphicsWindow, self, [item], GRAPH_PARALLEL)
if self.CursorTick is not None:
panel.SetCursorTick(self.CursorTick)
panel = DebugVariableText(self.GraphicsWindow, self, [item])
self.GraphicPanels.insert(idx, panel)
self.GraphicPanels.append(panel)
self.ResetVariableNameMask()
self.RefreshGraphicsSizer()
self.Table.InsertItem(idx, item)
def MoveValue(self, iec_path, idx = None):
idx = len(self.GraphicPanels)
for panel in self.GraphicPanels:
item = panel.GetItem(iec_path)
if source_panel is not None:
source_panel.RemoveItem(item)
source_size = source_panel.GetSize()
if source_panel.IsEmpty():
if source_panel.HasCapture():
source_panel.ReleaseMouse()
self.GraphicPanels.remove(source_panel)
panel = DebugVariableGraphic(self.GraphicsWindow, self, [item], GRAPH_PARALLEL)
panel.SetCanvasSize(source_size.width, source_size.height)
if self.CursorTick is not None:
panel.SetCursorTick(self.CursorTick)
panel = DebugVariableText(self.GraphicsWindow, self, [item])
self.GraphicPanels.insert(idx, panel)
self.ResetVariableNameMask()
self.RefreshGraphicsSizer()
def MergeGraphs(self, source, target_idx, merge_type, force=False):
for panel in self.GraphicPanels:
source_item = panel.GetItem(source)
if source_item is not None:
item = DebugVariableItem(self, source, USE_MPL)
result = self.AddDataConsumer(source.upper(), item)
if result is not None or force:
if source_item is not None and source_item.IsNumVariable():
if source_panel is not None:
source_size = source_panel.GetSize()
target_panel = self.GraphicPanels[target_idx]
graph_type = target_panel.GraphType
if target_panel != source_panel:
if (merge_type == GRAPH_PARALLEL and graph_type != merge_type or
merge_type == GRAPH_ORTHOGONAL and
(graph_type == GRAPH_PARALLEL and len(target_panel.Items) > 1 or
graph_type == GRAPH_ORTHOGONAL and len(target_panel.Items) >= 3)):
if source_panel is not None:
source_panel.RemoveItem(source_item)
if source_panel.IsEmpty():
if source_panel.HasCapture():
source_panel.ReleaseMouse()
self.GraphicPanels.remove(source_panel)
elif (merge_type != graph_type and len(target_panel.Items) == 2):
target_panel.RemoveItem(source_item)
if target_panel is not None:
target_panel.AddItem(source_item)
target_panel.GraphType = merge_type
size = target_panel.GetSize()
if merge_type == GRAPH_ORTHOGONAL:
target_panel.SetCanvasSize(size.width, size.width)
elif source_size is not None:
target_panel.SetCanvasSize(size.width, size.height + source_size.height)
target_panel.SetCanvasSize(size.width, size.height)
target_panel.ResetGraphics()
self.ResetVariableNameMask()
self.RefreshGraphicsSizer()
def DeleteValue(self, source_panel, item=None):
source_idx = self.GetViewerIndex(source_panel)
if source_idx is not None:
self.GraphicPanels.remove(source_panel)
self.ResetVariableNameMask()
self.RefreshGraphicsSizer()
source_panel.RemoveItem(item)
if source_panel.IsEmpty():
self.GraphicPanels.remove(source_panel)
self.ResetVariableNameMask()
self.RefreshGraphicsSizer()
def ResetGraphicsValues(self):
self.Ticks = numpy.array([])
for panel in self.GraphicPanels:
def RefreshGraphicsWindowScrollbars(self):
xstart, ystart = self.GraphicsWindow.GetViewStart()
window_size = self.GraphicsWindow.GetClientSize()
vwidth, vheight = self.GraphicsSizer.GetMinSize()
posx = max(0, min(xstart, (vwidth - window_size[0]) / SCROLLBAR_UNIT))
posy = max(0, min(ystart, (vheight - window_size[1]) / SCROLLBAR_UNIT))
self.GraphicsWindow.Scroll(posx, posy)
self.GraphicsWindow.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT,
vwidth / SCROLLBAR_UNIT, vheight / SCROLLBAR_UNIT, posx, posy)
def OnGraphicsWindowEraseBackground(self, event):
def OnGraphicsWindowPaint(self, event):
def OnGraphicsWindowResize(self, event):
for panel in self.GraphicPanels:
panel_size = panel.GetSize()
if (isinstance(panel, DebugVariableGraphic) and
panel.GraphType == GRAPH_ORTHOGONAL and
panel_size.width == panel_size.height):
panel.SetCanvasSize(size.width, size.width)
self.RefreshGraphicsWindowScrollbars()
self.GraphicsSizer.Layout()
def OnGraphicsWindowMouseWheel(self, event):
self.VetoScrollEvent = False