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
qdesigner_widgetitem.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
8
9#include <QtDesigner/abstractformwindow.h>
10#include <QtDesigner/qextensionmanager.h>
11#include <QtDesigner/abstractformeditor.h>
12#include <QtDesigner/container.h>
13#include <QtDesigner/abstractwidgetdatabase.h>
14
15#include <QtWidgets/qboxlayout.h>
16#include <QtWidgets/qgridlayout.h>
17#include <QtWidgets/qformlayout.h>
18#include <QtWidgets/qapplication.h>
19
20#include <QtCore/qtextstream.h>
21#include <QtCore/qdebug.h>
22#include <private/qlayout_p.h>
23
25
26enum { DebugWidgetItem = 0 };
27enum { MinimumLength = 10 };
28
29// Widget item creation function to be registered as factory method with
30// QLayoutPrivate
31static QWidgetItem *createDesignerWidgetItem(const QLayout *layout, QWidget *widget)
32{
33 Qt::Orientations orientations;
34 if (qdesigner_internal::QDesignerWidgetItem::check(layout, widget, &orientations)) {
36 qDebug() << "QDesignerWidgetItem: Creating on " << layout << widget << orientations;
37 return new qdesigner_internal::QDesignerWidgetItem(layout, widget, orientations);
38 }
40 qDebug() << "QDesignerWidgetItem: Noncontainer: " << layout << widget;
41
42 return nullptr;
43}
44
45static QString sizePolicyToString(const QSizePolicy &p)
46{
47 QString rc; {
48 QTextStream str(&rc);
49 str << "Control=" << p.controlType() << " expdirs=" << p.expandingDirections()
50 << " hasHeightForWidth=" << p.hasHeightForWidth()
51 << " H: Policy=" << p.horizontalPolicy()
52 << " stretch=" << p.horizontalStretch()
53 << " V: Policy=" << p.verticalPolicy()
54 << " stretch=" << p.verticalStretch();
55 }
56 return rc;
57}
58
59// Find the layout the item is contained in, recursing over
60// child layouts
61static const QLayout *findLayoutOfItem(const QLayout *haystack, const QLayoutItem *needle)
62{
63 const int count = haystack->count();
64 for (int i = 0; i < count; i++) {
65 QLayoutItem *item = haystack->itemAt(i);
66 if (item == needle)
67 return haystack;
68 if (QLayout *childLayout = item->layout())
69 if (const QLayout *containing = findLayoutOfItem(childLayout, needle))
70 return containing;
71 }
72 return nullptr;
73}
74
75
76namespace qdesigner_internal {
77
78// ------------------ QDesignerWidgetItem
98
100{
101 // Expand the size if its too small
102 if (m_orientations & Qt::Horizontal && s->width() <= 0)
104 if (m_orientations & Qt::Vertical && s->height() <= 0)
106}
107
109{
110 // Just track the size in case we are laid-out or stretched.
112 QWidget * w = constWidget();
115 return baseMinSize;
116 }
117 // Nonlaid out: Maintain last laid-out size
119 if (DebugWidgetItem > 1)
120 qDebug() << "minimumSize" << constWidget() << baseMinSize << rc;
121 return rc;
122}
123
125{
126 // Just track the size in case we are laid-out or stretched.
128 QWidget * w = constWidget();
131 return baseSizeHint;
132 }
133 // Nonlaid out: Maintain last laid-out size
135 if (DebugWidgetItem > 1)
136 qDebug() << "sizeHint" << constWidget() << baseSizeHint << rc;
137 return rc;
138}
139
141{
142 if (!layout)
143 return false;
144 // Are we under some stretch factor?
145 if (const QBoxLayout *bl = qobject_cast<const QBoxLayout *>(layout)) {
146 const int index = bl->indexOf(w);
147 Q_ASSERT(index != -1);
148 return bl->stretch(index) != 0;
149 }
150 if (const QGridLayout *cgl = qobject_cast<const QGridLayout *>(layout)) {
151 QGridLayout *gl = const_cast<QGridLayout *>(cgl);
152 const int index = cgl->indexOf(w);
153 Q_ASSERT(index != -1);
156 const int rend = row + rowSpan;
157 const int cend = column + columnSpan;
158 for (int r = row; r < rend; r++)
159 if (cgl->rowStretch(r) != 0)
160 return true;
161 for (int c = column; c < cend; c++)
162 if (cgl->columnStretch(c) != 0)
163 return true;
164 }
165 return false;
166}
167
168/* Return the orientations mask for a layout, specifying
169 * in which directions squeezing should be prevented. */
170static Qt::Orientations layoutOrientation(const QLayout *layout)
171{
172 if (const QBoxLayout *bl = qobject_cast<const QBoxLayout *>(layout)) {
173 const QBoxLayout::Direction direction = bl->direction();
174 return direction == QBoxLayout::LeftToRight || direction == QBoxLayout::RightToLeft ? Qt::Horizontal : Qt::Vertical;
175 }
176 if (qobject_cast<const QFormLayout*>(layout))
177 return Qt::Vertical;
178 return Qt::Horizontal|Qt::Vertical;
179}
180
181// Check for a non-container extension container
183{
185 return false;
187 const int widx = wdb->indexOfObject(w);
188 if (widx == -1 || !wdb->item(widx)->isContainer())
189 return false;
191 return false;
192 return true;
193}
194
196{
197 // Check for form-editor non-containerextension-containers (QFrame, etc)
198 // within laid-out form editor widgets. No check for managed() here as we
199 // want container pages and widgets in the process of being morphed as
200 // well. Avoid nested layouts (as the effective stretch cannot be easily
201 // computed and may mess things up).
203 *ptrToOrientations = {};
204
205 const QObject *layoutParent = layout->parent();
207 return false;
208
210 if (!fw || !isContainer(fw->core(), w))
211 return false;
212
213 // If it is a box, restrict to its orientation
216
217 return true;
218}
219
224
226{
227 if (DebugWidgetItem > 1)
228 qDebug() << "setNonLaidOutMinSize" << constWidget() << s;
230}
231
236
238{
239 if (DebugWidgetItem > 1)
240 qDebug() << "setNonLaidOutSizeHint" << constWidget() << s;
242}
243
248
253
270
272{
273 if (DebugWidgetItem)
274 qDebug() << Q_FUNC_INFO;
275 m_cachedContainingLayout = nullptr;
276}
277
279{
280 if (event->type() == QEvent::ParentChange)
282 return false;
283}
284
285// ------------------ QDesignerWidgetItemInstaller
286
288
290{
291 if (m_instanceCount++ == 0) {
292 if (DebugWidgetItem)
293 qDebug() << "QDesignerWidgetItemInstaller: installing";
295 }
296}
297
299{
300 if (--m_instanceCount == 0) {
301 if (DebugWidgetItem)
302 qDebug() << "QDesignerWidgetItemInstaller: deinstalling";
304 }
305}
306
307}
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 Qt::Orientations layoutOrientation(const QLayout *layout)
static QWidgetItem * createDesignerWidgetItem(const QLayout *layout, QWidget *widget)
static const QLayout * findLayoutOfItem(const QLayout *haystack, const QLayoutItem *needle)
@ DebugWidgetItem
static QString sizePolicyToString(const QSizePolicy &p)