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/qsplitter.h>
15#include <QtCore/qdebug.h>
16#include <QtCore/qhash.h>
17#include <QtCore/qrect.h>
18
20
21using namespace Qt::StringLiterals;
22
23namespace qdesigner_internal {
24/*!
25 \internal
26 \overload
27*/
29{
31 if (!layout)
32 return NoLayout;
33 if (qobject_cast<const QHBoxLayout*>(layout))
34 return HBox;
35 if (qobject_cast<const QVBoxLayout*>(layout))
36 return VBox;
37 if (qobject_cast<const QGridLayout*>(layout))
38 return Grid;
39 if (qobject_cast<const QFormLayout*>(layout))
40 return Form;
41 return UnknownLayout;
42}
43
45{
46 static const QHash<QString, LayoutInfo::Type> nameTypeMap = {
47 {u"QVBoxLayout"_s, LayoutInfo::VBox},
48 {u"QHBoxLayout"_s, LayoutInfo::HBox},
49 {u"QGridLayout"_s, LayoutInfo::Grid},
50 {u"QFormLayout"_s, LayoutInfo::Form}
51 };
52 return nameTypeMap;
53}
54
59
64
65/*!
66 \internal
67 \overload
68*/
75
91
93{
95
96 QObject *o = layout;
97 while (o) {
99 return widget;
100
101 o = o->parent();
102 }
103 return nullptr;
104}
105
107{
110
111 Q_ASSERT(widget != nullptr);
112
114
115 if (layout == nullptr || core->metaDataBase()->item(layout) != nullptr) {
116 delete layout;
118 return;
119 }
120
121 qDebug() << "trying to delete an unmanaged layout:" << "widget:" << widget << "layout:" << layout;
122}
123
126 bool *isManaged,
128{
129 if (isManaged)
130 *isManaged = false;
131 if (ptrToLayout)
132 *ptrToLayout = nullptr;
133
135 if (!parent)
136 return NoLayout;
137
138 // 1) Splitter
140 if (isManaged)
143 }
144
145 // 2) Layout of parent
147 if (!parentLayout)
148 return NoLayout;
149
150 if (parentLayout->indexOf(widget) != -1) {
151 if (isManaged)
153 if (ptrToLayout)
156 }
157
158 // 3) Some child layout (see below comment about Q3GroupBox)
160 if (childLayouts.isEmpty())
161 return NoLayout;
162 for (QLayout *layout : childLayouts) {
163 if (layout->indexOf(widget) != -1) {
164 if (isManaged)
166 if (ptrToLayout)
168 return layoutType(core, layout);
169 }
170 }
171
172 return NoLayout;
173}
174
176{
177 return widget->layout();
178}
179
180
182{
183 if (widget == nullptr)
184 return nullptr;
185
187 if (!layout)
188 return nullptr;
189
190 return managedLayout(core, layout);
191}
192
194{
195 if (!layout)
196 return nullptr;
197
199
200 if (!metaDataBase)
201 return layout;
202 /* This code exists mainly for the Q3GroupBox class, for which
203 * widget->layout() returns an internal VBoxLayout. */
205 if (item == nullptr) {
208 }
209 if (!item)
210 return nullptr;
211 return layout;
212}
213
214// Is it a a dummy grid placeholder created by Designer?
216{
217 if (item == nullptr) {
218 qDebug() << "** WARNING Zero-item passed on to isEmptyItem(). This indicates a layout inconsistency.";
219 return true;
220 }
221 return item->spacerItem() != nullptr;
222}
223
224QDESIGNER_SHARED_EXPORT void getFormLayoutItemPosition(const QFormLayout *formLayout, int index, int *rowPtr, int *columnPtr, int *rowspanPtr, int *colspanPtr)
225{
226 int row;
227 QFormLayout::ItemRole role;
228 formLayout->getItemPosition(index, &row, &role);
229 const int columnspan = role == QFormLayout::SpanningRole ? 2 : 1;
230 const int column = (columnspan > 1 || role == QFormLayout::LabelRole) ? 0 : 1;
231 if (rowPtr)
232 *rowPtr = row;
233 if (columnPtr)
234 *columnPtr = column;
235 if (rowspanPtr)
236 *rowspanPtr = 1;
237 if (colspanPtr)
238 *colspanPtr = columnspan;
239}
240
241static inline QFormLayout::ItemRole formLayoutRole(int column, int colspan)
242{
243 if (colspan > 1)
244 return QFormLayout::SpanningRole;
245 return column == 0 ? QFormLayout::LabelRole : QFormLayout::FieldRole;
246}
247
248QDESIGNER_SHARED_EXPORT void formLayoutAddWidget(QFormLayout *formLayout, QWidget *w, const QRect &r, bool insert)
249{
250 // Consistent API galore...
251 if (insert) {
252 const bool spanning = r.width() > 1;
253 if (spanning) {
254 formLayout->insertRow(r.y(), w);
255 } else {
256 QWidget *label = nullptr;
257 QWidget *field = nullptr;
258 if (r.x() == 0) {
259 label = w;
260 } else {
261 field = w;
262 }
263 formLayout->insertRow(r.y(), label, field);
264 }
265 } else {
266 formLayout->setWidget(r.y(), formLayoutRole(r.x(), r.width()), w);
267 }
268}
269
270} // namespace qdesigner_internal
271
272QT_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)
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