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 \overload
25*/
27{
29 if (!layout)
30 return NoLayout;
31 if (qobject_cast<const QHBoxLayout*>(layout))
32 return HBox;
33 if (qobject_cast<const QVBoxLayout*>(layout))
34 return VBox;
35 if (qobject_cast<const QGridLayout*>(layout))
36 return Grid;
37 if (qobject_cast<const QFormLayout*>(layout))
38 return Form;
39 return UnknownLayout;
40}
41
43{
44 static const QHash<QString, LayoutInfo::Type> nameTypeMap = {
45 {u"QVBoxLayout"_s, LayoutInfo::VBox},
46 {u"QHBoxLayout"_s, LayoutInfo::HBox},
47 {u"QGridLayout"_s, LayoutInfo::Grid},
48 {u"QFormLayout"_s, LayoutInfo::Form}
49 };
50 return nameTypeMap;
51}
52
57
62
63/*!
64 \overload
65*/
72
88
90{
92
93 QObject *o = layout;
94 while (o) {
96 return widget;
97
98 o = o->parent();
99 }
100 return nullptr;
101}
102
104{
107
108 Q_ASSERT(widget != nullptr);
109
111
112 if (layout == nullptr || core->metaDataBase()->item(layout) != nullptr) {
113 delete layout;
115 return;
116 }
117
118 qDebug() << "trying to delete an unmanaged layout:" << "widget:" << widget << "layout:" << layout;
119}
120
123 bool *isManaged,
125{
126 if (isManaged)
127 *isManaged = false;
128 if (ptrToLayout)
129 *ptrToLayout = nullptr;
130
132 if (!parent)
133 return NoLayout;
134
135 // 1) Splitter
137 if (isManaged)
140 }
141
142 // 2) Layout of parent
144 if (!parentLayout)
145 return NoLayout;
146
147 if (parentLayout->indexOf(widget) != -1) {
148 if (isManaged)
150 if (ptrToLayout)
153 }
154
155 // 3) Some child layout (see below comment about Q3GroupBox)
157 if (childLayouts.isEmpty())
158 return NoLayout;
159 for (QLayout *layout : childLayouts) {
160 if (layout->indexOf(widget) != -1) {
161 if (isManaged)
163 if (ptrToLayout)
165 return layoutType(core, layout);
166 }
167 }
168
169 return NoLayout;
170}
171
173{
174 return widget->layout();
175}
176
177
179{
180 if (widget == nullptr)
181 return nullptr;
182
184 if (!layout)
185 return nullptr;
186
187 return managedLayout(core, layout);
188}
189
191{
192 if (!layout)
193 return nullptr;
194
196
197 if (!metaDataBase)
198 return layout;
199 /* This code exists mainly for the Q3GroupBox class, for which
200 * widget->layout() returns an internal VBoxLayout. */
202 if (item == nullptr) {
205 }
206 if (!item)
207 return nullptr;
208 return layout;
209}
210
211// Is it a a dummy grid placeholder created by Designer?
213{
214 if (item == nullptr) {
215 qDebug() << "** WARNING Zero-item passed on to isEmptyItem(). This indicates a layout inconsistency.";
216 return true;
217 }
218 return item->spacerItem() != nullptr;
219}
220
221QDESIGNER_SHARED_EXPORT void getFormLayoutItemPosition(const QFormLayout *formLayout, int index, int *rowPtr, int *columnPtr, int *rowspanPtr, int *colspanPtr)
222{
223 int row;
224 QFormLayout::ItemRole role;
225 formLayout->getItemPosition(index, &row, &role);
226 const int columnspan = role == QFormLayout::SpanningRole ? 2 : 1;
227 const int column = (columnspan > 1 || role == QFormLayout::LabelRole) ? 0 : 1;
228 if (rowPtr)
229 *rowPtr = row;
230 if (columnPtr)
231 *columnPtr = column;
232 if (rowspanPtr)
233 *rowspanPtr = 1;
234 if (colspanPtr)
235 *colspanPtr = columnspan;
236}
237
238static inline QFormLayout::ItemRole formLayoutRole(int column, int colspan)
239{
240 if (colspan > 1)
241 return QFormLayout::SpanningRole;
242 return column == 0 ? QFormLayout::LabelRole : QFormLayout::FieldRole;
243}
244
245QDESIGNER_SHARED_EXPORT void formLayoutAddWidget(QFormLayout *formLayout, QWidget *w, const QRect &r, bool insert)
246{
247 // Consistent API galore...
248 if (insert) {
249 const bool spanning = r.width() > 1;
250 if (spanning) {
251 formLayout->insertRow(r.y(), w);
252 } else {
253 QWidget *label = nullptr;
254 QWidget *field = nullptr;
255 if (r.x() == 0) {
256 label = w;
257 } else {
258 field = w;
259 }
260 formLayout->insertRow(r.y(), label, field);
261 }
262 } else {
263 formLayout->setWidget(r.y(), formLayoutRole(r.x(), r.width()), w);
264 }
265}
266
267} // namespace qdesigner_internal
268
269QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:421
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