Skip to content

Commit a49bf9f

Browse files
committedDec 10, 2018
Unit tests for item opacity
1 parent d08ecc2 commit a49bf9f

File tree

5 files changed

+63
-9
lines changed

5 files changed

+63
-9
lines changed
 

‎tests/src/core/testqgslayoutitem.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,31 +1945,46 @@ void TestQgsLayoutItem::opacity()
19451945
{
19461946
QgsProject proj;
19471947
QgsLayout l( &proj );
1948+
l.initializeDefaults();
1949+
1950+
QgsSimpleFillSymbolLayer *simpleFill = new QgsSimpleFillSymbolLayer();
1951+
QgsFillSymbol *fillSymbol = new QgsFillSymbol();
1952+
fillSymbol->changeSymbolLayer( 0, simpleFill );
1953+
simpleFill->setColor( QColor( 255, 150, 0 ) );
1954+
simpleFill->setStrokeColor( Qt::black );
19481955

19491956
QgsLayoutItemShape *item = new QgsLayoutItemShape( &l );
1957+
item->setShapeType( QgsLayoutItemShape::Rectangle );
1958+
item->attemptSetSceneRect( QRectF( 50, 50, 150, 100 ) );
1959+
item->setSymbol( fillSymbol->clone() );
1960+
19501961
l.addLayoutItem( item );
19511962

19521963
item->setItemOpacity( 0.75 );
19531964
QCOMPARE( item->itemOpacity(), 0.75 );
1954-
QCOMPARE( item->opacity(), 0.75 );
1965+
1966+
// we handle opacity ourselves, so QGraphicsItem opacity should never be set
1967+
QCOMPARE( item->opacity(), 1.0 );
1968+
1969+
QgsLayoutChecker checker( QStringLiteral( "composereffects_transparency75" ), &l );
1970+
checker.setControlPathPrefix( QStringLiteral( "composer_effects" ) );
1971+
QVERIFY( checker.testLayout( mReport ) );
19551972

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

1978+
checker = QgsLayoutChecker( QStringLiteral( "composereffects_transparency35" ), &l );
1979+
checker.setControlPathPrefix( QStringLiteral( "composer_effects" ) );
1980+
QVERIFY( checker.testLayout( mReport ) );
19611981

19621982
QgsLayout l2( QgsProject::instance() );
19631983
l2.initializeDefaults();
19641984
QgsLayoutItemShape *mComposerRect1 = new QgsLayoutItemShape( &l2 );
19651985
mComposerRect1->attemptSetSceneRect( QRectF( 20, 20, 150, 100 ) );
19661986
mComposerRect1->setShapeType( QgsLayoutItemShape::Rectangle );
1967-
QgsSimpleFillSymbolLayer *simpleFill = new QgsSimpleFillSymbolLayer();
1968-
QgsFillSymbol *fillSymbol = new QgsFillSymbol();
1969-
fillSymbol->changeSymbolLayer( 0, simpleFill );
1970-
simpleFill->setColor( QColor( 255, 150, 0 ) );
1971-
simpleFill->setStrokeColor( Qt::black );
1972-
mComposerRect1->setSymbol( fillSymbol );
1987+
mComposerRect1->setSymbol( fillSymbol->clone() );
19731988
delete fillSymbol;
19741989

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

19882003
mComposerRect2->setItemOpacity( 0.5 );
19892004

1990-
QgsLayoutChecker checker( QStringLiteral( "composereffects_transparency" ), &l2 );
2005+
checker = QgsLayoutChecker( QStringLiteral( "composereffects_transparency" ), &l2 );
19912006
checker.setControlPathPrefix( QStringLiteral( "composer_effects" ) );
19922007
QVERIFY( checker.testLayout( mReport ) );
19932008
}

‎tests/src/python/test_qgslayoutitem.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,45 @@ def testCasting(self):
199199
label2 = [i for i in items if isinstance(i, QgsLayoutItem) and i.id() == 'label'][0]
200200
self.assertIsInstance(label2, QgsLayoutItemLabel)
201201

202+
def testContainsAdvancedEffectsAndRasterization(self):
203+
layout = QgsLayout(QgsProject.instance())
204+
item = QgsLayoutItemLabel(layout)
205+
206+
self.assertFalse(item.containsAdvancedEffects())
207+
208+
# item opacity requires that the individual item be flattened to a raster item
209+
item.setItemOpacity(0.5)
210+
self.assertTrue(item.containsAdvancedEffects())
211+
# but not the WHOLE layout
212+
self.assertFalse(item.requiresRasterization())
213+
item.dataDefinedProperties().setProperty(QgsLayoutObject.Opacity, QgsProperty.fromExpression('100'))
214+
item.refresh()
215+
self.assertFalse(item.containsAdvancedEffects())
216+
self.assertFalse(item.requiresRasterization())
217+
item.dataDefinedProperties().setProperty(QgsLayoutObject.Opacity, QgsProperty())
218+
item.refresh()
219+
self.assertTrue(item.containsAdvancedEffects())
220+
self.assertFalse(item.requiresRasterization())
221+
item.setItemOpacity(1.0)
222+
self.assertFalse(item.containsAdvancedEffects())
223+
self.assertFalse(item.requiresRasterization())
224+
225+
# item blend mode is NOT an advanced effect -- rather it requires that the WHOLE layout be rasterized to achieve
226+
item.setBlendMode(QPainter.CompositionMode_DestinationAtop)
227+
self.assertFalse(item.containsAdvancedEffects())
228+
self.assertTrue(item.requiresRasterization())
229+
230+
map = QgsLayoutItemMap(layout)
231+
# map items are different -- because they override paint, they don't get the auto-flattening and rasterization
232+
map.setItemOpacity(0.5)
233+
self.assertFalse(map.containsAdvancedEffects())
234+
# rather, a map with opacity requires the WHOLE layout to be rasterized
235+
self.assertTrue(map.requiresRasterization())
236+
map.dataDefinedProperties().setProperty(QgsLayoutObject.Opacity, QgsProperty.fromExpression('100'))
237+
map.refresh()
238+
self.assertFalse(map.containsAdvancedEffects())
239+
self.assertTrue(map.requiresRasterization())
240+
202241

203242
if __name__ == '__main__':
204243
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.