QtBase  v6.3.1
qsize.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qsize.h"
41 #include "qdatastream.h"
42 
43 #include <private/qdebug_p.h>
44 
46 
80 /*****************************************************************************
81  QSize member functions
82  *****************************************************************************/
83 
166 void QSize::transpose() noexcept
167 {
168  qSwap(wd, ht);
169 }
170 
226 {
227  if (mode == Qt::IgnoreAspectRatio || wd == 0 || ht == 0) {
228  return s;
229  } else {
230  bool useHeight;
231  qint64 rw = qint64(s.ht) * qint64(wd) / qint64(ht);
232 
233  if (mode == Qt::KeepAspectRatio) {
234  useHeight = (rw <= s.wd);
235  } else { // mode == Qt::KeepAspectRatioByExpanding
236  useHeight = (rw >= s.wd);
237  }
238 
239  if (useHeight) {
240  return QSize(rw, s.ht);
241  } else {
242  return QSize(s.wd,
243  qint32(qint64(s.wd) * qint64(ht) / qint64(wd)));
244  }
245  }
246 }
247 
406 /*****************************************************************************
407  QSize stream functions
408  *****************************************************************************/
409 #ifndef QT_NO_DATASTREAM
421 {
422  if (s.version() == 1)
423  s << (qint16)sz.width() << (qint16)sz.height();
424  else
425  s << (qint32)sz.width() << (qint32)sz.height();
426  return s;
427 }
428 
440 {
441  if (s.version() == 1) {
442  qint16 w, h;
443  s >> w; sz.rwidth() = w;
444  s >> h; sz.rheight() = h;
445  }
446  else {
447  qint32 w, h;
448  s >> w; sz.rwidth() = w;
449  s >> h; sz.rheight() = h;
450  }
451  return s;
452 }
453 #endif // QT_NO_DATASTREAM
454 
455 #ifndef QT_NO_DEBUG_STREAM
457 {
458  QDebugStateSaver saver(dbg);
459  dbg.nospace();
460  dbg << "QSize(";
461  QtDebugUtils::formatQSize(dbg, s);
462  dbg << ')';
463  return dbg;
464 }
465 #endif
466 
467 
468 
505 /*****************************************************************************
506  QSizeF member functions
507  *****************************************************************************/
508 
608 void QSizeF::transpose() noexcept
609 {
610  qSwap(wd, ht);
611 }
612 
668 {
669  if (mode == Qt::IgnoreAspectRatio || qIsNull(wd) || qIsNull(ht)) {
670  return s;
671  } else {
672  bool useHeight;
673  qreal rw = s.ht * wd / ht;
674 
675  if (mode == Qt::KeepAspectRatio) {
676  useHeight = (rw <= s.wd);
677  } else { // mode == Qt::KeepAspectRatioByExpanding
678  useHeight = (rw >= s.wd);
679  }
680 
681  if (useHeight) {
682  return QSizeF(rw, s.ht);
683  } else {
684  return QSizeF(s.wd, s.wd * ht / wd);
685  }
686  }
687 }
688 
840 /*****************************************************************************
841  QSizeF stream functions
842  *****************************************************************************/
843 #ifndef QT_NO_DATASTREAM
855 {
856  s << double(sz.width()) << double(sz.height());
857  return s;
858 }
859 
871 {
872  double w, h;
873  s >> w;
874  s >> h;
875  sz.setWidth(qreal(w));
876  sz.setHeight(qreal(h));
877  return s;
878 }
879 #endif // QT_NO_DATASTREAM
880 
881 #ifndef QT_NO_DEBUG_STREAM
883 {
884  QDebugStateSaver saver(dbg);
885  dbg.nospace();
886  dbg << "QSizeF(";
887  QtDebugUtils::formatQSize(dbg, s);
888  dbg << ')';
889  return dbg;
890 }
891 #endif
892 
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
operator>>(QDataStream &ds, qfloat16 &f)
Definition: qfloat16.cpp:344
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
QDebug & nospace()
Definition: qdebug.h:113
Convenience class for custom QDebug operators.
Definition: qdebug.h:176
The QSizeF class defines the size of a two-dimensional object using floating point precision.
Definition: qsize.h:235
constexpr void setHeight(qreal h) noexcept
Definition: qsize.h:358
constexpr void setWidth(qreal w) noexcept
Definition: qsize.h:355
constexpr qreal width() const noexcept
Definition: qsize.h:349
QSizeF scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const noexcept
Definition: qsize.h:370
constexpr qreal height() const noexcept
Definition: qsize.h:352
void transpose() noexcept
Definition: qsize.cpp:608
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
constexpr int height() const noexcept
Definition: qsize.h:160
constexpr int width() const noexcept
Definition: qsize.h:157
constexpr int & rheight() noexcept
Definition: qsize.h:184
QSize scaled(int w, int h, Qt::AspectRatioMode mode) const noexcept
Definition: qsize.h:178
constexpr int & rwidth() noexcept
Definition: qsize.h:181
void transpose() noexcept
Definition: qsize.cpp:166
qSwap(pi, e)
AspectRatioMode
Definition: qnamespace.h:1211
@ KeepAspectRatio
Definition: qnamespace.h:1213
@ IgnoreAspectRatio
Definition: qnamespace.h:1212
bool qIsNull(qfloat16 f) noexcept
Definition: qfloat16.h:254
short qint16
Definition: qglobal.h:285
int qint32
Definition: qglobal.h:287
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
long long qint64
Definition: qglobal.h:298
GLenum mode
GLfloat GLfloat GLfloat w
[0]
GLfloat GLfloat GLfloat GLfloat h
GLdouble s
[6]
Definition: qopenglext.h:235