1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
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
*****************************************************************************/
10
#include <qapplication.h>
12
#if QT_VERSION < 0x040000
13
#include <qpaintdevicemetrics.h>
15
#define QwtMatrix QWMatrix
18
#define QwtMatrix QMatrix
20
#include <qpaintdevice.h>
21
#include <qdesktopwidget.h>
23
#include "qwt_polygon.h"
24
#include "qwt_layout_metrics.h"
26
static QSize deviceDpi(const QPaintDevice *device)
29
#if QT_VERSION < 0x040000
30
const QPaintDeviceMetrics metrics(device);
31
dpi.setWidth(metrics.logicalDpiX());
32
dpi.setHeight(metrics.logicalDpiY());
34
dpi.setWidth(device->logicalDpiX());
35
dpi.setHeight(device->logicalDpiY());
41
#if QT_VERSION < 0x040000
43
inline static const QWMatrix &matrix(const QPainter *painter)
45
return painter->worldMatrix();
47
inline static QWMatrix invMatrix(const QPainter *painter)
49
return painter->worldMatrix().invert();
52
#else // QT_VERSION >= 0x040000
54
inline static const QMatrix &matrix(const QPainter *painter)
56
return painter->matrix();
58
inline static QMatrix invMatrix(const QPainter *painter)
60
return painter->matrix().inverted();
65
QwtMetricsMap::QwtMetricsMap()
67
d_screenToLayoutX = d_screenToLayoutY =
68
d_deviceToLayoutX = d_deviceToLayoutY = 1.0;
71
void QwtMetricsMap::setMetrics(const QPaintDevice *layoutDevice,
72
const QPaintDevice *paintDevice)
74
const QSize screenDpi = deviceDpi(QApplication::desktop());
75
const QSize layoutDpi = deviceDpi(layoutDevice);
76
const QSize paintDpi = deviceDpi(paintDevice);
78
d_screenToLayoutX = double(layoutDpi.width()) /
79
double(screenDpi.width());
80
d_screenToLayoutY = double(layoutDpi.height()) /
81
double(screenDpi.height());
83
d_deviceToLayoutX = double(layoutDpi.width()) /
84
double(paintDpi.width());
85
d_deviceToLayoutY = double(layoutDpi.height()) /
86
double(paintDpi.height());
89
#ifndef QT_NO_TRANSFORMATIONS
90
QPoint QwtMetricsMap::layoutToDevice(const QPoint &point,
91
const QPainter *painter) const
93
QPoint QwtMetricsMap::layoutToDevice(const QPoint &point,
94
const QPainter *) const
100
QPoint mappedPoint(point);
102
#ifndef QT_NO_TRANSFORMATIONS
104
mappedPoint = matrix(painter).map(mappedPoint);
107
mappedPoint.setX(layoutToDeviceX(mappedPoint.x()));
108
mappedPoint.setY(layoutToDeviceY(mappedPoint.y()));
110
#ifndef QT_NO_TRANSFORMATIONS
112
mappedPoint = invMatrix(painter).map(mappedPoint);
118
#ifndef QT_NO_TRANSFORMATIONS
119
QPoint QwtMetricsMap::deviceToLayout(const QPoint &point,
120
const QPainter *painter) const
122
QPoint QwtMetricsMap::deviceToLayout(const QPoint &point,
123
const QPainter *) const
129
QPoint mappedPoint(point);
131
#ifndef QT_NO_TRANSFORMATIONS
133
mappedPoint = matrix(painter).map(mappedPoint);
136
mappedPoint.setX(deviceToLayoutX(mappedPoint.x()));
137
mappedPoint.setY(deviceToLayoutY(mappedPoint.y()));
139
#ifndef QT_NO_TRANSFORMATIONS
141
mappedPoint = invMatrix(painter).map(mappedPoint);
147
QPoint QwtMetricsMap::screenToLayout(const QPoint &point) const
149
if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
152
return QPoint(screenToLayoutX(point.x()), screenToLayoutY(point.y()));
155
QPoint QwtMetricsMap::layoutToScreen(const QPoint &point) const
157
if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
160
return QPoint(layoutToScreenX(point.x()), layoutToScreenY(point.y()));
163
#ifndef QT_NO_TRANSFORMATIONS
164
QRect QwtMetricsMap::layoutToDevice(const QRect &rect,
165
const QPainter *painter) const
167
QRect QwtMetricsMap::layoutToDevice(const QRect &rect,
168
const QPainter *) const
174
QRect mappedRect(rect);
175
#ifndef QT_NO_TRANSFORMATIONS
177
mappedRect = translate(matrix(painter), mappedRect);
181
layoutToDevice(mappedRect.topLeft()),
182
layoutToDevice(mappedRect.bottomRight())
185
#ifndef QT_NO_TRANSFORMATIONS
187
mappedRect = translate(invMatrix(painter), mappedRect);
193
#ifndef QT_NO_TRANSFORMATIONS
194
QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
195
const QPainter *painter) const
197
QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
198
const QPainter *) const
204
QRect mappedRect(rect);
205
#ifndef QT_NO_TRANSFORMATIONS
207
mappedRect = translate(matrix(painter), mappedRect);
211
deviceToLayout(mappedRect.topLeft()),
212
deviceToLayout(mappedRect.bottomRight())
215
#ifndef QT_NO_TRANSFORMATIONS
217
mappedRect = translate(invMatrix(painter), mappedRect);
223
QRect QwtMetricsMap::screenToLayout(const QRect &rect) const
225
if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
228
return QRect(screenToLayoutX(rect.x()), screenToLayoutY(rect.y()),
229
screenToLayoutX(rect.width()), screenToLayoutY(rect.height()));
232
QRect QwtMetricsMap::layoutToScreen(const QRect &rect) const
234
if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
237
return QRect(layoutToScreenX(rect.x()), layoutToScreenY(rect.y()),
238
layoutToScreenX(rect.width()), layoutToScreenY(rect.height()));
241
#ifndef QT_NO_TRANSFORMATIONS
242
QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa,
243
const QPainter *painter) const
245
QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa,
246
const QPainter *) const
252
QwtPolygon mappedPa(pa);
254
#ifndef QT_NO_TRANSFORMATIONS
256
mappedPa = translate(matrix(painter), mappedPa);
260
m.scale(1.0 / d_deviceToLayoutX, 1.0 / d_deviceToLayoutY);
261
mappedPa = translate(m, mappedPa);
263
#ifndef QT_NO_TRANSFORMATIONS
265
mappedPa = translate(invMatrix(painter), mappedPa);
271
#ifndef QT_NO_TRANSFORMATIONS
272
QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa,
273
const QPainter *painter) const
275
QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa,
276
const QPainter *) const
282
QwtPolygon mappedPa(pa);
284
#ifndef QT_NO_TRANSFORMATIONS
286
mappedPa = translate(matrix(painter), mappedPa);
290
m.scale(d_deviceToLayoutX, d_deviceToLayoutY);
291
mappedPa = translate(m, mappedPa);
293
#ifndef QT_NO_TRANSFORMATIONS
295
mappedPa = translate(invMatrix(painter), mappedPa);
302
Wrapper for QMatrix::mapRect.
305
\param rect Rectangle to translate
306
\return Translated rectangle
309
QRect QwtMetricsMap::translate(
310
const QwtMatrix &m, const QRect &rect)
312
return m.mapRect(rect);
316
Wrapper for QMatrix::map.
319
\param pa Polygon to translate
320
\return Translated polygon
322
QwtPolygon QwtMetricsMap::translate(
323
const QwtMatrix &m, const QwtPolygon &pa)