1
0
mirror of https://github.com/qgis/QGIS.git synced 2025-04-17 00:04:02 -04:00

Unit tests for item opacity

This commit is contained in:
Nyall Dawson 2018-12-10 15:32:50 +10:00
parent d08ecc2b52
commit a49bf9f68f
5 changed files with 63 additions and 9 deletions
tests
src
testdata/control_images/composer_effects
expected_composereffects_transparency
expected_composereffects_transparency35
expected_composereffects_transparency75

@ -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 @@ class TestQgsLayoutItem(unittest.TestCase):
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.

Before

(image error) Size: 5.7 KiB

After

(image error) Size: 5.7 KiB