34 from __future__ 
import division
 
   38 from python_qt_binding.QtCore 
import QEvent, QSize, QPointF, Qt, Signal, Slot, qWarning
 
   39 from python_qt_binding.QtGui 
import QColor, QPen, QBrush, QVector2D
 
   45     mouseCoordinatesChanged = Signal(QPointF)
 
   46     _num_value_saved = 1000
 
   47     _num_values_ploted = 1000
 
   49     limits_changed = Signal()
 
   52         super(QwtDataPlot, self).
__init__(*args)
 
   53         self.setCanvasBackground(Qt.white)
 
   54         self.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend)
 
   56         grid = Qwt.QwtPlotGrid()
 
   58         grid.setPen(QPen(Qt.black, 0, Qt.DotLine))
 
   77         marker_axis_y = Qwt.QwtPlotMarker()
 
   78         marker_axis_y.setLabelAlignment(Qt.AlignRight | Qt.AlignTop)
 
   79         marker_axis_y.setLineStyle(Qwt.QwtPlotMarker.HLine)
 
   80         marker_axis_y.setYValue(0.0)
 
   81         marker_axis_y.attach(self)
 
   86         self.canvas().setMouseTracking(
True)
 
   87         self.canvas().installEventFilter(self)
 
   90         if event.type() == QEvent.MouseButtonPress:
 
   91             x = self.invTransform(Qwt.QwtPlot.xBottom, event.pos().
x())
 
   92             y = self.invTransform(Qwt.QwtPlot.yLeft, event.pos().y())
 
   94         elif event.type() == QEvent.MouseButtonRelease:
 
   97         elif event.type() == QEvent.MouseMove:
 
   98             x = self.invTransform(Qwt.QwtPlot.xBottom, event.pos().
x())
 
   99             y = self.invTransform(Qwt.QwtPlot.yLeft, event.pos().y())
 
  100             coords = QPointF(x, y)
 
  103                 toolTip = 
'origin x: %.5f, y: %.5f\ndelta x: %.5f, y: %.5f\nlength: %.5f' % (
 
  105                     delta.x(), delta.y(), QVector2D(delta).length())
 
  107                 toolTip = 
'buttons\nleft: measure\nmiddle: move\nright: zoom x/y\nwheel: zoom y' 
  108             self.setToolTip(toolTip)
 
  112     def log(self, level, message):
 
  117         Qwt.QwtPlot.resizeEvent(self, event)
 
  120     def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=
False):
 
  121         curve_id = str(curve_id)
 
  124         curve_object = Qwt.QwtPlotCurve(curve_name)
 
  125         curve_object.attach(self)
 
  126         curve_object.setPen(curve_color)
 
  128             curve_object.setSymbol(
 
  129                 Qwt.QwtSymbol(Qwt.QwtSymbol.Ellipse, QBrush(curve_color), QPen(Qt.black), QSize(4, 4)))
 
  130         self.
_curves[curve_id] = curve_object
 
  133         curve_id = str(curve_id)
 
  136             self.
_curves[curve_id].attach(
None)
 
  140         curve_id = str(curve_id)
 
  142         curve.setData(data_x, data_y)
 
  151         self.setAxisScale(Qwt.QwtPlot.yLeft,
 
  154         self.setAxisScale(Qwt.QwtPlot.xBottom,
 
  164         add delta_x to the length of the x axis 
  173         set the y axis height to max_value, about the current center 
  175         canvas_display_height = max_value
 
  177         y_lower_limit = canvas_offset_y - (canvas_display_height / 2)
 
  178         y_upper_limit = canvas_offset_y + (canvas_display_height / 2)
 
  179         self.
_y_limits = [y_lower_limit, y_upper_limit]
 
  184         move the canvas by delta_x and delta_y in SCREEN COORDINATES 
  198         canvas_x = event.x() - self.canvas().
x()
 
  199         canvas_y = event.y() - self.canvas().y()
 
  200         if event.buttons() & Qt.MiddleButton:  
 
  204         elif event.buttons() & Qt.RightButton:   
 
  205             zoom_factor = max(-0.6, min(0.6, (self.
_last_canvas_y - canvas_y) / 20.0 / 2.0))
 
  207             self.
move_canvas(0, zoom_factor * delta_y * 1.0225)
 
  216         canvas_y = event.y() - self.canvas().y()
 
  219             delta = event.angleDelta().y()
 
  220         except AttributeError:
 
  221             delta = event.delta()
 
  222         zoom_factor = max(-0.6, min(0.6, (delta / 120) / 6.0))
 
  223         delta_y = (self.canvas().height() / 2.0) - canvas_y
 
  224         self.
move_canvas(0, zoom_factor * delta_y * 1.0225)
 
  231         qWarning(
"QwtDataPlot.vline is not implemented yet")
 
  234         self.setAxisScale(Qwt.QwtPlot.xBottom, limits[0], limits[1])
 
  238         self.setAxisScale(Qwt.QwtPlot.yLeft, limits[0], limits[1])
 
  248 if __name__ == 
'__main__':
 
  249     from python_qt_binding.QtWidgets 
import QApplication
 
  250     from numpy 
import arange, sin
 
  252     app = QApplication(sys.argv)
 
  254     plot.resize(700, 500)
 
  256     plot.add_curve(0, 
'(x/50)^2')
 
  257     plot.add_curve(1, 
'sin(x / 20) * 500')
 
  258     x = arange(plot._num_value_saved)
 
  259     plot.set_values(0, x, (x / 50.0)**2)
 
  260     plot.set_values(1, x, sin(x / 20.0) * 500)
 
  261     plot.set_xlim([x[0], x[-1]])
 
  262     plot.set_ylim([-500, 500])
 
  264     sys.exit(app.exec_())