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
4#include "layoutinfo_p.h"
5
6#include <QtDesigner/abstractformeditor.h>
7#include <QtDesigner/container.h>
8#include <QtDesigner/abstractmetadatabase.h>
9#include <QtDesigner/qextensionmanager.h>
10
11#include <QtWidgets/qboxlayout.h>
12#include <QtWidgets/qformlayout.h>
13#include <QtWidgets/qsplitter.h>
14#include <QtCore/qdebug.h>
15#include <QtCore/qhash.h>
16#include <QtCore/qrect.h>
17
19
20using namespace Qt::StringLiterals;
21
22namespace qdesigner_internal {
23/*!
24 \internal
25 \overload
26*/
28{
30 if (!layout)
31 return NoLayout;
32 if (qobject_cast<const QHBoxLayout*>(layout))
33 return HBox;
34 if (qobject_cast<const QVBoxLayout*>(layout))
35 return VBox;
36 if (qobject_cast<const QGridLayout*>(layout))
37 return Grid;
38 if (qobject_cast<const QFormLayout*>(layout))
39 return Form;
40 return UnknownLayout;
41}
42
44{
45 static const QHash<QString, LayoutInfo::Type> nameTypeMap = {
46 {u"QVBoxLayout"_s, LayoutInfo::VBox},
47 {u"QHBoxLayout"_s, LayoutInfo::HBox},
48 {u"QGridLayout"_s, LayoutInfo::Grid},
49 {u"QFormLayout"_s, LayoutInfo::Form}
50 };
51 return nameTypeMap;
52}
53
58
63
64/*!
65 \internal
66 \overload
67*/
74
90
92{
94
95 QObject *o = layout;
96 while (o) {
98 return widget;
99
100 o = o->parent();
101 }
102 return nullptr;
103}
104
106{
109
110 Q_ASSERT(widget != nullptr);
111
113
114 if (layout == nullptr || core->metaDataBase()->item(layout) != nullptr) {
115 delete layout;
117 return;
118 }
119
120 qDebug() << "trying to delete an unmanaged layout:" << "widget:" << widget << "layout:" << layout;
121}
122
125 bool *isManaged,
127{
128 if (isManaged)
129 *isManaged = false;
130 if (ptrToLayout)
131 *ptrToLayout = nullptr;
132
134 if (!parent)
135 return NoLayout;
136
137 // 1) Splitter
139 if (isManaged)
142 }
143
144 // 2) Layout of parent
146 if (!parentLayout)
147 return NoLayout;
148
149 if (parentLayout->indexOf(widget) != -1) {
150 if (isManaged)
152 if (ptrToLayout)
155 }
156
157 // 3) Some child layout (see below comment about Q3GroupBox)
159 if (childLayouts.isEmpty())
160 return NoLayout;
161 for (QLayout *layout : childLayouts) {
162 if (layout->indexOf(widget) != -1) {
163 if (isManaged)
165 if (ptrToLayout)
167 return layoutType(core, layout);
168 }
169 }
170
171 return NoLayout;
172}
173
175{
176 return widget->layout();
177}
178
179
181{
182 if (widget == nullptr)
183 return nullptr;
184
186 if (!layout)
187 return nullptr;
188
189 return managedLayout(core, layout);
190}
191
193{
194 if (!layout)
195 return nullptr;
196
198
199 if (!metaDataBase)
200 return layout;
201 /* This code exists mainly for the Q3GroupBox class, for which
202 * widget->layout() returns an internal VBoxLayout. */
204 if (item == nullptr) {
207 }
208 if (!item)
209 return nullptr;
210 return layout;
211}
212
213// Is it a a dummy grid placeholder created by Designer?
215{
216 if (item == nullptr) {
217 qDebug() << "** WARNING Zero-item passed on to isEmptyItem(). This indicates a layout inconsistency.";
218 return true;
219 }
220 return item->spacerItem() != nullptr;
221}
222
223QDESIGNER_SHARED_EXPORT void getFormLayoutItemPosition(const QFormLayout *formLayout, int index, int *rowPtr, int *columnPtr, int *rowspanPtr, int *colspanPtr)
224{
225 int row;
226 QFormLayout::ItemRole role;
227 formLayout->getItemPosition(index, &row, &role);
228 const int columnspan = role == QFormLayout::SpanningRole ? 2 : 1;
229 const int column = (columnspan > 1 || role == QFormLayout::LabelRole) ? 0 : 1;
230 if (rowPtr)
231 *rowPtr = row;
232 if (columnPtr)
233 *columnPtr = column;
234 if (rowspanPtr)
235 *rowspanPtr = 1;
236 if (colspanPtr)
237 *colspanPtr = columnspan;
238}
239
240static inline QFormLayout::ItemRole formLayoutRole(int column, int colspan)
241{
242 if (colspan > 1)
243 return QFormLayout::SpanningRole;
244 return column == 0 ? QFormLayout::LabelRole : QFormLayout::FieldRole;
245}
246
247QDESIGNER_SHARED_EXPORT void formLayoutAddWidget(QFormLayout *formLayout, QWidget *w, const QRect &r, bool insert)
248{
249 // Consistent API galore...
250 if (insert) {
251 const bool spanning = r.width() > 1;
252 if (spanning) {
253 formLayout->insertRow(r.y(), w);
254 } else {
255 QWidget *label = nullptr;
256 QWidget *field = nullptr;
257 if (r.x() == 0) {
258 label = w;
259 } else {
260 field = w;
261 }
262 formLayout->insertRow(r.y(), label, field);
263 }
264 } else {
265 formLayout->setWidget(r.y(), formLayoutRole(r.x(), r.width()), w);
266 }
267}
268
269} // namespace qdesigner_internal
270
271QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:431
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