Skip to content

Commit

Permalink
Unit tests for item opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 10, 2018
1 parent d08ecc2 commit a49bf9f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
33 changes: 24 additions & 9 deletions tests/src/core/testqgslayoutitem.cpp
Expand Up @@ -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 );
Expand All @@ -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 ) );
}
Expand Down
39 changes: 39 additions & 0 deletions tests/src/python/test_qgslayoutitem.py
Expand Up @@ -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()
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a49bf9f

Please sign in to comment.