qwt_scale_map.cpp 5.48 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
2
3
4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6
7
8
9
10
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_scale_map.h"
Bryant's avatar
Bryant committed
11
12
13
#include "qwt_math.h"
#include <qrect.h>
#include <qdebug.h>
pixhawk's avatar
pixhawk committed
14
15
16
17
18
19
20

/*!
  \brief Constructor

  The scale and paint device intervals are both set to [0,1].
*/
QwtScaleMap::QwtScaleMap():
Bryant's avatar
Bryant committed
21
22
23
24
25
26
27
    d_s1( 0.0 ),
    d_s2( 1.0 ),
    d_p1( 0.0 ),
    d_p2( 1.0 ),
    d_cnv( 1.0 ),
    d_ts1( 0.0 ),
    d_transform( NULL )
pixhawk's avatar
pixhawk committed
28
29
30
{
}

Bryant's avatar
Bryant committed
31
32
33
34
35
36
37
38
39
//! Copy constructor
QwtScaleMap::QwtScaleMap( const QwtScaleMap& other ):
    d_s1( other.d_s1 ),
    d_s2( other.d_s2 ),
    d_p1( other.d_p1 ),
    d_p2( other.d_p2 ),
    d_cnv( other.d_cnv ),
    d_ts1( other.d_ts1 ),
    d_transform( NULL )
pixhawk's avatar
pixhawk committed
40
{
Bryant's avatar
Bryant committed
41
42
    if ( other.d_transform )
        d_transform = other.d_transform->copy();
pixhawk's avatar
pixhawk committed
43
44
45
46
47
48
49
}

/*!
  Destructor
*/
QwtScaleMap::~QwtScaleMap()
{
Bryant's avatar
Bryant committed
50
    delete d_transform;
pixhawk's avatar
pixhawk committed
51
52
}

Bryant's avatar
Bryant committed
53
54
//! Assignment operator
QwtScaleMap &QwtScaleMap::operator=( const QwtScaleMap & other )
pixhawk's avatar
pixhawk committed
55
56
57
58
59
60
{
    d_s1 = other.d_s1;
    d_s2 = other.d_s2;
    d_p1 = other.d_p1;
    d_p2 = other.d_p2;
    d_cnv = other.d_cnv;
Bryant's avatar
Bryant committed
61
    d_ts1 = other.d_ts1;
pixhawk's avatar
pixhawk committed
62

Bryant's avatar
Bryant committed
63
64
65
66
67
    delete d_transform;
    d_transform = NULL;

    if ( other.d_transform )
        d_transform = other.d_transform->copy();
pixhawk's avatar
pixhawk committed
68
69
70
71
72
73
74

    return *this;
}

/*!
   Initialize the map with a transformation
*/
Bryant's avatar
Bryant committed
75
void QwtScaleMap::setTransformation( QwtTransform *transform )
pixhawk's avatar
pixhawk committed
76
{
Bryant's avatar
Bryant committed
77
78
79
80
81
    if ( transform != d_transform )
    {
        delete d_transform;
        d_transform = transform;
    }
pixhawk's avatar
pixhawk committed
82

Bryant's avatar
Bryant committed
83
    setScaleInterval( d_s1, d_s2 );
pixhawk's avatar
pixhawk committed
84
85
86
}

//! Get the transformation
Bryant's avatar
Bryant committed
87
const QwtTransform *QwtScaleMap::transformation() const
pixhawk's avatar
pixhawk committed
88
{
Bryant's avatar
Bryant committed
89
    return d_transform;
pixhawk's avatar
pixhawk committed
90
91
92
93
94
}

/*!
  \brief Specify the borders of the scale interval
  \param s1 first border
95
  \param s2 second border
Bryant's avatar
Bryant committed
96
97
  \warning scales might be aligned to 
           transformation depending boundaries
pixhawk's avatar
pixhawk committed
98
*/
Bryant's avatar
Bryant committed
99
void QwtScaleMap::setScaleInterval( double s1, double s2 )
pixhawk's avatar
pixhawk committed
100
101
102
103
{
    d_s1 = s1;
    d_s2 = s2;

Bryant's avatar
Bryant committed
104
105
106
107
108
109
110
    if ( d_transform )
    {
        d_s1 = d_transform->bounded( d_s1 );
        d_s2 = d_transform->bounded( d_s2 );
    }

    updateFactor();
pixhawk's avatar
pixhawk committed
111
112
113
114
115
116
117
}

/*!
  \brief Specify the borders of the paint device interval
  \param p1 first border
  \param p2 second border
*/
Bryant's avatar
Bryant committed
118
void QwtScaleMap::setPaintInterval( double p1, double p2 )
pixhawk's avatar
pixhawk committed
119
120
121
122
{
    d_p1 = p1;
    d_p2 = p2;

Bryant's avatar
Bryant committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    updateFactor();
}

void QwtScaleMap::updateFactor()
{
    d_ts1 = d_s1;
    double ts2 = d_s2;

    if ( d_transform )
    {
        d_ts1 = d_transform->transform( d_ts1 );
        ts2 = d_transform->transform( ts2 );
    }

    d_cnv = 1.0;
    if ( d_ts1 != ts2 )
        d_cnv = ( d_p2 - d_p1 ) / ( ts2 - d_ts1 );
pixhawk's avatar
pixhawk committed
140
141
142
}

/*!
Bryant's avatar
Bryant committed
143
144
145
146
147
148
149
150
   Transform a rectangle from scale to paint coordinates

   \param xMap X map
   \param yMap Y map
   \param rect Rectangle in scale coordinates
   \return Rectangle in paint coordinates

   \sa invTransform()
pixhawk's avatar
pixhawk committed
151
*/
Bryant's avatar
Bryant committed
152
153
QRectF QwtScaleMap::transform( const QwtScaleMap &xMap,
    const QwtScaleMap &yMap, const QRectF &rect )
pixhawk's avatar
pixhawk committed
154
{
Bryant's avatar
Bryant committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    double x1 = xMap.transform( rect.left() );
    double x2 = xMap.transform( rect.right() );
    double y1 = yMap.transform( rect.top() );
    double y2 = yMap.transform( rect.bottom() );

    if ( x2 < x1 )
        qSwap( x1, x2 );
    if ( y2 < y1 )
        qSwap( y1, y2 );

    if ( qwtFuzzyCompare( x1, 0.0, x2 - x1 ) == 0 )
        x1 = 0.0;
    if ( qwtFuzzyCompare( x2, 0.0, x2 - x1 ) == 0 )
        x2 = 0.0;
    if ( qwtFuzzyCompare( y1, 0.0, y2 - y1 ) == 0 )
        y1 = 0.0;
    if ( qwtFuzzyCompare( y2, 0.0, y2 - y1 ) == 0 )
        y2 = 0.0;

    return QRectF( x1, y1, x2 - x1 + 1, y2 - y1 + 1 );
}

/*!
   Transform a rectangle from paint to scale coordinates
pixhawk's avatar
pixhawk committed
179

Bryant's avatar
Bryant committed
180
181
182
183
184
185
186
187
188
189
190
191
192
   \param xMap X map
   \param yMap Y map
   \param pos Position in paint coordinates
   \return Position in scale coordinates
   \sa transform()
*/
QPointF QwtScaleMap::invTransform( const QwtScaleMap &xMap,
    const QwtScaleMap &yMap, const QPointF &pos )
{
    return QPointF( 
        xMap.invTransform( pos.x() ), 
        yMap.invTransform( pos.y() ) 
    );
pixhawk's avatar
pixhawk committed
193
194
195
}

/*!
Bryant's avatar
Bryant committed
196
197
198
199
200
201
202
203
   Transform a point from scale to paint coordinates

   \param xMap X map
   \param yMap Y map
   \param pos Position in scale coordinates
   \return Position in paint coordinates

   \sa invTransform()
pixhawk's avatar
pixhawk committed
204
*/
Bryant's avatar
Bryant committed
205
206
QPointF QwtScaleMap::transform( const QwtScaleMap &xMap,
    const QwtScaleMap &yMap, const QPointF &pos )
pixhawk's avatar
pixhawk committed
207
{
Bryant's avatar
Bryant committed
208
209
210
211
212
    return QPointF( 
        xMap.transform( pos.x() ), 
        yMap.transform( pos.y() )
    );
}
pixhawk's avatar
pixhawk committed
213

Bryant's avatar
Bryant committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*!
   Transform a rectangle from paint to scale coordinates

   \param xMap X map
   \param yMap Y map
   \param rect Rectangle in paint coordinates
   \return Rectangle in scale coordinates
   \sa transform()
*/
QRectF QwtScaleMap::invTransform( const QwtScaleMap &xMap,
    const QwtScaleMap &yMap, const QRectF &rect )
{
    const double x1 = xMap.invTransform( rect.left() );
    const double x2 = xMap.invTransform( rect.right() - 1 );
    const double y1 = yMap.invTransform( rect.top() );
    const double y2 = yMap.invTransform( rect.bottom() - 1 );

    const QRectF r( x1, y1, x2 - x1, y2 - y1 );
    return r.normalized();
pixhawk's avatar
pixhawk committed
233
}
Bryant's avatar
Bryant committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

#ifndef QT_NO_DEBUG_STREAM

QDebug operator<<( QDebug debug, const QwtScaleMap &map )
{
    debug.nospace() << "QwtScaleMap("
        << map.transformation()
        << ", s:" << map.s1() << "->" << map.s2()
        << ", p:" << map.p1() << "->" << map.p2()
        << ")";

    return debug.space();
}

#endif