Back to home page

EIC code displayed by LXR

 
 

    


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

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