Merge remote-tracking branch 'origin/master' into api_changes
[profile/ivi/qtbase.git] / src / corelib / tools / qpoint.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
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
52 class Q_CORE_EXPORT QPoint
53 {
54 public:
55     Q_DECL_CONSTEXPR QPoint();
56     Q_DECL_CONSTEXPR QPoint(int xpos, int ypos);
57
58     Q_DECL_CONSTEXPR bool isNull() const;
59
60     Q_DECL_CONSTEXPR int x() const;
61     Q_DECL_CONSTEXPR int y() const;
62     void setX(int x);
63     void setY(int y);
64
65     Q_DECL_CONSTEXPR int manhattanLength() const;
66
67     int &rx();
68     int &ry();
69
70     QPoint &operator+=(const QPoint &p);
71     QPoint &operator-=(const QPoint &p);
72
73     QPoint &operator*=(float c);
74     QPoint &operator*=(double c);
75     QPoint &operator*=(int c);
76
77     QPoint &operator/=(qreal c);
78
79     friend Q_DECL_CONSTEXPR inline bool operator==(const QPoint &, const QPoint &);
80     friend Q_DECL_CONSTEXPR inline bool operator!=(const QPoint &, const QPoint &);
81     friend Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &, const QPoint &);
82     friend Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &, const QPoint &);
83     friend Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &, float);
84     friend Q_DECL_CONSTEXPR inline const QPoint operator*(float, const QPoint &);
85     friend Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &, double);
86     friend Q_DECL_CONSTEXPR inline const QPoint operator*(double, const QPoint &);
87     friend Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &, int);
88     friend Q_DECL_CONSTEXPR inline const QPoint operator*(int, const QPoint &);
89     friend Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &);
90     friend Q_DECL_CONSTEXPR inline const QPoint operator/(const QPoint &, qreal);
91
92 private:
93     friend class QTransform;
94     int xp;
95     int yp;
96 };
97
98 Q_DECLARE_TYPEINFO(QPoint, Q_MOVABLE_TYPE);
99
100 /*****************************************************************************
101   QPoint stream functions
102  *****************************************************************************/
103 #ifndef QT_NO_DATASTREAM
104 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
105 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
106 #endif
107
108 /*****************************************************************************
109   QPoint inline functions
110  *****************************************************************************/
111
112 Q_DECL_CONSTEXPR inline QPoint::QPoint() : xp(0), yp(0) {}
113
114 Q_DECL_CONSTEXPR inline QPoint::QPoint(int xpos, int ypos) : xp(xpos), yp(ypos) {}
115
116 Q_DECL_CONSTEXPR inline bool QPoint::isNull() const
117 { return xp == 0 && yp == 0; }
118
119 Q_DECL_CONSTEXPR inline int QPoint::x() const
120 { return xp; }
121
122 Q_DECL_CONSTEXPR inline int QPoint::y() const
123 { return yp; }
124
125 inline void QPoint::setX(int xpos)
126 { xp = xpos; }
127
128 inline void QPoint::setY(int ypos)
129 { yp = ypos; }
130
131 inline int Q_DECL_CONSTEXPR QPoint::manhattanLength() const
132 { return qAbs(x())+qAbs(y()); }
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 Q_DECL_CONSTEXPR inline bool operator==(const QPoint &p1, const QPoint &p2)
156 { return p1.xp == p2.xp && p1.yp == p2.yp; }
157
158 Q_DECL_CONSTEXPR inline bool operator!=(const QPoint &p1, const QPoint &p2)
159 { return p1.xp != p2.xp || p1.yp != p2.yp; }
160
161 Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &p1, const QPoint &p2)
162 { return QPoint(p1.xp+p2.xp, p1.yp+p2.yp); }
163
164 Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &p1, const QPoint &p2)
165 { return QPoint(p1.xp-p2.xp, p1.yp-p2.yp); }
166
167 Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &p, float c)
168 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
169
170 Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &p, double c)
171 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
172
173 Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &p, int c)
174 { return QPoint(p.xp*c, p.yp*c); }
175
176 Q_DECL_CONSTEXPR inline const QPoint operator*(float c, const QPoint &p)
177 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
178
179 Q_DECL_CONSTEXPR inline const QPoint operator*(double c, const QPoint &p)
180 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
181
182 Q_DECL_CONSTEXPR inline const QPoint operator*(int c, const QPoint &p)
183 { return QPoint(p.xp*c, p.yp*c); }
184
185 Q_DECL_CONSTEXPR 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 Q_DECL_CONSTEXPR 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     Q_DECL_CONSTEXPR QPointF();
212     Q_DECL_CONSTEXPR QPointF(const QPoint &p);
213     Q_DECL_CONSTEXPR QPointF(qreal xpos, qreal ypos);
214
215     Q_DECL_CONSTEXPR qreal manhattanLength() const;
216
217     bool isNull() const;
218
219     Q_DECL_CONSTEXPR qreal x() const;
220     Q_DECL_CONSTEXPR 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 Q_DECL_CONSTEXPR inline bool operator==(const QPointF &, const QPointF &);
233     friend Q_DECL_CONSTEXPR inline bool operator!=(const QPointF &, const QPointF &);
234     friend Q_DECL_CONSTEXPR inline const QPointF operator+(const QPointF &, const QPointF &);
235     friend Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &, const QPointF &);
236     friend Q_DECL_CONSTEXPR inline const QPointF operator*(qreal, const QPointF &);
237     friend Q_DECL_CONSTEXPR inline const QPointF operator*(const QPointF &, qreal);
238     friend Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &);
239     friend Q_DECL_CONSTEXPR inline const QPointF operator/(const QPointF &, qreal);
240
241     Q_DECL_CONSTEXPR 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 Q_DECL_CONSTEXPR inline QPointF::QPointF() : xp(0), yp(0) { }
266
267 Q_DECL_CONSTEXPR inline QPointF::QPointF(qreal xpos, qreal ypos) : xp(xpos), yp(ypos) { }
268
269 Q_DECL_CONSTEXPR inline QPointF::QPointF(const QPoint &p) : xp(p.x()), yp(p.y()) { }
270
271 Q_DECL_CONSTEXPR inline qreal QPointF::manhattanLength() const
272 {
273     return qAbs(x())+qAbs(y());
274 }
275
276 inline bool QPointF::isNull() const
277 {
278     return qIsNull(xp) && qIsNull(yp);
279 }
280
281 Q_DECL_CONSTEXPR inline qreal QPointF::x() const
282 {
283     return xp;
284 }
285
286 Q_DECL_CONSTEXPR inline qreal QPointF::y() const
287 {
288     return yp;
289 }
290
291 inline void QPointF::setX(qreal xpos)
292 {
293     xp = xpos;
294 }
295
296 inline void QPointF::setY(qreal ypos)
297 {
298     yp = ypos;
299 }
300
301 inline qreal &QPointF::rx()
302 {
303     return xp;
304 }
305
306 inline qreal &QPointF::ry()
307 {
308     return yp;
309 }
310
311 inline QPointF &QPointF::operator+=(const QPointF &p)
312 {
313     xp+=p.xp;
314     yp+=p.yp;
315     return *this;
316 }
317
318 inline QPointF &QPointF::operator-=(const QPointF &p)
319 {
320     xp-=p.xp; yp-=p.yp; return *this;
321 }
322
323 inline QPointF &QPointF::operator*=(qreal c)
324 {
325     xp*=c; yp*=c; return *this;
326 }
327
328 Q_DECL_CONSTEXPR 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 Q_DECL_CONSTEXPR inline bool operator!=(const QPointF &p1, const QPointF &p2)
334 {
335     return !qFuzzyIsNull(p1.xp - p2.xp) || !qFuzzyIsNull(p1.yp - p2.yp);
336 }
337
338 Q_DECL_CONSTEXPR 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 Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &p1, const QPointF &p2)
344 {
345     return QPointF(p1.xp-p2.xp, p1.yp-p2.yp);
346 }
347
348 Q_DECL_CONSTEXPR inline const QPointF operator*(const QPointF &p, qreal c)
349 {
350     return QPointF(p.xp*c, p.yp*c);
351 }
352
353 Q_DECL_CONSTEXPR inline const QPointF operator*(qreal c, const QPointF &p)
354 {
355     return QPointF(p.xp*c, p.yp*c);
356 }
357
358 Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &p)
359 {
360     return QPointF(-p.xp, -p.yp);
361 }
362
363 inline QPointF &QPointF::operator/=(qreal c)
364 {
365     xp/=c;
366     yp/=c;
367     return *this;
368 }
369
370 Q_DECL_CONSTEXPR inline const QPointF operator/(const QPointF &p, qreal c)
371 {
372     return QPointF(p.xp/c, p.yp/c);
373 }
374
375 Q_DECL_CONSTEXPR inline QPoint QPointF::toPoint() const
376 {
377     return QPoint(qRound(xp), qRound(yp));
378 }
379
380 #ifndef QT_NO_DEBUG_STREAM
381 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
382 #endif
383
384 QT_END_NAMESPACE
385
386 QT_END_HEADER
387
388 #endif // QPOINT_H