double QwtPlotRescaler::pixelDist( int axis, const QSize &size ) const { const QwtInterval intv = intervalHint( axis ); double dist = 0.0; if ( !intv.isNull() ) { if ( axis == referenceAxis() ) dist = intv.width(); else { const double r = aspectRatio( axis ); if ( r > 0.0 ) dist = intv.width() * r; } } if ( dist > 0.0 ) { if ( orientation( axis ) == Qt::Horizontal ) dist /= size.width(); else dist /= size.height(); } return dist; }
/*! Synchronize an axis scale according to the scale of the reference axis \param axis Axis index ( see QwtPlot::AxisId ) \param reference Interval of the reference axis \param size Size of the canvas \return New interval for axis */ QwtInterval QwtPlotRescaler::syncScale( int axis, const QwtInterval& reference, const QSize &size ) const { double dist; if ( orientation( referenceAxis() ) == Qt::Horizontal ) dist = reference.width() / size.width(); else dist = reference.width() / size.height(); if ( orientation( axis ) == Qt::Horizontal ) dist *= size.width(); else dist *= size.height(); dist /= aspectRatio( axis ); QwtInterval intv; if ( rescalePolicy() == Fitting ) intv = intervalHint( axis ); else intv = interval( axis ); intv = expandInterval( intv, dist, expandingDirection( axis ) ); return intv; }
/*! Calculate the new scale interval of a plot axis \param axis Axis index ( see QwtPlot::AxisId ) \param oldSize Previous size of the canvas \param newSize New size of the canvas \return Calculated new interval for the axis */ QwtInterval QwtPlotRescaler::expandScale( int axis, const QSize &oldSize, const QSize &newSize ) const { const QwtInterval oldInterval = interval( axis ); QwtInterval expanded = oldInterval; switch ( rescalePolicy() ) { case Fixed: { break; // do nothing } case Expanding: { if ( !oldSize.isEmpty() ) { double width = oldInterval.width(); if ( orientation( axis ) == Qt::Horizontal ) width *= double( newSize.width() ) / oldSize.width(); else width *= double( newSize.height() ) / oldSize.height(); expanded = expandInterval( oldInterval, width, expandingDirection( axis ) ); } break; } case Fitting: { double dist = 0.0; for ( int ax = 0; ax < QwtPlot::axisCnt; ax++ ) { const double d = pixelDist( ax, newSize ); if ( d > dist ) dist = d; } if ( dist > 0.0 ) { double width; if ( orientation( axis ) == Qt::Horizontal ) width = newSize.width() * dist; else width = newSize.height() * dist; expanded = expandInterval( intervalHint( axis ), width, expandingDirection( axis ) ); } break; } } return expanded; }
//! Adjust the plot axes scales void QwtPlotRescaler::rescale() const { #if 0 const int axis = referenceAxis(); if ( axis < 0 || axis >= QwtPlot::axisCnt ) return; const QwtDoubleInterval hint = intervalHint(axis); if ( !hint.isNull() ) { QwtPlot *plt = (QwtPlot *)plot(); const bool doReplot = plt->autoReplot(); plt->setAutoReplot(false); plt->setAxisScale(axis, hint.minValue(), hint.maxValue()); plt->setAutoReplot(doReplot); plt->updateAxes(); } #endif const QSize size = canvas()->contentsRect().size(); rescale(size, size); }