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
7
8#include <QtDesigner/abstractformwindow.h>
9#include <QtDesigner/qextensionmanager.h>
10#include <QtDesigner/abstractformeditor.h>
11#include <QtDesigner/container.h>
12#include <QtDesigner/abstractwidgetdatabase.h>
13
14#include <QtWidgets/qboxlayout.h>
15#include <QtWidgets/qgridlayout.h>
16#include <QtWidgets/qformlayout.h>
17#include <QtWidgets/qapplication.h>
18
19#include <QtCore/qtextstream.h>
20#include <QtCore/qdebug.h>
21#include <private/qlayout_p.h>
22
24
25enum { DebugWidgetItem = 0 };
26enum { MinimumLength = 10 };
27
28// Widget item creation function to be registered as factory method with
29// QLayoutPrivate
30static QWidgetItem *createDesignerWidgetItem(const QLayout *layout, QWidget *widget)
31{
32 Qt::Orientations orientations;
33 if (qdesigner_internal::QDesignerWidgetItem::check(layout, widget, &orientations)) {
35 qDebug() << "QDesignerWidgetItem: Creating on " << layout << widget << orientations;
36 return new qdesigner_internal::QDesignerWidgetItem(layout, widget, orientations);
37 }
39 qDebug() << "QDesignerWidgetItem: Noncontainer: " << layout << widget;
40
41 return nullptr;
42}
43
44static QString sizePolicyToString(const QSizePolicy &p)
45{
46 QString rc; {
47 QTextStream str(&rc);
48 str << "Control=" << p.controlType() << " expdirs=" << p.expandingDirections()
49 << " hasHeightForWidth=" << p.hasHeightForWidth()
50 << " H: Policy=" << p.horizontalPolicy()
51 << " stretch=" << p.horizontalStretch()
52 << " V: Policy=" << p.verticalPolicy()
53 << " stretch=" << p.verticalStretch();
54 }
55 return rc;
56}
57
58// Find the layout the item is contained in, recursing over
59// child layouts
60static const QLayout *findLayoutOfItem(const QLayout *haystack, const QLayoutItem *needle)
61{
62 const int count = haystack->count();
63 for (int i = 0; i < count; i++) {
64 QLayoutItem *item = haystack->itemAt(i);
65 if (item == needle)
66 return haystack;
67 if (QLayout *childLayout = item->layout())
68 if (const QLayout *containing = findLayoutOfItem(childLayout, needle))
69 return containing;
70 }
71 return nullptr;
72}
73
74
75namespace qdesigner_internal {
76
77// ------------------ QDesignerWidgetItem
97
99{
100 // Expand the size if its too small
101 if (m_orientations & Qt::Horizontal && s->width() <= 0)
103 if (m_orientations & Qt::Vertical && s->height() <= 0)
105}
106
108{
109 // Just track the size in case we are laid-out or stretched.
111 QWidget * w = constWidget();
114 return baseMinSize;
115 }
116 // Nonlaid out: Maintain last laid-out size
118 if (DebugWidgetItem > 1)
119 qDebug() << "minimumSize" << constWidget() << baseMinSize << rc;
120 return rc;
121}
122
124{
125 // Just track the size in case we are laid-out or stretched.
127 QWidget * w = constWidget();
130 return baseSizeHint;
131 }
132 // Nonlaid out: Maintain last laid-out size
134 if (DebugWidgetItem > 1)
135 qDebug() << "sizeHint" << constWidget() << baseSizeHint << rc;
136 return rc;
137}
138
140{
141 if (!layout)
142 return false;
143 // Are we under some stretch factor?
144 if (const QBoxLayout *bl = qobject_cast<const QBoxLayout *>(layout)) {
145 const int index = bl->indexOf(w);
146 Q_ASSERT(index != -1);
147 return bl->stretch(index) != 0;
148 }
149 if (const QGridLayout *cgl = qobject_cast<const QGridLayout *>(layout)) {
150 QGridLayout *gl = const_cast<QGridLayout *>(cgl);
151 const int index = cgl->indexOf(w);
152 Q_ASSERT(index != -1);
155 const int rend = row + rowSpan;
156 const int cend = column + columnSpan;
157 for (int r = row; r < rend; r++)
158 if (cgl->rowStretch(r) != 0)
159 return true;
160 for (int c = column; c < cend; c++)
161 if (cgl->columnStretch(c) != 0)
162 return true;
163 }
164 return false;
165}
166
167/* Return the orientations mask for a layout, specifying
168 * in which directions squeezing should be prevented. */
169static Qt::Orientations layoutOrientation(const QLayout *layout)
170{
171 if (const QBoxLayout *bl = qobject_cast<const QBoxLayout *>(layout)) {
172 const QBoxLayout::Direction direction = bl->direction();
173 return direction == QBoxLayout::LeftToRight || direction == QBoxLayout::RightToLeft ? Qt::Horizontal : Qt::Vertical;
174 }
175 if (qobject_cast<const QFormLayout*>(layout))
176 return Qt::Vertical;
177 return Qt::Horizontal|Qt::Vertical;
178}
179
180// Check for a non-container extension container
182{
184 return false;
186 const int widx = wdb->indexOfObject(w);
187 if (widx == -1 || !wdb->item(widx)->isContainer())
188 return false;
190 return false;
191 return true;
192}
193
195{
196 // Check for form-editor non-containerextension-containers (QFrame, etc)
197 // within laid-out form editor widgets. No check for managed() here as we
198 // want container pages and widgets in the process of being morphed as
199 // well. Avoid nested layouts (as the effective stretch cannot be easily
200 // computed and may mess things up).
202 *ptrToOrientations = {};
203
204 const QObject *layoutParent = layout->parent();
206 return false;
207
209 if (!fw || !isContainer(fw->core(), w))
210 return false;
211
212 // If it is a box, restrict to its orientation
215
216 return true;
217}
218
223
225{
226 if (DebugWidgetItem > 1)
227 qDebug() << "setNonLaidOutMinSize" << constWidget() << s;
229}
230
235
237{
238 if (DebugWidgetItem > 1)
239 qDebug() << "setNonLaidOutSizeHint" << constWidget() << s;
241}
242
247
252
269
271{
272 if (DebugWidgetItem)
273 qDebug() << Q_FUNC_INFO;
274 m_cachedContainingLayout = nullptr;
275}
276
278{
279 if (event->type() == QEvent::ParentChange)
281 return false;
282}
283
284// ------------------ QDesignerWidgetItemInstaller
285
287
289{
290 if (m_instanceCount++ == 0) {
291 if (DebugWidgetItem)
292 qDebug() << "QDesignerWidgetItemInstaller: installing";
294 }
295}
296
298{
299 if (--m_instanceCount == 0) {
300 if (DebugWidgetItem)
301 qDebug() << "QDesignerWidgetItemInstaller: deinstalling";
303 }
304}
305
306}
307
308QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:421
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.
static Qt::Orientations layoutOrientation(const QLayout *layout)
@ DebugWidgetItem
static QWidgetItem * createDesignerWidgetItem(const QLayout *layout, QWidget *widget)
static const QLayout * findLayoutOfItem(const QLayout *haystack, const QLayoutItem *needle)
static QString sizePolicyToString(const QSizePolicy &p)