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
qgraphicslayout_p.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// Qt-Security score:significant reason:default
4
5#include "qglobal.h"
6
10#include "qapplication.h"
11
13
14/*!
15 \internal
16
17 \a mw is the new parent. all items in the layout will be a child of \a mw.
18 */
19void QGraphicsLayoutPrivate::reparentChildItems(QGraphicsItem *newParent)
20{
21 Q_Q(QGraphicsLayout);
22 int n = q->count();
23 //bool mwVisible = mw && mw->isVisible();
24 for (int i = 0; i < n; ++i) {
25 QGraphicsLayoutItem *layoutChild = q->itemAt(i);
26 if (!layoutChild) {
27 // Skip stretch items
28 continue;
29 }
30 if (layoutChild->isLayout()) {
31 QGraphicsLayout *l = static_cast<QGraphicsLayout*>(layoutChild);
32 l->d_func()->reparentChildItems(newParent);
33 } else if (QGraphicsItem *itemChild = layoutChild->graphicsItem()){
34 QGraphicsItem *childParent = itemChild->parentItem();
35#ifdef QT_DEBUG
36 if (childParent && childParent != newParent && itemChild->isWidget() && qt_graphicsLayoutDebug()) {
37 QGraphicsWidget *w = static_cast<QGraphicsWidget*>(layoutChild);
38 qWarning("QGraphicsLayout::addChildLayout: widget %s \"%s\" in wrong parent; moved to correct parent",
39 w->metaObject()->className(), w->objectName().toLocal8Bit().constData());
40 }
41#endif
42 if (childParent != newParent)
43 itemChild->setParentItem(newParent);
44 }
45 }
46}
47
48void QGraphicsLayoutPrivate::getMargin(qreal *result, qreal userMargin, QStyle::PixelMetric pm) const
49{
50 if (!result)
51 return;
52 Q_Q(const QGraphicsLayout);
53
54 QGraphicsLayoutItem *parent = q->parentLayoutItem();
55 if (userMargin >= 0.0) {
56 *result = userMargin;
57 } else if (!parent) {
58 *result = 0.0;
59 } else if (parent->isLayout()) { // sublayouts have 0 margin by default
60 *result = 0.0;
61 } else {
62 *result = 0.0;
63 if (QGraphicsItem *layoutParentItem = parentItem()) {
64 if (layoutParentItem->isWidget())
65 *result = (qreal)static_cast<QGraphicsWidget*>(layoutParentItem)->style()->pixelMetric(pm, nullptr);
66 }
67 }
68}
69
70Qt::LayoutDirection QGraphicsLayoutPrivate::visualDirection() const
71{
72 if (QGraphicsItem *maybeWidget = parentItem()) {
73 if (maybeWidget->isWidget())
74 return static_cast<QGraphicsWidget*>(maybeWidget)->layoutDirection();
75 }
76 return QGuiApplication::layoutDirection();
77}
78
79static bool removeLayoutItemFromLayout(QGraphicsLayout *lay, QGraphicsLayoutItem *layoutItem)
80{
81 if (!lay)
82 return false;
83
84 for (int i = lay->count() - 1; i >= 0; --i) {
85 QGraphicsLayoutItem *child = lay->itemAt(i);
86 if (child && child->isLayout()) {
87 if (removeLayoutItemFromLayout(static_cast<QGraphicsLayout*>(child), layoutItem))
88 return true;
89 } else if (child == layoutItem) {
90 lay->removeAt(i);
91 return true;
92 }
93 }
94 return false;
95}
96
97/*!
98 \internal
99
100 This function is called from subclasses to add a layout item \a layoutItem
101 to a layout.
102
103 It takes care of automatically reparenting graphics items, if needed.
104
105 If \a layoutItem is a is already in a layout, it will remove it from that layout.
106
107*/
108void QGraphicsLayoutPrivate::addChildLayoutItem(QGraphicsLayoutItem *layoutItem)
109{
110 Q_Q(QGraphicsLayout);
111 if (QGraphicsLayoutItem *maybeLayout = layoutItem->parentLayoutItem()) {
112 if (maybeLayout->isLayout())
113 removeLayoutItemFromLayout(static_cast<QGraphicsLayout*>(maybeLayout), layoutItem);
114 }
115 layoutItem->setParentLayoutItem(q);
116 if (layoutItem->isLayout()) {
117 if (QGraphicsItem *parItem = parentItem()) {
118 static_cast<QGraphicsLayout*>(layoutItem)->d_func()->reparentChildItems(parItem);
119 }
120 } else {
121 if (QGraphicsItem *item = layoutItem->graphicsItem()) {
122 QGraphicsItem *newParent = parentItem();
123 QGraphicsItem *oldParent = item->parentItem();
124 if (oldParent == newParent || !newParent)
125 return;
126
127#ifdef QT_DEBUG
128 if (oldParent && item->isWidget()) {
129 QGraphicsWidget *w = static_cast<QGraphicsWidget*>(item);
130 qWarning("QGraphicsLayout::addChildLayoutItem: %s \"%s\" in wrong parent; moved to correct parent",
131 w->metaObject()->className(), w->objectName().toLocal8Bit().constData());
132 }
133#endif
134
135 item->setParentItem(newParent);
136 }
137 }
138}
139
140void QGraphicsLayoutPrivate::activateRecursive(QGraphicsLayoutItem *item)
141{
142 if (item->isLayout()) {
143 QGraphicsLayout *layout = static_cast<QGraphicsLayout *>(item);
144 if (layout->d_func()->activated) {
145 if (QGraphicsLayout::instantInvalidatePropagation()) {
146 return;
147 } else {
148 layout->invalidate(); // ### LOOKS SUSPICIOUSLY WRONG!!???
149 }
150 }
151
152 for (int i = layout->count() - 1; i >= 0; --i) {
153 QGraphicsLayoutItem *childItem = layout->itemAt(i);
154 if (childItem)
155 activateRecursive(childItem);
156 }
157 layout->d_func()->activated = true;
158 }
159}
160
161QT_END_NAMESPACE
static bool removeLayoutItemFromLayout(QGraphicsLayout *lay, QGraphicsLayoutItem *layoutItem)