Skip to content
Snippets Groups Projects
qwt_plot_magnifier.cpp 3.21 KiB
Newer Older
  • Learn to ignore specific revisions
  • pixhawk's avatar
    pixhawk committed
    /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
     * Qwt Widget Library
     * Copyright (C) 1997   Josef Wilgen
     * Copyright (C) 2002   Uwe Rathmann
     *
     * This library is free software; you can redistribute it and/or
     * modify it under the terms of the Qwt License, Version 1.0
     *****************************************************************************/
    
    #include "qwt_plot.h"
    #include "qwt_scale_div.h"
    #include "qwt_plot_magnifier.h"
    
    Bryant's avatar
    Bryant committed
    #include <qevent.h>
    
    pixhawk's avatar
    pixhawk committed
    
    class QwtPlotMagnifier::PrivateData
    {
    public:
    
    Bryant's avatar
    Bryant committed
        PrivateData()
        {
    
    pixhawk's avatar
    pixhawk committed
            for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
                isAxisEnabled[axis] = true;
        }
    
        bool isAxisEnabled[QwtPlot::axisCnt];
    };
    
    
    pixhawk's avatar
    pixhawk committed
       Constructor
       \param canvas Plot canvas to be magnified
    */
    
    Bryant's avatar
    Bryant committed
    QwtPlotMagnifier::QwtPlotMagnifier( QWidget *canvas ):
        QwtMagnifier( canvas )
    
    pixhawk's avatar
    pixhawk committed
    {
        d_data = new PrivateData();
    }
    
    //! Destructor
    QwtPlotMagnifier::~QwtPlotMagnifier()
    {
        delete d_data;
    }
    
    /*!
       \brief En/Disable an axis
    
    
    Bryant's avatar
    Bryant committed
       Only Axes that are enabled will be zoomed.
       All other axes will remain unchanged.
    
    pixhawk's avatar
    pixhawk committed
    
       \param axis Axis, see QwtPlot::Axis
       \param on On/Off
    
    
    Bryant's avatar
    Bryant committed
       \sa isAxisEnabled()
    
    pixhawk's avatar
    pixhawk committed
    */
    
    Bryant's avatar
    Bryant committed
    void QwtPlotMagnifier::setAxisEnabled( int axis, bool on )
    
    pixhawk's avatar
    pixhawk committed
    {
        if ( axis >= 0 && axis < QwtPlot::axisCnt )
            d_data->isAxisEnabled[axis] = on;
    }
    
    /*!
       Test if an axis is enabled
    
       \param axis Axis, see QwtPlot::Axis
       \return True, if the axis is enabled
    
    
    Bryant's avatar
    Bryant committed
       \sa setAxisEnabled()
    
    pixhawk's avatar
    pixhawk committed
    */
    
    Bryant's avatar
    Bryant committed
    bool QwtPlotMagnifier::isAxisEnabled( int axis ) const
    
    pixhawk's avatar
    pixhawk committed
    {
        if ( axis >= 0 && axis < QwtPlot::axisCnt )
            return d_data->isAxisEnabled[axis];
    
        return true;
    }
    
    //! Return observed plot canvas
    
    Bryant's avatar
    Bryant committed
    QWidget *QwtPlotMagnifier::canvas()
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        return parentWidget();
    
    pixhawk's avatar
    pixhawk committed
    }
    
    //! Return Observed plot canvas
    
    Bryant's avatar
    Bryant committed
    const QWidget *QwtPlotMagnifier::canvas() const
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        return parentWidget();
    
    pixhawk's avatar
    pixhawk committed
    }
    
    //! Return plot widget, containing the observed plot canvas
    QwtPlot *QwtPlotMagnifier::plot()
    {
    
    Bryant's avatar
    Bryant committed
        QWidget *w = canvas();
        if ( w )
            w = w->parentWidget();
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        return qobject_cast<QwtPlot *>( w );
    
    pixhawk's avatar
    pixhawk committed
    }
    
    //! Return plot widget, containing the observed plot canvas
    const QwtPlot *QwtPlotMagnifier::plot() const
    {
    
    Bryant's avatar
    Bryant committed
        const QWidget *w = canvas();
        if ( w )
            w = w->parentWidget();
    
        return qobject_cast<const QwtPlot *>( w );
    
    pixhawk's avatar
    pixhawk committed
    }
    
    
    pixhawk's avatar
    pixhawk committed
       Zoom in/out the axes scales
       \param factor A value < 1.0 zooms in, a value > 1.0 zooms out.
    */
    
    Bryant's avatar
    Bryant committed
    void QwtPlotMagnifier::rescale( double factor )
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        QwtPlot* plt = plot();
        if ( plt == NULL )
            return;
    
        factor = qAbs( factor );
    
    pixhawk's avatar
    pixhawk committed
        if ( factor == 1.0 || factor == 0.0 )
            return;
    
        bool doReplot = false;
    
        const bool autoReplot = plt->autoReplot();
    
    Bryant's avatar
    Bryant committed
        plt->setAutoReplot( false );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        for ( int axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
        {
            const QwtScaleDiv &scaleDiv = plt->axisScaleDiv( axisId );
            if ( isAxisEnabled( axisId ) )
            {
    
    pixhawk's avatar
    pixhawk committed
                const double center =
    
    Bryant's avatar
    Bryant committed
                    scaleDiv.lowerBound() + scaleDiv.range() / 2;
                const double width_2 = scaleDiv.range() / 2 * factor;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
                plt->setAxisScale( axisId, center - width_2, center + width_2 );
    
    pixhawk's avatar
    pixhawk committed
                doReplot = true;
            }
        }
    
    
    Bryant's avatar
    Bryant committed
        plt->setAutoReplot( autoReplot );
    
    pixhawk's avatar
    pixhawk committed
    
        if ( doReplot )
            plt->replot();
    }