qwt_scale_map.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include "qwt_scale_map.h"
00011 #include "qwt_math.h"
00012 #include <qrect.h>
00013 #include <qdebug.h>
00014 
00020 QwtScaleMap::QwtScaleMap():
00021     d_s1( 0.0 ),
00022     d_s2( 1.0 ),
00023     d_p1( 0.0 ),
00024     d_p2( 1.0 ),
00025     d_cnv( 1.0 ),
00026     d_ts1( 0.0 ),
00027     d_transform( NULL )
00028 {
00029 }
00030 
00032 QwtScaleMap::QwtScaleMap( const QwtScaleMap& other ):
00033     d_s1( other.d_s1 ),
00034     d_s2( other.d_s2 ),
00035     d_p1( other.d_p1 ),
00036     d_p2( other.d_p2 ),
00037     d_cnv( other.d_cnv ),
00038     d_ts1( other.d_ts1 ),
00039     d_transform( NULL )
00040 {
00041     if ( other.d_transform )
00042         d_transform = other.d_transform->copy();
00043 }
00044 
00048 QwtScaleMap::~QwtScaleMap()
00049 {
00050     delete d_transform;
00051 }
00052 
00054 QwtScaleMap &QwtScaleMap::operator=( const QwtScaleMap & other )
00055 {
00056     d_s1 = other.d_s1;
00057     d_s2 = other.d_s2;
00058     d_p1 = other.d_p1;
00059     d_p2 = other.d_p2;
00060     d_cnv = other.d_cnv;
00061     d_ts1 = other.d_ts1;
00062 
00063     delete d_transform;
00064     d_transform = NULL;
00065 
00066     if ( other.d_transform )
00067         d_transform = other.d_transform->copy();
00068 
00069     return *this;
00070 }
00071 
00075 void QwtScaleMap::setTransformation( QwtTransform *transform )
00076 {
00077     if ( transform != d_transform )
00078     {
00079         delete d_transform;
00080         d_transform = transform;
00081     }
00082 
00083     setScaleInterval( d_s1, d_s2 );
00084 }
00085 
00087 const QwtTransform *QwtScaleMap::transformation() const
00088 {
00089     return d_transform;
00090 }
00091 
00099 void QwtScaleMap::setScaleInterval( double s1, double s2 )
00100 {
00101     d_s1 = s1;
00102     d_s2 = s2;
00103 
00104     if ( d_transform )
00105     {
00106         d_s1 = d_transform->bounded( d_s1 );
00107         d_s2 = d_transform->bounded( d_s2 );
00108     }
00109 
00110     updateFactor();
00111 }
00112 
00118 void QwtScaleMap::setPaintInterval( double p1, double p2 )
00119 {
00120     d_p1 = p1;
00121     d_p2 = p2;
00122 
00123     updateFactor();
00124 }
00125 
00126 void QwtScaleMap::updateFactor()
00127 {
00128     d_ts1 = d_s1;
00129     double ts2 = d_s2;
00130 
00131     if ( d_transform )
00132     {
00133         d_ts1 = d_transform->transform( d_ts1 );
00134         ts2 = d_transform->transform( ts2 );
00135     }
00136 
00137     d_cnv = 1.0;
00138     if ( d_ts1 != ts2 )
00139         d_cnv = ( d_p2 - d_p1 ) / ( ts2 - d_ts1 );
00140 }
00141 
00152 QRectF QwtScaleMap::transform( const QwtScaleMap &xMap,
00153     const QwtScaleMap &yMap, const QRectF &rect )
00154 {
00155     double x1 = xMap.transform( rect.left() );
00156     double x2 = xMap.transform( rect.right() );
00157     double y1 = yMap.transform( rect.top() );
00158     double y2 = yMap.transform( rect.bottom() );
00159 
00160     if ( x2 < x1 )
00161         qSwap( x1, x2 );
00162     if ( y2 < y1 )
00163         qSwap( y1, y2 );
00164 
00165     if ( qwtFuzzyCompare( x1, 0.0, x2 - x1 ) == 0 )
00166         x1 = 0.0;
00167     if ( qwtFuzzyCompare( x2, 0.0, x2 - x1 ) == 0 )
00168         x2 = 0.0;
00169     if ( qwtFuzzyCompare( y1, 0.0, y2 - y1 ) == 0 )
00170         y1 = 0.0;
00171     if ( qwtFuzzyCompare( y2, 0.0, y2 - y1 ) == 0 )
00172         y2 = 0.0;
00173 
00174     return QRectF( x1, y1, x2 - x1 + 1, y2 - y1 + 1 );
00175 }
00176 
00186 QPointF QwtScaleMap::invTransform( const QwtScaleMap &xMap,
00187     const QwtScaleMap &yMap, const QPointF &pos )
00188 {
00189     return QPointF( 
00190         xMap.invTransform( pos.x() ), 
00191         yMap.invTransform( pos.y() ) 
00192     );
00193 }
00194 
00205 QPointF QwtScaleMap::transform( const QwtScaleMap &xMap,
00206     const QwtScaleMap &yMap, const QPointF &pos )
00207 {
00208     return QPointF( 
00209         xMap.transform( pos.x() ), 
00210         yMap.transform( pos.y() )
00211     );
00212 }
00213 
00223 QRectF QwtScaleMap::invTransform( const QwtScaleMap &xMap,
00224     const QwtScaleMap &yMap, const QRectF &rect )
00225 {
00226     const double x1 = xMap.invTransform( rect.left() );
00227     const double x2 = xMap.invTransform( rect.right() - 1 );
00228     const double y1 = yMap.invTransform( rect.top() );
00229     const double y2 = yMap.invTransform( rect.bottom() - 1 );
00230 
00231     const QRectF r( x1, y1, x2 - x1, y2 - y1 );
00232     return r.normalized();
00233 }
00234 
00235 #ifndef QT_NO_DEBUG_STREAM
00236 
00237 QDebug operator<<( QDebug debug, const QwtScaleMap &map )
00238 {
00239     debug.nospace() << "QwtScaleMap("
00240         << map.transformation()
00241         << ", s:" << map.s1() << "->" << map.s2()
00242         << ", p:" << map.p1() << "->" << map.p2()
00243         << ")";
00244 
00245     return debug.space();
00246 }
00247 
00248 #endif


plotjuggler
Author(s): Davide Faconti
autogenerated on Fri Sep 1 2017 02:41:56