# qwt_layout_metrics.cpp

    00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
    00002  * Qwt Widget Library
    00003  * Copyright (C) 1997   Josef Wilgen
    00004  * Copyright (C) 2002   Uwe Rathmann
    00005  *
    00006  * This library is free software; you can redistribute it and/or
    00007  * modify it under the terms of the Qwt License, Version 1.0
    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     QRect mappedRect(rect);
    00175 #ifndef QT_NO_TRANSFORMATIONS
    00176     if ( painter )
    00177         mappedRect = translate(matrix(painter), mappedRect);
    00178 #endif
    00179
    00180     mappedRect = QRect(
    00181         layoutToDeviceX(mappedRect.x()),
    00182         layoutToDeviceY(mappedRect.y()),
    00183         layoutToDeviceX(mappedRect.width()),
    00184         layoutToDeviceY(mappedRect.height())
    00185     );
    00186
    00187 #ifndef QT_NO_TRANSFORMATIONS
    00188     if ( painter )
    00189         mappedRect = translate(invMatrix(painter), mappedRect);
    00190 #endif
    00191
    00192     return mappedRect;
    00193 }
    00194
    00195 #ifndef QT_NO_TRANSFORMATIONS
    00196 QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
    00197     const QPainter *painter) const
    00198 #else
    00199 QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
    00200     const QPainter *) const
    00201 #endif
    00202 {
    00203     if ( isIdentity() )
    00204         return rect;
    00205
    00206     QRect mappedRect(rect);
    00207 #ifndef QT_NO_TRANSFORMATIONS
    00208     if ( painter )
    00209         mappedRect = translate(matrix(painter), mappedRect);
    00210 #endif
    00211
    00212     mappedRect = QRect(
    00213         deviceToLayoutX(mappedRect.x()),
    00214         deviceToLayoutY(mappedRect.y()),
    00215         deviceToLayoutX(mappedRect.width()),
    00216         deviceToLayoutY(mappedRect.height())
    00217     );
    00218
    00219 #ifndef QT_NO_TRANSFORMATIONS
    00220     if ( painter )
    00221         mappedRect = translate(invMatrix(painter), mappedRect);
    00222 #endif
    00223
    00224     return mappedRect;
    00225 }
    00226
    00227 QRect QwtMetricsMap::screenToLayout(const QRect &rect) const
    00228 {
    00229     if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
    00230         return rect;
    00231
    00232     return QRect(screenToLayoutX(rect.x()), screenToLayoutY(rect.y()),
    00233         screenToLayoutX(rect.width()), screenToLayoutY(rect.height()));
    00234 }
    00235
    00236 QRect QwtMetricsMap::layoutToScreen(const QRect &rect) const
    00237 {
    00238     if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
    00239         return rect;
    00240
    00241     return QRect(layoutToScreenX(rect.x()), layoutToScreenY(rect.y()),
    00242         layoutToScreenX(rect.width()), layoutToScreenY(rect.height()));
    00243 }
    00244
    00245 #ifndef QT_NO_TRANSFORMATIONS
    00246 QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa,
    00247     const QPainter *painter) const
    00248 #else
    00249 QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa,
    00250     const QPainter *) const
    00251 #endif
    00252 {
    00253     if ( isIdentity() )
    00254         return pa;
    00255
    00256     QwtPolygon mappedPa(pa);
    00257
    00258 #ifndef QT_NO_TRANSFORMATIONS
    00259     if ( painter )
    00260         mappedPa = translate(matrix(painter), mappedPa);
    00261 #endif
    00262
    00263     QwtMatrix m;
    00264     m.scale(1.0 / d_deviceToLayoutX, 1.0 / d_deviceToLayoutY);
    00265     mappedPa = translate(m, mappedPa);
    00266
    00267 #ifndef QT_NO_TRANSFORMATIONS
    00268     if ( painter )
    00269         mappedPa = translate(invMatrix(painter), mappedPa);
    00270 #endif
    00271
    00272     return mappedPa;
    00273 }
    00274
    00275 #ifndef QT_NO_TRANSFORMATIONS
    00276 QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa,
    00277     const QPainter *painter) const
    00278 #else
    00279 QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa,
    00280     const QPainter *) const
    00281 #endif
    00282 {
    00283     if ( isIdentity() )
    00284         return pa;
    00285
    00286     QwtPolygon mappedPa(pa);
    00287
    00288 #ifndef QT_NO_TRANSFORMATIONS
    00289     if ( painter )
    00290         mappedPa = translate(matrix(painter), mappedPa);
    00291 #endif
    00292
    00293     QwtMatrix m;
    00294     m.scale(d_deviceToLayoutX, d_deviceToLayoutY);
    00295     mappedPa = translate(m, mappedPa);
    00296
    00297 #ifndef QT_NO_TRANSFORMATIONS
    00298     if ( painter )
    00299         mappedPa = translate(invMatrix(painter), mappedPa);
    00300 #endif
    00301
    00302     return mappedPa;
    00303 }
    00304
    00313 QRect QwtMetricsMap::translate(
    00314     const QwtMatrix &m, const QRect &rect)
    00315 {
    00316     return m.mapRect(rect);
    00317 }
    00318
    00326 QwtPolygon QwtMetricsMap::translate(
    00327     const QwtMatrix &m, const QwtPolygon &pa)
    00328 {
    00329     return m.map(pa);
    00330 }