[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_scale_div.h"
|
---|
| 12 | #include "qwt_plot_magnifier.h"
|
---|
| 13 | #include <qevent.h>
|
---|
| 14 |
|
---|
| 15 | class QwtPlotMagnifier::PrivateData
|
---|
| 16 | {
|
---|
| 17 | public:
|
---|
| 18 | PrivateData()
|
---|
| 19 | {
|
---|
| 20 | for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
|
---|
| 21 | isAxisEnabled[axis] = true;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | bool isAxisEnabled[QwtPlot::axisCnt];
|
---|
| 25 | };
|
---|
| 26 |
|
---|
| 27 | /*!
|
---|
| 28 | Constructor
|
---|
| 29 | \param canvas Plot canvas to be magnified
|
---|
| 30 | */
|
---|
[8127] | 31 | QwtPlotMagnifier::QwtPlotMagnifier( QWidget *canvas ):
|
---|
[4271] | 32 | QwtMagnifier( canvas )
|
---|
| 33 | {
|
---|
| 34 | d_data = new PrivateData();
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | //! Destructor
|
---|
| 38 | QwtPlotMagnifier::~QwtPlotMagnifier()
|
---|
| 39 | {
|
---|
| 40 | delete d_data;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /*!
|
---|
| 44 | \brief En/Disable an axis
|
---|
| 45 |
|
---|
| 46 | Only Axes that are enabled will be zoomed.
|
---|
| 47 | All other axes will remain unchanged.
|
---|
| 48 |
|
---|
| 49 | \param axis Axis, see QwtPlot::Axis
|
---|
| 50 | \param on On/Off
|
---|
| 51 |
|
---|
| 52 | \sa isAxisEnabled()
|
---|
| 53 | */
|
---|
| 54 | void QwtPlotMagnifier::setAxisEnabled( int axis, bool on )
|
---|
| 55 | {
|
---|
| 56 | if ( axis >= 0 && axis < QwtPlot::axisCnt )
|
---|
| 57 | d_data->isAxisEnabled[axis] = on;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /*!
|
---|
| 61 | Test if an axis is enabled
|
---|
| 62 |
|
---|
| 63 | \param axis Axis, see QwtPlot::Axis
|
---|
| 64 | \return True, if the axis is enabled
|
---|
| 65 |
|
---|
| 66 | \sa setAxisEnabled()
|
---|
| 67 | */
|
---|
| 68 | bool QwtPlotMagnifier::isAxisEnabled( int axis ) const
|
---|
| 69 | {
|
---|
| 70 | if ( axis >= 0 && axis < QwtPlot::axisCnt )
|
---|
| 71 | return d_data->isAxisEnabled[axis];
|
---|
| 72 |
|
---|
| 73 | return true;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | //! Return observed plot canvas
|
---|
[8127] | 77 | QWidget *QwtPlotMagnifier::canvas()
|
---|
[4271] | 78 | {
|
---|
[8127] | 79 | return parentWidget();
|
---|
[4271] | 80 | }
|
---|
| 81 |
|
---|
| 82 | //! Return Observed plot canvas
|
---|
[8127] | 83 | const QWidget *QwtPlotMagnifier::canvas() const
|
---|
[4271] | 84 | {
|
---|
[8127] | 85 | return parentWidget();
|
---|
[4271] | 86 | }
|
---|
| 87 |
|
---|
| 88 | //! Return plot widget, containing the observed plot canvas
|
---|
| 89 | QwtPlot *QwtPlotMagnifier::plot()
|
---|
| 90 | {
|
---|
[8127] | 91 | QWidget *w = canvas();
|
---|
[4271] | 92 | if ( w )
|
---|
[8127] | 93 | w = w->parentWidget();
|
---|
[4271] | 94 |
|
---|
[8127] | 95 | return qobject_cast<QwtPlot *>( w );
|
---|
[4271] | 96 | }
|
---|
| 97 |
|
---|
| 98 | //! Return plot widget, containing the observed plot canvas
|
---|
| 99 | const QwtPlot *QwtPlotMagnifier::plot() const
|
---|
| 100 | {
|
---|
[8127] | 101 | const QWidget *w = canvas();
|
---|
[4271] | 102 | if ( w )
|
---|
[8127] | 103 | w = w->parentWidget();
|
---|
[4271] | 104 |
|
---|
[8127] | 105 | return qobject_cast<const QwtPlot *>( w );
|
---|
[4271] | 106 | }
|
---|
| 107 |
|
---|
| 108 | /*!
|
---|
| 109 | Zoom in/out the axes scales
|
---|
| 110 | \param factor A value < 1.0 zooms in, a value > 1.0 zooms out.
|
---|
| 111 | */
|
---|
| 112 | void QwtPlotMagnifier::rescale( double factor )
|
---|
| 113 | {
|
---|
[8127] | 114 | QwtPlot* plt = plot();
|
---|
| 115 | if ( plt == NULL )
|
---|
| 116 | return;
|
---|
| 117 |
|
---|
[4271] | 118 | factor = qAbs( factor );
|
---|
| 119 | if ( factor == 1.0 || factor == 0.0 )
|
---|
| 120 | return;
|
---|
| 121 |
|
---|
| 122 | bool doReplot = false;
|
---|
| 123 |
|
---|
| 124 | const bool autoReplot = plt->autoReplot();
|
---|
| 125 | plt->setAutoReplot( false );
|
---|
| 126 |
|
---|
| 127 | for ( int axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
|
---|
| 128 | {
|
---|
[8127] | 129 | if ( isAxisEnabled( axisId ) )
|
---|
[4271] | 130 | {
|
---|
[8127] | 131 | const QwtScaleMap scaleMap = plt->canvasMap( axisId );
|
---|
[4271] | 132 |
|
---|
[8127] | 133 | double v1 = scaleMap.s1();
|
---|
| 134 | double v2 = scaleMap.s2();
|
---|
| 135 |
|
---|
| 136 | if ( scaleMap.transformation() )
|
---|
| 137 | {
|
---|
| 138 | // the coordinate system of the paint device is always linear
|
---|
| 139 |
|
---|
| 140 | v1 = scaleMap.transform( v1 ); // scaleMap.p1()
|
---|
| 141 | v2 = scaleMap.transform( v2 ); // scaleMap.p2()
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | const double center = 0.5 * ( v1 + v2 );
|
---|
| 145 | const double width_2 = 0.5 * ( v2 - v1 ) * factor;
|
---|
| 146 |
|
---|
| 147 | v1 = center - width_2;
|
---|
| 148 | v2 = center + width_2;
|
---|
| 149 |
|
---|
| 150 | if ( scaleMap.transformation() )
|
---|
| 151 | {
|
---|
| 152 | v1 = scaleMap.invTransform( v1 );
|
---|
| 153 | v2 = scaleMap.invTransform( v2 );
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | plt->setAxisScale( axisId, v1, v2 );
|
---|
[4271] | 157 | doReplot = true;
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | plt->setAutoReplot( autoReplot );
|
---|
| 162 |
|
---|
| 163 | if ( doReplot )
|
---|
| 164 | plt->replot();
|
---|
| 165 | }
|
---|