Qt 4.8
qgraphicslayout.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 "qapplication.h"
43 
44 #ifndef QT_NO_GRAPHICSVIEW
45 #include "qgraphicslayout.h"
46 #include "qgraphicslayout_p.h"
47 #include "qgraphicslayoutitem.h"
48 #include "qgraphicslayoutitem_p.h"
49 #include "qgraphicswidget.h"
50 #include "qgraphicswidget_p.h"
51 #include "qgraphicsscene.h"
52 
54 
165 {
166  setParentLayoutItem(parent);
167  if (parent && !parent->isLayout()) {
168  // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
169  QGraphicsItem *itemParent = parent->graphicsItem();
170  if (itemParent && itemParent->isWidget()) {
171  static_cast<QGraphicsWidget *>(itemParent)->d_func()->setLayout_helper(this);
172  } else {
173  qWarning("QGraphicsLayout::QGraphicsLayout: Attempt to create a layout with a parent that is"
174  " neither a QGraphicsWidget nor QGraphicsLayout");
175  }
176  }
178  setOwnedByLayout(true);
179 }
180 
185  : QGraphicsLayoutItem(dd)
186 {
187  setParentLayoutItem(parent);
188  if (parent && !parent->isLayout()) {
189  // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
190  QGraphicsItem *itemParent = parent->graphicsItem();
191  if (itemParent && itemParent->isWidget()) {
192  static_cast<QGraphicsWidget *>(itemParent)->d_func()->setLayout_helper(this);
193  } else {
194  qWarning("QGraphicsLayout::QGraphicsLayout: Attempt to create a layout with a parent that is"
195  " neither a QGraphicsWidget nor QGraphicsLayout");
196  }
197  }
199  setOwnedByLayout(true);
200 }
201 
206 {
207 }
208 
223 {
225  if (d->left == left && d->top == top && d->right == right && d->bottom == bottom)
226  return;
227  d->left = left;
228  d->right = right;
229  d->top = top;
230  d->bottom = bottom;
231  invalidate();
232 }
233 
238 {
239  Q_D(const QGraphicsLayout);
240  d->getMargin(left, d->left, QStyle::PM_LayoutLeftMargin);
241  d->getMargin(top, d->top, QStyle::PM_LayoutTopMargin);
242  d->getMargin(right, d->right, QStyle::PM_LayoutRightMargin);
243  d->getMargin(bottom, d->bottom, QStyle::PM_LayoutBottomMargin);
244 }
245 
263 {
265  if (d->activated)
266  return;
267 
268  d->activateRecursive(this);
269 
270  // we don't call activate on a sublayout, but somebody might.
271  // Therefore, we walk to the parentitem of the toplevel layout.
272  QGraphicsLayoutItem *parentItem = this;
273  while (parentItem && parentItem->isLayout())
274  parentItem = parentItem->parentLayoutItem();
275  if (!parentItem)
276  return;
277  Q_ASSERT(!parentItem->isLayout());
278 
279  setGeometry(parentItem->contentsRect()); // relayout children
282  }
283 }
284 
294 {
295  Q_D(const QGraphicsLayout);
296  return d->activated;
297 }
298 
307 {
309  updateGeometry();
310  } else {
311  // only mark layouts as invalid (activated = false) if we can post a LayoutRequest event.
312  QGraphicsLayoutItem *layoutItem = this;
313  while (layoutItem && layoutItem->isLayout()) {
314  // we could call updateGeometry(), but what if that method
315  // does not call the base implementation? In addition, updateGeometry()
316  // does more than we need.
317  layoutItem->d_func()->sizeHintCacheDirty = true;
318  layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true;
319  layoutItem = layoutItem->parentLayoutItem();
320  }
321  if (layoutItem) {
322  layoutItem->d_func()->sizeHintCacheDirty = true;
323  layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true;
324  }
325 
326  bool postIt = layoutItem ? !layoutItem->isLayout() : false;
327  if (postIt) {
328  layoutItem = this;
329  while (layoutItem && layoutItem->isLayout()
330  && static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated) {
331  static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated = false;
332  layoutItem = layoutItem->parentLayoutItem();
333  }
334  if (layoutItem && !layoutItem->isLayout()) {
335  // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
336  QApplication::postEvent(static_cast<QGraphicsWidget *>(layoutItem), new QEvent(QEvent::LayoutRequest));
337  }
338  }
339  }
340 }
341 
346 {
349  d->activated = false;
351 
352  QGraphicsLayoutItem *parentItem = parentLayoutItem();
353  if (!parentItem)
354  return;
355 
356  if (parentItem->isLayout())
357  static_cast<QGraphicsLayout *>(parentItem)->invalidate();
358  else
359  parentItem->updateGeometry();
360  } else {
362  if (QGraphicsLayoutItem *parentItem = parentLayoutItem()) {
363  if (parentItem->isLayout()) {
364  parentItem->updateGeometry();
365  } else {
366  invalidate();
367  }
368  }
369  }
370 }
371 
386 {
387  switch (e->type()) {
389  if (isActivated()) {
391  } else {
392  activate(); // relies on that activate() will call updateGeometry()
393  }
394  break;
396  activate();
397  break;
399  invalidate();
400  break;
401  default:
402  break;
403  }
404 }
405 
477 {
479  d->addChildLayoutItem(layoutItem);
480 }
481 
482 static bool g_instantInvalidatePropagation = false;
483 
509 {
511 }
512 
520 {
522 }
523 
525 
526 #endif //QT_NO_GRAPHICSVIEW
double d
Definition: qnumeric_p.h:62
virtual void setGeometry(const QRectF &rect)
This virtual function sets the geometry of the QGraphicsLayoutItem to rect, which is in parent coordi...
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
Reimplemented Function
virtual void updateGeometry()
Reimplemented Function
QGraphicsLayout(QGraphicsLayoutItem *parent=0)
Contructs a QGraphicsLayout object.
QGraphicsItem * graphicsItem() const
Returns the QGraphicsItem that this layout item represents.
static void postEvent(QObject *receiver, QEvent *event)
Adds the event event, with the object receiver as the receiver of the event, to an event queue and re...
QRectF contentsRect() const
Returns the contents rect in local coordinates.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
void setParentLayoutItem(QGraphicsLayoutItem *parent)
Sets the parent of this QGraphicsLayoutItem to parent.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
static bool g_instantInvalidatePropagation
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
virtual void invalidate()
Clears any cached geometry and size hint information in the layout, and posts a LayoutRequest event t...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual void updateGeometry()
This virtual function discards any cached size hint information.
Q_CORE_EXPORT void qWarning(const char *,...)
virtual void widgetEvent(QEvent *e)
This virtual event handler receives all events for the managed widget.
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts...
static bool instantInvalidatePropagation()
bool isActivated() const
Returns true if the layout is currently being activated; otherwise, returns false.
bool isLayout() const
Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges o...
void setOwnedByLayout(bool ownedByLayout)
Sets whether a layout should delete this item in its destructor or not.
QGraphicsLayoutItem * parentLayoutItem() const
Returns the parent of this QGraphicsLayoutItem, or 0 if there is no parent, or if the parent does not...
~QGraphicsLayout()
Destroys the QGraphicsLayout object.
void addChildLayoutItem(QGraphicsLayoutItem *layoutItem)
This function is a convenience function provided for custom layouts, and will go through all items in...
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.
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.
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
static void setInstantInvalidatePropagation(bool enable)
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.