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 QtGui 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 "qlayoutpolicy_p.h"
41#include <QtCore/qdebug.h>
42#include <QtCore/qdatastream.h>
43
44QT_BEGIN_NAMESPACE
45
46void QLayoutPolicy::setControlType(ControlType type)
47{
48 /*
49 The control type is a flag type, with values 0x1, 0x2, 0x4, 0x8, 0x10,
50 etc. In memory, we pack it onto the available bits (CTSize) in
51 setControlType(), and unpack it here.
52
53 Example:
54
55 0x00000001 maps to 0
56 0x00000002 maps to 1
57 0x00000004 maps to 2
58 0x00000008 maps to 3
59 etc.
60 */
61
62 int i = 0;
63 while (true) {
64 if (type & (0x1 << i)) {
65 bits.ctype = i;
66 return;
67 }
68 ++i;
69 }
70}
71
72QLayoutPolicy::ControlType QLayoutPolicy::controlType() const
73{
74 return QLayoutPolicy::ControlType(1 << bits.ctype);
75}
76
77#ifndef QT_NO_DATASTREAM
78
79/*!
80 \relates QLayoutPolicy
81
82 Writes the size \a policy to the data stream \a stream.
83
84 \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
85*/
86QDataStream &operator<<(QDataStream &stream, const QLayoutPolicy &policy)
87{
88 // The order here is for historical reasons. (compatibility with Qt4)
89 quint32 data = (policy.bits.horPolicy | // [0, 3]
90 policy.bits.verPolicy << 4 | // [4, 7]
91 policy.bits.hfw << 8 | // [8]
92 policy.bits.ctype << 9 | // [9, 13]
93 policy.bits.wfh << 14 | // [14]
94 //policy.bits.padding << 15 | // [15]
95 policy.bits.verStretch << 16 | // [16, 23]
96 policy.bits.horStretch << 24); // [24, 31]
97 return stream << data;
98}
99
100#define VALUE_OF_BITS(data, bitstart, bitcount) ((data >> bitstart) & ((1 << bitcount) -1))
101
102/*!
103 \relates QLayoutPolicy
104
105 Reads the size \a policy from the data stream \a stream.
106
107 \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
108*/
109QDataStream &operator>>(QDataStream &stream, QLayoutPolicy &policy)
110{
111 quint32 data;
112 stream >> data;
113 policy.bits.horPolicy = VALUE_OF_BITS(data, 0, 4);
114 policy.bits.verPolicy = VALUE_OF_BITS(data, 4, 4);
115 policy.bits.hfw = VALUE_OF_BITS(data, 8, 1);
116 policy.bits.ctype = VALUE_OF_BITS(data, 9, 5);
117 policy.bits.wfh = VALUE_OF_BITS(data, 14, 1);
118 policy.bits.padding = 0;
119 policy.bits.verStretch = VALUE_OF_BITS(data, 16, 8);
120 policy.bits.horStretch = VALUE_OF_BITS(data, 24, 8);
121 return stream;
122}
123#endif // QT_NO_DATASTREAM
124
125#ifndef QT_NO_DEBUG_STREAM
126QDebug operator<<(QDebug dbg, const QLayoutPolicy &p)
127{
128 QDebugStateSaver saver(dbg);
129 dbg.nospace() << "QLayoutPolicy(horizontalPolicy = " << p.horizontalPolicy()
130 << ", verticalPolicy = " << p.verticalPolicy() << ')';
131 return dbg;
132}
133#endif
134
135QT_END_NAMESPACE
136

source code of qtbase/src/gui/util/qlayoutpolicy.cpp