qwt_plot_magnifier.cpp 3.21 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12
/* -*- 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
13
#include <qevent.h>
pixhawk's avatar
pixhawk committed
14 15 16 17

class QwtPlotMagnifier::PrivateData
{
public:
Bryant's avatar
Bryant committed
18 19
    PrivateData()
    {
pixhawk's avatar
pixhawk committed
20 21 22 23 24 25 26
        for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
            isAxisEnabled[axis] = true;
    }

    bool isAxisEnabled[QwtPlot::axisCnt];
};

27
/*!
pixhawk's avatar
pixhawk committed
28 29 30
   Constructor
   \param canvas Plot canvas to be magnified
*/
Bryant's avatar
Bryant committed
31 32
QwtPlotMagnifier::QwtPlotMagnifier( QWidget *canvas ):
    QwtMagnifier( canvas )
pixhawk's avatar
pixhawk committed
33 34 35 36 37 38 39 40 41 42 43 44 45
{
    d_data = new PrivateData();
}

//! Destructor
QwtPlotMagnifier::~QwtPlotMagnifier()
{
    delete d_data;
}

/*!
   \brief En/Disable an axis

Bryant's avatar
Bryant committed
46 47
   Only Axes that are enabled will be zoomed.
   All other axes will remain unchanged.
pixhawk's avatar
pixhawk committed
48 49 50 51

   \param axis Axis, see QwtPlot::Axis
   \param on On/Off

Bryant's avatar
Bryant committed
52
   \sa isAxisEnabled()
pixhawk's avatar
pixhawk committed
53
*/
Bryant's avatar
Bryant committed
54
void QwtPlotMagnifier::setAxisEnabled( int axis, bool on )
pixhawk's avatar
pixhawk committed
55 56 57 58 59 60 61 62 63 64 65
{
    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
66
   \sa setAxisEnabled()
pixhawk's avatar
pixhawk committed
67
*/
Bryant's avatar
Bryant committed
68
bool QwtPlotMagnifier::isAxisEnabled( int axis ) const
pixhawk's avatar
pixhawk committed
69 70 71 72 73 74 75 76
{
    if ( axis >= 0 && axis < QwtPlot::axisCnt )
        return d_data->isAxisEnabled[axis];

    return true;
}

//! Return observed plot canvas
Bryant's avatar
Bryant committed
77
QWidget *QwtPlotMagnifier::canvas()
pixhawk's avatar
pixhawk committed
78
{
Bryant's avatar
Bryant committed
79
    return parentWidget();
pixhawk's avatar
pixhawk committed
80 81 82
}

//! Return Observed plot canvas
Bryant's avatar
Bryant committed
83
const QWidget *QwtPlotMagnifier::canvas() const
pixhawk's avatar
pixhawk committed
84
{
Bryant's avatar
Bryant committed
85
    return parentWidget();
pixhawk's avatar
pixhawk committed
86 87 88 89 90
}

//! Return plot widget, containing the observed plot canvas
QwtPlot *QwtPlotMagnifier::plot()
{
Bryant's avatar
Bryant committed
91 92 93
    QWidget *w = canvas();
    if ( w )
        w = w->parentWidget();
pixhawk's avatar
pixhawk committed
94

Bryant's avatar
Bryant committed
95
    return qobject_cast<QwtPlot *>( w );
pixhawk's avatar
pixhawk committed
96 97 98 99 100
}

//! Return plot widget, containing the observed plot canvas
const QwtPlot *QwtPlotMagnifier::plot() const
{
Bryant's avatar
Bryant committed
101 102 103 104 105
    const QWidget *w = canvas();
    if ( w )
        w = w->parentWidget();

    return qobject_cast<const QwtPlot *>( w );
pixhawk's avatar
pixhawk committed
106 107
}

108
/*!
pixhawk's avatar
pixhawk committed
109 110 111
   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
112
void QwtPlotMagnifier::rescale( double factor )
pixhawk's avatar
pixhawk committed
113
{
Bryant's avatar
Bryant committed
114 115 116 117 118
    QwtPlot* plt = plot();
    if ( plt == NULL )
        return;

    factor = qAbs( factor );
pixhawk's avatar
pixhawk committed
119 120 121 122 123 124
    if ( factor == 1.0 || factor == 0.0 )
        return;

    bool doReplot = false;

    const bool autoReplot = plt->autoReplot();
Bryant's avatar
Bryant committed
125
    plt->setAutoReplot( false );
pixhawk's avatar
pixhawk committed
126

Bryant's avatar
Bryant committed
127 128 129 130 131
    for ( int axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
    {
        const QwtScaleDiv &scaleDiv = plt->axisScaleDiv( axisId );
        if ( isAxisEnabled( axisId ) )
        {
pixhawk's avatar
pixhawk committed
132
            const double center =
Bryant's avatar
Bryant committed
133 134
                scaleDiv.lowerBound() + scaleDiv.range() / 2;
            const double width_2 = scaleDiv.range() / 2 * factor;
pixhawk's avatar
pixhawk committed
135

Bryant's avatar
Bryant committed
136
            plt->setAxisScale( axisId, center - width_2, center + width_2 );
pixhawk's avatar
pixhawk committed
137 138 139 140
            doReplot = true;
        }
    }

Bryant's avatar
Bryant committed
141
    plt->setAutoReplot( autoReplot );
pixhawk's avatar
pixhawk committed
142 143 144 145

    if ( doReplot )
        plt->replot();
}