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