| 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_scale_map.h"
|
|---|
| 11 | #include "qwt_math.h"
|
|---|
| 12 | #include <qrect.h>
|
|---|
| 13 | #include <qdebug.h>
|
|---|
| 14 |
|
|---|
| 15 | /*!
|
|---|
| 16 | \brief Constructor
|
|---|
| 17 |
|
|---|
| 18 | The scale and paint device intervals are both set to [0,1].
|
|---|
| 19 | */
|
|---|
| 20 | QwtScaleMap::QwtScaleMap():
|
|---|
| 21 | d_s1( 0.0 ),
|
|---|
| 22 | d_s2( 1.0 ),
|
|---|
| 23 | d_p1( 0.0 ),
|
|---|
| 24 | d_p2( 1.0 ),
|
|---|
| 25 | d_cnv( 1.0 ),
|
|---|
| 26 | d_ts1( 0.0 ),
|
|---|
| 27 | d_transform( NULL )
|
|---|
| 28 | {
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | //! Copy constructor
|
|---|
| 32 | QwtScaleMap::QwtScaleMap( const QwtScaleMap& other ):
|
|---|
| 33 | d_s1( other.d_s1 ),
|
|---|
| 34 | d_s2( other.d_s2 ),
|
|---|
| 35 | d_p1( other.d_p1 ),
|
|---|
| 36 | d_p2( other.d_p2 ),
|
|---|
| 37 | d_cnv( other.d_cnv ),
|
|---|
| 38 | d_ts1( other.d_ts1 ),
|
|---|
| 39 | d_transform( NULL )
|
|---|
| 40 | {
|
|---|
| 41 | if ( other.d_transform )
|
|---|
| 42 | d_transform = other.d_transform->copy();
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | /*!
|
|---|
| 46 | Destructor
|
|---|
| 47 | */
|
|---|
| 48 | QwtScaleMap::~QwtScaleMap()
|
|---|
| 49 | {
|
|---|
| 50 | delete d_transform;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | //! Assignment operator
|
|---|
| 54 | QwtScaleMap &QwtScaleMap::operator=( const QwtScaleMap & other )
|
|---|
| 55 | {
|
|---|
| 56 | d_s1 = other.d_s1;
|
|---|
| 57 | d_s2 = other.d_s2;
|
|---|
| 58 | d_p1 = other.d_p1;
|
|---|
| 59 | d_p2 = other.d_p2;
|
|---|
| 60 | d_cnv = other.d_cnv;
|
|---|
| 61 | d_ts1 = other.d_ts1;
|
|---|
| 62 |
|
|---|
| 63 | delete d_transform;
|
|---|
| 64 | d_transform = NULL;
|
|---|
| 65 |
|
|---|
| 66 | if ( other.d_transform )
|
|---|
| 67 | d_transform = other.d_transform->copy();
|
|---|
| 68 |
|
|---|
| 69 | return *this;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /*!
|
|---|
| 73 | Initialize the map with a transformation
|
|---|
| 74 | */
|
|---|
| 75 | void QwtScaleMap::setTransformation( QwtTransform *transform )
|
|---|
| 76 | {
|
|---|
| 77 | if ( transform != d_transform )
|
|---|
| 78 | {
|
|---|
| 79 | delete d_transform;
|
|---|
| 80 | d_transform = transform;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | setScaleInterval( d_s1, d_s2 );
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | //! Get the transformation
|
|---|
| 87 | const QwtTransform *QwtScaleMap::transformation() const
|
|---|
| 88 | {
|
|---|
| 89 | return d_transform;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /*!
|
|---|
| 93 | \brief Specify the borders of the scale interval
|
|---|
| 94 | \param s1 first border
|
|---|
| 95 | \param s2 second border
|
|---|
| 96 | \warning scales might be aligned to
|
|---|
| 97 | transformation depending boundaries
|
|---|
| 98 | */
|
|---|
| 99 | void QwtScaleMap::setScaleInterval( double s1, double s2 )
|
|---|
| 100 | {
|
|---|
| 101 | d_s1 = s1;
|
|---|
| 102 | d_s2 = s2;
|
|---|
| 103 |
|
|---|
| 104 | if ( d_transform )
|
|---|
| 105 | {
|
|---|
| 106 | d_s1 = d_transform->bounded( d_s1 );
|
|---|
| 107 | d_s2 = d_transform->bounded( d_s2 );
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | updateFactor();
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /*!
|
|---|
| 114 | \brief Specify the borders of the paint device interval
|
|---|
| 115 | \param p1 first border
|
|---|
| 116 | \param p2 second border
|
|---|
| 117 | */
|
|---|
| 118 | void QwtScaleMap::setPaintInterval( double p1, double p2 )
|
|---|
| 119 | {
|
|---|
| 120 | d_p1 = p1;
|
|---|
| 121 | d_p2 = p2;
|
|---|
| 122 |
|
|---|
| 123 | updateFactor();
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | void QwtScaleMap::updateFactor()
|
|---|
| 127 | {
|
|---|
| 128 | d_ts1 = d_s1;
|
|---|
| 129 | double ts2 = d_s2;
|
|---|
| 130 |
|
|---|
| 131 | if ( d_transform )
|
|---|
| 132 | {
|
|---|
| 133 | d_ts1 = d_transform->transform( d_ts1 );
|
|---|
| 134 | ts2 = d_transform->transform( ts2 );
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | d_cnv = 1.0;
|
|---|
| 138 | if ( d_ts1 != ts2 )
|
|---|
| 139 | d_cnv = ( d_p2 - d_p1 ) / ( ts2 - d_ts1 );
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /*!
|
|---|
| 143 | Transform a rectangle from scale to paint coordinates
|
|---|
| 144 |
|
|---|
| 145 | \param xMap X map
|
|---|
| 146 | \param yMap Y map
|
|---|
| 147 | \param rect Rectangle in scale coordinates
|
|---|
| 148 | \return Rectangle in paint coordinates
|
|---|
| 149 |
|
|---|
| 150 | \sa invTransform()
|
|---|
| 151 | */
|
|---|
| 152 | QRectF QwtScaleMap::transform( const QwtScaleMap &xMap,
|
|---|
| 153 | const QwtScaleMap &yMap, const QRectF &rect )
|
|---|
| 154 | {
|
|---|
| 155 | double x1 = xMap.transform( rect.left() );
|
|---|
| 156 | double x2 = xMap.transform( rect.right() );
|
|---|
| 157 | double y1 = yMap.transform( rect.top() );
|
|---|
| 158 | double y2 = yMap.transform( rect.bottom() );
|
|---|
| 159 |
|
|---|
| 160 | if ( x2 < x1 )
|
|---|
| 161 | qSwap( x1, x2 );
|
|---|
| 162 | if ( y2 < y1 )
|
|---|
| 163 | qSwap( y1, y2 );
|
|---|
| 164 |
|
|---|
| 165 | if ( qwtFuzzyCompare( x1, 0.0, x2 - x1 ) == 0 )
|
|---|
| 166 | x1 = 0.0;
|
|---|
| 167 | if ( qwtFuzzyCompare( x2, 0.0, x2 - x1 ) == 0 )
|
|---|
| 168 | x2 = 0.0;
|
|---|
| 169 | if ( qwtFuzzyCompare( y1, 0.0, y2 - y1 ) == 0 )
|
|---|
| 170 | y1 = 0.0;
|
|---|
| 171 | if ( qwtFuzzyCompare( y2, 0.0, y2 - y1 ) == 0 )
|
|---|
| 172 | y2 = 0.0;
|
|---|
| 173 |
|
|---|
| 174 | return QRectF( x1, y1, x2 - x1 + 1, y2 - y1 + 1 );
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /*!
|
|---|
| 178 | Transform a rectangle from paint to scale coordinates
|
|---|
| 179 |
|
|---|
| 180 | \param xMap X map
|
|---|
| 181 | \param yMap Y map
|
|---|
| 182 | \param pos Position in paint coordinates
|
|---|
| 183 | \return Position in scale coordinates
|
|---|
| 184 | \sa transform()
|
|---|
| 185 | */
|
|---|
| 186 | QPointF QwtScaleMap::invTransform( const QwtScaleMap &xMap,
|
|---|
| 187 | const QwtScaleMap &yMap, const QPointF &pos )
|
|---|
| 188 | {
|
|---|
| 189 | return QPointF(
|
|---|
| 190 | xMap.invTransform( pos.x() ),
|
|---|
| 191 | yMap.invTransform( pos.y() )
|
|---|
| 192 | );
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /*!
|
|---|
| 196 | Transform a point from scale to paint coordinates
|
|---|
| 197 |
|
|---|
| 198 | \param xMap X map
|
|---|
| 199 | \param yMap Y map
|
|---|
| 200 | \param pos Position in scale coordinates
|
|---|
| 201 | \return Position in paint coordinates
|
|---|
| 202 |
|
|---|
| 203 | \sa invTransform()
|
|---|
| 204 | */
|
|---|
| 205 | QPointF QwtScaleMap::transform( const QwtScaleMap &xMap,
|
|---|
| 206 | const QwtScaleMap &yMap, const QPointF &pos )
|
|---|
| 207 | {
|
|---|
| 208 | return QPointF(
|
|---|
| 209 | xMap.transform( pos.x() ),
|
|---|
| 210 | yMap.transform( pos.y() )
|
|---|
| 211 | );
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /*!
|
|---|
| 215 | Transform a rectangle from paint to scale coordinates
|
|---|
| 216 |
|
|---|
| 217 | \param xMap X map
|
|---|
| 218 | \param yMap Y map
|
|---|
| 219 | \param rect Rectangle in paint coordinates
|
|---|
| 220 | \return Rectangle in scale coordinates
|
|---|
| 221 | \sa transform()
|
|---|
| 222 | */
|
|---|
| 223 | QRectF QwtScaleMap::invTransform( const QwtScaleMap &xMap,
|
|---|
| 224 | const QwtScaleMap &yMap, const QRectF &rect )
|
|---|
| 225 | {
|
|---|
| 226 | const double x1 = xMap.invTransform( rect.left() );
|
|---|
| 227 | const double x2 = xMap.invTransform( rect.right() - 1 );
|
|---|
| 228 | const double y1 = yMap.invTransform( rect.top() );
|
|---|
| 229 | const double y2 = yMap.invTransform( rect.bottom() - 1 );
|
|---|
| 230 |
|
|---|
| 231 | const QRectF r( x1, y1, x2 - x1, y2 - y1 );
|
|---|
| 232 | return r.normalized();
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | #ifndef QT_NO_DEBUG_STREAM
|
|---|
| 236 |
|
|---|
| 237 | QDebug operator<<( QDebug debug, const QwtScaleMap &map )
|
|---|
| 238 | {
|
|---|
| 239 | debug.nospace() << "QwtScaleMap("
|
|---|
| 240 | << map.transformation()
|
|---|
| 241 | << ", s:" << map.s1() << "->" << map.s2()
|
|---|
| 242 | << ", p:" << map.p1() << "->" << map.p2()
|
|---|
| 243 | << ")";
|
|---|
| 244 |
|
|---|
| 245 | return debug.space();
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | #endif
|
|---|