qdsfit/QDSToolbar.py
2013-07-17 13:00:24 +02:00

98 lines
3.7 KiB
Python

from PyQt4.QtCore import pyqtSignal
from PyQt4.QtGui import QAction, QIcon, QPixmap
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar, NavigationToolbar2QTAgg
__author__ = 'markusro'
class CustomToolbar(NavigationToolbar):
# our spanChanged signal
spanSelectedTrigger = pyqtSignal(float, float, name='spanChanged')
def __init__(self, plotCanvas, plotWidget, parent=None):
NavigationToolbar.__init__(self, plotCanvas, plotWidget, coordinates=True)
self.canvas = plotCanvas
# Span select Button
#self.span_button = QAction(QIcon("border-1d-right-icon.png" ), "Span", self)
self.span_button = QAction(QIcon(QPixmap(":/icons/fit_limits.png")), "Fit Limits", self)
self.span_button.setCheckable(True)
self.cids = []
self.cids.append(self.canvas.mpl_connect('button_press_event', self.press))
self.cids.append(self.canvas.mpl_connect('motion_notify_event', self.onmove))
self.cids.append(self.canvas.mpl_connect('button_release_event', self.release))
self.cids.append(self.canvas.mpl_connect('draw_event', self.update_background))
# act.setCheckable(True)
# add actions before the coordinates widget
self.insertAction(self.actions()[-1], self.span_button)
self.buttons = {}
self._pressed = False
self.background = None
self.span = None
self.istart = 0
self.iend = 0
self.xstart = 0
self.xend = 0
def set_span(self, x1, x2):
#trans = blended_transform_factory(self.axes.transData, self.axes.transAxes)
cur = self.span.get_xy()
cur[0, 0] = x1
cur[1, 0] = x1
cur[2, 0] = x2
cur[3, 0] = x2
self.span.set_xy(cur)
def ignore(self, event):
# 'return ``True`` if *event* should be ignored'
return event.inaxes != self.canvas.axes or event.button != 1
def update_background(self, event):
#if self.canvas.axes is None:
# raise SyntaxError,"Need an axes reference!"
self.background = self.canvas.copy_from_bbox(self.canvas.axes.bbox)
def press(self, event):
if self.span_button.isChecked():
if self.background is None:
self.update_background()
else:
self.canvas.restore_region(self.background)
self.xstart = event.xdata
self.istart = event.x
if self.span is None:
self.span = self.canvas.axes.axvspan(event.xdata, event.xdata, alpha=0.10, color="k", animated=False)
else:
self.set_span(event.xdata, event.xdata)
self._pressed = True
def onmove(self, event):
if self.span_button.isChecked() and self._pressed and not self.ignore(event):
self.set_span(self.xstart, event.xdata)
self.update()
def update(self):
"""Overrides method of NavigationToolbar.
Allows fast drawing of the span selector with blitting
"""
self.canvas.restore_region(self.background)
self.canvas.axes.draw_artist(self.span)
for line in self.canvas.axes.get_lines():
self.canvas.axes.draw_artist(line)
self.canvas.blit(self.canvas.axes.bbox)
def release(self, event):
self.span_button.setChecked(False)
self.xend = event.xdata
self.iend = event.x
if self.iend < self.istart:
self.iend, self.istart = self.istart, self.iend
#print "released", self.xstart, self.xend
if self._pressed:
if self.ignore(event):
self.istart = 0
self.spanSelectedTrigger.emit(self.xstart, self.xend)
self._pressed = False