qpoint.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QPOINT_H
00043 #define QPOINT_H
00044 
00045 #include <QtCore/qnamespace.h>
00046 
00047 QT_BEGIN_HEADER
00048 
00049 QT_BEGIN_NAMESPACE
00050 
00051 QT_MODULE(Core)
00052 
00053 class Q_CORE_EXPORT QPoint
00054 {
00055 public:
00056     QPoint();
00057     QPoint(int xpos, int ypos);
00058 
00059     bool isNull() const;
00060 
00061     int x() const;
00062     int y() const;
00063     void setX(int x);
00064     void setY(int y);
00065 
00066     int manhattanLength() const;
00067 
00068     int &rx();
00069     int &ry();
00070 
00071     QPoint &operator+=(const QPoint &p);
00072     QPoint &operator-=(const QPoint &p);
00073     QPoint &operator*=(qreal c);
00074     QPoint &operator/=(qreal c);
00075 
00076     friend inline bool operator==(const QPoint &, const QPoint &);
00077     friend inline bool operator!=(const QPoint &, const QPoint &);
00078     friend inline const QPoint operator+(const QPoint &, const QPoint &);
00079     friend inline const QPoint operator-(const QPoint &, const QPoint &);
00080     friend inline const QPoint operator*(const QPoint &, qreal);
00081     friend inline const QPoint operator*(qreal, const QPoint &);
00082     friend inline const QPoint operator-(const QPoint &);
00083     friend inline const QPoint operator/(const QPoint &, qreal);
00084 
00085 private:
00086     friend class QTransform;
00087     // ### Qt 5;  remove the ifdef and just have the same order on all platforms.
00088 #if defined(Q_OS_MAC)
00089     int yp;
00090     int xp;
00091 #else
00092     int xp;
00093     int yp;
00094 #endif
00095 };
00096 
00097 Q_DECLARE_TYPEINFO(QPoint, Q_MOVABLE_TYPE);
00098 
00099 /*****************************************************************************
00100   QPoint stream functions
00101  *****************************************************************************/
00102 #ifndef QT_NO_DATASTREAM
00103 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
00104 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
00105 #endif
00106 
00107 /*****************************************************************************
00108   QPoint inline functions
00109  *****************************************************************************/
00110 
00111 inline QPoint::QPoint()
00112 { xp=0; yp=0; }
00113 
00114 inline QPoint::QPoint(int xpos, int ypos)
00115 { xp = xpos; yp = ypos; }
00116 
00117 inline bool QPoint::isNull() const
00118 { return xp == 0 && yp == 0; }
00119 
00120 inline int QPoint::x() const
00121 { return xp; }
00122 
00123 inline int QPoint::y() const
00124 { return yp; }
00125 
00126 inline void QPoint::setX(int xpos)
00127 { xp = xpos; }
00128 
00129 inline void QPoint::setY(int ypos)
00130 { yp = ypos; }
00131 
00132 inline int &QPoint::rx()
00133 { return xp; }
00134 
00135 inline int &QPoint::ry()
00136 { return yp; }
00137 
00138 inline QPoint &QPoint::operator+=(const QPoint &p)
00139 { xp+=p.xp; yp+=p.yp; return *this; }
00140 
00141 inline QPoint &QPoint::operator-=(const QPoint &p)
00142 { xp-=p.xp; yp-=p.yp; return *this; }
00143 
00144 inline QPoint &QPoint::operator*=(qreal c)
00145 { xp = qRound(xp*c); yp = qRound(yp*c); return *this; }
00146 
00147 inline bool operator==(const QPoint &p1, const QPoint &p2)
00148 { return p1.xp == p2.xp && p1.yp == p2.yp; }
00149 
00150 inline bool operator!=(const QPoint &p1, const QPoint &p2)
00151 { return p1.xp != p2.xp || p1.yp != p2.yp; }
00152 
00153 inline const QPoint operator+(const QPoint &p1, const QPoint &p2)
00154 { return QPoint(p1.xp+p2.xp, p1.yp+p2.yp); }
00155 
00156 inline const QPoint operator-(const QPoint &p1, const QPoint &p2)
00157 { return QPoint(p1.xp-p2.xp, p1.yp-p2.yp); }
00158 
00159 inline const QPoint operator*(const QPoint &p, qreal c)
00160 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
00161 
00162 inline const QPoint operator*(qreal c, const QPoint &p)
00163 { return QPoint(qRound(p.xp*c), qRound(p.yp*c)); }
00164 
00165 inline const QPoint operator-(const QPoint &p)
00166 { return QPoint(-p.xp, -p.yp); }
00167 
00168 inline QPoint &QPoint::operator/=(qreal c)
00169 {
00170     xp = qRound(xp/c);
00171     yp = qRound(yp/c);
00172     return *this;
00173 }
00174 
00175 inline const QPoint operator/(const QPoint &p, qreal c)
00176 {
00177     return QPoint(qRound(p.xp/c), qRound(p.yp/c));
00178 }
00179 
00180 #ifndef QT_NO_DEBUG_STREAM
00181 Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
00182 #endif
00183 
00184 
00185 
00186 
00187 
00188 class Q_CORE_EXPORT QPointF
00189 {
00190 public:
00191     QPointF();
00192     QPointF(const QPoint &p);
00193     QPointF(qreal xpos, qreal ypos);
00194 
00195     qreal manhattanLength() const;
00196 
00197     bool isNull() const;
00198 
00199     qreal x() const;
00200     qreal y() const;
00201     void setX(qreal x);
00202     void setY(qreal y);
00203 
00204     qreal &rx();
00205     qreal &ry();
00206 
00207     QPointF &operator+=(const QPointF &p);
00208     QPointF &operator-=(const QPointF &p);
00209     QPointF &operator*=(qreal c);
00210     QPointF &operator/=(qreal c);
00211 
00212     friend inline bool operator==(const QPointF &, const QPointF &);
00213     friend inline bool operator!=(const QPointF &, const QPointF &);
00214     friend inline const QPointF operator+(const QPointF &, const QPointF &);
00215     friend inline const QPointF operator-(const QPointF &, const QPointF &);
00216     friend inline const QPointF operator*(qreal, const QPointF &);
00217     friend inline const QPointF operator*(const QPointF &, qreal);
00218     friend inline const QPointF operator-(const QPointF &);
00219     friend inline const QPointF operator/(const QPointF &, qreal);
00220 
00221     QPoint toPoint() const;
00222 
00223 private:
00224     friend class QMatrix;
00225     friend class QTransform;
00226 
00227     qreal xp;
00228     qreal yp;
00229 };
00230 
00231 Q_DECLARE_TYPEINFO(QPointF, Q_MOVABLE_TYPE);
00232 
00233 /*****************************************************************************
00234   QPointF stream functions
00235  *****************************************************************************/
00236 #ifndef QT_NO_DATASTREAM
00237 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &);
00238 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &);
00239 #endif
00240 
00241 /*****************************************************************************
00242   QPointF inline functions
00243  *****************************************************************************/
00244 
00245 inline QPointF::QPointF() : xp(0), yp(0) { }
00246 
00247 inline QPointF::QPointF(qreal xpos, qreal ypos) : xp(xpos), yp(ypos) { }
00248 
00249 inline QPointF::QPointF(const QPoint &p) : xp(p.x()), yp(p.y()) { }
00250 
00251 inline bool QPointF::isNull() const
00252 {
00253     return qIsNull(xp) && qIsNull(yp);
00254 }
00255 
00256 inline qreal QPointF::x() const
00257 {
00258     return xp;
00259 }
00260 
00261 inline qreal QPointF::y() const
00262 {
00263     return yp;
00264 }
00265 
00266 inline void QPointF::setX(qreal xpos)
00267 {
00268     xp = xpos;
00269 }
00270 
00271 inline void QPointF::setY(qreal ypos)
00272 {
00273     yp = ypos;
00274 }
00275 
00276 inline qreal &QPointF::rx()
00277 {
00278     return xp;
00279 }
00280 
00281 inline qreal &QPointF::ry()
00282 {
00283     return yp;
00284 }
00285 
00286 inline QPointF &QPointF::operator+=(const QPointF &p)
00287 {
00288     xp+=p.xp;
00289     yp+=p.yp;
00290     return *this;
00291 }
00292 
00293 inline QPointF &QPointF::operator-=(const QPointF &p)
00294 {
00295     xp-=p.xp; yp-=p.yp; return *this;
00296 }
00297 
00298 inline QPointF &QPointF::operator*=(qreal c)
00299 {
00300     xp*=c; yp*=c; return *this;
00301 }
00302 
00303 inline bool operator==(const QPointF &p1, const QPointF &p2)
00304 {
00305     return qFuzzyIsNull(p1.xp - p2.xp) && qFuzzyIsNull(p1.yp - p2.yp);
00306 }
00307 
00308 inline bool operator!=(const QPointF &p1, const QPointF &p2)
00309 {
00310     return !qFuzzyIsNull(p1.xp - p2.xp) || !qFuzzyIsNull(p1.yp - p2.yp);
00311 }
00312 
00313 inline const QPointF operator+(const QPointF &p1, const QPointF &p2)
00314 {
00315     return QPointF(p1.xp+p2.xp, p1.yp+p2.yp);
00316 }
00317 
00318 inline const QPointF operator-(const QPointF &p1, const QPointF &p2)
00319 {
00320     return QPointF(p1.xp-p2.xp, p1.yp-p2.yp);
00321 }
00322 
00323 inline const QPointF operator*(const QPointF &p, qreal c)
00324 {
00325     return QPointF(p.xp*c, p.yp*c);
00326 }
00327 
00328 inline const QPointF operator*(qreal c, const QPointF &p)
00329 {
00330     return QPointF(p.xp*c, p.yp*c);
00331 }
00332 
00333 inline const QPointF operator-(const QPointF &p)
00334 {
00335     return QPointF(-p.xp, -p.yp);
00336 }
00337 
00338 inline QPointF &QPointF::operator/=(qreal c)
00339 {
00340     xp/=c;
00341     yp/=c;
00342     return *this;
00343 }
00344 
00345 inline const QPointF operator/(const QPointF &p, qreal c)
00346 {
00347     return QPointF(p.xp/c, p.yp/c);
00348 }
00349 
00350 inline QPoint QPointF::toPoint() const
00351 {
00352     return QPoint(qRound(xp), qRound(yp));
00353 }
00354 
00355 #ifndef QT_NO_DEBUG_STREAM
00356 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
00357 #endif
00358 
00359 QT_END_NAMESPACE
00360 
00361 QT_END_HEADER
00362 
00363 #endif // QPOINT_H