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
layoutinfo.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
"layoutinfo_p.h"
6
7
#
include
<
QtDesigner
/
abstractformeditor
.
h
>
8
#
include
<
QtDesigner
/
container
.
h
>
9
#
include
<
QtDesigner
/
abstractmetadatabase
.
h
>
10
#
include
<
QtDesigner
/
qextensionmanager
.
h
>
11
12
#
include
<
QtWidgets
/
qboxlayout
.
h
>
13
#
include
<
QtWidgets
/
qformlayout
.
h
>
14
#
include
<
QtWidgets
/
qmainwindow
.
h
>
15
#
include
<
QtWidgets
/
qmenubar
.
h
>
16
#
include
<
QtWidgets
/
qscrollarea
.
h
>
17
#
include
<
QtWidgets
/
qsplitter
.
h
>
18
#
include
<
QtWidgets
/
qstatusbar
.
h
>
19
#
include
<
QtCore
/
qdebug
.
h
>
20
#
include
<
QtCore
/
qhash
.
h
>
21
#
include
<
QtCore
/
qrect
.
h
>
22
23
QT_BEGIN_NAMESPACE
24
25
using
namespace
Qt::StringLiterals;
26
27
namespace
qdesigner_internal
{
28
/*!
29
\internal
30
\overload
31
*/
32
LayoutInfo
::
Type
LayoutInfo
::
layoutType
(
const
QDesignerFormEditorInterface
*
core
,
const
QLayout
*
layout
)
33
{
34
Q_UNUSED
(
core
);
35
if
(!
layout
)
36
return
NoLayout
;
37
if
(
qobject_cast
<
const
QHBoxLayout
*>(
layout
))
38
return
HBox
;
39
if
(
qobject_cast
<
const
QVBoxLayout
*>(
layout
))
40
return
VBox
;
41
if
(
qobject_cast
<
const
QGridLayout
*>(
layout
))
42
return
Grid
;
43
if
(
qobject_cast
<
const
QFormLayout
*>(
layout
))
44
return
Form
;
45
return
UnknownLayout
;
46
}
47
48
static
const
QHash
<
QString
,
LayoutInfo
::
Type
> &
layoutNameTypeMap
()
49
{
50
static
const
QHash<QString, LayoutInfo::Type> nameTypeMap = {
51
{u"QVBoxLayout"_s, LayoutInfo::VBox},
52
{u"QHBoxLayout"_s, LayoutInfo::HBox},
53
{u"QGridLayout"_s, LayoutInfo::Grid},
54
{u"QFormLayout"_s, LayoutInfo::Form}
55
};
56
return
nameTypeMap;
57
}
58
59
LayoutInfo
::
Type
LayoutInfo
::
layoutType
(
const
QString
&
typeName
)
60
{
61
return
layoutNameTypeMap
().
value
(
typeName
,
NoLayout
);
62
}
63
64
QString
LayoutInfo
::
layoutName
(
Type
t
)
65
{
66
return
layoutNameTypeMap
().
key
(
t
);
67
}
68
69
/*!
70
\internal
71
\overload
72
*/
73
LayoutInfo
::
Type
LayoutInfo
::
layoutType
(
const
QDesignerFormEditorInterface
*
core
,
const
QWidget
*
w
)
74
{
75
if
(
const
QSplitter
*
splitter
=
qobject_cast
<
const
QSplitter
*>(
w
))
76
return
splitter
->
orientation
() ==
Qt
::
Horizontal
?
HSplitter
:
VSplitter
;
77
return
layoutType
(
core
,
w
->
layout
());
78
}
79
80
LayoutInfo
::
Type
LayoutInfo
::
managedLayoutType
(
const
QDesignerFormEditorInterface
*
core
,
81
const
QWidget
*
w
,
82
QLayout
**
ptrToLayout
)
83
{
84
if
(
ptrToLayout
)
85
*
ptrToLayout
=
nullptr
;
86
if
(
const
QSplitter
*
splitter
=
qobject_cast
<
const
QSplitter
*>(
w
))
87
return
splitter
->
orientation
() ==
Qt
::
Horizontal
?
HSplitter
:
VSplitter
;
88
QLayout
*
layout
=
managedLayout
(
core
,
w
);
89
if
(!
layout
)
90
return
NoLayout
;
91
if
(
ptrToLayout
)
92
*
ptrToLayout
=
layout
;
93
return
layoutType
(
core
,
layout
);
94
}
95
96
QWidget
*
LayoutInfo
::
layoutParent
(
const
QDesignerFormEditorInterface
*
core
,
QLayout
*
layout
)
97
{
98
Q_UNUSED
(
core
);
99
100
QObject
*
o
=
layout
;
101
while
(
o
) {
102
if
(
QWidget
*
widget
=
qobject_cast
<
QWidget
*>(
o
))
103
return
widget
;
104
105
o
=
o
->
parent
();
106
}
107
return
nullptr
;
108
}
109
110
void
LayoutInfo
::
deleteLayout
(
const
QDesignerFormEditorInterface
*
core
,
QWidget
*
widget
)
111
{
112
if
(
QDesignerContainerExtension
*
container
=
qt_extension
<
QDesignerContainerExtension
*>(
core
->
extensionManager
(),
widget
))
113
widget
=
container
->
widget
(
container
->
currentIndex
());
114
115
Q_ASSERT
(
widget
!=
nullptr
);
116
117
QLayout
*
layout
=
managedLayout
(
core
,
widget
);
118
119
if
(
layout
==
nullptr
||
core
->
metaDataBase
()->
item
(
layout
) !=
nullptr
) {
120
delete
layout
;
121
widget
->
updateGeometry
();
122
return
;
123
}
124
125
qDebug
() <<
"trying to delete an unmanaged layout:"
<<
"widget:"
<<
widget
<<
"layout:"
<<
layout
;
126
}
127
128
LayoutInfo
::
Type
LayoutInfo
::
laidoutWidgetType
(
const
QDesignerFormEditorInterface
*
core
,
129
QWidget
*
widget
,
130
bool
*
isManaged
,
131
QLayout
**
ptrToLayout
)
132
{
133
if
(
isManaged
)
134
*
isManaged
=
false
;
135
if
(
ptrToLayout
)
136
*
ptrToLayout
=
nullptr
;
137
138
QWidget
*
parent
=
widget
->
parentWidget
();
139
if
(!
parent
)
140
return
NoLayout
;
141
142
// 1) Splitter
143
if
(
QSplitter
*
splitter
=
qobject_cast
<
QSplitter
*>(
parent
)) {
144
if
(
isManaged
)
145
*
isManaged
=
core
->
metaDataBase
()->
item
(
splitter
);
146
return
splitter
->
orientation
() ==
Qt
::
Horizontal
?
HSplitter
:
VSplitter
;
147
}
148
149
// 2) Layout of parent
150
QLayout
*
parentLayout
=
parent
->
layout
();
151
if
(!
parentLayout
)
152
return
NoLayout
;
153
154
if
(
parentLayout
->
indexOf
(
widget
) != -1) {
155
if
(
isManaged
)
156
*
isManaged
=
core
->
metaDataBase
()->
item
(
parentLayout
);
157
if
(
ptrToLayout
)
158
*
ptrToLayout
=
parentLayout
;
159
return
layoutType
(
core
,
parentLayout
);
160
}
161
162
// 3) Some child layout (see below comment about Q3GroupBox)
163
const
auto
childLayouts
=
parentLayout
->
findChildren
<
QLayout
*>();
164
if
(
childLayouts
.
isEmpty
())
165
return
NoLayout
;
166
for
(
QLayout
*
layout
:
childLayouts
) {
167
if
(
layout
->
indexOf
(
widget
) != -1) {
168
if
(
isManaged
)
169
*
isManaged
=
core
->
metaDataBase
()->
item
(
layout
);
170
if
(
ptrToLayout
)
171
*
ptrToLayout
=
layout
;
172
return
layoutType
(
core
,
layout
);
173
}
174
}
175
176
return
NoLayout
;
177
}
178
179
// Containers position their internal children ("pages") themselves, so that
180
// setting their geometry has no effect (QTBUG-2801). Most of them use an
181
// internal layout, which is covered by laidoutWidgetType(); the remaining
182
// cases are handled here.
183
static
bool
isContainerPositionedChild
(
QWidget
*widget)
184
{
185
QWidget
*parent = widget->parentWidget();
186
if
(parent ==
nullptr
)
187
return
false
;
188
189
// QMainWindow lays out the menu and the status bar outside of
190
// QMainWindowLayout's items.
191
if
(qobject_cast<
const
QMainWindow *>(parent) !=
nullptr
) {
192
return
qobject_cast<
const
QMenuBar *>(widget) !=
nullptr
193
|| qobject_cast<
const
QStatusBar *>(widget) !=
nullptr
;
194
}
195
196
// Scroll areas (QScrollArea itself, but also the ones QToolBox uses for
197
// its pages) resize their widget unless told otherwise.
198
if
(
const
auto
*scrollArea = qobject_cast<
const
QScrollArea *>(parent->parentWidget())) {
199
return
scrollArea->viewport() == parent && scrollArea->widget() == widget
200
&& scrollArea->widgetResizable();
201
}
202
203
return
false
;
204
}
205
206
bool
LayoutInfo
::
isGeometryControlledByParent
(
const
QDesignerFormEditorInterface
*
core
,
207
QWidget
*
widget
)
208
{
209
return
isWidgetLaidout
(
core
,
widget
) ||
isContainerPositionedChild
(
widget
);
210
}
211
212
QLayout
*
LayoutInfo
::
internalLayout
(
const
QWidget
*
widget
)
213
{
214
return
widget
->
layout
();
215
}
216
217
218
QLayout
*
LayoutInfo
::
managedLayout
(
const
QDesignerFormEditorInterface
*
core
,
const
QWidget
*
widget
)
219
{
220
if
(
widget
==
nullptr
)
221
return
nullptr
;
222
223
QLayout
*
layout
=
widget
->
layout
();
224
if
(!
layout
)
225
return
nullptr
;
226
227
return
managedLayout
(
core
,
layout
);
228
}
229
230
QLayout
*
LayoutInfo
::
managedLayout
(
const
QDesignerFormEditorInterface
*
core
,
QLayout
*
layout
)
231
{
232
if
(!
layout
)
233
return
nullptr
;
234
235
QDesignerMetaDataBaseInterface
*
metaDataBase
=
core
->
metaDataBase
();
236
237
if
(!
metaDataBase
)
238
return
layout
;
239
/* This code exists mainly for the Q3GroupBox class, for which
240
* widget->layout() returns an internal VBoxLayout. */
241
const
QDesignerMetaDataBaseItemInterface
*
item
=
metaDataBase
->
item
(
layout
);
242
if
(
item
==
nullptr
) {
243
layout
=
layout
->
findChild
<
QLayout
*>();
244
item
=
metaDataBase
->
item
(
layout
);
245
}
246
if
(!
item
)
247
return
nullptr
;
248
return
layout
;
249
}
250
251
// Is it a a dummy grid placeholder created by Designer?
252
bool
LayoutInfo
::
isEmptyItem
(
QLayoutItem
*
item
)
253
{
254
if
(
item
==
nullptr
) {
255
qDebug
() <<
"** WARNING Zero-item passed on to isEmptyItem(). This indicates a layout inconsistency."
;
256
return
true
;
257
}
258
return
item
->
spacerItem
() !=
nullptr
;
259
}
260
261
QDESIGNER_SHARED_EXPORT
void
getFormLayoutItemPosition
(
const
QFormLayout *formLayout,
int
index,
int
*rowPtr,
int
*columnPtr,
int
*rowspanPtr,
int
*colspanPtr)
262
{
263
int
row;
264
QFormLayout::ItemRole role;
265
formLayout->getItemPosition(index, &row, &role);
266
const
int
columnspan = role == QFormLayout::SpanningRole ? 2 : 1;
267
const
int
column = (columnspan > 1 || role == QFormLayout::LabelRole) ? 0 : 1;
268
if
(rowPtr)
269
*rowPtr = row;
270
if
(columnPtr)
271
*columnPtr = column;
272
if
(rowspanPtr)
273
*rowspanPtr = 1;
274
if
(colspanPtr)
275
*colspanPtr = columnspan;
276
}
277
278
static
inline
QFormLayout
::
ItemRole
formLayoutRole
(
int
column,
int
colspan)
279
{
280
if
(colspan > 1)
281
return
QFormLayout::SpanningRole;
282
return
column == 0 ? QFormLayout::LabelRole : QFormLayout::FieldRole;
283
}
284
285
QDESIGNER_SHARED_EXPORT
void
formLayoutAddWidget
(QFormLayout *formLayout,
QWidget
*w,
const
QRect &r,
bool
insert)
286
{
287
// Consistent API galore...
288
if
(insert) {
289
const
bool
spanning = r.width() > 1;
290
if
(spanning) {
291
formLayout->insertRow(r.y(), w);
292
}
else
{
293
QWidget
*label =
nullptr
;
294
QWidget
*field =
nullptr
;
295
if
(r.x() == 0) {
296
label = w;
297
}
else
{
298
field = w;
299
}
300
formLayout->insertRow(r.y(), label, field);
301
}
302
}
else
{
303
formLayout->setWidget(r.y(), formLayoutRole(r.x(), r.width()), w);
304
}
305
}
306
307
}
// namespace qdesigner_internal
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::formLayoutRole
static QFormLayout::ItemRole formLayoutRole(int column, int colspan)
Definition
layoutinfo.cpp:278
qdesigner_internal::getFormLayoutItemPosition
QDESIGNER_SHARED_EXPORT void getFormLayoutItemPosition(const QFormLayout *formLayout, int index, int *rowPtr, int *columnPtr=nullptr, int *rowspanPtr=nullptr, int *colspanPtr=nullptr)
Definition
layoutinfo.cpp:261
qdesigner_internal::isContainerPositionedChild
static bool isContainerPositionedChild(QWidget *widget)
Definition
layoutinfo.cpp:183
qdesigner_internal::formLayoutAddWidget
QDESIGNER_SHARED_EXPORT void formLayoutAddWidget(QFormLayout *formLayout, QWidget *w, const QRect &r, bool insert)
Definition
layoutinfo.cpp:285
qdesigner_internal::layoutNameTypeMap
static const QHash< QString, LayoutInfo::Type > & layoutNameTypeMap()
Definition
layoutinfo.cpp:48
QDESIGNER_SHARED_EXPORT
#define QDESIGNER_SHARED_EXPORT
Definition
shared_global_p.h:33
qttools
src
designer
src
lib
shared
layoutinfo.cpp
Generated on
for Qt by
1.18.0