1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997   Josef Wilgen
4  * Copyright (C) 2002   Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #ifndef QWT_LAYOUT_METRICS_H
11 #define QWT_LAYOUT_METRICS_H
12 
13 #include <qsize.h>
14 #include <qrect.h>
15 #include "qwt_polygon.h"
16 #include "qwt_global.h"
17 
18 class QPainter;
19 class QString;
20 class QFontMetrics;
21 #if QT_VERSION < 0x040000
22 class QWMatrix;
23 #else
24 class QTransform;
25 #endif
26 class QPaintDevice;
27 
28 /*!
29   \brief A Map to translate between layout, screen and paint device metrics
30 
31   Qt3 supports painting in integer coordinates only. Therefore it is not
32   possible to scale the layout in screen coordinates to layouts in higher
33   resolutions ( f.e printing ) without losing the higher precision.
34   QwtMetricsMap is used to incorporate the various widget attributes
35   ( always in screen resolution ) into the layout/printing code of QwtPlot.
36 
37   Qt4 is able to paint floating point based coordinates, what makes it
38   possible always to render in screen coordinates
39   ( with a common scale factor ).
40   QwtMetricsMap will be obsolete as soon as Qt3 support has been
41   dropped ( Qwt 6.x ).
42 */
43 class QWT_EXPORT QwtMetricsMap
44 {
45 public:
46     QwtMetricsMap();
47 
48     bool isIdentity() const;
49 
50     void setMetrics(const QPaintDevice *layoutMetrics,
51         const QPaintDevice *deviceMetrics);
52 
53     int layoutToDeviceX(int x) const;
54     int deviceToLayoutX(int x) const;
55     int screenToLayoutX(int x) const;
56     int layoutToScreenX(int x) const;
57 
58     int layoutToDeviceY(int y) const;
59     int deviceToLayoutY(int y) const;
60     int screenToLayoutY(int y) const;
61     int layoutToScreenY(int y) const;
62 
63     QPoint layoutToDevice(const QPoint &, const QPainter * = NULL) const;
64     QPoint deviceToLayout(const QPoint &, const QPainter * = NULL) const;
65     QPoint screenToLayout(const QPoint &) const;
66     QPoint layoutToScreen(const QPoint &point) const;
67 
68 
69     QSize layoutToDevice(const QSize &) const;
70     QSize deviceToLayout(const QSize &) const;
71     QSize screenToLayout(const QSize &) const;
72     QSize layoutToScreen(const QSize &) const;
73 
74     QRect layoutToDevice(const QRect &, const QPainter * = NULL) const;
75     QRect deviceToLayout(const QRect &, const QPainter * = NULL) const;
76     QRect screenToLayout(const QRect &) const;
77     QRect layoutToScreen(const QRect &) const;
78 
79     QwtPolygon layoutToDevice(const QwtPolygon &,
80         const QPainter * = NULL) const;
81     QwtPolygon deviceToLayout(const QwtPolygon &,
82         const QPainter * = NULL) const;
83 
84 #if QT_VERSION < 0x040000
85     static QwtPolygon translate(const QWMatrix &, const QwtPolygon &);
86     static QRect translate(const QWMatrix &, const QRect &);
87 #else
88     static QwtPolygon translate(const QTransform &, const QwtPolygon &);
89     static QRect translate(const QTransform &, const QRect &);
90 #endif
91 
92 private:
93     double d_screenToLayoutX;
94     double d_screenToLayoutY;
95 
96     double d_deviceToLayoutX;
97     double d_deviceToLayoutY;
98 };
99 
isIdentity()100 inline bool QwtMetricsMap::isIdentity() const
101 {
102     return d_deviceToLayoutX == 1.0 && d_deviceToLayoutY == 1.0;
103 }
104 
layoutToDeviceX(int x)105 inline int QwtMetricsMap::layoutToDeviceX(int x) const
106 {
107     return qRound(x / d_deviceToLayoutX);
108 }
109 
deviceToLayoutX(int x)110 inline int QwtMetricsMap::deviceToLayoutX(int x) const
111 {
112     return qRound(x * d_deviceToLayoutX);
113 }
114 
screenToLayoutX(int x)115 inline int QwtMetricsMap::screenToLayoutX(int x) const
116 {
117     return qRound(x * d_screenToLayoutX);
118 }
119 
layoutToScreenX(int x)120 inline int QwtMetricsMap::layoutToScreenX(int x) const
121 {
122     return qRound(x / d_screenToLayoutX);
123 }
124 
layoutToDeviceY(int y)125 inline int QwtMetricsMap::layoutToDeviceY(int y) const
126 {
127     return qRound(y / d_deviceToLayoutY);
128 }
129 
deviceToLayoutY(int y)130 inline int QwtMetricsMap::deviceToLayoutY(int y) const
131 {
132     return qRound(y * d_deviceToLayoutY);
133 }
134 
screenToLayoutY(int y)135 inline int QwtMetricsMap::screenToLayoutY(int y) const
136 {
137     return qRound(y * d_screenToLayoutY);
138 }
139 
layoutToScreenY(int y)140 inline int QwtMetricsMap::layoutToScreenY(int y) const
141 {
142     return qRound(y / d_screenToLayoutY);
143 }
144 
layoutToDevice(const QSize & size)145 inline QSize QwtMetricsMap::layoutToDevice(const QSize &size) const
146 {
147     return QSize(layoutToDeviceX(size.width()),
148         layoutToDeviceY(size.height()));
149 }
150 
deviceToLayout(const QSize & size)151 inline QSize QwtMetricsMap::deviceToLayout(const QSize &size) const
152 {
153     return QSize(deviceToLayoutX(size.width()),
154         deviceToLayoutY(size.height()));
155 }
156 
screenToLayout(const QSize & size)157 inline QSize QwtMetricsMap::screenToLayout(const QSize &size) const
158 {
159     return QSize(screenToLayoutX(size.width()),
160         screenToLayoutY(size.height()));
161 }
162 
layoutToScreen(const QSize & size)163 inline QSize QwtMetricsMap::layoutToScreen(const QSize &size) const
164 {
165     return QSize(layoutToScreenX(size.width()),
166         layoutToScreenY(size.height()));
167 }
168 
169 #endif
170