1// Copyright (C) 2016 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 "qlayoutpolicy_p.h"
5#include <QtCore/qdebug.h>
6#include <QtCore/qdatastream.h>
7
8QT_BEGIN_NAMESPACE
9
10void QLayoutPolicy::setControlType(ControlType type)
11{
12 /*
13 The control type is a flag type, with values 0x1, 0x2, 0x4, 0x8, 0x10,
14 etc. In memory, we pack it onto the available bits (CTSize) in
15 setControlType(), and unpack it here.
16
17 Example:
18
19 0x00000001 maps to 0
20 0x00000002 maps to 1
21 0x00000004 maps to 2
22 0x00000008 maps to 3
23 etc.
24 */
25
26 int i = 0;
27 while (true) {
28 if (type & (0x1 << i)) {
29 bits.ctype = i;
30 return;
31 }
32 ++i;
33 }
34}
35
36QLayoutPolicy::ControlType QLayoutPolicy::controlType() const
37{
38 return QLayoutPolicy::ControlType(1 << bits.ctype);
39}
40
41#ifndef QT_NO_DATASTREAM
42
43/*!
44 \relates QLayoutPolicy
45
46 Writes the size \a policy to the data stream \a stream.
47
48 \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
49*/
50QDataStream &operator<<(QDataStream &stream, const QLayoutPolicy &policy)
51{
52 // The order here is for historical reasons. (compatibility with Qt4)
53 quint32 data = (policy.bits.horPolicy | // [0, 3]
54 policy.bits.verPolicy << 4 | // [4, 7]
55 policy.bits.hfw << 8 | // [8]
56 policy.bits.ctype << 9 | // [9, 13]
57 policy.bits.wfh << 14 | // [14]
58 //policy.bits.padding << 15 | // [15]
59 policy.bits.verStretch << 16 | // [16, 23]
60 policy.bits.horStretch << 24); // [24, 31]
61 return stream << data;
62}
63
64#define VALUE_OF_BITS(data, bitstart, bitcount) ((data >> bitstart) & ((1 << bitcount) -1))
65
66/*!
67 \relates QLayoutPolicy
68
69 Reads the size \a policy from the data stream \a stream.
70
71 \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
72*/
73QDataStream &operator>>(QDataStream &stream, QLayoutPolicy &policy)
74{
75 quint32 data;
76 stream >> data;
77 policy.bits.horPolicy = VALUE_OF_BITS(data, 0, 4);
78 policy.bits.verPolicy = VALUE_OF_BITS(data, 4, 4);
79 policy.bits.hfw = VALUE_OF_BITS(data, 8, 1);
80 policy.bits.ctype = VALUE_OF_BITS(data, 9, 5);
81 policy.bits.wfh = VALUE_OF_BITS(data, 14, 1);
82 policy.bits.padding = 0;
83 policy.bits.verStretch = VALUE_OF_BITS(data, 16, 8);
84 policy.bits.horStretch = VALUE_OF_BITS(data, 24, 8);
85 return stream;
86}
87#endif // QT_NO_DATASTREAM
88
89#ifndef QT_NO_DEBUG_STREAM
90QDebug operator<<(QDebug dbg, const QLayoutPolicy &p)
91{
92 QDebugStateSaver saver(dbg);
93 dbg.nospace() << "QLayoutPolicy(horizontalPolicy = " << p.horizontalPolicy()
94 << ", verticalPolicy = " << p.verticalPolicy() << ')';
95 return dbg;
96}
97#endif
98
99QT_END_NAMESPACE
100
101#include "moc_qlayoutpolicy_p.cpp"
102

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