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