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