[4271] | 1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
| 2 | * Qwt Widget Library
|
---|
| 3 | * Copyright (C) 1997 Josef Wilgen
|
---|
| 4 | * Copyright (C) 2002 Uwe Rathmann
|
---|
| 5 | *
|
---|
| 6 | * This library is free software; you can redistribute it and/or
|
---|
| 7 | * modify it under the terms of the Qwt License, Version 1.0
|
---|
| 8 | *****************************************************************************/
|
---|
| 9 |
|
---|
| 10 | #include "qwt_plot.h"
|
---|
| 11 | #include "qwt_plot_canvas.h"
|
---|
| 12 | #include "qwt_scale_div.h"
|
---|
| 13 | #include "qwt_plot_magnifier.h"
|
---|
| 14 | #include <qevent.h>
|
---|
| 15 |
|
---|
| 16 | class QwtPlotMagnifier::PrivateData
|
---|
| 17 | {
|
---|
| 18 | public:
|
---|
| 19 | PrivateData()
|
---|
| 20 | {
|
---|
| 21 | for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
|
---|
| 22 | isAxisEnabled[axis] = true;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | bool isAxisEnabled[QwtPlot::axisCnt];
|
---|
| 26 | };
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
| 29 | Constructor
|
---|
| 30 | \param canvas Plot canvas to be magnified
|
---|
| 31 | */
|
---|
| 32 | QwtPlotMagnifier::QwtPlotMagnifier( QwtPlotCanvas *canvas ):
|
---|
| 33 | QwtMagnifier( canvas )
|
---|
| 34 | {
|
---|
| 35 | d_data = new PrivateData();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | //! Destructor
|
---|
| 39 | QwtPlotMagnifier::~QwtPlotMagnifier()
|
---|
| 40 | {
|
---|
| 41 | delete d_data;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /*!
|
---|
| 45 | \brief En/Disable an axis
|
---|
| 46 |
|
---|
| 47 | Only Axes that are enabled will be zoomed.
|
---|
| 48 | All other axes will remain unchanged.
|
---|
| 49 |
|
---|
| 50 | \param axis Axis, see QwtPlot::Axis
|
---|
| 51 | \param on On/Off
|
---|
| 52 |
|
---|
| 53 | \sa isAxisEnabled()
|
---|
| 54 | */
|
---|
| 55 | void QwtPlotMagnifier::setAxisEnabled( int axis, bool on )
|
---|
| 56 | {
|
---|
| 57 | if ( axis >= 0 && axis < QwtPlot::axisCnt )
|
---|
| 58 | d_data->isAxisEnabled[axis] = on;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /*!
|
---|
| 62 | Test if an axis is enabled
|
---|
| 63 |
|
---|
| 64 | \param axis Axis, see QwtPlot::Axis
|
---|
| 65 | \return True, if the axis is enabled
|
---|
| 66 |
|
---|
| 67 | \sa setAxisEnabled()
|
---|
| 68 | */
|
---|
| 69 | bool QwtPlotMagnifier::isAxisEnabled( int axis ) const
|
---|
| 70 | {
|
---|
| 71 | if ( axis >= 0 && axis < QwtPlot::axisCnt )
|
---|
| 72 | return d_data->isAxisEnabled[axis];
|
---|
| 73 |
|
---|
| 74 | return true;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | //! Return observed plot canvas
|
---|
| 78 | QwtPlotCanvas *QwtPlotMagnifier::canvas()
|
---|
| 79 | {
|
---|
| 80 | return qobject_cast<QwtPlotCanvas *>( parent() );
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | //! Return Observed plot canvas
|
---|
| 84 | const QwtPlotCanvas *QwtPlotMagnifier::canvas() const
|
---|
| 85 | {
|
---|
| 86 | return qobject_cast<const QwtPlotCanvas *>( parent() );
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | //! Return plot widget, containing the observed plot canvas
|
---|
| 90 | QwtPlot *QwtPlotMagnifier::plot()
|
---|
| 91 | {
|
---|
| 92 | QwtPlotCanvas *w = canvas();
|
---|
| 93 | if ( w )
|
---|
| 94 | return w->plot();
|
---|
| 95 |
|
---|
| 96 | return NULL;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | //! Return plot widget, containing the observed plot canvas
|
---|
| 100 | const QwtPlot *QwtPlotMagnifier::plot() const
|
---|
| 101 | {
|
---|
| 102 | const QwtPlotCanvas *w = canvas();
|
---|
| 103 | if ( w )
|
---|
| 104 | return w->plot();
|
---|
| 105 |
|
---|
| 106 | return NULL;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /*!
|
---|
| 110 | Zoom in/out the axes scales
|
---|
| 111 | \param factor A value < 1.0 zooms in, a value > 1.0 zooms out.
|
---|
| 112 | */
|
---|
| 113 | void QwtPlotMagnifier::rescale( double factor )
|
---|
| 114 | {
|
---|
| 115 | factor = qAbs( factor );
|
---|
| 116 | if ( factor == 1.0 || factor == 0.0 )
|
---|
| 117 | return;
|
---|
| 118 |
|
---|
| 119 | bool doReplot = false;
|
---|
| 120 | QwtPlot* plt = plot();
|
---|
| 121 |
|
---|
| 122 | const bool autoReplot = plt->autoReplot();
|
---|
| 123 | plt->setAutoReplot( false );
|
---|
| 124 |
|
---|
| 125 | for ( int axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
|
---|
| 126 | {
|
---|
| 127 | const QwtScaleDiv *scaleDiv = plt->axisScaleDiv( axisId );
|
---|
| 128 | if ( isAxisEnabled( axisId ) && scaleDiv->isValid() )
|
---|
| 129 | {
|
---|
| 130 | const double center =
|
---|
| 131 | scaleDiv->lowerBound() + scaleDiv->range() / 2;
|
---|
| 132 | const double width_2 = scaleDiv->range() / 2 * factor;
|
---|
| 133 |
|
---|
| 134 | plt->setAxisScale( axisId, center - width_2, center + width_2 );
|
---|
| 135 | doReplot = true;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | plt->setAutoReplot( autoReplot );
|
---|
| 140 |
|
---|
| 141 | if ( doReplot )
|
---|
| 142 | plt->replot();
|
---|
| 143 | }
|
---|