Skip to content
Permalink
Browse files

Respect clip region when rendering diagrams

  • Loading branch information
nyalldawson committed Jul 2, 2020
1 parent e028067 commit 4e815b92d389d16f98ce23ba22bffd697a7d6fed
@@ -173,6 +173,10 @@ void QgsVectorLayerDiagramProvider::registerFeature( QgsFeature &feature, QgsRen
mFeatures << label;
}

void QgsVectorLayerDiagramProvider::setClipFeatureGeometry( const QgsGeometry &geometry )
{
mLabelClipFeatureGeom = geometry;
}

QgsLabelFeature *QgsVectorLayerDiagramProvider::registerDiagram( QgsFeature &feat, QgsRenderContext &context, const QgsGeometry &obstacleGeometry )
{
@@ -214,23 +218,22 @@ QgsLabelFeature *QgsVectorLayerDiagramProvider::registerDiagram( QgsFeature &fea
extentGeom.rotate( -mapSettings.rotation(), mapSettings.visibleExtent().center() );
}

geos::unique_ptr geomCopy;
std::unique_ptr<QgsGeometry> scopedPreparedGeom;
if ( QgsPalLabeling::geometryRequiresPreparation( geom, context, mSettings.coordinateTransform(), extentGeom ) )
{
scopedPreparedGeom.reset( new QgsGeometry( QgsPalLabeling::prepareGeometry( geom, context, mSettings.coordinateTransform(), extentGeom ) ) );
QgsGeometry *preparedGeom = scopedPreparedGeom.get();
if ( preparedGeom->isNull() )
return nullptr;
geomCopy = QgsGeos::asGeos( *preparedGeom );
geom = QgsPalLabeling::prepareGeometry( geom, context, mSettings.coordinateTransform(), extentGeom );
}
else
if ( geom.isEmpty() )
return nullptr;

const QgsGeometry clipGeometry = mLabelClipFeatureGeom.isNull() ? context.featureClipGeometry() : mLabelClipFeatureGeom;
if ( !clipGeometry.isEmpty() )
{
geomCopy = QgsGeos::asGeos( geom );
const QgsWkbTypes::GeometryType expectedType = geom.type();
geom = geom.intersection( clipGeometry );
geom.convertGeometryCollectionToSubclass( expectedType );
}

if ( !geomCopy )
return nullptr; // invalid geometry
if ( geom.isEmpty() )
return nullptr;

QgsGeometry preparedObstacleGeom;
if ( isObstacle && !obstacleGeometry.isNull() && QgsPalLabeling::geometryRequiresPreparation( obstacleGeometry, context, mSettings.coordinateTransform(), extentGeom ) )
@@ -287,7 +290,7 @@ QgsLabelFeature *QgsVectorLayerDiagramProvider::registerDiagram( QgsFeature &fea
}
}

QgsDiagramLabelFeature *lf = new QgsDiagramLabelFeature( feat.id(), std::move( geomCopy ), QSizeF( diagramWidth, diagramHeight ) );
QgsDiagramLabelFeature *lf = new QgsDiagramLabelFeature( feat.id(), QgsGeos::asGeos( geom ), QSizeF( diagramWidth, diagramHeight ) );
lf->setHasFixedPosition( ddPos );
lf->setFixedPosition( QgsPointXY( ddPosX, ddPosY ) );
lf->setHasFixedAngle( true );
@@ -342,3 +345,4 @@ QgsLabelFeature *QgsVectorLayerDiagramProvider::registerDiagram( QgsFeature &fea
lf->setDistLabel( dist );
return lf;
}

@@ -97,6 +97,13 @@ class CORE_EXPORT QgsVectorLayerDiagramProvider : public QgsAbstractLabelProvide
*/
virtual void registerFeature( QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry = QgsGeometry() );

/**
* Sets a \a geometry to use to clip features to when registering them as diagrams.
*
* \since QGIS 3.16
*/
void setClipFeatureGeometry( const QgsGeometry &geometry );

protected:
//! initialization method - called from constructors
void init();
@@ -123,6 +130,8 @@ class CORE_EXPORT QgsVectorLayerDiagramProvider : public QgsAbstractLabelProvide

//! List of generated label features (owned by the provider)
QList<QgsLabelFeature *> mFeatures;

QgsGeometry mLabelClipFeatureGeom;
};

#endif // QGSVECTORLAYERDIAGRAMPROVIDER_H
@@ -196,6 +196,9 @@ bool QgsVectorLayerRenderer::render()
context.painter()->setClipPath( path, Qt::IntersectClip );

mLabelClipFeatureGeom = QgsMapClippingUtils::calculateLabelIntersectionGeometry( mClippingRegions, context, mApplyLabelClipGeometries );

if ( mDiagramProvider )
mDiagramProvider->setClipFeatureGeometry( mLabelClipFeatureGeom );
}
mRenderer->modifyRequestExtent( requestExtent, context );

@@ -894,6 +894,63 @@ class TestQgsDiagram : public QObject
QVERIFY( imageCheck( "textdiagram_datadefined_background" ) );
}

void testClipping()
{
const QString filename = QStringLiteral( TEST_DATA_DIR ) + "/lines.shp";
std::unique_ptr< QgsVectorLayer> vl2( new QgsVectorLayer( filename, QStringLiteral( "lines" ), QStringLiteral( "ogr" ) ) );

QgsStringMap props;
props.insert( QStringLiteral( "outline_color" ), QStringLiteral( "#487bb6" ) );
props.insert( QStringLiteral( "outline_width" ), QStringLiteral( "1" ) );
std::unique_ptr< QgsLineSymbol > symbol( QgsLineSymbol::createSimple( props ) );
vl2->setRenderer( new QgsSingleSymbolRenderer( symbol.release() ) );

QgsDiagramSettings ds;
QColor col1 = Qt::red;
QColor col2 = Qt::yellow;
col1.setAlphaF( 0.5 );
col2.setAlphaF( 0.5 );
ds.categoryColors = QList<QColor>() << col1;
ds.categoryAttributes = QList<QString>() << QStringLiteral( "\"Value\"" );
ds.minimumScale = -1;
ds.maximumScale = -1;
ds.minimumSize = 0;
ds.penColor = Qt::green;
ds.penWidth = .5;
ds.scaleByArea = true;
ds.sizeType = QgsUnitTypes::RenderMillimeters;
ds.size = QSizeF( 5, 5 );
ds.rotationOffset = 0;

QgsSingleCategoryDiagramRenderer *dr = new QgsSingleCategoryDiagramRenderer();
dr->setDiagram( new QgsPieDiagram() );
dr->setDiagramSettings( ds );
vl2->setDiagramRenderer( dr );

QgsDiagramLayerSettings dls = QgsDiagramLayerSettings();
dls.setPlacement( QgsDiagramLayerSettings::Line );
dls.setShowAllDiagrams( true );
vl2->setDiagramLayerSettings( dls );

mMapSettings->setLayers( QList<QgsMapLayer *>() << vl2.get() );

QgsMapClippingRegion region1( QgsGeometry::fromWkt( "Polygon ((-92 45, -99 36, -94 29, -82 29, -81 45, -92 45))" ) );
region1.setFeatureClip( QgsMapClippingRegion::FeatureClippingType::Intersect );
mMapSettings->addClippingRegion( region1 );

QgsMapClippingRegion region2( QgsGeometry::fromWkt( "Polygon ((-85 36, -85 46, -107 47, -108 28, -85 28, -85 36))" ) );
region2.setFeatureClip( QgsMapClippingRegion::FeatureClippingType::PainterClip );
mMapSettings->addClippingRegion( region2 );

const bool res = imageCheck( QStringLiteral( "diagram_clipping" ) );
mMapSettings->setClippingRegions( QList< QgsMapClippingRegion >() );
mMapSettings->setLayers( QList<QgsMapLayer *>() << mPointsLayer );

QVERIFY( res );
}



};

bool TestQgsDiagram::imageCheck( const QString &testType )
Binary file not shown.

0 comments on commit 4e815b9

Please sign in to comment.
You can’t perform that action at this time.