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 <paul.tvete@qt.io>
This commit is contained in:
Jan Arve Sæther 2020-02-11 15:33:56 +01:00
parent 26059d1b9b
commit aa9c6f9832
6 changed files with 32 additions and 26 deletions

View File

@ -1336,7 +1336,7 @@ void QGridLayoutEngine::fillRowData(QGridLayoutRowData *rowData,
if (rowIsIdenticalToPrevious && item != itemAt(row - 1, column, orientation))
rowIsIdenticalToPrevious = false;
if (item && !item->isIgnored())
if (item && !item->isEmpty())
rowIsEmpty = false;
}

View File

@ -303,7 +303,7 @@ public:
virtual QLayoutPolicy::Policy sizePolicy(Qt::Orientation orientation) const = 0;
virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const = 0;
virtual bool isIgnored() const { return false; }
virtual bool isEmpty() const { return false; }
virtual void setGeometry(const QRectF &rect) = 0;
/*

View File

@ -42,29 +42,9 @@
#include "qgraphicslayoutitem_p.h"
#include "qgraphicslayout_p.h"
#include "qgraphicswidget.h"
#include <private/qgraphicswidget_p.h>
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)
{

View File

@ -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:

View File

@ -45,6 +45,7 @@
#include "qgraphicslayoutitem_p.h"
#include "qwidget.h"
#include "qgraphicswidget.h"
#include "qgraphicsitem_p.h"
#include <QtDebug>
@ -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

View File

@ -94,6 +94,7 @@ public:
virtual void updateGeometry();
virtual bool isEmpty() const;
QGraphicsLayoutItem *parentLayoutItem() const;
void setParentLayoutItem(QGraphicsLayoutItem *parent);