00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- ***************************** 00002 * Qwt Widget Library 00003 * Copyright (C) 1997 Josef Wilgen 00004 * Copyright (C) 2002 Uwe Rathmann 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the Qwt License, Version 1.0 00008 *****************************************************************************/ 00009 00010 #include "qwt_plot.h" 00011 #include "qwt_scale_div.h" 00012 #include "qwt_plot_magnifier.h" 00013 00014 class QwtPlotMagnifier::PrivateData 00015 { 00016 public: 00017 PrivateData() 00018 { 00019 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 00020 isAxisEnabled[axis] = true; 00021 } 00022 00023 bool isAxisEnabled[QwtPlot::axisCnt]; 00024 }; 00025 00030 QwtPlotMagnifier::QwtPlotMagnifier( QWidget *canvas ): 00031 QwtMagnifier( canvas ) 00032 { 00033 d_data = new PrivateData(); 00034 } 00035 00037 QwtPlotMagnifier::~QwtPlotMagnifier() 00038 { 00039 delete d_data; 00040 } 00041 00053 void QwtPlotMagnifier::setAxisEnabled( int axis, bool on ) 00054 { 00055 if ( axis >= 0 && axis < QwtPlot::axisCnt ) 00056 d_data->isAxisEnabled[axis] = on; 00057 } 00058 00067 bool QwtPlotMagnifier::isAxisEnabled( int axis ) const 00068 { 00069 if ( axis >= 0 && axis < QwtPlot::axisCnt ) 00070 return d_data->isAxisEnabled[axis]; 00071 00072 return true; 00073 } 00074 00076 QWidget *QwtPlotMagnifier::canvas() 00077 { 00078 return parentWidget(); 00079 } 00080 00082 const QWidget *QwtPlotMagnifier::canvas() const 00083 { 00084 return parentWidget(); 00085 } 00086 00088 QwtPlot *QwtPlotMagnifier::plot() 00089 { 00090 QWidget *w = canvas(); 00091 if ( w ) 00092 w = w->parentWidget(); 00093 00094 return qobject_cast<QwtPlot *>( w ); 00095 } 00096 00098 const QwtPlot *QwtPlotMagnifier::plot() const 00099 { 00100 const QWidget *w = canvas(); 00101 if ( w ) 00102 w = w->parentWidget(); 00103 00104 return qobject_cast<const QwtPlot *>( w ); 00105 } 00106 00111 void QwtPlotMagnifier::rescale( double factor ) 00112 { 00113 QwtPlot* plt = plot(); 00114 if ( plt == NULL ) 00115 return; 00116 00117 factor = qAbs( factor ); 00118 if ( factor == 1.0 || factor == 0.0 ) 00119 return; 00120 00121 bool doReplot = false; 00122 00123 const bool autoReplot = plt->autoReplot(); 00124 plt->setAutoReplot( false ); 00125 00126 for ( int axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) 00127 { 00128 if ( isAxisEnabled( axisId ) ) 00129 { 00130 const QwtScaleMap scaleMap = plt->canvasMap( axisId ); 00131 00132 double v1 = scaleMap.s1(); 00133 double v2 = scaleMap.s2(); 00134 00135 if ( scaleMap.transformation() ) 00136 { 00137 // the coordinate system of the paint device is always linear 00138 00139 v1 = scaleMap.transform( v1 ); // scaleMap.p1() 00140 v2 = scaleMap.transform( v2 ); // scaleMap.p2() 00141 } 00142 00143 const double center = 0.5 * ( v1 + v2 ); 00144 const double width_2 = 0.5 * ( v2 - v1 ) * factor; 00145 00146 v1 = center - width_2; 00147 v2 = center + width_2; 00148 00149 if ( scaleMap.transformation() ) 00150 { 00151 v1 = scaleMap.invTransform( v1 ); 00152 v2 = scaleMap.invTransform( v2 ); 00153 } 00154 00155 plt->setAxisScale( axisId, v1, v2 ); 00156 doReplot = true; 00157 } 00158 } 00159 00160 plt->setAutoReplot( autoReplot ); 00161 00162 if ( doReplot ) 00163 plt->replot(); 00164 }