From aa9c6f983215b9a1e5eb0b6be1ed10f30a30f78a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Arve=20S=C3=A6ther?= Date: Tue, 11 Feb 2020 15:33:56 +0100 Subject: Introduce QGraphicsLayoutItem::isEmpty() This change does basically two things: * Renames QGraphicsGridLayoutEngineItem::isIgnored() to QGraphicsGridLayoutEngineItem::isEmpty() to be consistent with QLayoutItem::isEmpty() * Creates a new public API function QGraphicsLayoutItem::isEmpty() so that there is a public method of overriding the behavior. Change-Id: I771a8fb429f9764a75e220f0ff9d07f578f981d3 Reviewed-by: Paul Olav Tvete --- .../graphicsview/qgraphicsgridlayoutengine.cpp | 30 ++++++++-------------- .../graphicsview/qgraphicsgridlayoutengine_p.h | 6 ++--- src/widgets/graphicsview/qgraphicslayoutitem.cpp | 17 ++++++++++++ src/widgets/graphicsview/qgraphicslayoutitem.h | 1 + 4 files changed, 30 insertions(+), 24 deletions(-) (limited to 'src/widgets/graphicsview') diff --git a/src/widgets/graphicsview/qgraphicsgridlayoutengine.cpp b/src/widgets/graphicsview/qgraphicsgridlayoutengine.cpp index 5797d9e539..3dd1b1652a 100644 --- a/src/widgets/graphicsview/qgraphicsgridlayoutengine.cpp +++ b/src/widgets/graphicsview/qgraphicsgridlayoutengine.cpp @@ -42,29 +42,9 @@ #include "qgraphicslayoutitem_p.h" #include "qgraphicslayout_p.h" #include "qgraphicswidget.h" -#include QT_BEGIN_NAMESPACE -bool QGraphicsGridLayoutEngineItem::isHidden() const -{ - if (QGraphicsItem *item = q_layoutItem->graphicsItem()) - return QGraphicsItemPrivate::get(item)->explicitlyHidden; - return false; -} - -/*! - \internal - - If this returns true, the layout will arrange just as if the item was never added to the layout. - (Note that this shouldn't lead to a "double spacing" where the item was hidden) - ### Qt6: Move to QGraphicsLayoutItem and make virtual -*/ -bool QGraphicsGridLayoutEngineItem::isIgnored() const -{ - return isHidden() && !q_layoutItem->sizePolicy().retainSizeWhenHidden(); -} - /* returns \c true if the size policy returns \c true for either hasHeightForWidth() or hasWidthForHeight() @@ -83,6 +63,16 @@ Qt::Orientation QGraphicsGridLayoutEngineItem::dynamicConstraintOrientation() co return Qt::Horizontal; } +/*! + \internal + + If this returns true, the layout will arrange just as if the item was never added to the layout. + (Note that this shouldn't lead to a "double spacing" where the item was hidden) +*/ +bool QGraphicsGridLayoutEngineItem::isEmpty() const +{ + return q_layoutItem->isEmpty(); +} void QGraphicsGridLayoutEngine::setAlignment(QGraphicsLayoutItem *graphicsLayoutItem, Qt::Alignment alignment) { diff --git a/src/widgets/graphicsview/qgraphicsgridlayoutengine_p.h b/src/widgets/graphicsview/qgraphicsgridlayoutengine_p.h index 2f2c547977..a3e42c7b77 100644 --- a/src/widgets/graphicsview/qgraphicsgridlayoutengine_p.h +++ b/src/widgets/graphicsview/qgraphicsgridlayoutengine_p.h @@ -89,10 +89,6 @@ public: return q_layoutItem->effectiveSizeHint(which, constraint); } - bool isHidden() const; - - virtual bool isIgnored() const override; - virtual void setGeometry(const QRectF &rect) override { q_layoutItem->setGeometry(rect); @@ -101,6 +97,8 @@ public: virtual bool hasDynamicConstraint() const override; virtual Qt::Orientation dynamicConstraintOrientation() const override; + virtual bool isEmpty() const override; + QGraphicsLayoutItem *layoutItem() const { return q_layoutItem; } protected: diff --git a/src/widgets/graphicsview/qgraphicslayoutitem.cpp b/src/widgets/graphicsview/qgraphicslayoutitem.cpp index 8694dcb36b..2c484321e4 100644 --- a/src/widgets/graphicsview/qgraphicslayoutitem.cpp +++ b/src/widgets/graphicsview/qgraphicslayoutitem.cpp @@ -45,6 +45,7 @@ #include "qgraphicslayoutitem_p.h" #include "qwidget.h" #include "qgraphicswidget.h" +#include "qgraphicsitem_p.h" #include @@ -825,6 +826,22 @@ void QGraphicsLayoutItem::updateGeometry() d->sizeHintWithConstraintCacheDirty = true; } +/*! + * returns \c true if this item is empty, i.e whether it has no content and + * should not occupy any space. + * + * The default implementation returns true if the item has been hidden unless + * its size policy has retainSizeWhenHidden set to \c true + */ +bool QGraphicsLayoutItem::isEmpty() const +{ + bool isHidden = false; + if (QGraphicsItem *item = graphicsItem()) + isHidden = QGraphicsItemPrivate::get(item)->explicitlyHidden; + + return isHidden && !sizePolicy().retainSizeWhenHidden(); +} + /*! Returns the parent of this QGraphicsLayoutItem, or \nullptr if there is no parent, or if the parent does not inherit from QGraphicsLayoutItem diff --git a/src/widgets/graphicsview/qgraphicslayoutitem.h b/src/widgets/graphicsview/qgraphicslayoutitem.h index 86a0a87361..04905c7654 100644 --- a/src/widgets/graphicsview/qgraphicslayoutitem.h +++ b/src/widgets/graphicsview/qgraphicslayoutitem.h @@ -94,6 +94,7 @@ public: virtual void updateGeometry(); + virtual bool isEmpty() const; QGraphicsLayoutItem *parentLayoutItem() const; void setParentLayoutItem(QGraphicsLayoutItem *parent); -- cgit v1.2.3