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