Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:07:33

0001 // Copyright (C) 2022 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QPOINT_H
0005 #define QPOINT_H
0006 
0007 #include <QtCore/qnamespace.h>
0008 
0009 #include <QtCore/q20type_traits.h>
0010 #include <QtCore/q23utility.h>
0011 
0012 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0013 struct CGPoint;
0014 #endif
0015 
0016 QT_BEGIN_NAMESPACE
0017 
0018 QT_ENABLE_P0846_SEMANTICS_FOR(get)
0019 
0020 class QPointF;
0021 
0022 class QPoint
0023 {
0024 public:
0025     constexpr QPoint() noexcept;
0026     constexpr QPoint(int xpos, int ypos) noexcept;
0027 
0028     constexpr inline bool isNull() const noexcept;
0029 
0030     constexpr inline int x() const noexcept;
0031     constexpr inline int y() const noexcept;
0032     constexpr inline void setX(int x) noexcept;
0033     constexpr inline void setY(int y) noexcept;
0034 
0035     constexpr inline int manhattanLength() const;
0036 
0037     constexpr QPoint transposed() const noexcept { return {yp, xp}; }
0038 
0039     constexpr inline int &rx() noexcept;
0040     constexpr inline int &ry() noexcept;
0041 
0042     constexpr inline QPoint &operator+=(const QPoint &p);
0043     constexpr inline QPoint &operator-=(const QPoint &p);
0044 
0045     constexpr inline QPoint &operator*=(float factor);
0046     constexpr inline QPoint &operator*=(double factor);
0047     constexpr inline QPoint &operator*=(int factor);
0048 
0049     constexpr inline QPoint &operator/=(qreal divisor);
0050 
0051     constexpr static inline int dotProduct(const QPoint &p1, const QPoint &p2)
0052     { return p1.xp * p2.xp + p1.yp * p2.yp; }
0053 
0054     friend constexpr inline bool operator==(const QPoint &p1, const QPoint &p2) noexcept
0055     { return p1.xp == p2.xp && p1.yp == p2.yp; }
0056     friend constexpr inline bool operator!=(const QPoint &p1, const QPoint &p2) noexcept
0057     { return p1.xp != p2.xp || p1.yp != p2.yp; }
0058     friend constexpr inline QPoint operator+(const QPoint &p1, const QPoint &p2) noexcept
0059     { return QPoint(p1.xp + p2.xp, p1.yp + p2.yp); }
0060     friend constexpr inline QPoint operator-(const QPoint &p1, const QPoint &p2) noexcept
0061     { return QPoint(p1.xp - p2.xp, p1.yp - p2.yp); }
0062     friend constexpr inline QPoint operator*(const QPoint &p, float factor)
0063     { return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
0064     friend constexpr inline QPoint operator*(const QPoint &p, double factor)
0065     { return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
0066     friend constexpr inline QPoint operator*(const QPoint &p, int factor) noexcept
0067     { return QPoint(p.xp * factor, p.yp * factor); }
0068     friend constexpr inline QPoint operator*(float factor, const QPoint &p)
0069     { return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
0070     friend constexpr inline QPoint operator*(double factor, const QPoint &p)
0071     { return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
0072     friend constexpr inline QPoint operator*(int factor, const QPoint &p) noexcept
0073     { return QPoint(p.xp * factor, p.yp * factor); }
0074     friend constexpr inline QPoint operator+(const QPoint &p) noexcept
0075     { return p; }
0076     friend constexpr inline QPoint operator-(const QPoint &p) noexcept
0077     { return QPoint(-p.xp, -p.yp); }
0078     friend constexpr inline QPoint operator/(const QPoint &p, qreal c)
0079     { return QPoint(qRound(p.xp / c), qRound(p.yp / c)); }
0080 
0081 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0082     [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept;
0083 #endif
0084     [[nodiscard]] constexpr inline QPointF toPointF() const noexcept;
0085 
0086 private:
0087     friend class QTransform;
0088     int xp;
0089     int yp;
0090 
0091     template <std::size_t I,
0092               typename P,
0093               std::enable_if_t<(I < 2), bool> = true,
0094               std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPoint>, bool> = true>
0095     friend constexpr decltype(auto) get(P &&p) noexcept
0096     {
0097         if constexpr (I == 0)
0098             return q23::forward_like<P>(p.xp);
0099         else if constexpr (I == 1)
0100             return q23::forward_like<P>(p.yp);
0101     }
0102 };
0103 
0104 Q_DECLARE_TYPEINFO(QPoint, Q_PRIMITIVE_TYPE);
0105 
0106 /*****************************************************************************
0107   QPoint stream functions
0108  *****************************************************************************/
0109 #ifndef QT_NO_DATASTREAM
0110 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
0111 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
0112 #endif
0113 
0114 /*****************************************************************************
0115   QPoint inline functions
0116  *****************************************************************************/
0117 
0118 constexpr inline QPoint::QPoint() noexcept : xp(0), yp(0) {}
0119 
0120 constexpr inline QPoint::QPoint(int xpos, int ypos) noexcept : xp(xpos), yp(ypos) {}
0121 
0122 constexpr inline bool QPoint::isNull() const noexcept
0123 {
0124     return xp == 0 && yp == 0;
0125 }
0126 
0127 constexpr inline int QPoint::x() const noexcept
0128 {
0129     return xp;
0130 }
0131 
0132 constexpr inline int QPoint::y() const noexcept
0133 {
0134     return yp;
0135 }
0136 
0137 constexpr inline void QPoint::setX(int xpos) noexcept
0138 {
0139     xp = xpos;
0140 }
0141 
0142 constexpr inline void QPoint::setY(int ypos) noexcept
0143 {
0144     yp = ypos;
0145 }
0146 
0147 inline int constexpr QPoint::manhattanLength() const
0148 {
0149     return qAbs(x()) + qAbs(y());
0150 }
0151 
0152 constexpr inline int &QPoint::rx() noexcept
0153 {
0154     return xp;
0155 }
0156 
0157 constexpr inline int &QPoint::ry() noexcept
0158 {
0159     return yp;
0160 }
0161 
0162 constexpr inline QPoint &QPoint::operator+=(const QPoint &p)
0163 {
0164     xp += p.xp;
0165     yp += p.yp;
0166     return *this;
0167 }
0168 
0169 constexpr inline QPoint &QPoint::operator-=(const QPoint &p)
0170 {
0171     xp -= p.xp;
0172     yp -= p.yp;
0173     return *this;
0174 }
0175 
0176 constexpr inline QPoint &QPoint::operator*=(float factor)
0177 {
0178     xp = qRound(xp * factor);
0179     yp = qRound(yp * factor);
0180     return *this;
0181 }
0182 
0183 constexpr inline QPoint &QPoint::operator*=(double factor)
0184 {
0185     xp = qRound(xp * factor);
0186     yp = qRound(yp * factor);
0187     return *this;
0188 }
0189 
0190 constexpr inline QPoint &QPoint::operator*=(int factor)
0191 {
0192     xp = xp * factor;
0193     yp = yp * factor;
0194     return *this;
0195 }
0196 
0197 constexpr inline QPoint &QPoint::operator/=(qreal c)
0198 {
0199     xp = qRound(xp / c);
0200     yp = qRound(yp / c);
0201     return *this;
0202 }
0203 
0204 #ifndef QT_NO_DEBUG_STREAM
0205 Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
0206 #endif
0207 
0208 Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept;
0209 
0210 
0211 
0212 
0213 class QPointF
0214 {
0215 public:
0216     constexpr QPointF() noexcept;
0217     constexpr QPointF(const QPoint &p) noexcept;
0218     constexpr QPointF(qreal xpos, qreal ypos) noexcept;
0219 
0220     constexpr inline qreal manhattanLength() const;
0221 
0222     inline bool isNull() const noexcept;
0223 
0224     constexpr inline qreal x() const noexcept;
0225     constexpr inline qreal y() const noexcept;
0226     constexpr inline void setX(qreal x) noexcept;
0227     constexpr inline void setY(qreal y) noexcept;
0228 
0229     constexpr QPointF transposed() const noexcept { return {yp, xp}; }
0230 
0231     constexpr inline qreal &rx() noexcept;
0232     constexpr inline qreal &ry() noexcept;
0233 
0234     constexpr inline QPointF &operator+=(const QPointF &p);
0235     constexpr inline QPointF &operator-=(const QPointF &p);
0236     constexpr inline QPointF &operator*=(qreal c);
0237     constexpr inline QPointF &operator/=(qreal c);
0238 
0239     constexpr static inline qreal dotProduct(const QPointF &p1, const QPointF &p2)
0240     {
0241         return p1.xp * p2.xp + p1.yp * p2.yp;
0242     }
0243 
0244     QT_WARNING_PUSH
0245     QT_WARNING_DISABLE_FLOAT_COMPARE
0246     friend constexpr inline bool operator==(const QPointF &p1, const QPointF &p2)
0247     {
0248         return ((!p1.xp || !p2.xp) ? qFuzzyIsNull(p1.xp - p2.xp) : qFuzzyCompare(p1.xp, p2.xp))
0249             && ((!p1.yp || !p2.yp) ? qFuzzyIsNull(p1.yp - p2.yp) : qFuzzyCompare(p1.yp, p2.yp));
0250     }
0251     friend constexpr inline bool operator!=(const QPointF &p1, const QPointF &p2)
0252     {
0253         return !(p1 == p2);
0254     }
0255     QT_WARNING_POP
0256 
0257     friend constexpr inline QPointF operator+(const QPointF &p1, const QPointF &p2)
0258     { return QPointF(p1.xp + p2.xp, p1.yp + p2.yp); }
0259     friend constexpr inline QPointF operator-(const QPointF &p1, const QPointF &p2)
0260     { return QPointF(p1.xp - p2.xp, p1.yp - p2.yp); }
0261     friend constexpr inline QPointF operator*(const QPointF &p, qreal c)
0262     { return QPointF(p.xp * c, p.yp * c); }
0263     friend constexpr inline QPointF operator*(qreal c, const QPointF &p)
0264     { return QPointF(p.xp * c, p.yp * c); }
0265     friend constexpr inline QPointF operator+(const QPointF &p)
0266     { return p; }
0267     friend constexpr inline QPointF operator-(const QPointF &p)
0268     { return QPointF(-p.xp, -p.yp); }
0269     friend constexpr inline QPointF operator/(const QPointF &p, qreal divisor)
0270     {
0271         Q_ASSERT(divisor < 0 || divisor > 0);
0272         return QPointF(p.xp / divisor, p.yp / divisor);
0273     }
0274 
0275     constexpr QPoint toPoint() const;
0276 
0277 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0278     [[nodiscard]] Q_CORE_EXPORT static QPointF fromCGPoint(CGPoint point) noexcept;
0279     [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept;
0280 #endif
0281 
0282 private:
0283     friend class QTransform;
0284 
0285     qreal xp;
0286     qreal yp;
0287 
0288     template <std::size_t I,
0289               typename P,
0290               std::enable_if_t<(I < 2), bool> = true,
0291               std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPointF>, bool> = true>
0292     friend constexpr decltype(auto) get(P &&p) noexcept
0293     {
0294         if constexpr (I == 0)
0295             return q23::forward_like<P>(p.xp);
0296         else if constexpr (I == 1)
0297             return q23::forward_like<P>(p.yp);
0298     }
0299 };
0300 
0301 Q_DECLARE_TYPEINFO(QPointF, Q_PRIMITIVE_TYPE);
0302 
0303 size_t qHash(QPointF, size_t seed = 0) = delete;
0304 
0305 /*****************************************************************************
0306   QPointF stream functions
0307  *****************************************************************************/
0308 #ifndef QT_NO_DATASTREAM
0309 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &);
0310 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &);
0311 #endif
0312 
0313 /*****************************************************************************
0314   QPointF inline functions
0315  *****************************************************************************/
0316 
0317 constexpr inline QPointF::QPointF() noexcept : xp(0), yp(0) { }
0318 
0319 constexpr inline QPointF::QPointF(qreal xpos, qreal ypos) noexcept : xp(xpos), yp(ypos) { }
0320 
0321 constexpr inline QPointF::QPointF(const QPoint &p) noexcept : xp(p.x()), yp(p.y()) { }
0322 
0323 constexpr inline qreal QPointF::manhattanLength() const
0324 {
0325     return qAbs(x()) + qAbs(y());
0326 }
0327 
0328 inline bool QPointF::isNull() const noexcept
0329 {
0330     return qIsNull(xp) && qIsNull(yp);
0331 }
0332 
0333 constexpr inline qreal QPointF::x() const noexcept
0334 {
0335     return xp;
0336 }
0337 
0338 constexpr inline qreal QPointF::y() const noexcept
0339 {
0340     return yp;
0341 }
0342 
0343 constexpr inline void QPointF::setX(qreal xpos) noexcept
0344 {
0345     xp = xpos;
0346 }
0347 
0348 constexpr inline void QPointF::setY(qreal ypos) noexcept
0349 {
0350     yp = ypos;
0351 }
0352 
0353 constexpr inline qreal &QPointF::rx() noexcept
0354 {
0355     return xp;
0356 }
0357 
0358 constexpr inline qreal &QPointF::ry() noexcept
0359 {
0360     return yp;
0361 }
0362 
0363 constexpr inline QPointF &QPointF::operator+=(const QPointF &p)
0364 {
0365     xp += p.xp;
0366     yp += p.yp;
0367     return *this;
0368 }
0369 
0370 constexpr inline QPointF &QPointF::operator-=(const QPointF &p)
0371 {
0372     xp -= p.xp;
0373     yp -= p.yp;
0374     return *this;
0375 }
0376 
0377 constexpr inline QPointF &QPointF::operator*=(qreal c)
0378 {
0379     xp *= c;
0380     yp *= c;
0381     return *this;
0382 }
0383 
0384 constexpr inline QPointF &QPointF::operator/=(qreal divisor)
0385 {
0386     Q_ASSERT(divisor > 0 || divisor < 0);
0387     xp /= divisor;
0388     yp /= divisor;
0389     return *this;
0390 }
0391 
0392 constexpr QPointF QPoint::toPointF() const noexcept { return *this; }
0393 
0394 constexpr inline QPoint QPointF::toPoint() const
0395 {
0396     return QPoint(qRound(xp), qRound(yp));
0397 }
0398 
0399 #ifndef QT_NO_DEBUG_STREAM
0400 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
0401 #endif
0402 
0403 QT_END_NAMESPACE
0404 
0405 /*****************************************************************************
0406   QPoint/QPointF tuple protocol
0407  *****************************************************************************/
0408 
0409 namespace std {
0410     template <>
0411     class tuple_size<QT_PREPEND_NAMESPACE(QPoint)> : public integral_constant<size_t, 2> {};
0412     template <>
0413     class tuple_element<0, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; };
0414     template <>
0415     class tuple_element<1, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; };
0416 
0417     template <>
0418     class tuple_size<QT_PREPEND_NAMESPACE(QPointF)> : public integral_constant<size_t, 2> {};
0419     template <>
0420     class tuple_element<0, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); };
0421     template <>
0422     class tuple_element<1, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); };
0423 }
0424 
0425 #endif // QPOINT_H