Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <qapplication.h>
00011 #include <qpainter.h>
00012 #if QT_VERSION < 0x040000
00013 #include <qpaintdevicemetrics.h>
00014 #include <qwmatrix.h>
00015 #define QwtMatrix QWMatrix
00016 #else
00017 #include <qmatrix.h>
00018 #define QwtMatrix QMatrix
00019 #endif
00020 #include <qpaintdevice.h>
00021 #include <qdesktopwidget.h>
00022 #include "qwt_math.h"
00023 #include "qwt_polygon.h"
00024 #include "qwt_layout_metrics.h"
00025
00026 static QSize deviceDpi(const QPaintDevice *device)
00027 {
00028 QSize dpi;
00029 #if QT_VERSION < 0x040000
00030 const QPaintDeviceMetrics metrics(device);
00031 dpi.setWidth(metrics.logicalDpiX());
00032 dpi.setHeight(metrics.logicalDpiY());
00033 #else
00034 dpi.setWidth(device->logicalDpiX());
00035 dpi.setHeight(device->logicalDpiY());
00036 #endif
00037
00038 return dpi;
00039 }
00040
00041 #if QT_VERSION < 0x040000
00042
00043 inline static const QWMatrix &matrix(const QPainter *painter)
00044 {
00045 return painter->worldMatrix();
00046 }
00047 inline static QWMatrix invMatrix(const QPainter *painter)
00048 {
00049 return painter->worldMatrix().invert();
00050 }
00051
00052 #else // QT_VERSION >= 0x040000
00053
00054 inline static const QMatrix &matrix(const QPainter *painter)
00055 {
00056 return painter->matrix();
00057 }
00058 inline static QMatrix invMatrix(const QPainter *painter)
00059 {
00060 return painter->matrix().inverted();
00061 }
00062
00063 #endif
00064
00065 QwtMetricsMap::QwtMetricsMap()
00066 {
00067 d_screenToLayoutX = d_screenToLayoutY =
00068 d_deviceToLayoutX = d_deviceToLayoutY = 1.0;
00069 }
00070
00071 void QwtMetricsMap::setMetrics(const QPaintDevice *layoutDevice,
00072 const QPaintDevice *paintDevice)
00073 {
00074 const QSize screenDpi = deviceDpi(QApplication::desktop());
00075 const QSize layoutDpi = deviceDpi(layoutDevice);
00076 const QSize paintDpi = deviceDpi(paintDevice);
00077
00078 d_screenToLayoutX = double(layoutDpi.width()) /
00079 double(screenDpi.width());
00080 d_screenToLayoutY = double(layoutDpi.height()) /
00081 double(screenDpi.height());
00082
00083 d_deviceToLayoutX = double(layoutDpi.width()) /
00084 double(paintDpi.width());
00085 d_deviceToLayoutY = double(layoutDpi.height()) /
00086 double(paintDpi.height());
00087 }
00088
00089 #ifndef QT_NO_TRANSFORMATIONS
00090 QPoint QwtMetricsMap::layoutToDevice(const QPoint &point,
00091 const QPainter *painter) const
00092 #else
00093 QPoint QwtMetricsMap::layoutToDevice(const QPoint &point,
00094 const QPainter *) const
00095 #endif
00096 {
00097 if ( isIdentity() )
00098 return point;
00099
00100 QPoint mappedPoint(point);
00101
00102 #ifndef QT_NO_TRANSFORMATIONS
00103 if ( painter )
00104 mappedPoint = matrix(painter).map(mappedPoint);
00105 #endif
00106
00107 mappedPoint.setX(layoutToDeviceX(mappedPoint.x()));
00108 mappedPoint.setY(layoutToDeviceY(mappedPoint.y()));
00109
00110 #ifndef QT_NO_TRANSFORMATIONS
00111 if ( painter )
00112 mappedPoint = invMatrix(painter).map(mappedPoint);
00113 #endif
00114
00115 return mappedPoint;
00116 }
00117
00118 #ifndef QT_NO_TRANSFORMATIONS
00119 QPoint QwtMetricsMap::deviceToLayout(const QPoint &point,
00120 const QPainter *painter) const
00121 #else
00122 QPoint QwtMetricsMap::deviceToLayout(const QPoint &point,
00123 const QPainter *) const
00124 #endif
00125 {
00126 if ( isIdentity() )
00127 return point;
00128
00129 QPoint mappedPoint(point);
00130
00131 #ifndef QT_NO_TRANSFORMATIONS
00132 if ( painter )
00133 mappedPoint = matrix(painter).map(mappedPoint);
00134 #endif
00135
00136 mappedPoint.setX(deviceToLayoutX(mappedPoint.x()));
00137 mappedPoint.setY(deviceToLayoutY(mappedPoint.y()));
00138
00139 #ifndef QT_NO_TRANSFORMATIONS
00140 if ( painter )
00141 mappedPoint = invMatrix(painter).map(mappedPoint);
00142 #endif
00143
00144 return mappedPoint;
00145 }
00146
00147 QPoint QwtMetricsMap::screenToLayout(const QPoint &point) const
00148 {
00149 if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
00150 return point;
00151
00152 return QPoint(screenToLayoutX(point.x()), screenToLayoutY(point.y()));
00153 }
00154
00155 QPoint QwtMetricsMap::layoutToScreen(const QPoint &point) const
00156 {
00157 if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
00158 return point;
00159
00160 return QPoint(layoutToScreenX(point.x()), layoutToScreenY(point.y()));
00161 }
00162
00163 #ifndef QT_NO_TRANSFORMATIONS
00164 QRect QwtMetricsMap::layoutToDevice(const QRect &rect,
00165 const QPainter *painter) const
00166 #else
00167 QRect QwtMetricsMap::layoutToDevice(const QRect &rect,
00168 const QPainter *) const
00169 #endif
00170 {
00171 if ( isIdentity() )
00172 return rect;
00173
00174 int dx = 0;
00175 int dy = 0;
00176
00177 QRect mappedRect(rect);
00178 #ifndef QT_NO_TRANSFORMATIONS
00179 if ( painter )
00180 {
00181
00182
00183
00184
00185
00186
00187 dx = qRound(matrix(painter).dx());
00188 dy = qRound(matrix(painter).dy());
00189
00190 mappedRect = QRect(mappedRect.x() + dx, mappedRect.y() + dy,
00191 mappedRect.width(), mappedRect.height() );
00192 }
00193 #endif
00194
00195 mappedRect = QRect(
00196 layoutToDevice(mappedRect.topLeft()),
00197 layoutToDevice(mappedRect.bottomRight())
00198 );
00199
00200 mappedRect = QRect(mappedRect.x() - dx, mappedRect.y() - dy,
00201 mappedRect.width(), mappedRect.height() );
00202
00203 return mappedRect;
00204 }
00205
00206 #ifndef QT_NO_TRANSFORMATIONS
00207 QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
00208 const QPainter *painter) const
00209 #else
00210 QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
00211 const QPainter *) const
00212 #endif
00213 {
00214 if ( isIdentity() )
00215 return rect;
00216
00217 QRect mappedRect(rect);
00218 #ifndef QT_NO_TRANSFORMATIONS
00219 if ( painter )
00220 mappedRect = translate(matrix(painter), mappedRect);
00221 #endif
00222
00223 mappedRect = QRect(
00224 deviceToLayout(mappedRect.topLeft()),
00225 deviceToLayout(mappedRect.bottomRight())
00226 );
00227
00228 #ifndef QT_NO_TRANSFORMATIONS
00229 if ( painter )
00230 mappedRect = translate(invMatrix(painter), mappedRect);
00231 #endif
00232
00233 return mappedRect;
00234 }
00235
00236 QRect QwtMetricsMap::screenToLayout(const QRect &rect) const
00237 {
00238 if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
00239 return rect;
00240
00241 return QRect(screenToLayoutX(rect.x()), screenToLayoutY(rect.y()),
00242 screenToLayoutX(rect.width()), screenToLayoutY(rect.height()));
00243 }
00244
00245 QRect QwtMetricsMap::layoutToScreen(const QRect &rect) const
00246 {
00247 if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
00248 return rect;
00249
00250 return QRect(layoutToScreenX(rect.x()), layoutToScreenY(rect.y()),
00251 layoutToScreenX(rect.width()), layoutToScreenY(rect.height()));
00252 }
00253
00254 #ifndef QT_NO_TRANSFORMATIONS
00255 QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa,
00256 const QPainter *painter) const
00257 #else
00258 QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa,
00259 const QPainter *) const
00260 #endif
00261 {
00262 if ( isIdentity() )
00263 return pa;
00264
00265 QwtPolygon mappedPa(pa);
00266
00267 #ifndef QT_NO_TRANSFORMATIONS
00268 if ( painter )
00269 mappedPa = translate(matrix(painter), mappedPa);
00270 #endif
00271
00272 QwtMatrix m;
00273 m.scale(1.0 / d_deviceToLayoutX, 1.0 / d_deviceToLayoutY);
00274 mappedPa = translate(m, mappedPa);
00275
00276 #ifndef QT_NO_TRANSFORMATIONS
00277 if ( painter )
00278 mappedPa = translate(invMatrix(painter), mappedPa);
00279 #endif
00280
00281 return mappedPa;
00282 }
00283
00284 #ifndef QT_NO_TRANSFORMATIONS
00285 QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa,
00286 const QPainter *painter) const
00287 #else
00288 QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa,
00289 const QPainter *) const
00290 #endif
00291 {
00292 if ( isIdentity() )
00293 return pa;
00294
00295 QwtPolygon mappedPa(pa);
00296
00297 #ifndef QT_NO_TRANSFORMATIONS
00298 if ( painter )
00299 mappedPa = translate(matrix(painter), mappedPa);
00300 #endif
00301
00302 QwtMatrix m;
00303 m.scale(d_deviceToLayoutX, d_deviceToLayoutY);
00304 mappedPa = translate(m, mappedPa);
00305
00306 #ifndef QT_NO_TRANSFORMATIONS
00307 if ( painter )
00308 mappedPa = translate(invMatrix(painter), mappedPa);
00309 #endif
00310
00311 return mappedPa;
00312 }
00313
00322 QRect QwtMetricsMap::translate(
00323 const QwtMatrix &m, const QRect &rect)
00324 {
00325 return m.mapRect(rect);
00326 }
00327
00335 QwtPolygon QwtMetricsMap::translate(
00336 const QwtMatrix &m, const QwtPolygon &pa)
00337 {
00338 return m.map(pa);
00339 }