Skip to content

Commit

Permalink
[composer] Make some set symbol methods clone symbol, nicer API
Browse files Browse the repository at this point in the history
for PyQGIS (fix #13304)
  • Loading branch information
nyalldawson committed Sep 3, 2015
1 parent 7ac8f41 commit 166e5bf
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 51 deletions.
2 changes: 1 addition & 1 deletion python/core/composer/qgscomposershape.sip
Expand Up @@ -47,7 +47,7 @@ class QgsComposerShape: QgsComposerItem
/** Sets the QgsFillSymbolV2 used to draw the shape. Must also call setUseSymbolV2( true ) to
* enable drawing with a symbol.
* Note: added in version 2.1*/
void setShapeStyleSymbol( QgsFillSymbolV2* symbol /Transfer/ );
void setShapeStyleSymbol( QgsFillSymbolV2* symbol );
/** Returns the QgsFillSymbolV2 used to draw the shape.
* Note: added in version 2.1*/
QgsFillSymbolV2* shapeStyleSymbol();
Expand Down
2 changes: 1 addition & 1 deletion python/core/composer/qgscomposition.sip
Expand Up @@ -115,7 +115,7 @@ class QgsComposition : QGraphicsScene
bool shouldExportPage( const int page ) const;

/** Note: added in version 2.1*/
void setPageStyleSymbol( QgsFillSymbolV2* symbol /Transfer/ );
void setPageStyleSymbol( QgsFillSymbolV2* symbol );
/** Note: added in version 2.1*/
QgsFillSymbolV2* pageStyleSymbol();

Expand Down
5 changes: 1 addition & 4 deletions src/app/composer/qgscomposershapewidget.cpp
Expand Up @@ -117,10 +117,7 @@ void QgsComposerShapeWidget::on_mShapeStyleButton_clicked()
updateShapeStyle();
mComposerShape->endCommand();
}
else
{
delete newSymbol;
}
delete newSymbol;
}

void QgsComposerShapeWidget::updateShapeStyle()
Expand Down
5 changes: 1 addition & 4 deletions src/app/composer/qgscompositionwidget.cpp
Expand Up @@ -563,10 +563,7 @@ void QgsCompositionWidget::on_mPageStyleButton_clicked()
mComposition->setPageStyleSymbol( newSymbol );
updatePageStyle();
}
else
{
delete newSymbol;
}
delete newSymbol;
}

void QgsCompositionWidget::updatePageStyle()
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposershape.cpp
Expand Up @@ -74,7 +74,7 @@ void QgsComposerShape::setUseSymbolV2( bool useSymbolV2 )
void QgsComposerShape::setShapeStyleSymbol( QgsFillSymbolV2* symbol )
{
delete mShapeStyleSymbol;
mShapeStyleSymbol = symbol;
mShapeStyleSymbol = static_cast<QgsFillSymbolV2*>( symbol->clone() );
refreshSymbol();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposition.cpp
Expand Up @@ -492,7 +492,7 @@ bool QgsComposition::shouldExportPage( const int page ) const
void QgsComposition::setPageStyleSymbol( QgsFillSymbolV2* symbol )
{
delete mPageStyleSymbol;
mPageStyleSymbol = symbol;
mPageStyleSymbol = static_cast<QgsFillSymbolV2*>( symbol->clone() );
QgsProject::instance()->dirty( true );
}

Expand Down
48 changes: 23 additions & 25 deletions tests/src/core/testqgscomposerpaper.cpp
Expand Up @@ -36,10 +36,6 @@ class TestQgsComposerPaper : public QObject
public:
TestQgsComposerPaper()
: mComposition( 0 )
, mSimpleFill( 0 )
, mMarkerLine( 0 )
, mFillSymbol( 0 )
, mMarkerLineSymbol( 0 )
, mMapSettings( 0 )
{}

Expand All @@ -56,10 +52,6 @@ class TestQgsComposerPaper : public QObject
private:
QgsComposition* mComposition;
QString mReport;
QgsSimpleFillSymbolLayerV2* mSimpleFill;
QgsMarkerLineSymbolLayerV2* mMarkerLine;
QgsFillSymbolV2* mFillSymbol;
QgsFillSymbolV2* mMarkerLineSymbol;
QgsMapSettings *mMapSettings;
// QgsSingleSymbolRendererV2* mSymbolRenderer;

Expand All @@ -75,16 +67,6 @@ void TestQgsComposerPaper::initTestCase()
mComposition = new QgsComposition( *mMapSettings );
mComposition->setPaperSize( 297, 210 ); //A4 landscape

//setup simple fill
mSimpleFill = new QgsSimpleFillSymbolLayerV2();
mFillSymbol = new QgsFillSymbolV2();
mFillSymbol->changeSymbolLayer( 0, mSimpleFill );

//setup marker line fill
mMarkerLine = new QgsMarkerLineSymbolLayerV2();
mMarkerLineSymbol = new QgsFillSymbolV2();
mMarkerLineSymbol->changeSymbolLayer( 0, mMarkerLine );

mReport = "<h1>Composer Paper Tests</h1>\n";
}

Expand Down Expand Up @@ -123,27 +105,43 @@ void TestQgsComposerPaper::defaultPaper()

void TestQgsComposerPaper::transparentPaper()
{
mSimpleFill->setColor( Qt::transparent );
mSimpleFill->setBorderColor( Qt::transparent );
mComposition->setPageStyleSymbol( mFillSymbol );
QgsSimpleFillSymbolLayerV2* simpleFill = new QgsSimpleFillSymbolLayerV2();
QgsFillSymbolV2* fillSymbol = new QgsFillSymbolV2();
fillSymbol->changeSymbolLayer( 0, simpleFill );
simpleFill->setColor( Qt::transparent );
simpleFill->setBorderColor( Qt::transparent );
mComposition->setPageStyleSymbol( fillSymbol );
delete fillSymbol;

QgsCompositionChecker checker( "composerpaper_transparent", mComposition );
checker.setControlPathPrefix( "composer_paper" );
QVERIFY( checker.testComposition( mReport ) );
}

void TestQgsComposerPaper::borderedPaper()
{
mSimpleFill->setColor( Qt::white );
mSimpleFill->setBorderColor( Qt::black );
mSimpleFill->setBorderWidth( 6 );
QgsSimpleFillSymbolLayerV2* simpleFill = new QgsSimpleFillSymbolLayerV2();
QgsFillSymbolV2* fillSymbol = new QgsFillSymbolV2();
fillSymbol->changeSymbolLayer( 0, simpleFill );
simpleFill->setColor( Qt::white );
simpleFill->setBorderColor( Qt::black );
simpleFill->setBorderWidth( 6 );
mComposition->setPageStyleSymbol( fillSymbol );
delete fillSymbol;

QgsCompositionChecker checker( "composerpaper_bordered", mComposition );
checker.setControlPathPrefix( "composer_paper" );
QVERIFY( checker.testComposition( mReport ) );
}

void TestQgsComposerPaper::markerLinePaper()
{
mComposition->setPageStyleSymbol( mMarkerLineSymbol );
QgsMarkerLineSymbolLayerV2* markerLine = new QgsMarkerLineSymbolLayerV2();
QgsFillSymbolV2* markerLineSymbol = new QgsFillSymbolV2();
markerLineSymbol->changeSymbolLayer( 0, markerLine );
mComposition->setPageStyleSymbol( markerLineSymbol );
delete markerLineSymbol;

QgsCompositionChecker checker( "composerpaper_markerborder", mComposition );
checker.setControlPathPrefix( "composer_paper" );
QVERIFY( checker.testComposition( mReport, 0, 0 ) );
Expand Down
24 changes: 10 additions & 14 deletions tests/src/core/testqgscomposershapes.cpp
Expand Up @@ -37,8 +37,6 @@ class TestQgsComposerShapes : public QObject
: mComposition( 0 )
, mComposerShape( 0 )
, mMapSettings( 0 )
, mSimpleFill( 0 )
, mFillSymbol( 0 )
{}

private slots:
Expand All @@ -56,8 +54,6 @@ class TestQgsComposerShapes : public QObject
QgsComposition* mComposition;
QgsComposerShape* mComposerShape;
QgsMapSettings *mMapSettings;
QgsSimpleFillSymbolLayerV2* mSimpleFill;
QgsFillSymbolV2* mFillSymbol;
QString mReport;
};

Expand All @@ -75,11 +71,6 @@ void TestQgsComposerShapes::initTestCase()
mComposerShape->setBackgroundColor( QColor::fromRgb( 255, 150, 0 ) );
mComposition->addComposerShape( mComposerShape );

//setup simple fill
mSimpleFill = new QgsSimpleFillSymbolLayerV2();
mFillSymbol = new QgsFillSymbolV2();
mFillSymbol->changeSymbolLayer( 0, mSimpleFill );

mReport = "<h1>Composer Shape Tests</h1>\n";
}

Expand Down Expand Up @@ -151,12 +142,17 @@ void TestQgsComposerShapes::symbolV2()
{
mComposerShape->setShapeType( QgsComposerShape::Rectangle );

mSimpleFill->setColor( Qt::green );
mSimpleFill->setBorderColor( Qt::yellow );
mSimpleFill->setBorderWidth( 6 );

mComposerShape->setShapeStyleSymbol( mFillSymbol );
//setup simple fill
QgsSimpleFillSymbolLayerV2* simpleFill = new QgsSimpleFillSymbolLayerV2();
QgsFillSymbolV2* fillSymbol = new QgsFillSymbolV2();
fillSymbol->changeSymbolLayer( 0, simpleFill );
simpleFill->setColor( Qt::green );
simpleFill->setBorderColor( Qt::yellow );
simpleFill->setBorderWidth( 6 );

mComposerShape->setShapeStyleSymbol( fillSymbol );
mComposerShape->setUseSymbolV2( true );
delete fillSymbol;

QgsCompositionChecker checker( "composershapes_symbolv2", mComposition );
checker.setControlPathPrefix( "composer_shapes" );
Expand Down

0 comments on commit 166e5bf

Please sign in to comment.