Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
24
25using namespace Qt::StringLiterals;
26
27namespace qdesigner_internal {
28/*!
29 \internal
30 \overload
31*/
33{
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
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
63
68
69/*!
70 \internal
71 \overload
72*/
79
95
97{
99
100 QObject *o = layout;
101 while (o) {
103 return widget;
104
105 o = o->parent();
106 }
107 return nullptr;
108}
109
111{
114
115 Q_ASSERT(widget != nullptr);
116
118
119 if (layout == nullptr || core->metaDataBase()->item(layout) != nullptr) {
120 delete layout;
122 return;
123 }
124
125 qDebug() << "trying to delete an unmanaged layout:" << "widget:" << widget << "layout:" << layout;
126}
127
130 bool *isManaged,
132{
133 if (isManaged)
134 *isManaged = false;
135 if (ptrToLayout)
136 *ptrToLayout = nullptr;
137
139 if (!parent)
140 return NoLayout;
141
142 // 1) Splitter
144 if (isManaged)
147 }
148
149 // 2) Layout of parent
151 if (!parentLayout)
152 return NoLayout;
153
154 if (parentLayout->indexOf(widget) != -1) {
155 if (isManaged)
157 if (ptrToLayout)
160 }
161
162 // 3) Some child layout (see below comment about Q3GroupBox)
164 if (childLayouts.isEmpty())
165 return NoLayout;
166 for (QLayout *layout : childLayouts) {
167 if (layout->indexOf(widget) != -1) {
168 if (isManaged)
170 if (ptrToLayout)
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.
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
211
213{
214 return widget->layout();
215}
216
217
219{
220 if (widget == nullptr)
221 return nullptr;
222
224 if (!layout)
225 return nullptr;
226
227 return managedLayout(core, layout);
228}
229
231{
232 if (!layout)
233 return nullptr;
234
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. */
242 if (item == nullptr) {
245 }
246 if (!item)
247 return nullptr;
248 return layout;
249}
250
251// Is it a a dummy grid placeholder created by Designer?
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
261QDESIGNER_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
278static 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
285QDESIGNER_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
309QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:432
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.
static QFormLayout::ItemRole formLayoutRole(int column, int colspan)
QDESIGNER_SHARED_EXPORT void getFormLayoutItemPosition(const QFormLayout *formLayout, int index, int *rowPtr, int *columnPtr=nullptr, int *rowspanPtr=nullptr, int *colspanPtr=nullptr)
static bool isContainerPositionedChild(QWidget *widget)
QDESIGNER_SHARED_EXPORT void formLayoutAddWidget(QFormLayout *formLayout, QWidget *w, const QRect &r, bool insert)
static const QHash< QString, LayoutInfo::Type > & layoutNameTypeMap()
#define QDESIGNER_SHARED_EXPORT