1// Copyright (C) 2017 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 "qquickpaddedrectangle_p.h"
5
6#include <QtQuick/private/qsgadaptationlayer_p.h>
7
8QT_BEGIN_NAMESPACE
9
10QQuickPaddedRectangle::QQuickPaddedRectangle(QQuickItem *parent) :
11 QQuickRectangle(parent)
12{
13}
14
15qreal QQuickPaddedRectangle::padding() const
16{
17 return m_padding;
18}
19
20void QQuickPaddedRectangle::setPadding(qreal padding)
21{
22 if (!qFuzzyCompare(p1: m_padding, p2: padding)) {
23 m_padding = padding;
24 update();
25 emit paddingChanged();
26 if (!m_hasTopPadding)
27 emit topPaddingChanged();
28 if (!m_hasLeftPadding)
29 emit leftPaddingChanged();
30 if (!m_hasRightPadding)
31 emit rightPaddingChanged();
32 if (!m_hasBottomPadding)
33 emit bottomPaddingChanged();
34 }
35}
36
37void QQuickPaddedRectangle::resetPadding()
38{
39 setPadding(0);
40}
41
42qreal QQuickPaddedRectangle::topPadding() const
43{
44 return m_hasTopPadding ? m_topPadding : m_padding;
45}
46
47void QQuickPaddedRectangle::setTopPadding(qreal padding)
48{
49 setTopPadding(padding, has: true);
50}
51
52void QQuickPaddedRectangle::resetTopPadding()
53{
54 setTopPadding(padding: 0, has: false);
55}
56
57qreal QQuickPaddedRectangle::leftPadding() const
58{
59 return m_hasLeftPadding ? m_leftPadding : m_padding;
60}
61
62void QQuickPaddedRectangle::setLeftPadding(qreal padding)
63{
64 setLeftPadding(padding, has: true);
65}
66
67void QQuickPaddedRectangle::resetLeftPadding()
68{
69 setLeftPadding(padding: 0, has: false);
70}
71
72qreal QQuickPaddedRectangle::rightPadding() const
73{
74 return m_hasRightPadding ? m_rightPadding : m_padding;
75}
76
77void QQuickPaddedRectangle::setRightPadding(qreal padding)
78{
79 setRightPadding(padding, has: true);
80}
81
82void QQuickPaddedRectangle::resetRightPadding()
83{
84 setRightPadding(padding: 0, has: false);
85}
86
87qreal QQuickPaddedRectangle::bottomPadding() const
88{
89 return m_hasBottomPadding ? m_bottomPadding : m_padding;
90}
91
92void QQuickPaddedRectangle::setBottomPadding(qreal padding)
93{
94 setBottomPadding(padding, has: true);
95}
96
97void QQuickPaddedRectangle::resetBottomPadding()
98{
99 setBottomPadding(padding: 0, has: false);
100}
101
102void QQuickPaddedRectangle::setTopPadding(qreal padding, bool has)
103{
104 qreal oldPadding = topPadding();
105 m_hasTopPadding = has;
106 m_topPadding = padding;
107 if (!qFuzzyCompare(p1: oldPadding, p2: padding)) {
108 update();
109 emit topPaddingChanged();
110 }
111}
112
113void QQuickPaddedRectangle::setLeftPadding(qreal padding, bool has)
114{
115 qreal oldPadding = leftPadding();
116 m_hasLeftPadding = has;
117 m_leftPadding = padding;
118 if (!qFuzzyCompare(p1: oldPadding, p2: padding)) {
119 update();
120 emit leftPaddingChanged();
121 }
122}
123
124void QQuickPaddedRectangle::setRightPadding(qreal padding, bool has)
125{
126 qreal oldPadding = rightPadding();
127 m_hasRightPadding = has;
128 m_rightPadding = padding;
129 if (!qFuzzyCompare(p1: oldPadding, p2: padding)) {
130 update();
131 emit rightPaddingChanged();
132 }
133}
134
135void QQuickPaddedRectangle::setBottomPadding(qreal padding, bool has)
136{
137 qreal oldPadding = bottomPadding();
138 m_hasBottomPadding = has;
139 m_bottomPadding = padding;
140 if (!qFuzzyCompare(p1: oldPadding, p2: padding)) {
141 update();
142 emit bottomPaddingChanged();
143 }
144}
145
146QSGNode *QQuickPaddedRectangle::updatePaintNode(QSGNode *node, UpdatePaintNodeData *data)
147{
148 QSGTransformNode *transformNode = static_cast<QSGTransformNode *>(node);
149 if (!transformNode)
150 transformNode = new QSGTransformNode;
151
152 QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode *>(QQuickRectangle::updatePaintNode(transformNode->firstChild(), data));
153
154 if (rectNode) {
155 if (!transformNode->firstChild())
156 transformNode->appendChildNode(node: rectNode);
157
158 qreal top = topPadding();
159 qreal left = leftPadding();
160 qreal right = rightPadding();
161 qreal bottom = bottomPadding();
162
163 if (!qFuzzyIsNull(d: top) || !qFuzzyIsNull(d: left) || !qFuzzyIsNull(d: right) || !qFuzzyIsNull(d: bottom)) {
164 QMatrix4x4 m;
165 m.translate(x: left, y: top);
166 transformNode->setMatrix(m);
167
168 qreal w = qMax<qreal>(a: 0.0, b: width() -left-right);
169 qreal h = qMax<qreal>(a: 0.0, b: height() -top-bottom);
170
171 rectNode->setRect(QRectF(0, 0, w, h));
172 rectNode->update();
173 }
174 }
175 return transformNode;
176}
177
178QT_END_NAMESPACE
179
180#include "moc_qquickpaddedrectangle_p.cpp"
181

source code of qtdeclarative/src/quickcontrolsimpl/qquickpaddedrectangle.cpp