Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsize.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsize.h"
5#include "qdatastream.h"
6
7#include <private/qdebug_p.h>
8
10
44/*****************************************************************************
45 QSize member functions
46 *****************************************************************************/
47
130void QSize::transpose() noexcept
131{
132 qSwap(wd, ht);
133}
134
190{
191 if (mode == Qt::IgnoreAspectRatio || wd == 0 || ht == 0) {
192 return s;
193 } else {
194 bool useHeight;
195 qint64 rw = qint64(s.ht) * qint64(wd) / qint64(ht);
196
197 if (mode == Qt::KeepAspectRatio) {
198 useHeight = (rw <= s.wd);
199 } else { // mode == Qt::KeepAspectRatioByExpanding
200 useHeight = (rw >= s.wd);
201 }
202
203 if (useHeight) {
204 return QSize(rw, s.ht);
205 } else {
206 return QSize(s.wd,
207 qint32(qint64(s.wd) * qint64(ht) / qint64(wd)));
208 }
209 }
210}
211
379/*****************************************************************************
380 QSize stream functions
381 *****************************************************************************/
382#ifndef QT_NO_DATASTREAM
394{
395 if (s.version() == 1)
396 s << (qint16)sz.width() << (qint16)sz.height();
397 else
398 s << (qint32)sz.width() << (qint32)sz.height();
399 return s;
400}
401
413{
414 if (s.version() == 1) {
415 qint16 w, h;
416 s >> w; sz.rwidth() = w;
417 s >> h; sz.rheight() = h;
418 }
419 else {
420 qint32 w, h;
421 s >> w; sz.rwidth() = w;
422 s >> h; sz.rheight() = h;
423 }
424 return s;
425}
426#endif // QT_NO_DATASTREAM
427
428#ifndef QT_NO_DEBUG_STREAM
430{
431 QDebugStateSaver saver(dbg);
432 dbg.nospace();
433 dbg << "QSize(";
435 dbg << ')';
436 return dbg;
437}
438#endif
439
440
441
478/*****************************************************************************
479 QSizeF member functions
480 *****************************************************************************/
481
581void QSizeF::transpose() noexcept
582{
583 qSwap(wd, ht);
584}
585
641{
642 if (mode == Qt::IgnoreAspectRatio || qIsNull(wd) || qIsNull(ht)) {
643 return s;
644 } else {
645 bool useHeight;
646 qreal rw = s.ht * wd / ht;
647
648 if (mode == Qt::KeepAspectRatio) {
649 useHeight = (rw <= s.wd);
650 } else { // mode == Qt::KeepAspectRatioByExpanding
651 useHeight = (rw >= s.wd);
652 }
653
654 if (useHeight) {
655 return QSizeF(rw, s.ht);
656 } else {
657 return QSizeF(s.wd, s.wd * ht / wd);
658 }
659 }
660}
661
813/*****************************************************************************
814 QSizeF stream functions
815 *****************************************************************************/
816#ifndef QT_NO_DATASTREAM
828{
829 s << double(sz.width()) << double(sz.height());
830 return s;
831}
832
844{
845 double w, h;
846 s >> w;
847 s >> h;
848 sz.setWidth(qreal(w));
849 sz.setHeight(qreal(h));
850 return s;
851}
852#endif // QT_NO_DATASTREAM
853
854#ifndef QT_NO_DEBUG_STREAM
856{
857 QDebugStateSaver saver(dbg);
858 dbg.nospace();
859 dbg << "QSizeF(";
861 dbg << ')';
862 return dbg;
863}
864#endif
865
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qsize.h:207
constexpr void setHeight(qreal h) noexcept
Sets the height to the given finite height.
Definition qsize.h:330
constexpr void setWidth(qreal w) noexcept
Sets the width to the given finite width.
Definition qsize.h:327
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:321
QSizeF scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const noexcept
Definition qsize.h:342
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:324
void transpose() noexcept
Swaps the width and height values.
Definition qsize.cpp:581
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:132
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:129
constexpr int & rheight() noexcept
Returns a reference to the height.
Definition qsize.h:156
QSize scaled(int w, int h, Qt::AspectRatioMode mode) const noexcept
Definition qsize.h:150
constexpr int & rwidth() noexcept
Returns a reference to the width.
Definition qsize.h:153
void transpose() noexcept
Swaps the width and height values.
Definition qsize.cpp:130
qSwap(pi, e)
Combined button and popup list for selecting options.
static void formatQSize(QDebug &debug, const Size &size)
Definition qdebug_p.h:39
AspectRatioMode
@ KeepAspectRatio
@ IgnoreAspectRatio
bool qIsNull(qfloat16 f) noexcept
Definition qfloat16.h:308
GLenum mode
GLfloat GLfloat GLfloat w
[0]
GLfloat GLfloat GLfloat GLfloat h
GLdouble s
[6]
Definition qopenglext.h:235
QDataStream & operator<<(QDataStream &s, const QSize &sz)
Definition qsize.cpp:393
QDataStream & operator>>(QDataStream &s, QSize &sz)
Definition qsize.cpp:412
short qint16
Definition qtypes.h:42
int qint32
Definition qtypes.h:44
long long qint64
Definition qtypes.h:55
double qreal
Definition qtypes.h:92