Skip to content

Commit

Permalink
Ensure we correctly set qt item caching only when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 19, 2023
1 parent 7ec2083 commit 7e73b9b
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/layout/qgslayoutitem.sip.in
Expand Up @@ -280,6 +280,7 @@ Base class for graphical items within a :py:class:`QgsLayout`.
{
FlagOverridesPaint,
FlagProvidesClipPath,
FlagDisableSceneCaching,
};
typedef QFlags<QgsLayoutItem::Flag> Flags;

Expand Down
Expand Up @@ -127,6 +127,7 @@ Constructor for a QgsLayoutNodesItem with the given ``polygon`` nodes, attached

virtual void draw( QgsLayoutItemRenderContext &context );

virtual QgsLayoutItem::Flags itemFlags() const;

virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;

Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitem.cpp
Expand Up @@ -1579,7 +1579,7 @@ void QgsLayoutItem::refreshBlendMode()

// we can only enable caching if no blend mode is set -- otherwise
// we need to redraw the item every time it is painted
if ( mEvaluatedBlendMode == QPainter::CompositionMode_Source )
if ( mEvaluatedBlendMode == QPainter::CompositionMode_Source && !( itemFlags() & QgsLayoutItem::FlagDisableSceneCaching ) )
setCacheMode( QGraphicsItem::DeviceCoordinateCache );
else
setCacheMode( QGraphicsItem::NoCache );
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -334,6 +334,7 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
{
FlagOverridesPaint = 1 << 1, //!< Item overrides the default layout item painting method
FlagProvidesClipPath = 1 << 2, //!< Item can act as a clipping path provider (see clipPath())
FlagDisableSceneCaching = 1 << 3, //!< Item should not have QGraphicsItem caching enabled
};
Q_DECLARE_FLAGS( Flags, Flag )

Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitemelevationprofile.cpp
Expand Up @@ -446,7 +446,7 @@ void QgsLayoutItemElevationProfile::refreshDataDefinedProperty( DataDefinedPrope

QgsLayoutItem::Flags QgsLayoutItemElevationProfile::itemFlags() const
{
return QgsLayoutItem::FlagOverridesPaint;
return QgsLayoutItem::FlagOverridesPaint | QgsLayoutItem::FlagDisableSceneCaching;
}

bool QgsLayoutItemElevationProfile::requiresRasterization() const
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitemmap.cpp
Expand Up @@ -113,7 +113,7 @@ QIcon QgsLayoutItemMap::icon() const

QgsLayoutItem::Flags QgsLayoutItemMap::itemFlags() const
{
return QgsLayoutItem::FlagOverridesPaint;
return QgsLayoutItem::FlagOverridesPaint | QgsLayoutItem::FlagDisableSceneCaching;
}

void QgsLayoutItemMap::assignFreeId()
Expand Down
5 changes: 5 additions & 0 deletions src/core/layout/qgslayoutitemnodeitem.cpp
Expand Up @@ -85,6 +85,11 @@ void QgsLayoutNodesItem::draw( QgsLayoutItemRenderContext &context )
drawNodes( context );
}

QgsLayoutItem::Flags QgsLayoutNodesItem::itemFlags() const
{
return QgsLayoutItem::FlagDisableSceneCaching;
}

double QgsLayoutNodesItem::computeDistance( QPointF pt1,
QPointF pt2 ) const
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitemnodeitem.h
Expand Up @@ -127,7 +127,7 @@ class CORE_EXPORT QgsLayoutNodesItem: public QgsLayoutItem
QgsLayoutNodesItem( const QPolygonF &polygon, QgsLayout *layout );

void draw( QgsLayoutItemRenderContext &context ) override;

QgsLayoutItem::Flags itemFlags() const override;
bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;

Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgslayoutitem.cpp
Expand Up @@ -1933,19 +1933,25 @@ void TestQgsLayoutItem::blendMode()
QgsLayoutItemShape *item = new QgsLayoutItemShape( &l );
l.addLayoutItem( item );

QCOMPARE( item->cacheMode(), QGraphicsItem::DeviceCoordinateCache );

item->setBlendMode( QPainter::CompositionMode_Darken );
QCOMPARE( item->blendMode(), QPainter::CompositionMode_Darken );
QCOMPARE( item->blendModeForRender(), QPainter::CompositionMode_Darken );
// can't use caching when blend modes are active
QCOMPARE( item->cacheMode(), QGraphicsItem::NoCache );

l.renderContext().setFlag( QgsLayoutRenderContext::FlagUseAdvancedEffects, false );
QCOMPARE( item->blendModeForRender(), QPainter::CompositionMode_SourceOver );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagUseAdvancedEffects, true );
QCOMPARE( item->blendModeForRender(), QPainter::CompositionMode_Darken );
QCOMPARE( item->cacheMode(), QGraphicsItem::NoCache );

item->dataDefinedProperties().setProperty( QgsLayoutObject::BlendMode, QgsProperty::fromExpression( "'lighten'" ) );
item->refreshDataDefinedProperty();
QCOMPARE( item->blendMode(), QPainter::CompositionMode_Darken ); // should not change
QCOMPARE( item->blendModeForRender(), QPainter::CompositionMode_Lighten );
QCOMPARE( item->cacheMode(), QGraphicsItem::NoCache );

QgsLayout l2( QgsProject::instance() );
l2.initializeDefaults();
Expand Down

0 comments on commit 7e73b9b

Please sign in to comment.