From a68ada633f165d1a6f9d4a4b3cab26487e92cb21 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 16 Feb 2012 16:38:52 +0100 Subject: [PATCH] QSize/QSizeF: add const versions of scale()/transpose() Non-mutating functions can (potentially) be constexpr. On top of that, they often make for more readable code. Change-Id: I3547327cf5a7162737353a864a1025d0d02ccc2f Reviewed-by: Gunnar Sletta Reviewed-by: David Faure --- src/corelib/tools/qsize.cpp | 79 ++++++++++++++++++++++++++++--------- src/corelib/tools/qsize.h | 24 +++++++++++ 2 files changed, 85 insertions(+), 18 deletions(-) diff --git a/src/corelib/tools/qsize.cpp b/src/corelib/tools/qsize.cpp index d5e6f18326..4c94f899e7 100644 --- a/src/corelib/tools/qsize.cpp +++ b/src/corelib/tools/qsize.cpp @@ -160,7 +160,7 @@ QT_BEGIN_NAMESPACE /*! Swaps the width and height values. - \sa setWidth(), setHeight() + \sa setWidth(), setHeight(), transposed() */ void QSize::transpose() @@ -170,6 +170,15 @@ void QSize::transpose() ht = tmp; } +/*! + \fn QSize QSize::transposed() const + \since 5.0 + + Returns a QSize with width and height swapped. + + \sa transpose() +*/ + /*! \fn void QSize::scale(int width, int height, Qt::AspectRatioMode mode) @@ -187,7 +196,7 @@ void QSize::transpose() Example: \snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 0 - \sa setWidth(), setHeight() + \sa setWidth(), setHeight(), scaled() */ /*! @@ -197,11 +206,25 @@ void QSize::transpose() Scales the size to a rectangle with the given \a size, according to the specified \a mode. */ -void QSize::scale(const QSize &s, Qt::AspectRatioMode mode) + +/*! + \fn QSize QSize::scaled(int width, int height, Qt::AspectRatioMode mode) const + \since 5.0 + + Return a size scaled to a rectangle with the given \a width and \a + height, according to the specified \a mode. + + \sa scale() +*/ + +/*! + \overload + \since 5.0 +*/ +QSize QSize::scaled(const QSize &s, Qt::AspectRatioMode mode) const { if (mode == Qt::IgnoreAspectRatio || wd == 0 || ht == 0) { - wd = s.wd; - ht = s.ht; + return s; } else { bool useHeight; qint64 rw = qint64(s.ht) * qint64(wd) / qint64(ht); @@ -213,11 +236,10 @@ void QSize::scale(const QSize &s, Qt::AspectRatioMode mode) } if (useHeight) { - wd = rw; - ht = s.ht; + return QSize(rw, s.ht); } else { - ht = qint32(qint64(s.wd) * qint64(ht) / qint64(wd)); - wd = s.wd; + return QSize(s.wd, + qint32(qint64(s.wd) * qint64(ht) / qint64(wd))); } } } @@ -566,7 +588,7 @@ QDebug operator<<(QDebug dbg, const QSize &s) { /*! Swaps the width and height values. - \sa setWidth(), setHeight() + \sa setWidth(), setHeight(), transposed() */ void QSizeF::transpose() @@ -576,6 +598,15 @@ void QSizeF::transpose() ht = tmp; } +/*! + \fn QSizeF QSizeF::transposed() const + \since 5.0 + + Returns the size with width and height values swapped. + + \sa transpose() +*/ + /*! \fn void QSizeF::scale(qreal width, qreal height, Qt::AspectRatioMode mode) @@ -593,7 +624,7 @@ void QSizeF::transpose() Example: \snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 5 - \sa setWidth(), setHeight() + \sa setWidth(), setHeight(), scaled() */ /*! @@ -603,11 +634,25 @@ void QSizeF::transpose() Scales the size to a rectangle with the given \a size, according to the specified \a mode. */ -void QSizeF::scale(const QSizeF &s, Qt::AspectRatioMode mode) + +/*! + \fn QSizeF QSizeF::scaled(int width, int height, Qt::AspectRatioMode mode) const + \since 5.0 + + Returns a size scaled to a rectangle with the given \a width and + \a height, according to the specified \mode. + + \sa scale() +*/ + +/*! + \overload + \since 5.0 +*/ +QSizeF QSizeF::scaled(const QSizeF &s, Qt::AspectRatioMode mode) const { if (mode == Qt::IgnoreAspectRatio || qIsNull(wd) || qIsNull(ht)) { - wd = s.wd; - ht = s.ht; + return s; } else { bool useHeight; qreal rw = s.ht * wd / ht; @@ -619,11 +664,9 @@ void QSizeF::scale(const QSizeF &s, Qt::AspectRatioMode mode) } if (useHeight) { - wd = rw; - ht = s.ht; + return QSizeF(rw, s.ht); } else { - ht = s.wd * ht / wd; - wd = s.wd; + return QSizeF(s.wd, s.wd * ht / wd); } } } diff --git a/src/corelib/tools/qsize.h b/src/corelib/tools/qsize.h index 5400f76a3a..fece0ac943 100644 --- a/src/corelib/tools/qsize.h +++ b/src/corelib/tools/qsize.h @@ -64,9 +64,12 @@ public: void setWidth(int w); void setHeight(int h); void transpose(); + QSize transposed() const; void scale(int w, int h, Qt::AspectRatioMode mode); void scale(const QSize &s, Qt::AspectRatioMode mode); + QSize scaled(int w, int h, Qt::AspectRatioMode mode) const; + QSize scaled(const QSize &s, Qt::AspectRatioMode mode) const; QSize expandedTo(const QSize &) const; QSize boundedTo(const QSize &) const; @@ -134,9 +137,18 @@ inline void QSize::setWidth(int w) inline void QSize::setHeight(int h) { ht = h; } +inline QSize QSize::transposed() const +{ return QSize(ht, wd); } + inline void QSize::scale(int w, int h, Qt::AspectRatioMode mode) { scale(QSize(w, h), mode); } +inline void QSize::scale(const QSize &s, Qt::AspectRatioMode mode) +{ *this = scaled(s, mode); } + +inline QSize QSize::scaled(int w, int h, Qt::AspectRatioMode mode) const +{ return scaled(QSize(w, h), mode); } + inline int &QSize::rwidth() { return wd; } @@ -214,9 +226,12 @@ public: void setWidth(qreal w); void setHeight(qreal h); void transpose(); + QSizeF transposed() const; void scale(qreal w, qreal h, Qt::AspectRatioMode mode); void scale(const QSizeF &s, Qt::AspectRatioMode mode); + QSizeF scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const; + QSizeF scaled(const QSizeF &s, Qt::AspectRatioMode mode) const; QSizeF expandedTo(const QSizeF &) const; QSizeF boundedTo(const QSizeF &) const; @@ -292,9 +307,18 @@ inline void QSizeF::setWidth(qreal w) inline void QSizeF::setHeight(qreal h) { ht = h; } +inline QSizeF QSizeF::transposed() const +{ return QSizeF(ht, wd); } + inline void QSizeF::scale(qreal w, qreal h, Qt::AspectRatioMode mode) { scale(QSizeF(w, h), mode); } +inline void QSizeF::scale(const QSizeF &s, Qt::AspectRatioMode mode) +{ *this = scaled(s, mode); } + +inline QSizeF QSizeF::scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const +{ return scaled(QSizeF(w, h), mode); } + inline qreal &QSizeF::rwidth() { return wd; }