Skip to content
Permalink
Browse files

Unit tests for item opacity

  • Loading branch information
nyalldawson committed Dec 10, 2018
1 parent d08ecc2 commit a49bf9f68f87e8b58fc7bf47c40fe8c246e927fc
@@ -1945,31 +1945,46 @@ void TestQgsLayoutItem::opacity()
{
QgsProject proj;
QgsLayout l( &proj );
l.initializeDefaults();

QgsSimpleFillSymbolLayer *simpleFill = new QgsSimpleFillSymbolLayer();
QgsFillSymbol *fillSymbol = new QgsFillSymbol();
fillSymbol->changeSymbolLayer( 0, simpleFill );
simpleFill->setColor( QColor( 255, 150, 0 ) );
simpleFill->setStrokeColor( Qt::black );

QgsLayoutItemShape *item = new QgsLayoutItemShape( &l );
item->setShapeType( QgsLayoutItemShape::Rectangle );
item->attemptSetSceneRect( QRectF( 50, 50, 150, 100 ) );
item->setSymbol( fillSymbol->clone() );

l.addLayoutItem( item );

item->setItemOpacity( 0.75 );
QCOMPARE( item->itemOpacity(), 0.75 );
QCOMPARE( item->opacity(), 0.75 );

// we handle opacity ourselves, so QGraphicsItem opacity should never be set
QCOMPARE( item->opacity(), 1.0 );

QgsLayoutChecker checker( QStringLiteral( "composereffects_transparency75" ), &l );
checker.setControlPathPrefix( QStringLiteral( "composer_effects" ) );
QVERIFY( checker.testLayout( mReport ) );

item->dataDefinedProperties().setProperty( QgsLayoutObject::Opacity, QgsProperty::fromExpression( "35" ) );
item->refreshDataDefinedProperty();
QCOMPARE( item->itemOpacity(), 0.75 ); // should not change
QCOMPARE( item->opacity(), 0.35 );
QCOMPARE( item->opacity(), 1.0 );

checker = QgsLayoutChecker( QStringLiteral( "composereffects_transparency35" ), &l );
checker.setControlPathPrefix( QStringLiteral( "composer_effects" ) );
QVERIFY( checker.testLayout( mReport ) );

QgsLayout l2( QgsProject::instance() );
l2.initializeDefaults();
QgsLayoutItemShape *mComposerRect1 = new QgsLayoutItemShape( &l2 );
mComposerRect1->attemptSetSceneRect( QRectF( 20, 20, 150, 100 ) );
mComposerRect1->setShapeType( QgsLayoutItemShape::Rectangle );
QgsSimpleFillSymbolLayer *simpleFill = new QgsSimpleFillSymbolLayer();
QgsFillSymbol *fillSymbol = new QgsFillSymbol();
fillSymbol->changeSymbolLayer( 0, simpleFill );
simpleFill->setColor( QColor( 255, 150, 0 ) );
simpleFill->setStrokeColor( Qt::black );
mComposerRect1->setSymbol( fillSymbol );
mComposerRect1->setSymbol( fillSymbol->clone() );
delete fillSymbol;

l2.addLayoutItem( mComposerRect1 );
@@ -1987,7 +2002,7 @@ void TestQgsLayoutItem::opacity()

mComposerRect2->setItemOpacity( 0.5 );

QgsLayoutChecker checker( QStringLiteral( "composereffects_transparency" ), &l2 );
checker = QgsLayoutChecker( QStringLiteral( "composereffects_transparency" ), &l2 );
checker.setControlPathPrefix( QStringLiteral( "composer_effects" ) );
QVERIFY( checker.testLayout( mReport ) );
}
@@ -199,6 +199,45 @@ def testCasting(self):
label2 = [i for i in items if isinstance(i, QgsLayoutItem) and i.id() == 'label'][0]
self.assertIsInstance(label2, QgsLayoutItemLabel)

def testContainsAdvancedEffectsAndRasterization(self):
layout = QgsLayout(QgsProject.instance())
item = QgsLayoutItemLabel(layout)

self.assertFalse(item.containsAdvancedEffects())

# item opacity requires that the individual item be flattened to a raster item
item.setItemOpacity(0.5)
self.assertTrue(item.containsAdvancedEffects())
# but not the WHOLE layout
self.assertFalse(item.requiresRasterization())
item.dataDefinedProperties().setProperty(QgsLayoutObject.Opacity, QgsProperty.fromExpression('100'))
item.refresh()
self.assertFalse(item.containsAdvancedEffects())
self.assertFalse(item.requiresRasterization())
item.dataDefinedProperties().setProperty(QgsLayoutObject.Opacity, QgsProperty())
item.refresh()
self.assertTrue(item.containsAdvancedEffects())
self.assertFalse(item.requiresRasterization())
item.setItemOpacity(1.0)
self.assertFalse(item.containsAdvancedEffects())
self.assertFalse(item.requiresRasterization())

# item blend mode is NOT an advanced effect -- rather it requires that the WHOLE layout be rasterized to achieve
item.setBlendMode(QPainter.CompositionMode_DestinationAtop)
self.assertFalse(item.containsAdvancedEffects())
self.assertTrue(item.requiresRasterization())

map = QgsLayoutItemMap(layout)
# map items are different -- because they override paint, they don't get the auto-flattening and rasterization
map.setItemOpacity(0.5)
self.assertFalse(map.containsAdvancedEffects())
# rather, a map with opacity requires the WHOLE layout to be rasterized
self.assertTrue(map.requiresRasterization())
map.dataDefinedProperties().setProperty(QgsLayoutObject.Opacity, QgsProperty.fromExpression('100'))
map.refresh()
self.assertFalse(map.containsAdvancedEffects())
self.assertTrue(map.requiresRasterization())


if __name__ == '__main__':
unittest.main()
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit a49bf9f

Please sign in to comment.
You can’t perform that action at this time.