Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow drawing QgsRubberBand polygons using a fill symbol
  • Loading branch information
nyalldawson committed Mar 29, 2021
1 parent 07673fb commit d68cd29
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion python/gui/auto_generated/qgsrubberband.sip.in
Expand Up @@ -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::

Expand Down
16 changes: 15 additions & 1 deletion src/gui/qgsrubberband.cpp
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsrubberband.h
Expand Up @@ -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().
Expand Down
42 changes: 40 additions & 2 deletions tests/src/gui/testqgsrubberband.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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" ) ) );
Expand Down Expand Up @@ -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"
Expand Down
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 d68cd29

Please sign in to comment.