# 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         layoutToDevice(mappedRect.topLeft()),
    00182         layoutToDevice(mappedRect.bottomRight())
    00183     );
    00184
    00185 #ifndef QT_NO_TRANSFORMATIONS
    00186     if ( painter )
    00187         mappedRect = translate(invMatrix(painter), mappedRect);
    00188 #endif
    00189
    00190     return mappedRect;
    00191 }
    00192
    00193 #ifndef QT_NO_TRANSFORMATIONS
    00194 QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
    00195     const QPainter *painter) const
    00196 #else
    00197 QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
    00198     const QPainter *) const
    00199 #endif
    00200 {
    00201     if ( isIdentity() )
    00202         return rect;
    00203
    00204     QRect mappedRect(rect);
    00205 #ifndef QT_NO_TRANSFORMATIONS
    00206     if ( painter )
    00207         mappedRect = translate(matrix(painter), mappedRect);
    00208 #endif
    00209
    00210     mappedRect = QRect(
    00211         deviceToLayout(mappedRect.topLeft()),
    00212         deviceToLayout(mappedRect.bottomRight())
    00213     );
    00214
    00215 #ifndef QT_NO_TRANSFORMATIONS
    00216     if ( painter )
    00217         mappedRect = translate(invMatrix(painter), mappedRect);
    00218 #endif
    00219
    00220     return mappedRect;
    00221 }
    00222
    00223 QRect QwtMetricsMap::screenToLayout(const QRect &rect) const
    00224 {
    00225     if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
    00226         return rect;
    00227
    00228     return QRect(screenToLayoutX(rect.x()), screenToLayoutY(rect.y()),
    00229         screenToLayoutX(rect.width()), screenToLayoutY(rect.height()));
    00230 }
    00231
    00232 QRect QwtMetricsMap::layoutToScreen(const QRect &rect) const
    00233 {
    00234     if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
    00235         return rect;
    00236
    00237     return QRect(layoutToScreenX(rect.x()), layoutToScreenY(rect.y()),
    00238         layoutToScreenX(rect.width()), layoutToScreenY(rect.height()));
    00239 }
    00240
    00241 #ifndef QT_NO_TRANSFORMATIONS
    00242 QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa,
    00243     const QPainter *painter) const
    00244 #else
    00245 QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa,
    00246     const QPainter *) const
    00247 #endif
    00248 {
    00249     if ( isIdentity() )
    00250         return pa;
    00251
    00252     QwtPolygon mappedPa(pa);
    00253
    00254 #ifndef QT_NO_TRANSFORMATIONS
    00255     if ( painter )
    00256         mappedPa = translate(matrix(painter), mappedPa);
    00257 #endif
    00258
    00259     QwtMatrix m;
    00260     m.scale(1.0 / d_deviceToLayoutX, 1.0 / d_deviceToLayoutY);
    00261     mappedPa = translate(m, mappedPa);
    00262
    00263 #ifndef QT_NO_TRANSFORMATIONS
    00264     if ( painter )
    00265         mappedPa = translate(invMatrix(painter), mappedPa);
    00266 #endif
    00267
    00268     return mappedPa;
    00269 }
    00270
    00271 #ifndef QT_NO_TRANSFORMATIONS
    00272 QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa,
    00273     const QPainter *painter) const
    00274 #else
    00275 QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa,
    00276     const QPainter *) const
    00277 #endif
    00278 {
    00279     if ( isIdentity() )
    00280         return pa;
    00281
    00282     QwtPolygon mappedPa(pa);
    00283
    00284 #ifndef QT_NO_TRANSFORMATIONS
    00285     if ( painter )
    00286         mappedPa = translate(matrix(painter), mappedPa);
    00287 #endif
    00288
    00289     QwtMatrix m;
    00290     m.scale(d_deviceToLayoutX, d_deviceToLayoutY);
    00291     mappedPa = translate(m, mappedPa);
    00292
    00293 #ifndef QT_NO_TRANSFORMATIONS
    00294     if ( painter )
    00295         mappedPa = translate(invMatrix(painter), mappedPa);
    00296 #endif
    00297
    00298     return mappedPa;
    00299 }
    00300
    00309 QRect QwtMetricsMap::translate(
    00310     const QwtMatrix &m, const QRect &rect)
    00311 {
    00312     return m.mapRect(rect);
    00313 }
    00314
    00322 QwtPolygon QwtMetricsMap::translate(
    00323     const QwtMatrix &m, const QwtPolygon &pa)
    00324 {
    00325     return m.map(pa);
    00326 }