qwt_layout_metrics.h 4.13 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 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_LAYOUT_METRICS_H
#define QWT_LAYOUT_METRICS_H

#include <qsize.h>
#include <qrect.h>
#include "qwt_polygon.h"
#include "qwt_global.h"

class QPainter;
class QString;
class QFontMetrics;
#if QT_VERSION < 0x040000
class QWMatrix;
#else
class QMatrix;
#endif
class QPaintDevice;

/*!
  \brief A Map to translate between layout, screen and paint device metrics
*/
class QWT_EXPORT QwtMetricsMap
{
public:
    QwtMetricsMap();

    bool isIdentity() const;

    void setMetrics(const QPaintDevice *layoutMetrics,
39
                    const QPaintDevice *deviceMetrics);
pixhawk's avatar
pixhawk committed
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

    int layoutToDeviceX(int x) const;
    int deviceToLayoutX(int x) const;
    int screenToLayoutX(int x) const;
    int layoutToScreenX(int x) const;

    int layoutToDeviceY(int y) const;
    int deviceToLayoutY(int y) const;
    int screenToLayoutY(int y) const;
    int layoutToScreenY(int y) const;

    QPoint layoutToDevice(const QPoint &, const QPainter * = NULL) const;
    QPoint deviceToLayout(const QPoint &, const QPainter * = NULL) const;
    QPoint screenToLayout(const QPoint &) const;
    QPoint layoutToScreen(const QPoint &point) const;


    QSize layoutToDevice(const QSize &) const;
    QSize deviceToLayout(const QSize &) const;
    QSize screenToLayout(const QSize &) const;
    QSize layoutToScreen(const QSize &) const;

    QRect layoutToDevice(const QRect &, const QPainter * = NULL) const;
    QRect deviceToLayout(const QRect &, const QPainter * = NULL) const;
    QRect screenToLayout(const QRect &) const;
    QRect layoutToScreen(const QRect &) const;

67 68 69 70
    QwtPolygon layoutToDevice(const QwtPolygon &,
                              const QPainter * = NULL) const;
    QwtPolygon deviceToLayout(const QwtPolygon &,
                              const QPainter * = NULL) const;
pixhawk's avatar
pixhawk committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

#if QT_VERSION < 0x040000
    static QwtPolygon translate(const QWMatrix &, const QwtPolygon &);
    static QRect translate(const QWMatrix &, const QRect &);
#else
    static QwtPolygon translate(const QMatrix &, const QwtPolygon &);
    static QRect translate(const QMatrix &, const QRect &);
#endif

private:
    double d_screenToLayoutX;
    double d_screenToLayoutY;

    double d_deviceToLayoutX;
    double d_deviceToLayoutY;
};

inline bool QwtMetricsMap::isIdentity() const
{
    return d_deviceToLayoutX == 1.0 && d_deviceToLayoutY == 1.0;
}

inline int QwtMetricsMap::layoutToDeviceX(int x) const
{
    return qRound(x / d_deviceToLayoutX);
}

inline int QwtMetricsMap::deviceToLayoutX(int x) const
{
    return qRound(x * d_deviceToLayoutX);
}

inline int QwtMetricsMap::screenToLayoutX(int x) const
{
    return qRound(x * d_screenToLayoutX);
}

inline int QwtMetricsMap::layoutToScreenX(int x) const
{
    return qRound(x / d_screenToLayoutX);
}

inline int QwtMetricsMap::layoutToDeviceY(int y) const
{
    return qRound(y / d_deviceToLayoutY);
}

inline int QwtMetricsMap::deviceToLayoutY(int y) const
{
    return qRound(y * d_deviceToLayoutY);
}

inline int QwtMetricsMap::screenToLayoutY(int y) const
{
    return qRound(y * d_screenToLayoutY);
}

inline int QwtMetricsMap::layoutToScreenY(int y) const
{
    return qRound(y / d_screenToLayoutY);
}

inline QSize QwtMetricsMap::layoutToDevice(const QSize &size) const
{
135 136
    return QSize(layoutToDeviceX(size.width()),
                 layoutToDeviceY(size.height()));
pixhawk's avatar
pixhawk committed
137 138 139 140
}

inline QSize QwtMetricsMap::deviceToLayout(const QSize &size) const
{
141 142
    return QSize(deviceToLayoutX(size.width()),
                 deviceToLayoutY(size.height()));
pixhawk's avatar
pixhawk committed
143 144 145 146
}

inline QSize QwtMetricsMap::screenToLayout(const QSize &size) const
{
147 148
    return QSize(screenToLayoutX(size.width()),
                 screenToLayoutY(size.height()));
pixhawk's avatar
pixhawk committed
149 150 151 152
}

inline QSize QwtMetricsMap::layoutToScreen(const QSize &size) const
{
153 154
    return QSize(layoutToScreenX(size.width()),
                 layoutToScreenY(size.height()));
pixhawk's avatar
pixhawk committed
155 156 157
}

#endif