qwt_layout_metrics.cpp 8.17 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10 11 12
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include <qapplication.h>
#include <qpainter.h>
#if QT_VERSION < 0x040000
13 14
#include <qpaintdevicemetrics.h>
#include <qwmatrix.h>
pixhawk's avatar
pixhawk committed
15 16
#define QwtMatrix QWMatrix
#else
17
#include <qmatrix.h>
pixhawk's avatar
pixhawk committed
18 19
#define QwtMatrix QMatrix
#endif
20 21
#include <qpaintdevice.h>
#include <qdesktopwidget.h>
pixhawk's avatar
pixhawk committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "qwt_math.h"
#include "qwt_polygon.h"
#include "qwt_layout_metrics.h"

static QSize deviceDpi(const QPaintDevice *device)
{
    QSize dpi;
#if QT_VERSION < 0x040000
    const QPaintDeviceMetrics metrics(device);
    dpi.setWidth(metrics.logicalDpiX());
    dpi.setHeight(metrics.logicalDpiY());
#else
    dpi.setWidth(device->logicalDpiX());
    dpi.setHeight(device->logicalDpiY());
#endif

    return dpi;
}

#if QT_VERSION < 0x040000

inline static const QWMatrix &matrix(const QPainter *painter)
{
    return painter->worldMatrix();
}
inline static QWMatrix invMatrix(const QPainter *painter)
{
    return painter->worldMatrix().invert();
}

#else // QT_VERSION >= 0x040000

inline static const QMatrix &matrix(const QPainter *painter)
{
    return painter->matrix();
}
inline static QMatrix invMatrix(const QPainter *painter)
{
    return painter->matrix().inverted();
}

#endif

QwtMetricsMap::QwtMetricsMap()
{
67 68
    d_screenToLayoutX = d_screenToLayoutY =
                            d_deviceToLayoutX = d_deviceToLayoutY = 1.0;
pixhawk's avatar
pixhawk committed
69 70
}

71 72
void QwtMetricsMap::setMetrics(const QPaintDevice *layoutDevice,
                               const QPaintDevice *paintDevice)
pixhawk's avatar
pixhawk committed
73 74 75 76 77
{
    const QSize screenDpi = deviceDpi(QApplication::desktop());
    const QSize layoutDpi = deviceDpi(layoutDevice);
    const QSize paintDpi = deviceDpi(paintDevice);

78 79 80 81
    d_screenToLayoutX = double(layoutDpi.width()) /
                        double(screenDpi.width());
    d_screenToLayoutY = double(layoutDpi.height()) /
                        double(screenDpi.height());
pixhawk's avatar
pixhawk committed
82

83 84 85 86
    d_deviceToLayoutX = double(layoutDpi.width()) /
                        double(paintDpi.width());
    d_deviceToLayoutY = double(layoutDpi.height()) /
                        double(paintDpi.height());
pixhawk's avatar
pixhawk committed
87 88 89
}

#ifndef QT_NO_TRANSFORMATIONS
90 91
QPoint QwtMetricsMap::layoutToDevice(const QPoint &point,
                                     const QPainter *painter) const
pixhawk's avatar
pixhawk committed
92
#else
93 94
QPoint QwtMetricsMap::layoutToDevice(const QPoint &point,
                                     const QPainter *) const
pixhawk's avatar
pixhawk committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#endif
{
    if ( isIdentity() )
        return point;

    QPoint mappedPoint(point);

#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedPoint = matrix(painter).map(mappedPoint);
#endif

    mappedPoint.setX(layoutToDeviceX(mappedPoint.x()));
    mappedPoint.setY(layoutToDeviceY(mappedPoint.y()));

#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedPoint = invMatrix(painter).map(mappedPoint);
#endif

    return mappedPoint;
}

#ifndef QT_NO_TRANSFORMATIONS
119 120
QPoint QwtMetricsMap::deviceToLayout(const QPoint &point,
                                     const QPainter *painter) const
pixhawk's avatar
pixhawk committed
121
#else
122 123
QPoint QwtMetricsMap::deviceToLayout(const QPoint &point,
                                     const QPainter *) const
pixhawk's avatar
pixhawk committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#endif
{
    if ( isIdentity() )
        return point;

    QPoint mappedPoint(point);

#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedPoint = matrix(painter).map(mappedPoint);
#endif

    mappedPoint.setX(deviceToLayoutX(mappedPoint.x()));
    mappedPoint.setY(deviceToLayoutY(mappedPoint.y()));

#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedPoint = invMatrix(painter).map(mappedPoint);
#endif

    return mappedPoint;
}

QPoint QwtMetricsMap::screenToLayout(const QPoint &point) const
{
    if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
        return point;

    return QPoint(screenToLayoutX(point.x()), screenToLayoutY(point.y()));
}

QPoint QwtMetricsMap::layoutToScreen(const QPoint &point) const
{
    if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
        return point;

    return QPoint(layoutToScreenX(point.x()), layoutToScreenY(point.y()));
}

#ifndef QT_NO_TRANSFORMATIONS
164 165
QRect QwtMetricsMap::layoutToDevice(const QRect &rect,
                                    const QPainter *painter) const
pixhawk's avatar
pixhawk committed
166
#else
167 168
QRect QwtMetricsMap::layoutToDevice(const QRect &rect,
                                    const QPainter *) const
pixhawk's avatar
pixhawk committed
169 170 171 172 173 174 175 176 177 178 179 180
#endif
{
    if ( isIdentity() )
        return rect;

    QRect mappedRect(rect);
#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedRect = translate(matrix(painter), mappedRect);
#endif

    mappedRect = QRect(
181 182 183
                     layoutToDevice(mappedRect.topLeft()),
                     layoutToDevice(mappedRect.bottomRight())
                 );
pixhawk's avatar
pixhawk committed
184 185 186 187 188 189 190 191 192 193 194

#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedRect = translate(invMatrix(painter), mappedRect);
#endif

    return mappedRect;
}

#ifndef QT_NO_TRANSFORMATIONS
QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
195
                                    const QPainter *painter) const
pixhawk's avatar
pixhawk committed
196 197
#else
QRect QwtMetricsMap::deviceToLayout(const QRect &rect,
198
                                    const QPainter *) const
pixhawk's avatar
pixhawk committed
199 200 201 202 203 204 205 206 207 208 209 210
#endif
{
    if ( isIdentity() )
        return rect;

    QRect mappedRect(rect);
#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedRect = translate(matrix(painter), mappedRect);
#endif

    mappedRect = QRect(
211 212 213
                     deviceToLayout(mappedRect.topLeft()),
                     deviceToLayout(mappedRect.bottomRight())
                 );
pixhawk's avatar
pixhawk committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedRect = translate(invMatrix(painter), mappedRect);
#endif

    return mappedRect;
}

QRect QwtMetricsMap::screenToLayout(const QRect &rect) const
{
    if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
        return rect;

    return QRect(screenToLayoutX(rect.x()), screenToLayoutY(rect.y()),
229
                 screenToLayoutX(rect.width()), screenToLayoutY(rect.height()));
pixhawk's avatar
pixhawk committed
230 231 232 233 234 235 236 237
}

QRect QwtMetricsMap::layoutToScreen(const QRect &rect) const
{
    if ( d_screenToLayoutX == 1.0 && d_screenToLayoutY == 1.0 )
        return rect;

    return QRect(layoutToScreenX(rect.x()), layoutToScreenY(rect.y()),
238
                 layoutToScreenX(rect.width()), layoutToScreenY(rect.height()));
pixhawk's avatar
pixhawk committed
239 240 241
}

#ifndef QT_NO_TRANSFORMATIONS
242 243
QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa,
        const QPainter *painter) const
pixhawk's avatar
pixhawk committed
244
#else
245 246
QwtPolygon QwtMetricsMap::layoutToDevice(const QwtPolygon &pa,
        const QPainter *) const
pixhawk's avatar
pixhawk committed
247 248 249 250
#endif
{
    if ( isIdentity() )
        return pa;
251

pixhawk's avatar
pixhawk committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    QwtPolygon mappedPa(pa);

#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedPa = translate(matrix(painter), mappedPa);
#endif

    QwtMatrix m;
    m.scale(1.0 / d_deviceToLayoutX, 1.0 / d_deviceToLayoutY);
    mappedPa = translate(m, mappedPa);

#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedPa = translate(invMatrix(painter), mappedPa);
#endif

    return mappedPa;
}

#ifndef QT_NO_TRANSFORMATIONS
272 273
QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa,
        const QPainter *painter) const
pixhawk's avatar
pixhawk committed
274
#else
275 276
QwtPolygon QwtMetricsMap::deviceToLayout(const QwtPolygon &pa,
        const QPainter *) const
pixhawk's avatar
pixhawk committed
277 278 279 280
#endif
{
    if ( isIdentity() )
        return pa;
281

pixhawk's avatar
pixhawk committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    QwtPolygon mappedPa(pa);

#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedPa = translate(matrix(painter), mappedPa);
#endif

    QwtMatrix m;
    m.scale(d_deviceToLayoutX, d_deviceToLayoutY);
    mappedPa = translate(m, mappedPa);

#ifndef QT_NO_TRANSFORMATIONS
    if ( painter )
        mappedPa = translate(invMatrix(painter), mappedPa);
#endif

    return mappedPa;
}

/*!
302
  Wrapper for QMatrix::mapRect.
pixhawk's avatar
pixhawk committed
303 304 305 306 307 308 309

  \param m Matrix
  \param rect Rectangle to translate
  \return Translated rectangle
*/

QRect QwtMetricsMap::translate(
310
    const QwtMatrix &m, const QRect &rect)
pixhawk's avatar
pixhawk committed
311 312 313 314 315
{
    return m.mapRect(rect);
}

/*!
316
  Wrapper for QMatrix::map.
pixhawk's avatar
pixhawk committed
317 318 319 320 321 322

  \param m Matrix
  \param pa Polygon to translate
  \return Translated polygon
*/
QwtPolygon QwtMetricsMap::translate(
323
    const QwtMatrix &m, const QwtPolygon &pa)
pixhawk's avatar
pixhawk committed
324 325 326
{
    return m.map(pa);
}