Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qdesigner_widgetitem.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
// Qt-Security score:significant reason:default
4
5
#
include
"qdesigner_widgetitem_p.h"
6
#
include
"qdesigner_widget_p.h"
7
#
include
"widgetfactory_p.h"
8
9
#
include
<
QtDesigner
/
abstractformwindow
.
h
>
10
#
include
<
QtDesigner
/
qextensionmanager
.
h
>
11
#
include
<
QtDesigner
/
abstractformeditor
.
h
>
12
#
include
<
QtDesigner
/
container
.
h
>
13
#
include
<
QtDesigner
/
abstractwidgetdatabase
.
h
>
14
15
#
include
<
QtWidgets
/
qboxlayout
.
h
>
16
#
include
<
QtWidgets
/
qgridlayout
.
h
>
17
#
include
<
QtWidgets
/
qformlayout
.
h
>
18
#
include
<
QtWidgets
/
qapplication
.
h
>
19
20
#
include
<
QtCore
/
qtextstream
.
h
>
21
#
include
<
QtCore
/
qdebug
.
h
>
22
#
include
<
private
/
qlayout_p
.
h
>
23
24
QT_BEGIN_NAMESPACE
25
26
enum
{
DebugWidgetItem
= 0 };
27
enum
{
MinimumLength
= 10 };
28
29
// Widget item creation function to be registered as factory method with
30
// QLayoutPrivate
31
static
QWidgetItem
*
createDesignerWidgetItem
(
const
QLayout *layout,
QWidget
*widget)
32
{
33
Qt::Orientations orientations;
34
if
(qdesigner_internal::QDesignerWidgetItem::check(layout, widget, &orientations)) {
35
if
(
DebugWidgetItem
)
36
qDebug() <<
"QDesignerWidgetItem: Creating on "
<< layout << widget << orientations;
37
return
new
qdesigner_internal::QDesignerWidgetItem(layout, widget, orientations);
38
}
39
if
(
DebugWidgetItem
)
40
qDebug() <<
"QDesignerWidgetItem: Noncontainer: "
<< layout << widget;
41
42
return
nullptr
;
43
}
44
45
static
QString
sizePolicyToString
(
const
QSizePolicy &p)
46
{
47
QString rc; {
48
QTextStream str(&rc);
49
str <<
"Control="
<< p.controlType() <<
" expdirs="
<< p.expandingDirections()
50
<<
" hasHeightForWidth="
<< p.hasHeightForWidth()
51
<<
" H: Policy="
<< p.horizontalPolicy()
52
<<
" stretch="
<< p.horizontalStretch()
53
<<
" V: Policy="
<< p.verticalPolicy()
54
<<
" stretch="
<< p.verticalStretch();
55
}
56
return
rc;
57
}
58
59
// Find the layout the item is contained in, recursing over
60
// child layouts
61
static
const
QLayout *
findLayoutOfItem
(
const
QLayout *haystack,
const
QLayoutItem *needle)
62
{
63
const
int
count = haystack->count();
64
for
(
int
i = 0; i < count; i++) {
65
QLayoutItem *item = haystack->itemAt(i);
66
if
(item == needle)
67
return
haystack;
68
if
(QLayout *childLayout = item->layout())
69
if
(
const
QLayout *containing = findLayoutOfItem(childLayout, needle))
70
return
containing;
71
}
72
return
nullptr
;
73
}
74
75
76
namespace
qdesigner_internal
{
77
78
// ------------------ QDesignerWidgetItem
79
QDesignerWidgetItem
::
QDesignerWidgetItem
(
const
QLayout
*
containingLayout
,
QWidget
*
w
,
Qt
::
Orientations
o
) :
80
QWidgetItemV2
(
w
),
81
m_orientations
(
o
),
82
m_nonLaidOutMinSize
(
w
->
minimumSizeHint
()),
83
m_nonLaidOutSizeHint
(
w
->
sizeHint
()),
84
m_cachedContainingLayout
(
containingLayout
)
85
{
86
// Initialize the minimum size to prevent nonlaid-out frames/widgets
87
// from being slammed to zero
88
const
QSize
minimumSize
=
w
->
minimumSize
();
89
if
(!
minimumSize
.
isEmpty
())
90
m_nonLaidOutMinSize
=
minimumSize
;
91
expand
(&
m_nonLaidOutMinSize
);
92
expand
(&
m_nonLaidOutSizeHint
);
93
w
->
installEventFilter
(
this
);
94
connect
(
containingLayout
, &
QObject
::
destroyed
,
this
, &
QDesignerWidgetItem
::
layoutChanged
);
95
if
(
DebugWidgetItem
)
96
qDebug
() <<
"QDesignerWidgetItem"
<<
w
<<
sizePolicyToString
(
w
->
sizePolicy
()) <<
m_nonLaidOutMinSize
<<
m_nonLaidOutSizeHint
;
97
}
98
99
void
QDesignerWidgetItem
::
expand
(
QSize
*
s
)
const
100
{
101
// Expand the size if its too small
102
if
(
m_orientations
&
Qt
::
Horizontal
&&
s
->
width
() <= 0)
103
s
->
setWidth
(
MinimumLength
);
104
if
(
m_orientations
&
Qt
::
Vertical
&&
s
->
height
() <= 0)
105
s
->
setHeight
(
MinimumLength
);
106
}
107
108
QSize
QDesignerWidgetItem
::
minimumSize
()
const
109
{
110
// Just track the size in case we are laid-out or stretched.
111
const
QSize
baseMinSize
=
QWidgetItemV2
::
minimumSize
();
112
QWidget
*
w
=
constWidget
();
113
if
(
w
->
layout
() ||
subjectToStretch
(
containingLayout
(),
w
)) {
114
m_nonLaidOutMinSize
=
baseMinSize
;
115
return
baseMinSize
;
116
}
117
// Nonlaid out: Maintain last laid-out size
118
const
QSize
rc
=
baseMinSize
.
expandedTo
(
m_nonLaidOutMinSize
);
119
if
(
DebugWidgetItem
> 1)
120
qDebug
() <<
"minimumSize"
<<
constWidget
() <<
baseMinSize
<<
rc
;
121
return
rc
;
122
}
123
124
QSize
QDesignerWidgetItem
::
sizeHint
()
const
125
{
126
// Just track the size in case we are laid-out or stretched.
127
const
QSize
baseSizeHint
=
QWidgetItemV2
::
sizeHint
();
128
QWidget
*
w
=
constWidget
();
129
if
(
w
->
layout
() ||
subjectToStretch
(
containingLayout
(),
w
)) {
130
m_nonLaidOutSizeHint
=
baseSizeHint
;
131
return
baseSizeHint
;
132
}
133
// Nonlaid out: Maintain last laid-out size
134
const
QSize
rc
=
baseSizeHint
.
expandedTo
(
m_nonLaidOutSizeHint
);
135
if
(
DebugWidgetItem
> 1)
136
qDebug
() <<
"sizeHint"
<<
constWidget
() <<
baseSizeHint
<<
rc
;
137
return
rc
;
138
}
139
140
bool
QDesignerWidgetItem
::
subjectToStretch
(
const
QLayout
*
layout
,
QWidget
*
w
)
141
{
142
if
(!
layout
)
143
return
false
;
144
// Are we under some stretch factor?
145
if
(
const
QBoxLayout
*
bl
=
qobject_cast
<
const
QBoxLayout
*>(
layout
)) {
146
const
int
index
=
bl
->
indexOf
(
w
);
147
Q_ASSERT
(
index
!= -1);
148
return
bl
->
stretch
(
index
) != 0;
149
}
150
if
(
const
QGridLayout
*
cgl
=
qobject_cast
<
const
QGridLayout
*>(
layout
)) {
151
QGridLayout
*
gl
=
const_cast
<
QGridLayout
*>(
cgl
);
152
const
int
index
=
cgl
->
indexOf
(
w
);
153
Q_ASSERT
(
index
!= -1);
154
int
row
,
column
,
rowSpan
,
columnSpan
;
155
gl
->
getItemPosition
(
index
, &
row
, &
column
, &
rowSpan
, &
columnSpan
);
156
const
int
rend
=
row
+
rowSpan
;
157
const
int
cend
=
column
+
columnSpan
;
158
for
(
int
r
=
row
;
r
<
rend
;
r
++)
159
if
(
cgl
->
rowStretch
(
r
) != 0)
160
return
true
;
161
for
(
int
c
=
column
;
c
<
cend
;
c
++)
162
if
(
cgl
->
columnStretch
(
c
) != 0)
163
return
true
;
164
}
165
return
false
;
166
}
167
168
/* Return the orientations mask for a layout, specifying
169
* in which directions squeezing should be prevented. */
170
static
Qt
::
Orientations
layoutOrientation
(
const
QLayout *layout)
171
{
172
if
(
const
QBoxLayout *bl = qobject_cast<
const
QBoxLayout *>(layout)) {
173
const
QBoxLayout::Direction direction = bl->direction();
174
return
direction == QBoxLayout::LeftToRight || direction == QBoxLayout::RightToLeft ? Qt::Horizontal : Qt::Vertical;
175
}
176
if
(qobject_cast<
const
QFormLayout*>(layout))
177
return
Qt::Vertical;
178
return
Qt::Horizontal|Qt::Vertical;
179
}
180
181
// Check for a non-container extension container
182
bool
QDesignerWidgetItem
::
isContainer
(
const
QDesignerFormEditorInterface
*
core
,
QWidget
*
w
)
183
{
184
if
(!
WidgetFactory
::
isFormEditorObject
(
w
))
185
return
false
;
186
const
QDesignerWidgetDataBaseInterface
*
wdb
=
core
->
widgetDataBase
();
187
const
int
widx
=
wdb
->
indexOfObject
(
w
);
188
if
(
widx
== -1 || !
wdb
->
item
(
widx
)->
isContainer
())
189
return
false
;
190
if
(
qt_extension
<
QDesignerContainerExtension
*>(
core
->
extensionManager
(),
w
))
191
return
false
;
192
return
true
;
193
}
194
195
bool
QDesignerWidgetItem
::
check
(
const
QLayout
*
layout
,
QWidget
*
w
,
Qt
::
Orientations
*
ptrToOrientations
)
196
{
197
// Check for form-editor non-containerextension-containers (QFrame, etc)
198
// within laid-out form editor widgets. No check for managed() here as we
199
// want container pages and widgets in the process of being morphed as
200
// well. Avoid nested layouts (as the effective stretch cannot be easily
201
// computed and may mess things up).
202
if
(
ptrToOrientations
)
203
*
ptrToOrientations
= {};
204
205
const
QObject
*
layoutParent
=
layout
->
parent
();
206
if
(!
layoutParent
|| !
layoutParent
->
isWidgetType
() || !
WidgetFactory
::
isFormEditorObject
(
layoutParent
))
207
return
false
;
208
209
QDesignerFormWindowInterface
*
fw
=
QDesignerFormWindowInterface
::
findFormWindow
(
w
);
210
if
(!
fw
|| !
isContainer
(
fw
->
core
(),
w
))
211
return
false
;
212
213
// If it is a box, restrict to its orientation
214
if
(
ptrToOrientations
)
215
*
ptrToOrientations
=
layoutOrientation
(
layout
);
216
217
return
true
;
218
}
219
220
QSize
QDesignerWidgetItem
::
nonLaidOutMinSize
()
const
221
{
222
return
m_nonLaidOutMinSize
;
223
}
224
225
void
QDesignerWidgetItem
::
setNonLaidOutMinSize
(
const
QSize
&
s
)
226
{
227
if
(
DebugWidgetItem
> 1)
228
qDebug
() <<
"setNonLaidOutMinSize"
<<
constWidget
() <<
s
;
229
m_nonLaidOutMinSize
=
s
;
230
}
231
232
QSize
QDesignerWidgetItem
::
nonLaidOutSizeHint
()
const
233
{
234
return
m_nonLaidOutSizeHint
;
235
}
236
237
void
QDesignerWidgetItem
::
setNonLaidOutSizeHint
(
const
QSize
&
s
)
238
{
239
if
(
DebugWidgetItem
> 1)
240
qDebug
() <<
"setNonLaidOutSizeHint"
<<
constWidget
() <<
s
;
241
m_nonLaidOutSizeHint
=
s
;
242
}
243
244
void
QDesignerWidgetItem
::
install
()
245
{
246
QLayoutPrivate
::
widgetItemFactoryMethod
=
createDesignerWidgetItem
;
247
}
248
249
void
QDesignerWidgetItem
::
deinstall
()
250
{
251
QLayoutPrivate
::
widgetItemFactoryMethod
=
nullptr
;
252
}
253
254
const
QLayout
*
QDesignerWidgetItem
::
containingLayout
()
const
255
{
256
if
(!
m_cachedContainingLayout
) {
257
if
(
QWidget
*
parentWidget
=
constWidget
()->
parentWidget
())
258
if
(
QLayout
*
parentLayout
=
parentWidget
->
layout
()) {
259
m_cachedContainingLayout
=
findLayoutOfItem
(
parentLayout
,
this
);
260
if
(
m_cachedContainingLayout
) {
261
connect
(
m_cachedContainingLayout
, &
QObject
::
destroyed
,
262
this
, &
QDesignerWidgetItem
::
layoutChanged
);
263
}
264
}
265
if
(
DebugWidgetItem
)
266
qDebug
() <<
Q_FUNC_INFO
<<
" found "
<<
m_cachedContainingLayout
<<
" after reparenting "
<<
constWidget
();
267
}
268
return
m_cachedContainingLayout
;
269
}
270
271
void
QDesignerWidgetItem
::
layoutChanged
()
272
{
273
if
(
DebugWidgetItem
)
274
qDebug
() <<
Q_FUNC_INFO
;
275
m_cachedContainingLayout
=
nullptr
;
276
}
277
278
bool
QDesignerWidgetItem
::
eventFilter
(
QObject
*
/* watched */
,
QEvent
*
event
)
279
{
280
if
(
event
->
type
() ==
QEvent
::
ParentChange
)
281
layoutChanged
();
282
return
false
;
283
}
284
285
// ------------------ QDesignerWidgetItemInstaller
286
287
int
QDesignerWidgetItemInstaller
::
m_instanceCount
= 0;
288
289
QDesignerWidgetItemInstaller
::
QDesignerWidgetItemInstaller
()
290
{
291
if
(
m_instanceCount
++ == 0) {
292
if
(
DebugWidgetItem
)
293
qDebug
() <<
"QDesignerWidgetItemInstaller: installing"
;
294
QDesignerWidgetItem
::
install
();
295
}
296
}
297
298
QDesignerWidgetItemInstaller
::~
QDesignerWidgetItemInstaller
()
299
{
300
if
(--
m_instanceCount
== 0) {
301
if
(
DebugWidgetItem
)
302
qDebug
() <<
"QDesignerWidgetItemInstaller: deinstalling"
;
303
QDesignerWidgetItem
::
deinstall
();
304
}
305
}
306
307
}
308
309
QT_END_NAMESPACE
QPainter::QWidget
friend class QWidget
Definition
qpainter.h:432
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qdesigner_internal
Auxiliary methods to store/retrieve settings.
Definition
buddyeditor.cpp:67
qdesigner_internal::layoutOrientation
static Qt::Orientations layoutOrientation(const QLayout *layout)
Definition
qdesigner_widgetitem.cpp:170
createDesignerWidgetItem
static QWidgetItem * createDesignerWidgetItem(const QLayout *layout, QWidget *widget)
Definition
qdesigner_widgetitem.cpp:31
findLayoutOfItem
static const QLayout * findLayoutOfItem(const QLayout *haystack, const QLayoutItem *needle)
Definition
qdesigner_widgetitem.cpp:61
MinimumLength
@ MinimumLength
Definition
qdesigner_widgetitem.cpp:27
DebugWidgetItem
@ DebugWidgetItem
Definition
qdesigner_widgetitem.cpp:26
sizePolicyToString
static QString sizePolicyToString(const QSizePolicy &p)
Definition
qdesigner_widgetitem.cpp:45
qttools
src
designer
src
lib
shared
qdesigner_widgetitem.cpp
Generated on
for Qt by
1.18.0