Make QPoint have the same layout on all platforms
[profile/ivi/qtbase.git] / src / corelib / tools / qpoint.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QPOINT_H
43 #define QPOINT_H
44
45 #include <QtCore/qnamespace.h>
46
47 QT_BEGIN_HEADER
48
49 QT_BEGIN_NAMESPACE
50
51 QT_MODULE(Core)
52
53 class Q_CORE_EXPORT QPoint
54 {
55 public:
56     QPoint();
57     QPoint(int xpos, int ypos);
58
59     bool isNull() const;
60
61     int x() const;
62     int y() const;
63     void setX(int x);
64     void setY(int y);
65
66     int manhattanLength() const;
67
68     int &rx();
69     int &ry();
70
71     QPoint &operator+=(const QPoint &p);
72     QPoint &operator-=(const QPoint &p);
73
74     QPoint &operator*=(float c);
75     QPoint &operator*=(double c);
76     QPoint &operator*=(int c);
77
78     QPoint &operator/=(qreal c);
79
80     friend inline bool operator==(const QPoint &, const QPoint &);
81     friend inline bool operator!=(const QPoint &, const QPoint &);
82     friend inline const QPoint operator+(const QPoint &, const QPoint &);
83     friend inline const QPoint operator-(const QPoint &, const QPoint &);
84     friend inline const QPoint operator*(const QPoint &, float);
85     friend inline const QPoint operator*(float, const QPoint &);
86     friend inline const QPoint operator*(const QPoint &, double);
87     friend inline const QPoint operator*(double, const QPoint &);
88     friend inline const QPoint operator*(const QPoint &, int);
89     friend inline const QPoint operator*(int, const QPoint &);
90     friend inline const QPoint operator-(const QPoint &);
91     friend inline const QPoint operator/(const QPoint &, qreal);
92
93 private:
94     friend class QTransform;
95     int xp;
96     int yp;
97 };
98
99 Q_DECLARE_TYPEINFO(QPoint, Q_MOVABLE_TYPE);
100
101 /*****************************************************************************
102   QPoint stream functions
103  *****************************************************************************/
104 #ifndef QT_NO_DATASTREAM
105 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
106 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
107 #endif
108
109 /*****************************************************************************
110   QPoint inline functions
111  *****************************************************************************/
112
113 inline QPoint::QPoint()
114 { xp=0; yp=0; }
115
116 inline QPoint::QPoint(int xpos, int ypos)
117 { xp = xpos; yp = ypos; }
118
119 inline bool QPoint::isNull() const
120 { return xp == 0 && yp == 0; }
121
122 inline int QPoint::x() const
123 { return xp; }
124
125 inline int QPoint::y() const
126 { return yp; }
127
128 inline void QPoint::setX(int xpos)
129 { xp = xpos; }
130
131 inline void QPoint::setY(int ypos)
132 { yp = ypos; }
133
134 inline int &QPoint::rx()
135 { return xp; }
136
137 inline int &QPoint::ry()
138 { return yp; }
139
140 inline QPoint &QPoint::operator+=(const QPoint &p)
141 { xp+=p.xp; yp+=p.yp; return *this; }
142
143 inline QPoint &QPoint::operator-=(const QPoint &p)
144 { xp-=p.xp; yp-=p.yp; return *this; }
145
146 inline QPoint &QPoint::operator*=(float c)
147 { xp = qRound(xp*c); yp = qRound(yp*c); return *this; }
148
149 inline QPoint &QPoint::operator*=(double c)
150 { xp = qRound(xp*c); yp = qRound(yp*c); return *this; }
151
152 inline QPoint &QPoint::operator*=(int c)
153 { xp = xp*c; yp = yp*c; return *this; }
154
155 inline bool operator==(const QPoint &p1, const QPoint &p2)
156 { return p1.xp == p2.xp && p1.yp == p2.yp; }
157
158 inline bool operator!=(const QPoint &p1, const QPoint &p2)
159 { return p1.xp != p2.xp || p1.yp != p2.yp; }
160
161 inline const QPoint operator+(const QPoint &p1, const QPoint &p2)
162 { return QPoint(p1.xp+p2.xp, p1.yp+p2.yp); }
163
164 inline const QPoint operator-(const QPoint &p1, const QPoint &p2)
165 { return QPoint(p1.xp-p2.xp, p1.yp-p2.yp); }
166
167 inline const QPoint operator*(const QPoint &p, float c)
168 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
169
170 inline const QPoint operator*(const QPoint &p, double c)
171 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
172
173 inline const QPoint operator*(const QPoint &p, int c)
174 { return QPoint(p.xp*c, p.yp*c); }
175
176 inline const QPoint operator*(float c, const QPoint &p)
177 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
178
179 inline const QPoint operator*(double c, const QPoint &p)
180 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
181
182 inline const QPoint operator*(int c, const QPoint &p)
183 { return QPoint(p.xp*c, p.yp*c); }
184
185 inline const QPoint operator-(const QPoint &p)
186 { return QPoint(-p.xp, -p.yp); }
187
188 inline QPoint &QPoint::operator/=(qreal c)
189 {
190     xp = qRound(xp/c);
191     yp = qRound(yp/c);
192     return *this;
193 }
194
195 inline const QPoint operator/(const QPoint &p, qreal c)
196 {
197     return QPoint(qRound(p.xp/c), qRound(p.yp/c));
198 }
199
200 #ifndef QT_NO_DEBUG_STREAM
201 Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
202 #endif
203
204
205
206
207
208 class Q_CORE_EXPORT QPointF
209 {
210 public:
211     QPointF();
212     QPointF(const QPoint &p);
213     QPointF(qreal xpos, qreal ypos);
214
215     qreal manhattanLength() const;
216
217     bool isNull() const;
218
219     qreal x() const;
220     qreal y() const;
221     void setX(qreal x);
222     void setY(qreal y);
223
224     qreal &rx();
225     qreal &ry();
226
227     QPointF &operator+=(const QPointF &p);
228     QPointF &operator-=(const QPointF &p);
229     QPointF &operator*=(qreal c);
230     QPointF &operator/=(qreal c);
231
232     friend inline bool operator==(const QPointF &, const QPointF &);
233     friend inline bool operator!=(const QPointF &, const QPointF &);
234     friend inline const QPointF operator+(const QPointF &, const QPointF &);
235     friend inline const QPointF operator-(const QPointF &, const QPointF &);
236     friend inline const QPointF operator*(qreal, const QPointF &);
237     friend inline const QPointF operator*(const QPointF &, qreal);
238     friend inline const QPointF operator-(const QPointF &);
239     friend inline const QPointF operator/(const QPointF &, qreal);
240
241     QPoint toPoint() const;
242
243 private:
244     friend class QMatrix;
245     friend class QTransform;
246
247     qreal xp;
248     qreal yp;
249 };
250
251 Q_DECLARE_TYPEINFO(QPointF, Q_MOVABLE_TYPE);
252
253 /*****************************************************************************
254   QPointF stream functions
255  *****************************************************************************/
256 #ifndef QT_NO_DATASTREAM
257 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &);
258 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &);
259 #endif
260
261 /*****************************************************************************
262   QPointF inline functions
263  *****************************************************************************/
264
265 inline QPointF::QPointF() : xp(0), yp(0) { }
266
267 inline QPointF::QPointF(qreal xpos, qreal ypos) : xp(xpos), yp(ypos) { }
268
269 inline QPointF::QPointF(const QPoint &p) : xp(p.x()), yp(p.y()) { }
270
271 inline bool QPointF::isNull() const
272 {
273     return qIsNull(xp) && qIsNull(yp);
274 }
275
276 inline qreal QPointF::x() const
277 {
278     return xp;
279 }
280
281 inline qreal QPointF::y() const
282 {
283     return yp;
284 }
285
286 inline void QPointF::setX(qreal xpos)
287 {
288     xp = xpos;
289 }
290
291 inline void QPointF::setY(qreal ypos)
292 {
293     yp = ypos;
294 }
295
296 inline qreal &QPointF::rx()
297 {
298     return xp;
299 }
300
301 inline qreal &QPointF::ry()
302 {
303     return yp;
304 }
305
306 inline QPointF &QPointF::operator+=(const QPointF &p)
307 {
308     xp+=p.xp;
309     yp+=p.yp;
310     return *this;
311 }
312
313 inline QPointF &QPointF::operator-=(const QPointF &p)
314 {
315     xp-=p.xp; yp-=p.yp; return *this;
316 }
317
318 inline QPointF &QPointF::operator*=(qreal c)
319 {
320     xp*=c; yp*=c; return *this;
321 }
322
323 inline bool operator==(const QPointF &p1, const QPointF &p2)
324 {
325     return qFuzzyIsNull(p1.xp - p2.xp) && qFuzzyIsNull(p1.yp - p2.yp);
326 }
327
328 inline bool operator!=(const QPointF &p1, const QPointF &p2)
329 {
330     return !qFuzzyIsNull(p1.xp - p2.xp) || !qFuzzyIsNull(p1.yp - p2.yp);
331 }
332
333 inline const QPointF operator+(const QPointF &p1, const QPointF &p2)
334 {
335     return QPointF(p1.xp+p2.xp, p1.yp+p2.yp);
336 }
337
338 inline const QPointF operator-(const QPointF &p1, const QPointF &p2)
339 {
340     return QPointF(p1.xp-p2.xp, p1.yp-p2.yp);
341 }
342
343 inline const QPointF operator*(const QPointF &p, qreal c)
344 {
345     return QPointF(p.xp*c, p.yp*c);
346 }
347
348 inline const QPointF operator*(qreal c, const QPointF &p)
349 {
350     return QPointF(p.xp*c, p.yp*c);
351 }
352
353 inline const QPointF operator-(const QPointF &p)
354 {
355     return QPointF(-p.xp, -p.yp);
356 }
357
358 inline QPointF &QPointF::operator/=(qreal c)
359 {
360     xp/=c;
361     yp/=c;
362     return *this;
363 }
364
365 inline const QPointF operator/(const QPointF &p, qreal c)
366 {
367     return QPointF(p.xp/c, p.yp/c);
368 }
369
370 inline QPoint QPointF::toPoint() const
371 {
372     return QPoint(qRound(xp), qRound(yp));
373 }
374
375 #ifndef QT_NO_DEBUG_STREAM
376 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
377 #endif
378
379 QT_END_NAMESPACE
380
381 QT_END_HEADER
382
383 #endif // QPOINT_H