Skip to content
Permalink
Browse files

Allow drawing QgsRubberBand polygons using a fill symbol

  • Loading branch information
nyalldawson committed Mar 29, 2021
1 parent 07673fb commit d68cd295db00331e03843d0fa3d7c90662578382
@@ -363,7 +363,7 @@ Ownership of ``symbol`` is transferred to the rubberband.

.. warning::

Only line symbols are currently supported.
Only line and fill symbols are currently supported.

.. note::

@@ -468,14 +468,28 @@ void QgsRubberBand::paint( QPainter *p )
lineSymbol->startRender( context );
for ( const QVector<QPolygonF> &shape : std::as_const( shapes ) )
{
drawShape( p, shape );
for ( const QPolygonF &ring : shape )
{
lineSymbol->renderPolyline( ring, nullptr, context );
}
}
lineSymbol->stopRender( context );
}
else if ( QgsFillSymbol *fillSymbol = dynamic_cast< QgsFillSymbol * >( mSymbol.get() ) )
{
QgsRenderContext context( QgsRenderContext::fromQPainter( p ) );
context.setFlag( QgsRenderContext::Antialiasing, true );

fillSymbol->startRender( context );
for ( const QVector<QPolygonF> &shape : std::as_const( shapes ) )
{
for ( const QPolygonF &ring : shape )
{
fillSymbol->renderPolygon( ring, nullptr, nullptr, context );
}
}
fillSymbol->stopRender( context );
}
else
{
int iterations = mSecondaryPen.color().isValid() ? 2 : 1;
@@ -400,7 +400,7 @@ class GUI_EXPORT QgsRubberBand : public QgsMapCanvasItem
*
* Ownership of \a symbol is transferred to the rubberband.
*
* \warning Only line symbols are currently supported.
* \warning Only line and fill symbols are currently supported.
*
* \note Setting a symbol for the rubberband overrides any other appearance setting,
* such as the strokeColor() or width().
@@ -44,7 +44,8 @@ class TestQgsRubberband : public QObject
void testBoundingRect(); //test for #12392
void testVisibility(); //test for 12486
void testClose(); //test closing geometry
void testSymbolRender();
void testLineSymbolRender();
void testFillSymbolRender();

private:
QgsMapCanvas *mCanvas = nullptr;
@@ -230,7 +231,7 @@ void TestQgsRubberband::testClose()
QCOMPARE( r.partSize( 0 ), 4 );
}

void TestQgsRubberband::testSymbolRender()
void TestQgsRubberband::testLineSymbolRender()
{
std::unique_ptr< QgsMapCanvas > canvas = std::make_unique< QgsMapCanvas >();
canvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
@@ -266,6 +267,43 @@ void TestQgsRubberband::testSymbolRender()
QVERIFY( result );
}

void TestQgsRubberband::testFillSymbolRender()
{
std::unique_ptr< QgsMapCanvas > canvas = std::make_unique< QgsMapCanvas >();
canvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
canvas->setFrameStyle( 0 );
canvas->resize( 600, 400 );
canvas->setExtent( QgsRectangle( 10, 30, 20, 35 ) );
canvas->show();

QgsRubberBand r( canvas.get(), QgsWkbTypes::LineGeometry );
r.addGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((12 32, 12 35, 18 35, 12 32))" ) ) );

std::unique_ptr< QgsFillSymbol > fillSymbol( QgsFillSymbol::createSimple(
{
{ QStringLiteral( "color" ), QStringLiteral( "#ff00ff" ) },
{ QStringLiteral( "line_color" ), QStringLiteral( "#0000ff" ) },
{ QStringLiteral( "line_width" ), QStringLiteral( "3" )},
{ QStringLiteral( "joinstyle" ), QStringLiteral( "round" )}
} ) );
r.setSymbol( fillSymbol.release() );

QPixmap pixmap( canvas->size() );
QPainter painter( &pixmap );
canvas->render( &painter );
painter.end();
QString destFile = QDir::tempPath() + QStringLiteral( "/rubberband_fill_symbol.png" );
pixmap.save( destFile );

QgsRenderChecker checker;
checker.setControlPathPrefix( QStringLiteral( "rubberband" ) );
checker.setControlName( QStringLiteral( "expected_fill_symbol" ) );
checker.setRenderedImage( destFile );
bool result = checker.compareImages( QStringLiteral( "expected_fill_symbol" ) );
mReport += checker.report();
QVERIFY( result );
}


QGSTEST_MAIN( TestQgsRubberband )
#include "testqgsrubberband.moc"
Binary file not shown.

0 comments on commit d68cd29

Please sign in to comment.