source: trunk/src/corelib/tools/qpoint.h@ 478

Last change on this file since 478 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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