qwt_plot_magnifier.cpp

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 // vim: expandtab
00011 
00012 #include <math.h>
00013 #include <qevent.h>
00014 #include "qwt_plot.h"
00015 #include "qwt_plot_canvas.h"
00016 #include "qwt_scale_div.h"
00017 #include "qwt_plot_magnifier.h"
00018 
00019 class QwtPlotMagnifier::PrivateData
00020 {
00021 public:
00022     PrivateData()
00023     {
00024         for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
00025             isAxisEnabled[axis] = true;
00026     }
00027 
00028     bool isAxisEnabled[QwtPlot::axisCnt];
00029 };
00030 
00035 QwtPlotMagnifier::QwtPlotMagnifier(QwtPlotCanvas *canvas):
00036     QwtMagnifier(canvas)
00037 {
00038     d_data = new PrivateData();
00039 }
00040 
00042 QwtPlotMagnifier::~QwtPlotMagnifier()
00043 {
00044     delete d_data;
00045 }
00046 
00058 void QwtPlotMagnifier::setAxisEnabled(int axis, bool on)
00059 {
00060     if ( axis >= 0 && axis < QwtPlot::axisCnt )
00061         d_data->isAxisEnabled[axis] = on;
00062 }
00063 
00072 bool QwtPlotMagnifier::isAxisEnabled(int axis) const
00073 {
00074     if ( axis >= 0 && axis < QwtPlot::axisCnt )
00075         return d_data->isAxisEnabled[axis];
00076 
00077     return true;
00078 }
00079 
00081 QwtPlotCanvas *QwtPlotMagnifier::canvas()
00082 {
00083     QObject *w = parent();
00084     if ( w && w->inherits("QwtPlotCanvas") )
00085         return (QwtPlotCanvas *)w;
00086 
00087     return NULL;
00088 }
00089 
00091 const QwtPlotCanvas *QwtPlotMagnifier::canvas() const
00092 {
00093     return ((QwtPlotMagnifier *)this)->canvas();
00094 }
00095 
00097 QwtPlot *QwtPlotMagnifier::plot()
00098 {
00099     QObject *w = canvas();
00100     if ( w )
00101     {
00102         w = w->parent();
00103         if ( w && w->inherits("QwtPlot") )
00104             return (QwtPlot *)w;
00105     }
00106 
00107     return NULL;
00108 }
00109 
00111 const QwtPlot *QwtPlotMagnifier::plot() const
00112 {
00113     return ((QwtPlotMagnifier *)this)->plot();
00114 }
00115 
00120 void QwtPlotMagnifier::rescale(double factor)
00121 {
00122     factor = qwtAbs(factor);
00123     if ( factor == 1.0 || factor == 0.0 )
00124         return;
00125 
00126     bool doReplot = false;
00127     QwtPlot* plt = plot();
00128 
00129     const bool autoReplot = plt->autoReplot();
00130     plt->setAutoReplot(false);
00131 
00132     for ( int axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
00133     {
00134         const QwtScaleDiv *scaleDiv = plt->axisScaleDiv(axisId);
00135         if ( isAxisEnabled(axisId) && scaleDiv->isValid() )
00136         {
00137             const double center =
00138                 scaleDiv->lBound() + scaleDiv->range() / 2;
00139             const double width_2 = scaleDiv->range() / 2 * factor;
00140 
00141             plt->setAxisScale(axisId, center - width_2, center + width_2);
00142             doReplot = true;
00143         }
00144     }
00145 
00146     plt->setAutoReplot(autoReplot);
00147 
00148     if ( doReplot )
00149         plt->replot();
00150 }

Generated on Sun Mar 22 16:54:07 2009 for Qwt User's Guide by  doxygen 1.5.0