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 <gunnar.sletta@nokia.com>
Reviewed-by: David Faure <faure@kde.org>
This commit is contained in:
Marc Mutz 2012-02-16 16:38:52 +01:00 committed by Qt by Nokia
parent f8e2753323
commit a68ada633f
2 changed files with 85 additions and 18 deletions

View File

@ -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);
}
}
}

View File

@ -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; }