1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qglobal.h"
41
42 #include "qgraphicslayout_p.h"
43 #include "qgraphicslayout.h"
44 #include "qgraphicswidget.h"
45 #include "qapplication.h"
46
47 QT_BEGIN_NAMESPACE
48
49 /*!
50 \internal
51
52 \a mw is the new parent. all items in the layout will be a child of \a mw.
53 */
reparentChildItems(QGraphicsItem * newParent)54 void QGraphicsLayoutPrivate::reparentChildItems(QGraphicsItem *newParent)
55 {
56 Q_Q(QGraphicsLayout);
57 int n = q->count();
58 //bool mwVisible = mw && mw->isVisible();
59 for (int i = 0; i < n; ++i) {
60 QGraphicsLayoutItem *layoutChild = q->itemAt(i);
61 if (!layoutChild) {
62 // Skip stretch items
63 continue;
64 }
65 if (layoutChild->isLayout()) {
66 QGraphicsLayout *l = static_cast<QGraphicsLayout*>(layoutChild);
67 l->d_func()->reparentChildItems(newParent);
68 } else if (QGraphicsItem *itemChild = layoutChild->graphicsItem()){
69 QGraphicsItem *childParent = itemChild->parentItem();
70 #ifdef QT_DEBUG
71 if (childParent && childParent != newParent && itemChild->isWidget() && qt_graphicsLayoutDebug()) {
72 QGraphicsWidget *w = static_cast<QGraphicsWidget*>(layoutChild);
73 qWarning("QGraphicsLayout::addChildLayout: widget %s \"%s\" in wrong parent; moved to correct parent",
74 w->metaObject()->className(), w->objectName().toLocal8Bit().constData());
75 }
76 #endif
77 if (childParent != newParent)
78 itemChild->setParentItem(newParent);
79 }
80 }
81 }
82
getMargin(qreal * result,qreal userMargin,QStyle::PixelMetric pm) const83 void QGraphicsLayoutPrivate::getMargin(qreal *result, qreal userMargin, QStyle::PixelMetric pm) const
84 {
85 if (!result)
86 return;
87 Q_Q(const QGraphicsLayout);
88
89 QGraphicsLayoutItem *parent = q->parentLayoutItem();
90 if (userMargin >= 0.0) {
91 *result = userMargin;
92 } else if (!parent) {
93 *result = 0.0;
94 } else if (parent->isLayout()) { // sublayouts have 0 margin by default
95 *result = 0.0;
96 } else {
97 *result = 0.0;
98 if (QGraphicsItem *layoutParentItem = parentItem()) {
99 if (layoutParentItem->isWidget())
100 *result = (qreal)static_cast<QGraphicsWidget*>(layoutParentItem)->style()->pixelMetric(pm, nullptr);
101 }
102 }
103 }
104
visualDirection() const105 Qt::LayoutDirection QGraphicsLayoutPrivate::visualDirection() const
106 {
107 if (QGraphicsItem *maybeWidget = parentItem()) {
108 if (maybeWidget->isWidget())
109 return static_cast<QGraphicsWidget*>(maybeWidget)->layoutDirection();
110 }
111 return QGuiApplication::layoutDirection();
112 }
113
removeLayoutItemFromLayout(QGraphicsLayout * lay,QGraphicsLayoutItem * layoutItem)114 static bool removeLayoutItemFromLayout(QGraphicsLayout *lay, QGraphicsLayoutItem *layoutItem)
115 {
116 if (!lay)
117 return false;
118
119 for (int i = lay->count() - 1; i >= 0; --i) {
120 QGraphicsLayoutItem *child = lay->itemAt(i);
121 if (child && child->isLayout()) {
122 if (removeLayoutItemFromLayout(static_cast<QGraphicsLayout*>(child), layoutItem))
123 return true;
124 } else if (child == layoutItem) {
125 lay->removeAt(i);
126 return true;
127 }
128 }
129 return false;
130 }
131
132 /*!
133 \internal
134
135 This function is called from subclasses to add a layout item \a layoutItem
136 to a layout.
137
138 It takes care of automatically reparenting graphics items, if needed.
139
140 If \a layoutItem is a is already in a layout, it will remove it from that layout.
141
142 */
addChildLayoutItem(QGraphicsLayoutItem * layoutItem)143 void QGraphicsLayoutPrivate::addChildLayoutItem(QGraphicsLayoutItem *layoutItem)
144 {
145 Q_Q(QGraphicsLayout);
146 if (QGraphicsLayoutItem *maybeLayout = layoutItem->parentLayoutItem()) {
147 if (maybeLayout->isLayout())
148 removeLayoutItemFromLayout(static_cast<QGraphicsLayout*>(maybeLayout), layoutItem);
149 }
150 layoutItem->setParentLayoutItem(q);
151 if (layoutItem->isLayout()) {
152 if (QGraphicsItem *parItem = parentItem()) {
153 static_cast<QGraphicsLayout*>(layoutItem)->d_func()->reparentChildItems(parItem);
154 }
155 } else {
156 if (QGraphicsItem *item = layoutItem->graphicsItem()) {
157 QGraphicsItem *newParent = parentItem();
158 QGraphicsItem *oldParent = item->parentItem();
159 if (oldParent == newParent || !newParent)
160 return;
161
162 #ifdef QT_DEBUG
163 if (oldParent && item->isWidget()) {
164 QGraphicsWidget *w = static_cast<QGraphicsWidget*>(item);
165 qWarning("QGraphicsLayout::addChildLayoutItem: %s \"%s\" in wrong parent; moved to correct parent",
166 w->metaObject()->className(), w->objectName().toLocal8Bit().constData());
167 }
168 #endif
169
170 item->setParentItem(newParent);
171 }
172 }
173 }
174
activateRecursive(QGraphicsLayoutItem * item)175 void QGraphicsLayoutPrivate::activateRecursive(QGraphicsLayoutItem *item)
176 {
177 if (item->isLayout()) {
178 QGraphicsLayout *layout = static_cast<QGraphicsLayout *>(item);
179 if (layout->d_func()->activated) {
180 if (QGraphicsLayout::instantInvalidatePropagation()) {
181 return;
182 } else {
183 layout->invalidate(); // ### LOOKS SUSPICIOUSLY WRONG!!???
184 }
185 }
186
187 for (int i = layout->count() - 1; i >= 0; --i) {
188 QGraphicsLayoutItem *childItem = layout->itemAt(i);
189 if (childItem)
190 activateRecursive(childItem);
191 }
192 layout->d_func()->activated = true;
193 }
194 }
195
196 QT_END_NAMESPACE
197