Qt 4.8
qgraphicslayout_p.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qglobal.h"
43 
44 #ifndef QT_NO_GRAPHICSVIEW
45 
46 #include "qgraphicslayout_p.h"
47 #include "qgraphicslayout.h"
48 #include "qgraphicswidget.h"
49 #include "qapplication.h"
50 
52 
59 {
61  int n = q->count();
62  //bool mwVisible = mw && mw->isVisible();
63  for (int i = 0; i < n; ++i) {
64  QGraphicsLayoutItem *layoutChild = q->itemAt(i);
65  if (!layoutChild) {
66  // Skip stretch items
67  continue;
68  }
69  if (layoutChild->isLayout()) {
70  QGraphicsLayout *l = static_cast<QGraphicsLayout*>(layoutChild);
71  l->d_func()->reparentChildItems(newParent);
72  } else if (QGraphicsItem *itemChild = layoutChild->graphicsItem()){
73  QGraphicsItem *childParent = itemChild->parentItem();
74 #ifdef QT_DEBUG
75  if (childParent && childParent != newParent && itemChild->isWidget() && qt_graphicsLayoutDebug()) {
76  QGraphicsWidget *w = static_cast<QGraphicsWidget*>(layoutChild);
77  qWarning("QGraphicsLayout::addChildLayout: widget %s \"%s\" in wrong parent; moved to correct parent",
79  }
80 #endif
81  if (childParent != newParent)
82  itemChild->setParentItem(newParent);
83  }
84  }
85 }
86 
88 {
89  if (!result)
90  return;
91  Q_Q(const QGraphicsLayout);
92 
94  if (userMargin >= 0.0) {
95  *result = userMargin;
96  } else if (!parent) {
97  *result = 0.0;
98  } else if (parent->isLayout()) { // sublayouts have 0 margin by default
99  *result = 0.0;
100  } else {
101  *result = 0.0;
102  if (QGraphicsItem *layoutParentItem = parentItem()) {
103  if (layoutParentItem->isWidget())
104  *result = (qreal)static_cast<QGraphicsWidget*>(layoutParentItem)->style()->pixelMetric(pm, 0);
105  }
106  }
107 }
108 
110 {
111  if (QGraphicsItem *maybeWidget = parentItem()) {
112  if (maybeWidget->isWidget())
113  return static_cast<QGraphicsWidget*>(maybeWidget)->layoutDirection();
114  }
116 }
117 
119 {
120  if (!lay)
121  return false;
122 
123  for (int i = lay->count() - 1; i >= 0; --i) {
124  QGraphicsLayoutItem *child = lay->itemAt(i);
125  if (child && child->isLayout()) {
126  if (removeLayoutItemFromLayout(static_cast<QGraphicsLayout*>(child), layoutItem))
127  return true;
128  } else if (child == layoutItem) {
129  lay->removeAt(i);
130  return true;
131  }
132  }
133  return false;
134 }
135 
151 {
153  if (QGraphicsLayoutItem *maybeLayout = layoutItem->parentLayoutItem()) {
154  if (maybeLayout->isLayout())
155  removeLayoutItemFromLayout(static_cast<QGraphicsLayout*>(maybeLayout), layoutItem);
156  }
157  layoutItem->setParentLayoutItem(q);
158  if (layoutItem->isLayout()) {
159  if (QGraphicsItem *parItem = parentItem()) {
160  static_cast<QGraphicsLayout*>(layoutItem)->d_func()->reparentChildItems(parItem);
161  }
162  } else {
163  if (QGraphicsItem *item = layoutItem->graphicsItem()) {
164  QGraphicsItem *newParent = parentItem();
165  QGraphicsItem *oldParent = item->parentItem();
166  if (oldParent == newParent || !newParent)
167  return;
168 
169 #ifdef QT_DEBUG
170  if (oldParent && item->isWidget()) {
171  QGraphicsWidget *w = static_cast<QGraphicsWidget*>(item);
172  qWarning("QGraphicsLayout::addChildLayoutItem: %s \"%s\" in wrong parent; moved to correct parent",
174  }
175 #endif
176 
177  item->setParentItem(newParent);
178  }
179  }
180 }
181 
183 {
184  if (item->isLayout()) {
185  QGraphicsLayout *layout = static_cast<QGraphicsLayout *>(item);
186  if (layout->d_func()->activated) {
188  return;
189  } else {
190  layout->invalidate(); // ### LOOKS SUSPICIOUSLY WRONG!!???
191  }
192  }
193 
194  for (int i = layout->count() - 1; i >= 0; --i) {
195  QGraphicsLayoutItem *childItem = layout->itemAt(i);
196  if (childItem)
197  activateRecursive(childItem);
198  }
199  layout->d_func()->activated = true;
200  }
201 }
202 
203 
205 
206 #endif //QT_NO_GRAPHICSVIEW
virtual void removeAt(int index)=0
This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to remove the item ...
static Qt::LayoutDirection layoutDirection()
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QGraphicsItem * graphicsItem() const
Returns the QGraphicsItem that this layout item represents.
void addChildLayoutItem(QGraphicsLayoutItem *item)
This function is called from subclasses to add a layout item layoutItem to a layout.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
bool qt_graphicsLayoutDebug()
PixelMetric
This enum describes the various available pixel metrics.
Definition: qstyle.h:474
void reparentChildItems(QGraphicsItem *newParent)
void setParentLayoutItem(QGraphicsLayoutItem *parent)
Sets the parent of this QGraphicsLayoutItem to parent.
virtual void invalidate()
Clears any cached geometry and size hint information in the layout, and posts a LayoutRequest event t...
#define Q_Q(Class)
Definition: qglobal.h:2483
void activateRecursive(QGraphicsLayoutItem *item)
void getMargin(qreal *result, qreal userMargin, QStyle::PixelMetric pm) const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const char * layout
LayoutDirection
Definition: qnamespace.h:1580
Q_CORE_EXPORT void qWarning(const char *,...)
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts...
QGraphicsItem * parentItem() const
Returns the parent item of this layout, or 0 if this layout is not installed on any widget...
static bool instantInvalidatePropagation()
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
bool isLayout() const
Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges o...
virtual int count() const =0
This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to return the numbe...
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QGraphicsItem * parentItem() const
Returns a pointer to this item&#39;s parent item.
Qt::LayoutDirection visualDirection() const
QGraphicsLayoutItem * parentLayoutItem() const
Returns the parent of this QGraphicsLayoutItem, or 0 if there is no parent, or if the parent does not...
const char * className() const
Returns the class name.
Definition: qobjectdefs.h:491
QString objectName() const
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
bool isWidget() const
Returns true if this item is a widget (i.
QFactoryLoader * l
virtual QGraphicsLayoutItem * itemAt(int i) const =0
This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to return a pointer...
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
static bool removeLayoutItemFromLayout(QGraphicsLayout *lay, QGraphicsLayoutItem *layoutItem)
QGraphicsLayoutItem * parent
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.