plotmagnifier.cpp
Go to the documentation of this file.
00001 #include "plotmagnifier.h"
00002 #include <qwt_plot.h>
00003 #include <QDebug>
00004 #include <limits>
00005 #include <QWheelEvent>
00006 
00007 PlotMagnifier::PlotMagnifier( QWidget *canvas) : QwtPlotMagnifier(canvas)
00008 {
00009     for ( int axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
00010     {
00011         _lower_bounds[axisId] = -std::numeric_limits<double>::max();
00012         _upper_bounds[axisId] =  std::numeric_limits<double>::max();
00013     }
00014 }
00015 
00016 PlotMagnifier::~PlotMagnifier() {}
00017 
00018 void PlotMagnifier::setAxisLimits(int axis, double lower, double upper)
00019 {
00020     if ( axis >= 0 && axis < QwtPlot::axisCnt )
00021     {
00022         _lower_bounds[axis] = lower;
00023         _upper_bounds[axis] = upper;
00024     }
00025 }
00026 
00027 void PlotMagnifier::rescale( double factor )
00028 {
00029     factor = qAbs( factor );
00030 
00031     QwtPlot* plt = plot();
00032     if ( plt == NULL || factor == 1.0 ){
00033         return;
00034     }
00035 
00036     bool doReplot = false;
00037 
00038     const bool autoReplot = plt->autoReplot();
00039     plt->setAutoReplot( false );
00040 
00041     const int axis_list[2] = {QwtPlot::xBottom, QwtPlot::yLeft};
00042     QRectF new_rect;
00043 
00044     for ( int i = 0; i <2; i++ )
00045     {
00046         int axisId = axis_list[i];
00047 
00048         if ( isAxisEnabled( axisId ) )
00049         {
00050             const QwtScaleMap scaleMap = plt->canvasMap( axisId );
00051 
00052             double v1 = scaleMap.s1();
00053             double v2 = scaleMap.s2();
00054             double center = _mouse_position.x();
00055 
00056             if( axisId == QwtPlot::yLeft){
00057                 center = _mouse_position.y();
00058             }
00059 
00060             if ( scaleMap.transformation() )
00061             {
00062                 // the coordinate system of the paint device is always linear
00063                 v1 = scaleMap.transform( v1 ); // scaleMap.p1()
00064                 v2 = scaleMap.transform( v2 ); // scaleMap.p2()
00065             }
00066 
00067             const double width = ( v2 - v1 );
00068             const double ratio = (v2-center)/ (width);
00069 
00070             v1 = center - width*factor*(1-ratio);
00071             v2 = center + width*factor*(ratio);
00072 
00073             if( v1 > v2 ) std::swap( v1, v2 );
00074 
00075             if ( scaleMap.transformation() )
00076             {
00077                 v1 = scaleMap.invTransform( v1 );
00078                 v2 = scaleMap.invTransform( v2 );
00079             }
00080 
00081             if( v1 < _lower_bounds[axisId]) v1 = _lower_bounds[axisId];
00082             if( v2 > _upper_bounds[axisId]) v2 = _upper_bounds[axisId];
00083 
00084             plt->setAxisScale( axisId, v1, v2 );
00085 
00086             if( axisId == QwtPlot::xBottom)
00087             {
00088                 new_rect.setLeft(  v1 );
00089                 new_rect.setRight( v2 );
00090             }
00091             else{
00092                 new_rect.setBottom( v1 );
00093                 new_rect.setTop( v2 );
00094             }
00095 
00096             doReplot = true;
00097         }
00098     }
00099 
00100     plt->setAutoReplot( autoReplot );
00101 
00102     if ( doReplot ){
00103         emit rescaled( new_rect );
00104     }
00105 }
00106 
00107 QPointF PlotMagnifier::invTransform(QPoint pos)
00108 {
00109     QwtScaleMap xMap = plot()->canvasMap( QwtPlot::xBottom );
00110     QwtScaleMap yMap = plot()->canvasMap( QwtPlot::yLeft );
00111     return QPointF ( xMap.invTransform( pos.x() ), yMap.invTransform( pos.y() ) );
00112 }
00113 
00114 void PlotMagnifier::widgetWheelEvent(QWheelEvent *event)
00115 {
00116     _mouse_position = invTransform(event->pos());
00117     QwtPlotMagnifier::widgetWheelEvent(event);
00118 }
00119 
00120 void PlotMagnifier::widgetMousePressEvent(QMouseEvent *event)
00121 {
00122     _mouse_position = invTransform(event->pos());
00123     QwtPlotMagnifier::widgetMousePressEvent(event);
00124 }


plotjuggler
Author(s): Davide Faconti
autogenerated on Fri Sep 1 2017 02:41:56