summaryrefslogtreecommitdiffstats
path: root/src/gui/graphicsview/qgraphicsitem.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/graphicsview/qgraphicsitem.cpp')
-rw-r--r--src/gui/graphicsview/qgraphicsitem.cpp226
1 files changed, 148 insertions, 78 deletions
diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp
index 616daaa..c53a893 100644
--- a/src/gui/graphicsview/qgraphicsitem.cpp
+++ b/src/gui/graphicsview/qgraphicsitem.cpp
@@ -798,8 +798,36 @@ void QGraphicsItemPrivate::updateAncestorFlag(QGraphicsItem::GraphicsItemFlag ch
return;
}
- foreach (QGraphicsItem *child, children)
- child->d_ptr->updateAncestorFlag(childFlag, flag, enabled, false);
+ for (int i = 0; i < children.size(); ++i)
+ children.at(i)->d_ptr->updateAncestorFlag(childFlag, flag, enabled, false);
+}
+
+void QGraphicsItemPrivate::updateAncestorFlags()
+{
+ int flags = 0;
+ if (parent) {
+ // Inherit the parent's ancestor flags.
+ QGraphicsItemPrivate *pd = parent->d_ptr.data();
+ flags = pd->ancestorFlags;
+
+ // Add in flags from the parent.
+ if (pd->filtersDescendantEvents)
+ flags |= AncestorFiltersChildEvents;
+ if (pd->handlesChildEvents)
+ flags |= AncestorHandlesChildEvents;
+ if (pd->flags & QGraphicsItem::ItemClipsChildrenToShape)
+ flags |= AncestorClipsChildren;
+ if (pd->flags & QGraphicsItem::ItemIgnoresTransformations)
+ flags |= AncestorIgnoresTransformations;
+ }
+
+ if (ancestorFlags == flags)
+ return; // No change; stop propagation.
+ ancestorFlags = flags;
+
+ // Propagate to children recursively.
+ for (int i = 0; i < children.size(); ++i)
+ children.at(i)->d_ptr->updateAncestorFlags();
}
/*!
@@ -984,25 +1012,17 @@ QVariant QGraphicsItemPrivate::inputMethodQueryHelper(Qt::InputMethodQuery query
prepareGeometryChange) if the item is in its destructor, i.e.
inDestructor is 1.
*/
-void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent)
+void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent, const QVariant *newParentVariant,
+ const QVariant *thisPointerVariant)
{
Q_Q(QGraphicsItem);
- if (newParent == q) {
- qWarning("QGraphicsItem::setParentItem: cannot assign %p as a parent of itself", this);
- return;
- }
- if (newParent == parent)
- return;
-
- const QVariant newParentVariant(q->itemChange(QGraphicsItem::ItemParentChange,
- qVariantFromValue<QGraphicsItem *>(newParent)));
- newParent = qVariantValue<QGraphicsItem *>(newParentVariant);
if (newParent == parent)
return;
if (scene) {
// Deliver the change to the index
- scene->d_func()->index->itemChange(q, QGraphicsItem::ItemParentChange, newParentVariant);
+ if (scene->d_func()->indexMethod != QGraphicsScene::NoIndex)
+ scene->d_func()->index->itemChange(q, QGraphicsItem::ItemParentChange, newParent);
// Disable scene pos notifications for old ancestors
if (scenePosDescendants || (flags & QGraphicsItem::ItemSendsScenePositionChanges))
@@ -1020,11 +1040,11 @@ void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent)
if (!inDestructor)
q_ptr->prepareGeometryChange();
- const QVariant thisPointerVariant(qVariantFromValue<QGraphicsItem *>(q));
if (parent) {
// Remove from current parent
parent->d_ptr->removeChild(q);
- parent->itemChange(QGraphicsItem::ItemChildRemovedChange, thisPointerVariant);
+ if (thisPointerVariant)
+ parent->itemChange(QGraphicsItem::ItemChildRemovedChange, *thisPointerVariant);
}
// Update toplevelitem list. If this item is being deleted, its parent
@@ -1042,7 +1062,7 @@ void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent)
QGraphicsItem *p = parent;
QGraphicsItem *parentFocusScopeItem = 0;
while (p) {
- if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
+ if (p->d_ptr->flags & QGraphicsItem::ItemIsFocusScope) {
// If this item's focus scope's focus scope item points
// to this item or a descendent, then clear it.
QGraphicsItem *fsi = p->d_ptr->focusScopeItem;
@@ -1055,6 +1075,10 @@ void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent)
p = p->d_ptr->parent;
}
+ // Update graphics effect optimization flag
+ if (newParent && (graphicsEffect || mayHaveChildWithGraphicsEffect))
+ newParent->d_ptr->updateChildWithGraphicsEffectFlagRecursively();
+
// Update focus scope item ptr in new scope.
QGraphicsItem *newFocusScopeItem = subFocusItem ? subFocusItem : parentFocusScopeItem;
if (newFocusScopeItem && newParent) {
@@ -1063,11 +1087,11 @@ void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent)
QGraphicsItem *ancestorScope = 0;
QGraphicsItem *p = subFocusItem->d_ptr->parent;
while (p) {
- if (p->flags() & QGraphicsItem::ItemIsFocusScope)
+ if (p->d_ptr->flags & QGraphicsItem::ItemIsFocusScope)
ancestorScope = p;
- if (p->isPanel())
+ if (p->d_ptr->flags & QGraphicsItem::ItemIsPanel)
break;
- p = p->parentItem();
+ p = p->d_ptr->parent;
}
if (ancestorScope)
newFocusScopeItem = ancestorScope;
@@ -1075,7 +1099,7 @@ void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent)
QGraphicsItem *p = newParent;
while (p) {
- if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
+ if (p->d_ptr->flags & QGraphicsItem::ItemIsFocusScope) {
p->d_ptr->focusScopeItem = newFocusScopeItem;
// Ensure the new item is no longer the subFocusItem. The
// only way to set focus on a child of a focus scope is
@@ -1089,52 +1113,45 @@ void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent)
}
if ((parent = newParent)) {
- bool implicitUpdate = false;
if (parent->d_func()->scene && parent->d_func()->scene != scene) {
// Move this item to its new parent's scene
parent->d_func()->scene->addItem(q);
- implicitUpdate = true;
} else if (!parent->d_func()->scene && scene) {
// Remove this item from its former scene
scene->removeItem(q);
}
parent->d_ptr->addChild(q);
- parent->itemChange(QGraphicsItem::ItemChildAddedChange, thisPointerVariant);
+ if (thisPointerVariant)
+ parent->itemChange(QGraphicsItem::ItemChildAddedChange, *thisPointerVariant);
if (scene) {
- if (!implicitUpdate)
- scene->d_func()->markDirty(q_ptr);
-
// Re-enable scene pos notifications for new ancestors
if (scenePosDescendants || (flags & QGraphicsItem::ItemSendsScenePositionChanges))
scene->d_func()->setScenePosItemEnabled(q, true);
}
+ // Propagate dirty flags to the new parent
+ markParentDirty(/*updateBoundingRect=*/true);
+
// Inherit ancestor flags from the new parent.
- updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-2));
- updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-1));
- updateAncestorFlag(QGraphicsItem::ItemClipsChildrenToShape);
- updateAncestorFlag(QGraphicsItem::ItemIgnoresTransformations);
+ updateAncestorFlags();
// Update item visible / enabled.
- if (parent->isVisible() != visible) {
- if (!parent->isVisible() || !explicitlyHidden)
- setVisibleHelper(parent->isVisible(), /* explicit = */ false, /* update = */ !implicitUpdate);
+ if (parent->d_ptr->visible != visible) {
+ if (!parent->d_ptr->visible || !explicitlyHidden)
+ setVisibleHelper(parent->d_ptr->visible, /* explicit = */ false, /* update = */ false);
}
if (parent->isEnabled() != enabled) {
- if (!parent->isEnabled() || !explicitlyDisabled)
- setEnabledHelper(parent->isEnabled(), /* explicit = */ false, /* update = */ !implicitUpdate);
+ if (!parent->d_ptr->enabled || !explicitlyDisabled)
+ setEnabledHelper(parent->d_ptr->enabled, /* explicit = */ false, /* update = */ false);
}
// Auto-activate if visible and the parent is active.
- if (q->isVisible() && parent->isActive())
+ if (visible && parent->isActive())
q->setActive(true);
} else {
// Inherit ancestor flags from the new parent.
- updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-2));
- updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-1));
- updateAncestorFlag(QGraphicsItem::ItemClipsChildrenToShape);
- updateAncestorFlag(QGraphicsItem::ItemIgnoresTransformations);
+ updateAncestorFlags();
if (!inDestructor) {
// Update item visible / enabled.
@@ -1142,10 +1159,6 @@ void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent)
setVisibleHelper(true, /* explicit = */ false);
if (!enabled && !explicitlyDisabled)
setEnabledHelper(true, /* explicit = */ false);
-
- // If the item is being deleted, the whole scene will be updated.
- if (scene)
- scene->d_func()->markDirty(q_ptr);
}
}
@@ -1161,7 +1174,8 @@ void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent)
}
// Deliver post-change notification
- q->itemChange(QGraphicsItem::ItemParentHasChanged, newParentVariant);
+ if (newParentVariant)
+ q->itemChange(QGraphicsItem::ItemParentHasChanged, *newParentVariant);
if (isObject)
emit static_cast<QGraphicsObject *>(q)->parentChanged();
@@ -1351,7 +1365,7 @@ QGraphicsItem::~QGraphicsItem()
d_ptr->scene->d_func()->removeItemHelper(this);
} else {
d_ptr->resetFocusProxy();
- d_ptr->setParentItemHelper(0);
+ setParentItem(0);
}
#ifndef QT_NO_GRAPHICSEFFECT
@@ -1556,9 +1570,23 @@ const QGraphicsObject *QGraphicsItem::toGraphicsObject() const
\sa parentItem(), childItems()
*/
-void QGraphicsItem::setParentItem(QGraphicsItem *parent)
+void QGraphicsItem::setParentItem(QGraphicsItem *newParent)
{
- d_ptr->setParentItemHelper(parent);
+ if (newParent == this) {
+ qWarning("QGraphicsItem::setParentItem: cannot assign %p as a parent of itself", this);
+ return;
+ }
+ if (newParent == d_ptr->parent)
+ return;
+
+ const QVariant newParentVariant(itemChange(QGraphicsItem::ItemParentChange,
+ qVariantFromValue<QGraphicsItem *>(newParent)));
+ newParent = qVariantValue<QGraphicsItem *>(newParentVariant);
+ if (newParent == d_ptr->parent)
+ return;
+
+ const QVariant thisPointerVariant(qVariantFromValue<QGraphicsItem *>(this));
+ d_ptr->setParentItemHelper(newParent, &newParentVariant, &thisPointerVariant);
}
/*!
@@ -1608,7 +1636,7 @@ bool QGraphicsItem::isWidget() const
*/
bool QGraphicsItem::isWindow() const
{
- return isWidget() && (static_cast<const QGraphicsWidget *>(this)->windowType() & Qt::Window);
+ return d_ptr->isWidget && (static_cast<const QGraphicsWidget *>(this)->windowType() & Qt::Window);
}
/*!
@@ -1645,9 +1673,9 @@ QGraphicsItem::GraphicsItemFlags QGraphicsItem::flags() const
void QGraphicsItem::setFlag(GraphicsItemFlag flag, bool enabled)
{
if (enabled)
- setFlags(flags() | flag);
+ setFlags(GraphicsItemFlags(d_ptr->flags) | flag);
else
- setFlags(flags() & ~flag);
+ setFlags(GraphicsItemFlags(d_ptr->flags) & ~flag);
}
/*!
@@ -1694,8 +1722,8 @@ void QGraphicsItem::setFlags(GraphicsItemFlags flags)
flags = GraphicsItemFlags(itemChange(ItemFlagsChange, quint32(flags)).toUInt());
if (quint32(d_ptr->flags) == quint32(flags))
return;
- if (d_ptr->scene)
- d_ptr->scene->d_func()->index->itemChange(this, ItemFlagsChange, quint32(flags));
+ if (d_ptr->scene && d_ptr->scene->d_func()->indexMethod != QGraphicsScene::NoIndex)
+ d_ptr->scene->d_func()->index->itemChange(this, ItemFlagsChange, &flags);
// Flags that alter the geometry of the item (or its children).
const quint32 geomChangeFlagsMask = (ItemClipsChildrenToShape | ItemClipsToShape | ItemIgnoresTransformations | ItemIsSelectable);
@@ -1704,7 +1732,7 @@ void QGraphicsItem::setFlags(GraphicsItemFlags flags)
d_ptr->paintedViewBoundingRectsNeedRepaint = 1;
// Keep the old flags to compare the diff.
- GraphicsItemFlags oldFlags = this->flags();
+ GraphicsItemFlags oldFlags = GraphicsItemFlags(d_ptr->flags);
// Update flags.
d_ptr->flags = flags;
@@ -1733,7 +1761,23 @@ void QGraphicsItem::setFlags(GraphicsItemFlags flags)
d_ptr->updateAncestorFlag(ItemIgnoresTransformations);
}
+ if ((flags & ItemNegativeZStacksBehindParent) != (oldFlags & ItemNegativeZStacksBehindParent)) {
+ // NB! We change the flags directly here, so we must also update d_ptr->flags.
+ // Note that this has do be done before the ItemStacksBehindParent check
+ // below; otherwise we will loose the change.
+
+ // Update stack-behind.
+ if (d_ptr->z < qreal(0.0))
+ flags |= ItemStacksBehindParent;
+ else
+ flags &= ~ItemStacksBehindParent;
+ d_ptr->flags = flags;
+ }
+
if ((flags & ItemStacksBehindParent) != (oldFlags & ItemStacksBehindParent)) {
+ // NB! This check has to come after the ItemNegativeZStacksBehindParent
+ // check above. Be careful.
+
// Ensure child item sorting is up to date when toggling this flag.
if (d_ptr->parent)
d_ptr->parent->d_ptr->needSortChildren = 1;
@@ -1747,10 +1791,6 @@ void QGraphicsItem::setFlags(GraphicsItemFlags flags)
d_ptr->scene->d_func()->updateInputMethodSensitivityInViews();
}
- if ((flags & ItemNegativeZStacksBehindParent) != (oldFlags & ItemNegativeZStacksBehindParent)) {
- // Update stack-behind.
- setFlag(ItemStacksBehindParent, d_ptr->z < qreal(0.0));
- }
if ((d_ptr->panelModality != NonModal)
&& d_ptr->scene
@@ -2524,7 +2564,9 @@ void QGraphicsItem::setOpacity(qreal opacity)
// Update.
if (d_ptr->scene) {
#ifndef QT_NO_GRAPHICSEFFECT
- d_ptr->invalidateGraphicsEffectsRecursively();
+ d_ptr->invalidateParentGraphicsEffectsRecursively();
+ if (!(d_ptr->flags & ItemDoesntPropagateOpacityToChildren))
+ d_ptr->invalidateChildGraphicsEffectsRecursively(QGraphicsItemPrivate::OpacityChanged);
#endif //QT_NO_GRAPHICSEFFECT
d_ptr->scene->d_func()->markDirty(this, QRectF(),
/*invalidateChildren=*/true,
@@ -2569,6 +2611,8 @@ void QGraphicsItem::setGraphicsEffect(QGraphicsEffect *effect)
if (d_ptr->graphicsEffect) {
delete d_ptr->graphicsEffect;
d_ptr->graphicsEffect = 0;
+ } else if (d_ptr->parent) {
+ d_ptr->parent->d_ptr->updateChildWithGraphicsEffectFlagRecursively();
}
if (effect) {
@@ -2582,6 +2626,19 @@ void QGraphicsItem::setGraphicsEffect(QGraphicsEffect *effect)
}
#endif //QT_NO_GRAPHICSEFFECT
+void QGraphicsItemPrivate::updateChildWithGraphicsEffectFlagRecursively()
+{
+#ifndef QT_NO_GRAPHICSEFFECT
+ QGraphicsItemPrivate *itemPrivate = this;
+ do {
+ // parent chain already notified?
+ if (itemPrivate->mayHaveChildWithGraphicsEffect)
+ return;
+ itemPrivate->mayHaveChildWithGraphicsEffect = 1;
+ } while ((itemPrivate = itemPrivate->parent ? itemPrivate->parent->d_ptr.data() : 0));
+#endif
+}
+
/*!
\internal
\since 4.6
@@ -2998,9 +3055,16 @@ void QGraphicsItem::setActive(bool active)
*/
bool QGraphicsItem::hasFocus() const
{
+ if (!d_ptr->scene || !d_ptr->scene->isActive())
+ return false;
+
if (d_ptr->focusProxy)
return d_ptr->focusProxy->hasFocus();
- return isActive() && (d_ptr->scene && d_ptr->scene->focusItem() == this);
+
+ if (d_ptr->scene->d_func()->focusItem != this)
+ return false;
+
+ return panel() == d_ptr->scene->d_func()->activePanel;
}
/*!
@@ -4258,9 +4322,9 @@ void QGraphicsItem::setZValue(qreal z)
if (newZ == d_ptr->z)
return;
- if (d_ptr->scene) {
+ if (d_ptr->scene && d_ptr->scene->d_func()->indexMethod != QGraphicsScene::NoIndex) {
// Z Value has changed, we have to notify the index.
- d_ptr->scene->d_func()->index->itemChange(this, ItemZValueChange, newZVariant);
+ d_ptr->scene->d_func()->index->itemChange(this, ItemZValueChange, &newZ);
}
d_ptr->z = newZ;
@@ -5027,7 +5091,7 @@ int QGraphicsItemPrivate::depth() const
\internal
*/
#ifndef QT_NO_GRAPHICSEFFECT
-void QGraphicsItemPrivate::invalidateGraphicsEffectsRecursively()
+void QGraphicsItemPrivate::invalidateParentGraphicsEffectsRecursively()
{
QGraphicsItemPrivate *itemPrivate = this;
do {
@@ -5039,6 +5103,24 @@ void QGraphicsItemPrivate::invalidateGraphicsEffectsRecursively()
}
} while ((itemPrivate = itemPrivate->parent ? itemPrivate->parent->d_ptr.data() : 0));
}
+
+void QGraphicsItemPrivate::invalidateChildGraphicsEffectsRecursively(QGraphicsItemPrivate::InvalidateReason reason)
+{
+ if (!mayHaveChildWithGraphicsEffect)
+ return;
+
+ for (int i = 0; i < children.size(); ++i) {
+ QGraphicsItemPrivate *childPrivate = children.at(i)->d_ptr.data();
+ if (reason == OpacityChanged && (childPrivate->flags & QGraphicsItem::ItemIgnoresParentOpacity))
+ continue;
+ if (childPrivate->graphicsEffect) {
+ childPrivate->notifyInvalidated = 1;
+ static_cast<QGraphicsItemEffectSourcePrivate *>(childPrivate->graphicsEffect->d_func()->source->d_func())->invalidateCache();
+ }
+
+ childPrivate->invalidateChildGraphicsEffectsRecursively(reason);
+ }
+}
#endif //QT_NO_GRAPHICSEFFECT
/*!
@@ -5283,7 +5365,7 @@ void QGraphicsItem::update(const QRectF &rect)
// Make sure we notify effects about invalidated source.
#ifndef QT_NO_GRAPHICSEFFECT
- d_ptr->invalidateGraphicsEffectsRecursively();
+ d_ptr->invalidateParentGraphicsEffectsRecursively();
#endif //QT_NO_GRAPHICSEFFECT
if (CacheMode(d_ptr->cacheMode) != NoCache) {
@@ -7176,19 +7258,7 @@ void QGraphicsItem::prepareGeometryChange()
}
}
- QGraphicsItem *parent = this;
- while ((parent = parent->d_ptr->parent)) {
- QGraphicsItemPrivate *parentp = parent->d_ptr.data();
- parentp->dirtyChildrenBoundingRect = 1;
- // ### Only do this if the parent's effect applies to the entire subtree.
- parentp->notifyBoundingRectChanged = 1;
-#ifndef QT_NO_GRAPHICSEFFECT
- if (parentp->scene && parentp->graphicsEffect) {
- parentp->notifyInvalidated = 1;
- static_cast<QGraphicsItemEffectSourcePrivate *>(parentp->graphicsEffect->d_func()->source->d_func())->invalidateCache();
- }
-#endif
- }
+ d_ptr->markParentDirty(/*updateBoundingRect=*/true);
}
/*!