Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:26:46

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