Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgraphicslayout.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qapplication.h"
5
6#include "qgraphicslayout.h"
7#include "qgraphicslayout_p.h"
10#include "qgraphicswidget.h"
11#include "qgraphicswidget_p.h"
12#include "qgraphicsscene.h"
13
15
124{
125 setParentLayoutItem(parent);
126 if (parent && !parent->isLayout()) {
127 // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
128 QGraphicsItem *itemParent = parent->graphicsItem();
129 if (itemParent && itemParent->isWidget()) {
130 static_cast<QGraphicsWidget *>(itemParent)->d_func()->setLayout_helper(this);
131 } else {
132 qWarning("QGraphicsLayout::QGraphicsLayout: Attempt to create a layout with a parent that is"
133 " neither a QGraphicsWidget nor QGraphicsLayout");
134 }
135 }
137 setOwnedByLayout(true);
138}
139
145{
146 setParentLayoutItem(parent);
147 if (parent && !parent->isLayout()) {
148 // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
149 QGraphicsItem *itemParent = parent->graphicsItem();
150 if (itemParent && itemParent->isWidget()) {
151 static_cast<QGraphicsWidget *>(itemParent)->d_func()->setLayout_helper(this);
152 } else {
153 qWarning("QGraphicsLayout::QGraphicsLayout: Attempt to create a layout with a parent that is"
154 " neither a QGraphicsWidget nor QGraphicsLayout");
155 }
156 }
158 setOwnedByLayout(true);
159}
160
167
182{
183 Q_D(QGraphicsLayout);
184 if (d->left == left && d->top == top && d->right == right && d->bottom == bottom)
185 return;
186 d->left = left;
187 d->right = right;
188 d->top = top;
189 d->bottom = bottom;
190 invalidate();
191}
192
197{
198 Q_D(const QGraphicsLayout);
199 d->getMargin(left, d->left, QStyle::PM_LayoutLeftMargin);
200 d->getMargin(top, d->top, QStyle::PM_LayoutTopMargin);
201 d->getMargin(right, d->right, QStyle::PM_LayoutRightMargin);
202 d->getMargin(bottom, d->bottom, QStyle::PM_LayoutBottomMargin);
203}
204
222{
223 Q_D(QGraphicsLayout);
224 if (d->activated)
225 return;
226
227 d->activateRecursive(this);
228
229 // we don't call activate on a sublayout, but somebody might.
230 // Therefore, we walk to the parentitem of the toplevel layout.
231 QGraphicsLayoutItem *parentItem = this;
232 while (parentItem && parentItem->isLayout())
233 parentItem = parentItem->parentLayoutItem();
234 if (!parentItem)
235 return;
236 Q_ASSERT(!parentItem->isLayout());
237
239 QGraphicsWidget *parentWidget = static_cast<QGraphicsWidget*>(parentItem);
240 if (!parentWidget->parentLayoutItem()) {
241 // we've reached the topmost widget, resize it
242 bool wasResized = parentWidget->testAttribute(Qt::WA_Resized);
243 parentWidget->resize(parentWidget->size());
244 parentWidget->setAttribute(Qt::WA_Resized, wasResized);
245 }
246
247 setGeometry(parentItem->contentsRect()); // relayout children
248 } else {
249 setGeometry(parentItem->contentsRect()); // relayout children
251 }
252}
253
263{
264 Q_D(const QGraphicsLayout);
265 return d->activated;
266}
267
276{
279 } else {
280 // only mark layouts as invalid (activated = false) if we can post a LayoutRequest event.
282 while (layoutItem && layoutItem->isLayout()) {
283 // we could call updateGeometry(), but what if that method
284 // does not call the base implementation? In addition, updateGeometry()
285 // does more than we need.
286 layoutItem->d_func()->sizeHintCacheDirty = true;
287 layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true;
288 layoutItem = layoutItem->parentLayoutItem();
289 }
290 if (layoutItem) {
291 layoutItem->d_func()->sizeHintCacheDirty = true;
292 layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true;
293 }
294
295 bool postIt = layoutItem ? !layoutItem->isLayout() : false;
296 if (postIt) {
297 layoutItem = this;
298 while (layoutItem && layoutItem->isLayout()
299 && static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated) {
300 static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated = false;
301 layoutItem = layoutItem->parentLayoutItem();
302 }
303 if (layoutItem && !layoutItem->isLayout()) {
304 // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
306 }
307 }
308 }
309}
310
315{
316 Q_D(QGraphicsLayout);
318 d->activated = false;
320
322 if (!parentItem)
323 return;
324
325 if (parentItem->isLayout())
326 static_cast<QGraphicsLayout *>(parentItem)->invalidate();
327 else
328 parentItem->updateGeometry();
329 } else {
331 if (QGraphicsLayoutItem *parentItem = parentLayoutItem()) {
332 if (parentItem->isLayout()) {
333 parentItem->updateGeometry();
334 } else {
335 invalidate();
336 }
337 }
338 }
339}
340
355{
356 switch (e->type()) {
358 if (isActivated()) {
360 } else {
361 activate(); // relies on that activate() will call updateGeometry()
362 }
363 break;
365 activate();
366 break;
368 invalidate();
369 break;
370 default:
371 break;
372 }
373}
374
441
443
473
485
static void postEvent(QObject *receiver, QEvent *event, int priority=Qt::NormalEventPriority)
\inmodule QtCore
Definition qcoreevent.h:45
@ LayoutDirectionChange
Definition qcoreevent.h:124
@ LayoutRequest
Definition qcoreevent.h:112
@ GraphicsSceneResize
Definition qcoreevent.h:225
Type type() const
Returns the event type.
Definition qcoreevent.h:304
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts.
QGraphicsItem * graphicsItem() const
Returns the QGraphicsItem that this layout item represents.
void setOwnedByLayout(bool ownedByLayout)
virtual void updateGeometry()
This virtual function discards any cached size hint information.
bool isLayout() const
Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges o...
QGraphicsLayoutItem * parentLayoutItem() const
Returns the parent of this QGraphicsLayoutItem, or \nullptr if there is no parent,...
void setParentLayoutItem(QGraphicsLayoutItem *parent)
Sets the parent of this QGraphicsLayoutItem to parent.
QRectF contentsRect() const
Returns the contents rect in local coordinates.
virtual void setGeometry(const QRectF &rect)
This virtual function sets the geometry of the QGraphicsLayoutItem to rect, which is in parent coordi...
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
virtual void updateGeometry() override
\reimp
static void setInstantInvalidatePropagation(bool enable)
bool isActivated() const
Returns true if the layout is currently being activated; otherwise, returns false.
virtual void invalidate()
Clears any cached geometry and size hint information in the layout, and posts a \l{QEvent::LayoutRequ...
virtual void widgetEvent(QEvent *e)
This virtual event handler receives all events for the managed widget.
~QGraphicsLayout()
Destroys the QGraphicsLayout object.
void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const override
\reimp
void activate()
Activates the layout, causing all items in the layout to be immediately rearranged.
void setContentsMargins(qreal left, qreal top, qreal right, qreal bottom)
Sets the contents margins to left, top, right and bottom.
void addChildLayoutItem(QGraphicsLayoutItem *layoutItem)
static bool instantInvalidatePropagation()
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.
void resize(const QSizeF &size)
void setAttribute(Qt::WidgetAttribute attribute, bool on=true)
If on is true, this function enables attribute; otherwise attribute is disabled.
bool testAttribute(Qt::WidgetAttribute attribute) const
Returns true if attribute is enabled for this widget; otherwise, returns false.
QSizeF size
the size of the widget
The QSizePolicy class is a layout attribute describing horizontal and vertical resizing policy.
Definition qsizepolicy.h:18
@ PM_LayoutBottomMargin
Definition qstyle.h:515
@ PM_LayoutLeftMargin
Definition qstyle.h:512
@ PM_LayoutTopMargin
Definition qstyle.h:513
@ PM_LayoutRightMargin
Definition qstyle.h:514
Combined button and popup list for selecting options.
@ WA_Resized
Definition qnamespace.h:308
static bool g_instantInvalidatePropagation
#define qWarning
Definition qlogging.h:166
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLint left
GLint GLint bottom
GLboolean enable
static void layoutItem(QQuickItem *item, qreal y, qreal width)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
double qreal
Definition qtypes.h:187