# This file is part of Beremiz, a Integrated Development Environment for
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
# Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
# See COPYING file for copyrights details.
# This program 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
# of the License, or (at your option) any later version.
# This program 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 program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from types import TupleType
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
from editors.DebugViewer import REFRESH_PERIOD
from DebugVariableItem import DebugVariableItem
from DebugVariableViewer import *
from GraphButton import GraphButton
from distutils.version import LooseVersion
if LooseVersion(matplotlib.__version__) >= LooseVersion("1.5.0"):
from cycler import cycler
# Graph variable display type
GRAPH_PARALLEL, GRAPH_ORTHOGONAL = range(2)
[SIZE_MINI, SIZE_MIDDLE, SIZE_MAXI] = [0, 100, 200]
CANVAS_BORDER = (20., 10.) # Border height on at bottom and top of graph
CANVAS_PADDING = 8.5 # Border inside graph where no label is drawn
VALUE_LABEL_HEIGHT = 17. # Height of variable label in graph
AXES_LABEL_HEIGHT = 12.75 # Height of variable value in graph
# Colors used cyclically for graph curves
COLOR_CYCLE = ['r', 'b', 'g', 'm', 'y', 'k']
#-------------------------------------------------------------------------------
# Debug Variable Graphic Viewer Helpers
#-------------------------------------------------------------------------------
def merge_ranges(ranges):
Merge variables data range in a list to return a range of minimal min range
value and maximal max range value extended of 10% for keeping a padding
@param ranges: [(range_min_value, range_max_value),...]
@return: merged_range_min_value, merged_range_max_value
# Get minimal and maximal range value
min_value = max_value = None
for range_min, range_max in ranges:
# Update minimal range value
elif range_min is not None:
min_value = min(min_value, range_min)
# Update maximal range value
elif range_min is not None:
max_value = max(max_value, range_max)
# Calculate range center and width if at least one valid range is defined
if min_value is not None and max_value is not None:
center = (min_value + max_value) / 2.
range_size = max(1.0, max_value - min_value)
# Set default center and with if no valid range is defined
# Return range expended from 10 %
return center - range_size * 0.55, center + range_size * 0.55
#-------------------------------------------------------------------------------
# Debug Variable Graphic Viewer Drop Target
#-------------------------------------------------------------------------------
Class that implements a custom drop target class for Debug Variable Graphic
class DebugVariableGraphicDropTarget(wx.TextDropTarget):
def __init__(self, parent, window):
@param parent: Reference to Debug Variable Graphic Viewer
@param window: Reference to the Debug Variable Panel
wx.TextDropTarget.__init__(self)
self.ParentControl = parent
self.ParentWindow = window
# Remove reference to Debug Variable Graphic Viewer and Debug Variable
self.ParentControl = None
def OnDragOver(self, x, y, d):
Function called when mouse is dragged over Drop Target
@param x: X coordinate of mouse pointer
@param y: Y coordinate of mouse pointer
@param d: Suggested default for return value
# Signal parent that mouse is dragged over
self.ParentControl.OnMouseDragging(x, y)
return wx.TextDropTarget.OnDragOver(self, x, y, d)
def OnDropText(self, x, y, data):
Function called when mouse is released in Drop Target
@param x: X coordinate of mouse pointer
@param y: Y coordinate of mouse pointer
@param data: Text associated to drag'n drop
# Signal Debug Variable Panel to reset highlight
self.ParentWindow.ResetHighlight()
# Check that data is valid regarding DebugVariablePanel
if not isinstance(values, TupleType):
message = _("Invalid value \"%s\" for debug variable")%data
# Display message if data is invalid
wx.CallAfter(self.ShowMessage, message)
# Data contain a reference to a variable to debug
elif values[1] == "debug":
target_idx = self.ParentControl.GetIndex()
# If mouse is dropped in graph canvas bounding box and graph is
# not 3D canvas, graphs will be merged
rect = self.ParentControl.GetAxesBoundingBox()
if not self.ParentControl.Is3DCanvas() and rect.InsideXY(x, y):
# Default merge type is parallel
merge_type = GRAPH_PARALLEL
# If mouse is dropped in left part of graph canvas, graph
# wall be merged orthogonally
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,
width, height = self.ParentControl.GetSize()
# Get Before which Viewer the variable has to be moved or added
# according to the position of mouse in Viewer.
# Drag'n Drop is an internal is an internal move inside Debug
if len(values) > 2 and values[2] == "move":
self.ParentWindow.MoveValue(values[0],
# Drag'n Drop was initiated by another control of Beremiz
self.ParentWindow.InsertValue(values[0],
Function called when mouse is leave Drop Target
# Signal Debug Variable Panel to reset highlight
self.ParentWindow.ResetHighlight()
return wx.TextDropTarget.OnLeave(self)
def ShowMessage(self, message):
Show error message in Error Dialog
@param message: Error message to display
dialog = wx.MessageDialog(self.ParentWindow,
#-------------------------------------------------------------------------------
# Debug Variable Graphic Viewer Class
#-------------------------------------------------------------------------------
Class that implements a Viewer that display variable values as a graphs
class DebugVariableGraphicViewer(DebugVariableViewer, FigureCanvas):
def __init__(self, parent, window, items, graph_type):
@param parent: Parent wx.Window of DebugVariableText
@param window: Reference to the Debug Variable Panel
@param items: List of DebugVariableItem displayed by Viewer
@param graph_type: Graph display type (Parallel or orthogonal)
DebugVariableViewer.__init__(self, window, items)
self.GraphType = graph_type # Graph type display
self.CursorTick = None # Tick of the graph cursor
# Mouse position when start dragging
self.MouseStartPos = None
# Tick when moving tick start
self.StartCursorTick = None
# Canvas size when starting to resize canvas
self.CanvasStartSize = None
# List of current displayed contextual buttons
self.ContextualButtons = []
# Reference to item for which contextual buttons was displayed
self.ContextualButtonsItem = None
# Flag indicating that zoom fit current displayed data range or whole
# Create figure for drawing graphs
self.Figure = matplotlib.figure.Figure(facecolor='w')
# Defined border around figure in canvas
self.Figure.subplotpars.update(top=0.95, left=0.1,
FigureCanvas.__init__(self, parent, -1, self.Figure)
self.SetWindowStyle(wx.WANTS_CHARS)
self.SetBackgroundColour(wx.WHITE)
self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick)
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)
# Define Viewer drop target
self.SetDropTarget(DebugVariableGraphicDropTarget(self, window))
# Connect matplotlib events
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)
# Add buttons for zooming on current displayed data range
GraphButton(0, 0, "fit_graph", self.OnZoomFitButton))
# Add buttons for changing canvas size with predefined height
[SIZE_MINI, SIZE_MIDDLE, SIZE_MAXI],
["minimize_graph", "middle_graph", "maximize_graph"]):
GraphButton(0, 0, bitmap,
self.GetOnChangeSizeButton(size)))
# Add buttons for exporting graph values to clipboard and close graph
for bitmap, callback in [
("export_graph_mini", self.OnExportGraphButton),
("delete_graph", self.OnCloseButton)]:
self.Buttons.append(GraphButton(0, 0, bitmap, callback))
self.RefreshLabelsPosition(canvas_size.height)
Add an item to the list of items displayed by Viewer
@param item: Item to add to the list
DebugVariableViewer.AddItem(self, item)
def RemoveItem(self, item):
Remove an item from the list of items displayed by Viewer
@param item: Item to remove from the list
DebugVariableViewer.RemoveItem(self, item)
# If list of items is not empty
if not self.ItemsIsEmpty():
# Return to parallel graph if there is only one item
# especially if it's actually orthogonal
self.GraphType = GRAPH_PARALLEL
def SetCursorTick(self, cursor_tick):
@param cursor_tick: Cursor tick
self.CursorTick = cursor_tick
def SetZoomFit(self, zoom_fit):
Set flag indicating that zoom fit current displayed data range
@param zoom_fit: Flag for zoom fit (False: zoom fit whole data range)
# Flag is different from the actual one
if zoom_fit != self.ZoomFit:
# Update button for zoom fit bitmap
self.Buttons[0].SetBitmap("full_graph" if zoom_fit else "fit_graph")
def SubscribeAllDataConsumers(self):
Function that unsubscribe and remove every item that store values of
a variable that doesn't exist in PLC anymore
DebugVariableViewer.SubscribeAllDataConsumers(self)
# Graph still have data to display
if not self.ItemsIsEmpty():
# Reset flag indicating that zoom fit current displayed data range
Return if Viewer is a 3D canvas
@return: True if Viewer is a 3D canvas
return self.GraphType == GRAPH_ORTHOGONAL and len(self.Items) == 3
Return list of buttons defined in Viewer
# Add contextual buttons to default buttons
return self.Buttons + self.ContextualButtons
def PopupContextualButtons(self, item, rect, direction=wx.RIGHT):
Show contextual menu for item aside a label of this item defined
by the bounding box of label in figure
@param item: Item for which contextual is shown
@param rect: Bounding box of label aside which drawing menu
@param direction: Direction in which buttons must be drawn
# Return immediately if contextual menu for item is already shown
if self.ContextualButtonsItem == item:
# Close already shown contextual menu
self.DismissContextualButtons()
# Save item for which contextual menu is shown
self.ContextualButtonsItem = item
# If item variable is forced, add button for release variable to
if self.ContextualButtonsItem.IsForced():
self.ContextualButtons.append(
GraphButton(0, 0, "release", self.OnReleaseItemButton))
# Add other buttons to contextual menu
for bitmap, callback in [
("force", self.OnForceItemButton),
("export_graph_mini", self.OnExportItemGraphButton),
("delete_graph", self.OnRemoveItemButton)]:
self.ContextualButtons.append(
GraphButton(0, 0, bitmap, callback))
# If buttons are shown at left side or upper side of rect, positions
# will be set in reverse order
buttons = self.ContextualButtons[:]
if direction in [wx.TOP, wx.LEFT]:
# Set contextual menu buttons position aside rect depending on
if direction in [wx.LEFT, wx.RIGHT]:
x = rect.x + (- w - offset
else rect.width + offset)
y = rect.y + (rect.height - h) / 2
x = rect.x + (rect.width - w ) / 2
y = rect.y + (- h - offset
else rect.height + offset)
self.ParentWindow.ForceRefresh()
def DismissContextualButtons(self):
Close current shown contextual menu
# Return immediately if no contextual menu is shown
if self.ContextualButtonsItem is None:
# Reset variables corresponding to contextual menu
self.ContextualButtonsItem = None
self.ContextualButtons = []
self.ParentWindow.ForceRefresh()
def IsOverContextualButton(self, x, y):
Return if point is over one contextual button of Viewer
@param x: X coordinate of point
@param y: Y coordinate of point
@return: contextual button where point is over
for button in self.ContextualButtons:
def ExportGraph(self, item=None):
Export item(s) data to clipboard in CSV format
@param item: Item from which data to export, all items if None
self.ParentWindow.CopyDataToClipboard(
[(item, [entry for entry in item.GetData()])
def OnZoomFitButton(self):
Function called when Viewer Zoom Fit button is pressed
# Toggle zoom fit flag value
self.SetZoomFit(not self.ZoomFit)
def GetOnChangeSizeButton(self, height):
Function that generate callback function for change Viewer height to
pre-defined height button
@param height: Height that change Viewer to
@return: callback function
def OnChangeSizeButton():
self.SetCanvasHeight(height)
return OnChangeSizeButton
def OnExportGraphButton(self):
Function called when Viewer Export button is pressed
# Export data of every item in Viewer
def OnForceItemButton(self):
Function called when contextual menu Force button is pressed
# Open dialog for forcing item variable value
self.ForceValue(self.ContextualButtonsItem)
self.DismissContextualButtons()
def OnReleaseItemButton(self):
Function called when contextual menu Release button is pressed
# Release item variable value
self.ReleaseValue(self.ContextualButtonsItem)
self.DismissContextualButtons()
def OnExportItemGraphButton(self):
Function called when contextual menu Export button is pressed
# Export data of item variable
self.ExportGraph(self.ContextualButtonsItem)
self.DismissContextualButtons()
def OnRemoveItemButton(self):
Function called when contextual menu Remove button is pressed
# Remove item from Viewer
wx.CallAfter(self.ParentWindow.DeleteValue, self,
self.ContextualButtonsItem)
self.DismissContextualButtons()
def HandleCursorMove(self, event):
Update Cursor position according to mouse position and graph type
@param event: Mouse event
start_tick, end_tick = self.ParentWindow.GetRange()
items = self.ItemsDict.values()
if self.GraphType == GRAPH_ORTHOGONAL:
# Extract items data displayed in canvas figure
start_tick = max(start_tick, self.GetItemsMinCommonTick())
end_tick = max(end_tick, start_tick)
x_data = items[0].GetData(start_tick, end_tick)
y_data = items[1].GetData(start_tick, end_tick)
# Search for the nearest point from mouse position
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)
# Set cursor tick to the tick of this point
cursor_tick = x_data[numpy.argmin(d), 0]
data = items[0].GetData(start_tick, end_tick)
# Search for point that tick is the nearest from mouse X position
# and set cursor tick to the tick of this point
cursor_tick = data[numpy.argmin(
numpy.abs(data[:,0] - event.xdata)), 0]
if cursor_tick is not None:
self.ParentWindow.SetCursorTick(cursor_tick)
def OnCanvasButtonPressed(self, event):
Function called when a button of mouse is pressed
@param event: Mouse event
# Get mouse position, graph Y coordinate is inverted in matplotlib
width, height = self.GetSize()
x, y = event.x, height - event.y
# Return immediately if mouse is over a button
if self.IsOverButton(x, y):
# Mouse was clicked inside graph figure
if event.inaxes == self.Axes:
# Find if it was on an item label
# Check every label paired with corresponding item
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)
# Check if mouse was over label
# If an item label have been clicked
# Hide buttons and contextual buttons
self.DismissContextualButtons()
# Start a drag'n drop from mouse position in wx coordinate of
xw, yw = self.GetPosition()
self.ParentWindow.StartDragNDrop(self,
self.ItemsDict.values()[item_idx],
x + xw, y + yw, # Current mouse position
x + xw, y + yw) # Mouse position when button was clicked
# Don't handle mouse button if canvas is 3D and let matplotlib do
# the default behavior (rotate 3D axes)
elif not self.Is3DCanvas():
# Save mouse position when clicked
self.MouseStartPos = wx.Point(x, y)
# Mouse button was left button, start moving cursor
# Save current tick in case a drag'n drop is initiate to
self.StartCursorTick = self.CursorTick
self.HandleCursorMove(event)
# Mouse button is middle button and graph is parallel, start
# moving graph along X coordinate (tick)
elif event.button == 2 and self.GraphType == GRAPH_PARALLEL:
self.StartCursorTick = self.ParentWindow.GetRange()[0]
# Mouse was clicked outside graph figure and over resize highlight with
# left button, start resizing Viewer
elif event.button == 1 and event.y <= 5:
self.MouseStartPos = wx.Point(x, y)
self.CanvasStartSize = height
def OnCanvasButtonReleased(self, event):
Function called when a button of mouse is released
@param event: Mouse event
# If a drag'n drop is in progress, stop it
if self.ParentWindow.IsDragging():
width, height = self.GetSize()
xw, yw = self.GetPosition()
item = self.ParentWindow.DraggingAxesPanel.ItemsDict.values()[0]
# Give mouse position in wx coordinate of parent
self.ParentWindow.StopDragNDrop(item.GetVariable(),
xw + event.x, yw + height - event.y)
# Reset any move in progress
self.MouseStartPos = None
self.CanvasStartSize = None
# Handle button under mouse if it exist
width, height = self.GetSize()
self.HandleButton(event.x, height - event.y)
def OnCanvasMotion(self, event):
Function called when a button of mouse is moved over Viewer
@param event: Mouse event
width, height = self.GetSize()
# If a drag'n drop is in progress, move canvas dragged
if self.ParentWindow.IsDragging():
xw, yw = self.GetPosition()
# Give mouse position in wx coordinate of parent
self.ParentWindow.MoveDragNDrop(
# If a Viewer resize is in progress, change Viewer size
elif event.button == 1 and self.CanvasStartSize is not None:
width, height = self.GetSize()
self.CanvasStartSize + height - event.y - self.MouseStartPos.y)
# If no button is pressed, show or hide contextual buttons or resize
elif event.button is None:
# Compute direction for items label according graph type
if self.GraphType == GRAPH_PARALLEL: # Graph is parallel
directions = [wx.RIGHT] * len(self.AxesLabels) + \
[wx.LEFT] * len(self.Labels)
elif len(self.AxesLabels) > 0: # Graph is orthogonal in 2D
directions = [wx.RIGHT, wx.TOP, # Directions for AxesLabels
wx.LEFT, wx.BOTTOM] # Directions for Labels
else: # Graph is orthogonal in 3D
directions = [wx.LEFT] * len(self.Labels)
# Find if mouse is over an item label
[pair for pair in enumerate(self.AxesLabels)] +
[pair for pair in enumerate(self.Labels)],
# Check every label paired with corresponding item
(x0, y0), (x1, y1) = t.get_window_extent().get_points()
rect = wx.Rect(x0, height - y1, x1 - x0, y1 - y0)
# Check if mouse was over label
if rect.InsideXY(event.x, height - event.y):
# If mouse is over an item label,
self.PopupContextualButtons(
self.ItemsDict.values()[item_idx],
# If mouse isn't over a contextual menu, hide the current shown one
if self.IsOverContextualButton(event.x, height - event.y) is None:
self.DismissContextualButtons()
# Update resize highlight
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()
# Handle buttons if canvas is not 3D
elif not self.Is3DCanvas():
# If left button is pressed
# Mouse is inside graph figure
if event.inaxes == self.Axes:
# If a cursor move is in progress, update cursor position
if self.MouseStartPos is not None:
self.HandleCursorMove(event)
# Mouse is outside graph figure, cursor move is in progress and
# there is only one item in Viewer, start a drag'n drop
elif self.MouseStartPos is not None and len(self.Items) == 1:
xw, yw = self.GetPosition()
self.ParentWindow.SetCursorTick(self.StartCursorTick)
self.ParentWindow.StartDragNDrop(self,
self.ItemsDict.values()[0],
event.x + xw, height - event.y + yw,
# Mouse position when button was clicked
self.MouseStartPos.x + xw,
self.MouseStartPos.y + yw)
# If middle button is pressed and moving graph along X coordinate
elif event.button == 2 and self.GraphType == GRAPH_PARALLEL and \
self.MouseStartPos is not None:
start_tick, end_tick = self.ParentWindow.GetRange()
rect = self.GetAxesBoundingBox()
# Move graph along X coordinate
self.ParentWindow.SetCanvasPosition(
(self.MouseStartPos.x - event.x) *
(end_tick - start_tick) / rect.width)
def OnCanvasScroll(self, event):
Function called when a wheel mouse is use in Viewer
@param event: Mouse event
# Change X range of graphs if mouse is in canvas figure and ctrl is
if event.inaxes is not None and event.guiEvent.ControlDown():
# Calculate position of fixed tick point according to graph type
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)
# Vetoing event to prevent parent panel to be scrolled
self.ParentWindow.VetoScrollEvent = True
def OnLeftDClick(self, event):
Function called when a left mouse button is double clicked
@param event: Mouse event
# Check that double click was done inside figure
pos = event.GetPosition()
rect = self.GetAxesBoundingBox()
if rect.InsideXY(pos.x, pos.y):
# Reset Cursor tick to value before starting clicking
self.ParentWindow.SetCursorTick(self.StartCursorTick)
# Toggle to text Viewer(s)
self.ParentWindow.ToggleViewerType(self)
# Cursor tick move for each arrow key
def OnKeyDown(self, event):
Function called when key is pressed
@param event: wx.KeyEvent
# If cursor is shown and arrow key is pressed, move cursor tick
if self.CursorTick is not None:
move = self.KEY_CURSOR_INCREMENT.get(event.GetKeyCode(), None)
self.ParentWindow.MoveCursorTick(move)
def OnLeave(self, event):
Function called when mouse leave Viewer
@param event: wx.MouseEvent
# If Viewer is not resizing, reset resize highlight
if self.CanvasStartSize is None:
self.SetHighlight(HIGHLIGHT_NONE)
self.SetCursor(wx.NullCursor)
DebugVariableViewer.OnLeave(self, event)
def GetCanvasMinSize(self):
Return the minimum size of Viewer so that all items label can be
@return: wx.Size containing Viewer minimum size
# The minimum height take in account the height of all items, padding
# inside figure and border around figure
CANVAS_BORDER[0] + CANVAS_BORDER[1] +
2 * CANVAS_PADDING + VALUE_LABEL_HEIGHT * len(self.Items))
def SetCanvasHeight(self, height):
Set Viewer size checking that it respects Viewer minimum size
@param height: Viewer height
min_width, min_height = self.GetCanvasMinSize()
height = max(height, min_height)
self.SetMinSize(wx.Size(min_width, height))
self.RefreshLabelsPosition(height)
self.ParentWindow.RefreshGraphicsSizer()
def GetAxesBoundingBox(self, parent_coordinate=False):
Return figure bounding box in wx coordinate
@param parent_coordinate: True if use parent coordinate (default False)
# Calculate figure bounding box. Y coordinate is inverted in matplotlib
# figure comparing to wx panel
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)
# If parent_coordinate, add Viewer position in parent
xw, yw = self.GetPosition()
def RefreshHighlight(self, x, y):
Refresh Viewer highlight according to mouse position
@param x: X coordinate of mouse pointer
@param y: Y coordinate of mouse pointer
width, height = self.GetSize()
# Mouse is over Viewer figure and graph is not 3D
bbox = self.GetAxesBoundingBox()
if bbox.InsideXY(x, y) and not self.Is3DCanvas():
rect = wx.Rect(bbox.x, bbox.y, bbox.width / 2, bbox.height)
# Mouse is over Viewer left part of figure
self.SetHighlight(HIGHLIGHT_LEFT)
# Mouse is over Viewer right part of figure
self.SetHighlight(HIGHLIGHT_RIGHT)
# Mouse is over upper part of Viewer
# Viewer is upper one in Debug Variable Panel, show highlight
if self.ParentWindow.IsViewerFirst(self):
self.SetHighlight(HIGHLIGHT_BEFORE)
# Viewer is not the upper one, show highlight in previous one
# It prevents highlight to move when mouse leave one Viewer to
self.SetHighlight(HIGHLIGHT_NONE)
self.ParentWindow.HighlightPreviousViewer(self)
# Mouse is over lower part of Viewer
self.SetHighlight(HIGHLIGHT_AFTER)
def OnAxesMotion(self, event):
Function overriding default function called when mouse is dragged for
rotating graph preventing refresh to be called too quickly
@param event: Mouse event
# Call default function at most 10 times per second
if current_time - self.LastMotionTime > REFRESH_PERIOD:
self.LastMotionTime = current_time
Axes3D._on_move(self.Axes, event)
def GetAddTextFunction(self):
Return function for adding text in figure according to graph type
@return: Function adding text to figure
text_func = (self.Axes.text2D if self.Is3DCanvas() else self.Axes.text)
def AddText(*args, **kwargs):
kwargs["transform"] = self.Axes.transAxes
return text_func(*args, **kwargs)
def SetAxesColor(self, color):
if LooseVersion(matplotlib.__version__) >= LooseVersion("1.5.0"):
self.Axes.set_prop_cycle(cycler('color',color))
self.Axes.set_color_cycle(color)
Reset figure and graphical elements displayed in it
Called any time list of items or graph type change
# Clear figure from any axes defined
# Add 3D projection if graph is in 3D
self.Axes = self.Figure.gca(projection='3d')
# Override function to prevent too much refresh when graph is
self.LastMotionTime = gettime()
setattr(self.Axes, "_on_move", self.OnAxesMotion)
# Init graph mouse event so that graph can be rotated
# Set size of Z axis labels
self.Axes.tick_params(axis='z', labelsize='small')
self.Axes = self.Figure.gca()
self.SetAxesColor(COLOR_CYCLE)
# Set size of X and Y axis labels
self.Axes.tick_params(axis='x', labelsize='small')
self.Axes.tick_params(axis='y', labelsize='small')
# Init variables storing graphical elements added to figure
self.Plots = [] # List of curves
self.VLine = None # Vertical line for cursor
self.HLine = None # Horizontal line for cursor (only orthogonal 2D)
self.AxesLabels = [] # List of items variable path text label
self.Labels = [] # List of items text label
# Get function to add a text in figure according to graph type
add_text_func = self.GetAddTextFunction()
# Graph type is parallel or orthogonal in 3D
if self.GraphType == GRAPH_PARALLEL or self.Is3DCanvas():
num_item = len(self.Items)
for idx in xrange(num_item):
# Get color from color cycle (black if only one item)
color = ('k' if num_item == 1
else COLOR_CYCLE[idx % len(COLOR_CYCLE)])
# In 3D graph items variable label are not displayed as text
# in figure, but as axis title
if not self.Is3DCanvas():
# Items variable labels are in figure upper left corner
add_text_func(size='small', color=color,
verticalalignment='top'))
# Items variable labels are in figure lower right corner
add_text_func(size='large', color=color,
horizontalalignment='right'))
# Graph type is orthogonal in 2D
# X coordinate labels are in figure lower side
self.AxesLabels.append(add_text_func(size='small'))
add_text_func(size='large',
horizontalalignment='right'))
# Y coordinate labels are vertical and in figure left side
add_text_func(size='small', rotation='vertical',
verticalalignment='bottom'))
add_text_func(size='large', rotation='vertical',
verticalalignment='top'))
# Refresh position of labels according to Viewer size
width, height = self.GetSize()
self.RefreshLabelsPosition(height)
def RefreshLabelsPosition(self, height):
Function called when mouse leave Viewer
@param event: wx.MouseEvent
# Figure position like text position in figure are expressed is ratio
# canvas size and figure size. As we want that border around figure and
# text position in figure don't change when canvas size change, we
# expressed border and text position in pixel on screen and apply the
# ratio calculated hereafter to get border and text position in
canvas_ratio = 1. / height # Divide by canvas height in pixel
(1.0 - (CANVAS_BORDER[0] + CANVAS_BORDER[1]) * canvas_ratio)
* height) # Divide by figure height in pixel
# Update position of figure (keeping up and bottom border the same
self.Figure.subplotpars.update(
top= 1.0 - CANVAS_BORDER[1] * canvas_ratio,
bottom= CANVAS_BORDER[0] * canvas_ratio)
# Update position of items labels
if self.GraphType == GRAPH_PARALLEL or self.Is3DCanvas():
num_item = len(self.Items)
for idx in xrange(num_item):
# In 3D graph items variable label are not displayed
if not self.Is3DCanvas():
# Items variable labels are in figure upper left corner
self.AxesLabels[idx].set_position(
AXES_LABEL_HEIGHT * idx) * graph_ratio))
# Items variable labels are in figure lower right corner
self.Labels[idx].set_position(
CANVAS_PADDING * graph_ratio +
(num_item - idx - 1) * VALUE_LABEL_HEIGHT * graph_ratio))
# X coordinate labels are in figure lower side
self.AxesLabels[0].set_position(
(0.1, CANVAS_PADDING * graph_ratio))
self.Labels[0].set_position(
(0.95, CANVAS_PADDING * graph_ratio))
# Y coordinate labels are vertical and in figure left side
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 RefreshViewer(self, refresh_graphics=True):
Function called to refresh displayed by matplotlib canvas
@param refresh_graphics: Flag indicating that graphs have to be
refreshed (False: only label values have to be refreshed)
# Refresh graphs if needed
# Get tick range of values to display
start_tick, end_tick = self.ParentWindow.GetRange()
if self.GraphType == GRAPH_PARALLEL:
# Init list of data range for each variable displayed
# Get data and range for each variable displayed
for idx, item in enumerate(self.Items):
data, min_value, max_value = item.GetDataAndValueRange(
start_tick, end_tick, not self.ZoomFit)
# Check that data is not empty
# Add variable range to list of variable data range
ranges.append((min_value, max_value))
# Add plot to canvas if not yet created
if len(self.Plots) <= idx:
self.Axes.plot(data[:, 0], data[:, 1])[0])
# Set data to already created plot in canvas
self.Plots[idx].set_data(data[:, 0], data[:, 1])
# Get X and Y axis ranges
x_min, x_max = start_tick, end_tick
y_min, y_max = merge_ranges(ranges)
# Display cursor in canvas if a cursor tick is defined and it is
# include in values tick range
if (self.CursorTick is not None and
start_tick <= self.CursorTick <= end_tick):
# Define a vertical line to display cursor position if no
# line is already defined
self.VLine = self.Axes.axvline(self.CursorTick,
# Set value of vertical line if already defined
self.VLine.set_xdata((self.CursorTick, self.CursorTick))
self.VLine.set_visible(True)
# Hide vertical line if cursor tick is not defined or reset
elif self.VLine is not None:
self.VLine.set_visible(False)
# Update tick range, removing ticks that don't have a value for
start_tick = max(start_tick, self.GetItemsMinCommonTick())
end_tick = max(end_tick, start_tick)
items = self.ItemsDict.values()
# Get data and range for first variable (X coordinate)
x_data, x_min, x_max = items[0].GetDataAndValueRange(
start_tick, end_tick, not self.ZoomFit)
# Get data and range for second variable (Y coordinate)
y_data, y_min, y_max = items[1].GetDataAndValueRange(
start_tick, end_tick, not self.ZoomFit)
# Normalize X and Y coordinates value range
x_min, x_max = merge_ranges([(x_min, x_max)])
y_min, y_max = merge_ranges([(y_min, y_max)])
# Get X and Y coordinates for cursor if cursor tick is defined
if self.CursorTick is not None:
x_cursor, x_forced = items[0].GetValue(
self.CursorTick, raw=True)
y_cursor, y_forced = items[1].GetValue(
self.CursorTick, raw=True)
# Get common data length so that each value has an x and y
length = (min(len(x_data), len(y_data))
if x_data is not None and y_data is not None
# Check that x and y data are not empty
if x_data is not None and y_data is not None:
# Add plot to canvas if not yet created
self.Axes.plot(x_data[:, 1][:length],
y_data[:, 1][:length])[0])
# Set data to already created plot in canvas
# Display cursor in canvas if a cursor tick is defined and it is
# include in values tick range
if (self.CursorTick is not None and
start_tick <= self.CursorTick <= end_tick):
# Define a vertical line to display cursor x coordinate
# if no line is already defined
self.VLine = self.Axes.axvline(x_cursor,
# Set value of vertical line if already defined
self.VLine.set_xdata((x_cursor, x_cursor))
# Define a horizontal line to display cursor y
# coordinate if no line is already defined
self.HLine = self.Axes.axhline(y_cursor,
# Set value of horizontal line if already defined
self.HLine.set_ydata((y_cursor, y_cursor))
self.VLine.set_visible(True)
self.HLine.set_visible(True)
# Hide vertical and horizontal line if cursor tick is not
if self.VLine is not None:
self.VLine.set_visible(False)
if self.HLine is not None:
self.HLine.set_visible(False)
# Remove all plots already defined in 3D canvas
while len(self.Axes.lines) > 0:
# Get data and range for third variable (Z coordinate)
z_data, z_min, z_max = items[2].GetDataAndValueRange(
start_tick, end_tick, not self.ZoomFit)
# Normalize Z coordinate value range
z_min, z_max = merge_ranges([(z_min, z_max)])
# Check that x, y and z data are not empty
if (x_data is not None and y_data is not None and
# Get common data length so that each value has an x, y
length = min(length, len(z_data))
self.Axes.plot(x_data[:, 1][:length],
zs = z_data[:, 1][:length])
# Display cursor in canvas if a cursor tick is defined and
# it is include in values tick range
if (self.CursorTick is not None and
start_tick <= self.CursorTick <= end_tick):
# Get Z coordinate for cursor
z_cursor, z_forced = items[2].GetValue(
self.CursorTick, raw=True)
# Add 3 lines parallel to x, y and z axis to display
for kwargs in [{"xs": numpy.array([x_min, x_max])},
{"ys": numpy.array([y_min, y_max])},
{"zs": numpy.array([z_min, z_max])}]:
("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_zlim(z_min, z_max)
# Set X and Y axis limits
self.Axes.set_xlim(x_min, x_max)
self.Axes.set_ylim(y_min, y_max)
# Get value and forced flag for each variable displayed in graph
# If cursor tick is not defined get value and flag of last received
# or get value and flag of variable at cursor tick
values, forced = apply(zip, [
(item.GetValue(self.CursorTick)
if self.CursorTick is not None
else (item.GetValue(), item.IsForced()))
# Get path of each variable displayed simplified using panel variable
labels = [item.GetVariable(self.ParentWindow.GetVariableNameMask())
# Get style for each variable according to
styles = map(lambda x: {True: 'italic', False: 'normal'}[x], forced)
# Graph is orthogonal 3D, set variables path as 3D axis label
for idx, label_func in enumerate([self.Axes.set_xlabel,
label_func(labels[idx], fontdict={'size': 'small',
'color': COLOR_CYCLE[idx]})
# Graph is not orthogonal 3D, set variables path in axes labels
for label, text in zip(self.AxesLabels, labels):
# Set value label text and style according to value and forced flag for
# each variable displayed
for label, value, style in zip(self.Labels, values, styles):
def draw(self, drawDC=None):
# Render figure using agg
FigureCanvasAgg.draw(self)
# Get bitmap of figure rendered
self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
if wx.VERSION < (3, 0, 0):
# Create DC for rendering graphics in bitmap
destDC.SelectObject(self.bitmap)
# Get Graphics Context for DC, for anti-aliased and transparent
# Get canvas size and figure bounding box in canvas
width, height = self.GetSize()
bbox = self.GetAxesBoundingBox()
# If highlight to display is resize, draw thick grey line at bottom
if self.Highlight == HIGHLIGHT_RESIZE:
destGC.SetPen(HIGHLIGHT_RESIZE_PEN)
destGC.SetBrush(HIGHLIGHT_RESIZE_BRUSH)
destGC.DrawRectangle(0, height - 5, width, 5)
# If highlight to display is merging graph, draw 50% transparent blue
# rectangle on left or right part of figure depending on highlight type
elif self.Highlight in [HIGHLIGHT_LEFT, HIGHLIGHT_RIGHT]:
destGC.SetPen(HIGHLIGHT_DROP_PEN)
destGC.SetBrush(HIGHLIGHT_DROP_BRUSH)
x_offset = (bbox.width / 2
if self.Highlight == HIGHLIGHT_RIGHT
destGC.DrawRectangle(bbox.x + x_offset, bbox.y,
bbox.width / 2, bbox.height)
# Draw other Viewer common elements
self.DrawCommonElements(destGC, self.GetButtons())
self.gui_repaint(drawDC=drawDC)