c0cf2192c1eb000a945f8fe85a0b1490fece53de
[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     // ### Qt 5;  remove the ifdef and just have the same order on all platforms.
96 #if defined(Q_OS_MAC)
97     int yp;
98     int xp;
99 #else
100     int xp;
101     int yp;
102 #endif
103 };
104
105 Q_DECLARE_TYPEINFO(QPoint, Q_MOVABLE_TYPE);
106
107 /*****************************************************************************
108   QPoint stream functions
109  *****************************************************************************/
110 #ifndef QT_NO_DATASTREAM
111 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
112 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
113 #endif
114
115 /*****************************************************************************
116   QPoint inline functions
117  *****************************************************************************/
118
119 inline QPoint::QPoint()
120 { xp=0; yp=0; }
121
122 inline QPoint::QPoint(int xpos, int ypos)
123 { xp = xpos; yp = ypos; }
124
125 inline bool QPoint::isNull() const
126 { return xp == 0 && yp == 0; }
127
128 inline int QPoint::x() const
129 { return xp; }
130
131 inline int QPoint::y() const
132 { return yp; }
133
134 inline void QPoint::setX(int xpos)
135 { xp = xpos; }
136
137 inline void QPoint::setY(int ypos)
138 { yp = ypos; }
139
140 inline int &QPoint::rx()
141 { return xp; }
142
143 inline int &QPoint::ry()
144 { return yp; }
145
146 inline QPoint &QPoint::operator+=(const QPoint &p)
147 { xp+=p.xp; yp+=p.yp; return *this; }
148
149 inline QPoint &QPoint::operator-=(const QPoint &p)
150 { xp-=p.xp; yp-=p.yp; return *this; }
151
152 inline QPoint &QPoint::operator*=(float c)
153 { xp = qRound(xp*c); yp = qRound(yp*c); return *this; }
154
155 inline QPoint &QPoint::operator*=(double c)
156 { xp = qRound(xp*c); yp = qRound(yp*c); return *this; }
157
158 inline QPoint &QPoint::operator*=(int c)
159 { xp = xp*c; yp = yp*c; return *this; }
160
161 inline bool operator==(const QPoint &p1, const QPoint &p2)
162 { return p1.xp == p2.xp && p1.yp == p2.yp; }
163
164 inline bool operator!=(const QPoint &p1, const QPoint &p2)
165 { return p1.xp != p2.xp || p1.yp != p2.yp; }
166
167 inline const QPoint operator+(const QPoint &p1, const QPoint &p2)
168 { return QPoint(p1.xp+p2.xp, p1.yp+p2.yp); }
169
170 inline const QPoint operator-(const QPoint &p1, const QPoint &p2)
171 { return QPoint(p1.xp-p2.xp, p1.yp-p2.yp); }
172
173 inline const QPoint operator*(const QPoint &p, float c)
174 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
175
176 inline const QPoint operator*(const QPoint &p, double c)
177 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
178
179 inline const QPoint operator*(const QPoint &p, int c)
180 { return QPoint(p.xp*c, p.yp*c); }
181
182 inline const QPoint operator*(float c, const QPoint &p)
183 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
184
185 inline const QPoint operator*(double c, const QPoint &p)
186 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
187
188 inline const QPoint operator*(int c, const QPoint &p)
189 { return QPoint(p.xp*c, p.yp*c); }
190
191 inline const QPoint operator-(const QPoint &p)
192 { return QPoint(-p.xp, -p.yp); }
193
194 inline QPoint &QPoint::operator/=(qreal c)
195 {
196     xp = qRound(xp/c);
197     yp = qRound(yp/c);
198     return *this;
199 }
200
201 inline const QPoint operator/(const QPoint &p, qreal c)
202 {
203     return QPoint(qRound(p.xp/c), qRound(p.yp/c));
204 }
205
206 #ifndef QT_NO_DEBUG_STREAM
207 Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
208 #endif
209
210
211
212
213
214 class Q_CORE_EXPORT QPointF
215 {
216 public:
217     QPointF();
218     QPointF(const QPoint &p);
219     QPointF(qreal xpos, qreal ypos);
220
221     qreal manhattanLength() const;
222
223     bool isNull() const;
224
225     qreal x() const;
226     qreal y() const;
227     void setX(qreal x);
228     void setY(qreal y);
229
230     qreal &rx();
231     qreal &ry();
232
233     QPointF &operator+=(const QPointF &p);
234     QPointF &operator-=(const QPointF &p);
235     QPointF &operator*=(qreal c);
236     QPointF &operator/=(qreal c);
237
238     friend inline bool operator==(const QPointF &, const QPointF &);
239     friend inline bool operator!=(const QPointF &, const QPointF &);
240     friend inline const QPointF operator+(const QPointF &, const QPointF &);
241     friend inline const QPointF operator-(const QPointF &, const QPointF &);
242     friend inline const QPointF operator*(qreal, const QPointF &);
243     friend inline const QPointF operator*(const QPointF &, qreal);
244     friend inline const QPointF operator-(const QPointF &);
245     friend inline const QPointF operator/(const QPointF &, qreal);
246
247     QPoint toPoint() const;
248
249 private:
250     friend class QMatrix;
251     friend class QTransform;
252
253     qreal xp;
254     qreal yp;
255 };
256
257 Q_DECLARE_TYPEINFO(QPointF, Q_MOVABLE_TYPE);
258
259 /*****************************************************************************
260   QPointF stream functions
261  *****************************************************************************/
262 #ifndef QT_NO_DATASTREAM
263 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &);
264 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &);
265 #endif
266
267 /*****************************************************************************
268   QPointF inline functions
269  *****************************************************************************/
270
271 inline QPointF::QPointF() : xp(0), yp(0) { }
272
273 inline QPointF::QPointF(qreal xpos, qreal ypos) : xp(xpos), yp(ypos) { }
274
275 inline QPointF::QPointF(const QPoint &p) : xp(p.x()), yp(p.y()) { }
276
277 inline bool QPointF::isNull() const
278 {
279     return qIsNull(xp) && qIsNull(yp);
280 }
281
282 inline qreal QPointF::x() const
283 {
284     return xp;
285 }
286
287 inline qreal QPointF::y() const
288 {
289     return yp;
290 }
291
292 inline void QPointF::setX(qreal xpos)
293 {
294     xp = xpos;
295 }
296
297 inline void QPointF::setY(qreal ypos)
298 {
299     yp = ypos;
300 }
301
302 inline qreal &QPointF::rx()
303 {
304     return xp;
305 }
306
307 inline qreal &QPointF::ry()
308 {
309     return yp;
310 }
311
312 inline QPointF &QPointF::operator+=(const QPointF &p)
313 {
314     xp+=p.xp;
315     yp+=p.yp;
316     return *this;
317 }
318
319 inline QPointF &QPointF::operator-=(const QPointF &p)
320 {
321     xp-=p.xp; yp-=p.yp; return *this;
322 }
323
324 inline QPointF &QPointF::operator*=(qreal c)
325 {
326     xp*=c; yp*=c; return *this;
327 }
328
329 inline bool operator==(const QPointF &p1, const QPointF &p2)
330 {
331     return qFuzzyIsNull(p1.xp - p2.xp) && qFuzzyIsNull(p1.yp - p2.yp);
332 }
333
334 inline bool operator!=(const QPointF &p1, const QPointF &p2)
335 {
336     return !qFuzzyIsNull(p1.xp - p2.xp) || !qFuzzyIsNull(p1.yp - p2.yp);
337 }
338
339 inline const QPointF operator+(const QPointF &p1, const QPointF &p2)
340 {
341     return QPointF(p1.xp+p2.xp, p1.yp+p2.yp);
342 }
343
344 inline const QPointF operator-(const QPointF &p1, const QPointF &p2)
345 {
346     return QPointF(p1.xp-p2.xp, p1.yp-p2.yp);
347 }
348
349 inline const QPointF operator*(const QPointF &p, qreal c)
350 {
351     return QPointF(p.xp*c, p.yp*c);
352 }
353
354 inline const QPointF operator*(qreal c, const QPointF &p)
355 {
356     return QPointF(p.xp*c, p.yp*c);
357 }
358
359 inline const QPointF operator-(const QPointF &p)
360 {
361     return QPointF(-p.xp, -p.yp);
362 }
363
364 inline QPointF &QPointF::operator/=(qreal c)
365 {
366     xp/=c;
367     yp/=c;
368     return *this;
369 }
370
371 inline const QPointF operator/(const QPointF &p, qreal c)
372 {
373     return QPointF(p.xp/c, p.yp/c);
374 }
375
376 inline QPoint QPointF::toPoint() const
377 {
378     return QPoint(qRound(xp), qRound(yp));
379 }
380
381 #ifndef QT_NO_DEBUG_STREAM
382 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
383 #endif
384
385 QT_END_NAMESPACE
386
387 QT_END_HEADER
388
389 #endif // QPOINT_H