Qt 4.8
qsize.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsize.h"
43 #include "qdatastream.h"
44 #include "qdebug.h"
45 
47 
83 /*****************************************************************************
84  QSize member functions
85  *****************************************************************************/
86 
197 {
198  int tmp = wd;
199  wd = ht;
200  ht = tmp;
201 }
202 
237 {
238  if (mode == Qt::IgnoreAspectRatio || wd == 0 || ht == 0) {
239  wd = s.wd;
240  ht = s.ht;
241  } else {
242  bool useHeight;
243  qint64 rw = qint64(s.ht) * qint64(wd) / qint64(ht);
244 
245  if (mode == Qt::KeepAspectRatio) {
246  useHeight = (rw <= s.wd);
247  } else { // mode == Qt::KeepAspectRatioByExpanding
248  useHeight = (rw >= s.wd);
249  }
250 
251  if (useHeight) {
252  wd = rw;
253  ht = s.ht;
254  } else {
255  ht = qint32(qint64(s.wd) * qint64(ht) / qint64(wd));
256  wd = s.wd;
257  }
258  }
259 }
260 
444 /*****************************************************************************
445  QSize stream functions
446  *****************************************************************************/
447 #ifndef QT_NO_DATASTREAM
448 
462 {
463  if (s.version() == 1)
464  s << (qint16)sz.width() << (qint16)sz.height();
465  else
466  s << (qint32)sz.width() << (qint32)sz.height();
467  return s;
468 }
469 
484 {
485  if (s.version() == 1) {
486  qint16 w, h;
487  s >> w; sz.rwidth() = w;
488  s >> h; sz.rheight() = h;
489  }
490  else {
491  qint32 w, h;
492  s >> w; sz.rwidth() = w;
493  s >> h; sz.rheight() = h;
494  }
495  return s;
496 }
497 #endif // QT_NO_DATASTREAM
498 
499 #ifndef QT_NO_DEBUG_STREAM
500 QDebug operator<<(QDebug dbg, const QSize &s) {
501  dbg.nospace() << "QSize(" << s.width() << ", " << s.height() << ')';
502  return dbg.space();
503 }
504 #endif
505 
506 
507 
546 /*****************************************************************************
547  QSizeF member functions
548  *****************************************************************************/
549 
687 {
688  qreal tmp = wd;
689  wd = ht;
690  ht = tmp;
691 }
692 
727 {
728  if (mode == Qt::IgnoreAspectRatio || qIsNull(wd) || qIsNull(ht)) {
729  wd = s.wd;
730  ht = s.ht;
731  } else {
732  bool useHeight;
733  qreal rw = s.ht * wd / ht;
734 
735  if (mode == Qt::KeepAspectRatio) {
736  useHeight = (rw <= s.wd);
737  } else { // mode == Qt::KeepAspectRatioByExpanding
738  useHeight = (rw >= s.wd);
739  }
740 
741  if (useHeight) {
742  wd = rw;
743  ht = s.ht;
744  } else {
745  ht = s.wd * ht / wd;
746  wd = s.wd;
747  }
748  }
749 }
750 
936 /*****************************************************************************
937  QSizeF stream functions
938  *****************************************************************************/
939 #ifndef QT_NO_DATASTREAM
940 
954 {
955  s << double(sz.width()) << double(sz.height());
956  return s;
957 }
958 
973 {
974  double w, h;
975  s >> w;
976  s >> h;
977  sz.setWidth(qreal(w));
978  sz.setHeight(qreal(h));
979  return s;
980 }
981 #endif // QT_NO_DATASTREAM
982 
983 #ifndef QT_NO_DEBUG_STREAM
984 QDebug operator<<(QDebug dbg, const QSizeF &s) {
985  dbg.nospace() << "QSizeF(" << s.width() << ", " << s.height() << ')';
986  return dbg.space();
987 }
988 #endif
989 
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int qint32
Definition: qglobal.h:937
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
qreal width() const
Returns the width.
Definition: qsize.h:284
qreal height() const
Returns the height.
Definition: qsize.h:287
void scale(qreal w, qreal h, Qt::AspectRatioMode mode)
Scales the size to a rectangle with the given width and height, according to the specified mode...
Definition: qsize.h:296
static bool qIsNull(double d)
Definition: qglobal.h:2061
void setWidth(qreal w)
Sets the width to the given width.
Definition: qsize.h:290
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
void transpose()
Swaps the width and height values.
Definition: qsize.cpp:686
int width() const
Returns the width.
Definition: qsize.h:126
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
short qint16
Definition: qglobal.h:935
void transpose()
Swaps the width and height values.
Definition: qsize.cpp:196
qreal wd
Definition: qsize.h:244
__int64 qint64
Definition: qglobal.h:942
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
int ht
Definition: qsize.h:93
qreal ht
Definition: qsize.h:245
int & rheight()
Returns a reference to the height.
Definition: qsize.h:144
int height() const
Returns the height.
Definition: qsize.h:129
void scale(int w, int h, Qt::AspectRatioMode mode)
Scales the size to a rectangle with the given width and height, according to the specified mode: ...
Definition: qsize.h:138
void setHeight(qreal h)
Sets the height to the given height.
Definition: qsize.h:293
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
QDataStream & operator<<(QDataStream &stream, const QSize &size)
Writes the given size to the given stream, and returns a reference to the stream. ...
Definition: qsize.cpp:461
QDataStream & operator>>(QDataStream &stream, QSize &size)
Reads a size from the given stream into the given size, and returns a reference to the stream...
Definition: qsize.cpp:483
int wd
Definition: qsize.h:92
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
int & rwidth()
Returns a reference to the width.
Definition: qsize.h:141
AspectRatioMode
Definition: qnamespace.h:1317