~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.1/src/qwt_layout_metrics.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2009-04-12 23:25:58 UTC
  • mfrom: (1.1.4 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090412232558-3bl06x785yr8xm8u
Tags: 5.1.2-1
* New upstream release.
* Bump compat/debhelper to 7.
* Bump Standards-Version to 3.8.1. No changes needed.
* Invert Maintainers and Uploaders field.
* Fix lintian warnings:
  - dh_clean _k deprecated.
  - missing dependency on libc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 <qapplication.h>
11
 
#include <qpainter.h>
12
 
#if QT_VERSION < 0x040000
13
 
#include <qpaintdevicemetrics.h> 
14
 
#include <qwmatrix.h> 
15
 
#define QwtMatrix QWMatrix
16
 
#else
17
 
#include <qmatrix.h> 
18
 
#define QwtMatrix QMatrix
19
 
#endif
20
 
#include <qpaintdevice.h> 
21
 
#include <qdesktopwidget.h> 
22
 
#include "qwt_math.h"
23
 
#include "qwt_polygon.h"
24
 
#include "qwt_layout_metrics.h"
25
 
 
26
 
static QSize deviceDpi(const QPaintDevice *device)
27
 
{
28
 
    QSize dpi;
29
 
#if QT_VERSION < 0x040000
30
 
    const QPaintDeviceMetrics metrics(device);
31
 
    dpi.setWidth(metrics.logicalDpiX());
32
 
    dpi.setHeight(metrics.logicalDpiY());
33
 
#else
34
 
    dpi.setWidth(device->logicalDpiX());
35
 
    dpi.setHeight(device->logicalDpiY());
36
 
#endif
37
 
 
38
 
    return dpi;
39
 
}
40
 
 
41
 
#if QT_VERSION < 0x040000
42
 
 
43
 
inline static const QWMatrix &matrix(const QPainter *painter)
44
 
{
45
 
    return painter->worldMatrix();
46
 
}
47
 
inline static QWMatrix invMatrix(const QPainter *painter)
48
 
{
49
 
    return painter->worldMatrix().invert();
50
 
}
51
 
 
52
 
#else // QT_VERSION >= 0x040000
53
 
 
54
 
inline static const QMatrix &matrix(const QPainter *painter)
55
 
{
56
 
    return painter->matrix();
57
 
}
58
 
inline static QMatrix invMatrix(const QPainter *painter)
59
 
{
60
 
    return painter->matrix().inverted();
61
 
}
62
 
 
63
 
#endif
64
 
 
65
 
QwtMetricsMap::QwtMetricsMap()
66
 
{
67
 
    d_screenToLayoutX = d_screenToLayoutY = 
68
 
        d_deviceToLayoutX = d_deviceToLayoutY = 1.0;
69
 
}
70
 
 
71
 
void QwtMetricsMap::setMetrics(const QPaintDevice *layoutDevice, 
72
 
    const QPaintDevice *paintDevice)
73
 
{
74
 
    const QSize screenDpi = deviceDpi(QApplication::desktop());
75
 
    const QSize layoutDpi = deviceDpi(layoutDevice);
76
 
    const QSize paintDpi = deviceDpi(paintDevice);
77
 
 
78
 
    d_screenToLayoutX = double(layoutDpi.width()) / 
79
 
        double(screenDpi.width());
80
 
    d_screenToLayoutY = double(layoutDpi.height()) / 
81
 
        double(screenDpi.height());
82
 
 
83
 
    d_deviceToLayoutX = double(layoutDpi.width()) / 
84
 
        double(paintDpi.width());
85
 
    d_deviceToLayoutY = double(layoutDpi.height()) / 
86
 
        double(paintDpi.height());
87
 
}
88
 
 
89
 
#ifndef QT_NO_TRANSFORMATIONS
90
 
QPoint QwtMetricsMap::layoutToDevice(const QPoint &point, 
91
 
    const QPainter *painter) const
92
 
#else
93
 
QPoint QwtMetricsMap::layoutToDevice(const QPoint &point, 
94
 
    const QPainter *) const
95
 
#endif
96
 
{
97
 
    if ( isIdentity() )
98
 
        return point;
99
 
 
100
 
    QPoint mappedPoint(point);
101
 
 
102
 
#ifndef QT_NO_TRANSFORMATIONS
103
 
    if ( painter )
104
 
        mappedPoint = matrix(painter).map(mappedPoint);
105
 
#endif
106
 
 
107
 
    mappedPoint.setX(layoutToDeviceX(mappedPoint.x()));
108
 
    mappedPoint.setY(layoutToDeviceY(mappedPoint.y()));
109
 
 
110
 
#ifndef QT_NO_TRANSFORMATIONS
111
 
    if ( painter )
112
 
        mappedPoint = invMatrix(painter).map(mappedPoint);
113
 
#endif
114
 
 
115
 
    return mappedPoint;
116
 
}
117
 
 
118
 
#ifndef QT_NO_TRANSFORMATIONS
119
 
QPoint QwtMetricsMap::deviceToLayout(const QPoint &point, 
120
 
    const QPainter *painter) const
121
 
#else
122
 
QPoint QwtMetricsMap::deviceToLayout(const QPoint &point, 
123
 
    const QPainter *) const
124
 
#endif
125
 
{
126
 
    if ( isIdentity() )
127
 
        return point;
128
 
 
129
 
    QPoint mappedPoint(point);
130
 
 
131
 
#ifndef QT_NO_TRANSFORMATIONS
132
 
    if ( painter )
133
 
        mappedPoint = matrix(painter).map(mappedPoint);
134
 
#endif
135
 
 
136
 
    mappedPoint.setX(deviceToLayoutX(mappedPoint.x()));
137
 
    mappedPoint.setY(deviceToLayoutY(mappedPoint.y()));
138
 
 
139
 
#ifndef QT_NO_TRANSFORMATIONS
140
 
    if ( painter )
141
 
        mappedPoint = invMatrix(painter).map(mappedPoint);
142
 
#endif
143
 
 
144
 
    return mappedPoint;
145
 
}
146
 
 
147
 
QPoint QwtMetricsMap::screenToLayout(const QPoint &point) const
148
 
{
149
 
    if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
150
 
        return point;
151
 
 
152
 
    return QPoint(screenToLayoutX(point.x()), screenToLayoutY(point.y()));
153
 
}
154
 
 
155
 
QPoint QwtMetricsMap::layoutToScreen(const QPoint &point) const
156
 
{
157
 
    if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
158
 
        return point;
159
 
 
160
 
    return QPoint(layoutToScreenX(point.x()), layoutToScreenY(point.y()));
161
 
}
162
 
 
163
 
#ifndef QT_NO_TRANSFORMATIONS
164
 
QRect QwtMetricsMap::layoutToDevice(const QRect &rect, 
165
 
    const QPainter *painter) const
166
 
#else
167
 
QRect QwtMetricsMap::layoutToDevice(const QRect &rect, 
168
 
    const QPainter *) const
169
 
#endif
170
 
{
171
 
    if ( isIdentity() )
172
 
        return rect;
173
 
 
174
 
    QRect mappedRect(rect);
175
 
#ifndef QT_NO_TRANSFORMATIONS
176
 
    if ( painter )
177
 
        mappedRect = translate(matrix(painter), mappedRect);
178
 
#endif
179
 
 
180
 
    mappedRect = QRect(
181
 
        layoutToDevice(mappedRect.topLeft()), 
182
 
        layoutToDevice(mappedRect.bottomRight())
183
 
    );
184
 
 
185
 
#ifndef QT_NO_TRANSFORMATIONS
186
 
    if ( painter )
187
 
        mappedRect = translate(invMatrix(painter), mappedRect);
188
 
#endif
189
 
 
190
 
    return mappedRect;
191
 
}
192
 
 
193
 
#ifndef QT_NO_TRANSFORMATIONS
194
 
QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
195
 
    const QPainter *painter) const
196
 
#else
197
 
QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
198
 
    const QPainter *) const
199
 
#endif
200
 
{
201
 
    if ( isIdentity() )
202
 
        return rect;
203
 
 
204
 
    QRect mappedRect(rect);
205
 
#ifndef QT_NO_TRANSFORMATIONS
206
 
    if ( painter )
207
 
        mappedRect = translate(matrix(painter), mappedRect);
208
 
#endif
209
 
 
210
 
    mappedRect = QRect(
211
 
        deviceToLayout(mappedRect.topLeft()), 
212
 
        deviceToLayout(mappedRect.bottomRight())
213
 
    );
214
 
 
215
 
#ifndef QT_NO_TRANSFORMATIONS
216
 
    if ( painter )
217
 
        mappedRect = translate(invMatrix(painter), mappedRect);
218
 
#endif
219
 
 
220
 
    return mappedRect;
221
 
}
222
 
 
223
 
QRect QwtMetricsMap::screenToLayout(const QRect &rect) const
224
 
{
225
 
    if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
226
 
        return rect;
227
 
 
228
 
    return QRect(screenToLayoutX(rect.x()), screenToLayoutY(rect.y()),
229
 
        screenToLayoutX(rect.width()), screenToLayoutY(rect.height()));
230
 
}
231
 
 
232
 
QRect QwtMetricsMap::layoutToScreen(const QRect &rect) const
233
 
{
234
 
    if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
235
 
        return rect;
236
 
 
237
 
    return QRect(layoutToScreenX(rect.x()), layoutToScreenY(rect.y()),
238
 
        layoutToScreenX(rect.width()), layoutToScreenY(rect.height()));
239
 
}
240
 
 
241
 
#ifndef QT_NO_TRANSFORMATIONS
242
 
QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa, 
243
 
    const QPainter *painter) const
244
 
#else
245
 
QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa, 
246
 
    const QPainter *) const
247
 
#endif
248
 
{
249
 
    if ( isIdentity() )
250
 
        return pa;
251
 
    
252
 
    QwtPolygon mappedPa(pa);
253
 
 
254
 
#ifndef QT_NO_TRANSFORMATIONS
255
 
    if ( painter )
256
 
        mappedPa = translate(matrix(painter), mappedPa);
257
 
#endif
258
 
 
259
 
    QwtMatrix m;
260
 
    m.scale(1.0 / d_deviceToLayoutX, 1.0 / d_deviceToLayoutY);
261
 
    mappedPa = translate(m, mappedPa);
262
 
 
263
 
#ifndef QT_NO_TRANSFORMATIONS
264
 
    if ( painter )
265
 
        mappedPa = translate(invMatrix(painter), mappedPa);
266
 
#endif
267
 
 
268
 
    return mappedPa;
269
 
}
270
 
 
271
 
#ifndef QT_NO_TRANSFORMATIONS
272
 
QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa, 
273
 
    const QPainter *painter) const
274
 
#else
275
 
QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa, 
276
 
    const QPainter *) const
277
 
#endif
278
 
{
279
 
    if ( isIdentity() )
280
 
        return pa;
281
 
    
282
 
    QwtPolygon mappedPa(pa);
283
 
 
284
 
#ifndef QT_NO_TRANSFORMATIONS
285
 
    if ( painter )
286
 
        mappedPa = translate(matrix(painter), mappedPa);
287
 
#endif
288
 
 
289
 
    QwtMatrix m;
290
 
    m.scale(d_deviceToLayoutX, d_deviceToLayoutY);
291
 
    mappedPa = translate(m, mappedPa);
292
 
 
293
 
#ifndef QT_NO_TRANSFORMATIONS
294
 
    if ( painter )
295
 
        mappedPa = translate(invMatrix(painter), mappedPa);
296
 
#endif
297
 
 
298
 
    return mappedPa;
299
 
}
300
 
 
301
 
/*!
302
 
  Wrapper for QMatrix::mapRect. 
303
 
 
304
 
  \param m Matrix
305
 
  \param rect Rectangle to translate
306
 
  \return Translated rectangle
307
 
*/
308
 
 
309
 
QRect QwtMetricsMap::translate(
310
 
    const QwtMatrix &m, const QRect &rect) 
311
 
{
312
 
    return m.mapRect(rect);
313
 
}
314
 
 
315
 
/*!
316
 
  Wrapper for QMatrix::map. 
317
 
 
318
 
  \param m Matrix
319
 
  \param pa Polygon to translate
320
 
  \return Translated polygon
321
 
*/
322
 
QwtPolygon QwtMetricsMap::translate(
323
 
    const QwtMatrix &m, const QwtPolygon &pa) 
324
 
{
325
 
    return m.map(pa);
326
 
}