Skip to content

Commit f143507

Browse files
committed
Fix for #6315, and keeps centroid calc. for whole feature in adv labeling
1 parent 595d2a5 commit f143507

4 files changed

Lines changed: 507 additions & 329 deletions

File tree

‎src/app/qgslabelinggui.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,17 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
9999
int distUnitIndex = lyr.distInMapUnits ? 1 : 0;
100100
mXQuadOffset = lyr.xQuadOffset;
101101
mYQuadOffset = lyr.yQuadOffset;
102+
mCentroidRadioWhole->setChecked( lyr.centroidWhole );
103+
mCentroidFrame->setVisible( false );
102104
switch ( lyr.placement )
103105
{
104106
case QgsPalLayerSettings::AroundPoint:
105107
radAroundPoint->setChecked( true );
106108
radAroundCentroid->setChecked( true );
107109
spinDistPoint->setValue( lyr.dist );
108110
mPointDistanceUnitComboBox->setCurrentIndex( distUnitIndex );
111+
mCentroidFrame->setVisible( layer->geometryType() == QGis::Polygon );
112+
109113
//spinAngle->setValue( lyr.angle );
110114
break;
111115
case QgsPalLayerSettings::OverPoint:
@@ -126,6 +130,7 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
126130
mPointOffsetYOffsetSpinBox->setValue( lyr.yOffset );
127131
mPointOffsetUnitsComboBox->setCurrentIndex( lyr.labelOffsetInMapUnits ? 1 : 0 );
128132
mPointOffsetAngleSpinBox->setValue( lyr.angleOffset );
133+
mCentroidFrame->setVisible( layer->geometryType() == QGis::Polygon );
129134

130135
break;
131136
case QgsPalLayerSettings::Line:
@@ -290,6 +295,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
290295
lyr.dist = 0;
291296
lyr.placementFlags = 0;
292297

298+
lyr.centroidWhole = mCentroidRadioWhole->isChecked();
293299
if (( stackedPlacement->currentWidget() == pagePoint && radAroundPoint->isChecked() )
294300
|| ( stackedPlacement->currentWidget() == pagePolygon && radAroundCentroid->isChecked() ) )
295301
{
@@ -820,15 +826,20 @@ void QgsLabelingGui::changeBufferColor()
820826

821827
void QgsLabelingGui::updateOptions()
822828
{
829+
mCentroidFrame->setVisible( false );
823830
if (( stackedPlacement->currentWidget() == pagePoint && radAroundPoint->isChecked() )
824831
|| ( stackedPlacement->currentWidget() == pagePolygon && radAroundCentroid->isChecked() ) )
825832
{
826833
stackedOptions->setCurrentWidget( pageOptionsPoint );
834+
mCentroidFrame->setVisible( stackedPlacement->currentWidget() == pagePolygon
835+
&& radAroundCentroid->isChecked() );
827836
}
828837
else if (( stackedPlacement->currentWidget() == pagePoint && radOverPoint->isChecked() )
829838
|| ( stackedPlacement->currentWidget() == pagePolygon && radOverCentroid->isChecked() ) )
830839
{
831840
stackedOptions->setCurrentWidget( pageOptionsPointOffset );
841+
mCentroidFrame->setVisible( stackedPlacement->currentWidget() == pagePolygon
842+
&& radOverCentroid->isChecked() );
832843
}
833844
else if (( stackedPlacement->currentWidget() == pageLine && radLineParallel->isChecked() )
834845
|| ( stackedPlacement->currentWidget() == pagePolygon && radPolygonPerimeter->isChecked() )

‎src/core/qgspallabeling.cpp‎

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
147147
xOffset = 0;
148148
yOffset = 0;
149149
angleOffset = 0;
150+
centroidWhole = false;
150151
//textFont = QFont();
151152
textNamedStyle = QString( "" );
152153
textColor = Qt::black;
@@ -193,6 +194,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
193194
xOffset = s.xOffset;
194195
yOffset = s.yOffset;
195196
angleOffset = s.angleOffset;
197+
centroidWhole = s.centroidWhole;
196198
textFont = s.textFont;
197199
textNamedStyle = s.textNamedStyle;
198200
textColor = s.textColor;
@@ -363,6 +365,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
363365
xOffset = layer->customProperty( "labeling/xOffset", QVariant( 0.0 ) ).toDouble();
364366
yOffset = layer->customProperty( "labeling/yOffset", QVariant( 0.0 ) ).toDouble();
365367
angleOffset = layer->customProperty( "labeling/angleOffset", QVariant( 0.0 ) ).toDouble();
368+
centroidWhole = layer->customProperty( "labeling/centroidWhole", QVariant( false ) ).toBool();
366369
QString fontFamily = layer->customProperty( "labeling/fontFamily" ).toString();
367370
double fontSize = layer->customProperty( "labeling/fontSize" ).toDouble();
368371
int fontWeight = layer->customProperty( "labeling/fontWeight" ).toInt();
@@ -421,6 +424,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
421424
layer->setCustomProperty( "labeling/xOffset", xOffset );
422425
layer->setCustomProperty( "labeling/yOffset", yOffset );
423426
layer->setCustomProperty( "labeling/angleOffset", angleOffset );
427+
layer->setCustomProperty( "labeling/centroidWhole", centroidWhole );
424428

425429
layer->setCustomProperty( "labeling/fontFamily", textFont.family() );
426430
layer->setCustomProperty( "labeling/namedStyle", textNamedStyle );
@@ -676,10 +680,29 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
676680
return;
677681
}
678682

683+
// whether we're going to create a centroid for polygon
684+
bool centroidPoly = (( placement == QgsPalLayerSettings::AroundPoint
685+
|| placement == QgsPalLayerSettings::OverPoint )
686+
&& geom->type() == QGis::Polygon );
687+
688+
// CLIP the geometry if it is bigger than the extent
689+
// don't clip if centroid is requested for whole feature
690+
bool do_clip = false;
691+
if ( !centroidPoly || ( centroidPoly && !centroidWhole ) )
692+
{
693+
do_clip = !extentGeom->contains( geom );
694+
if ( do_clip )
695+
{
696+
geom = geom->intersection( extentGeom ); // creates new geometry
697+
if ( !geom )
698+
{
699+
return;
700+
}
701+
}
702+
}
703+
679704
// convert centroids to points before processing to use GEOS instead of PAL calculation
680-
if (( placement == QgsPalLayerSettings::AroundPoint
681-
|| placement == QgsPalLayerSettings::OverPoint )
682-
&& geom->type() == QGis::Polygon )
705+
if ( centroidPoly )
683706
{
684707
QgsGeometry* centroidpt = geom->centroid();
685708
if ( centroidpt->isGeosValid() && extentGeom->contains( centroidpt ) )
@@ -692,34 +715,17 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
692715
}
693716
else
694717
{
695-
// invalid geom type, skip registering feature with PAL
718+
// invalid geom type or outside extents
696719
return;
697720
}
698721
}
699722

700-
// CLIP the geometry if it is bigger than the extent
701-
QgsGeometry* geomClipped = NULL;
702-
GEOSGeometry* geos_geom;
703-
bool do_clip = !extentGeom->contains( geom );
704-
if ( do_clip )
705-
{
706-
geomClipped = geom->intersection( extentGeom ); // creates new geometry
707-
if ( !geomClipped )
708-
{
709-
return;
710-
}
711-
geos_geom = geomClipped->asGeos();
712-
}
713-
else
714-
{
715-
geos_geom = geom->asGeos();
716-
}
723+
GEOSGeometry* geos_geom = geom->asGeos();
717724

718725
if ( geos_geom == NULL )
719726
return; // invalid geometry
720727
GEOSGeometry* geos_geom_clone = GEOSGeom_clone( geos_geom );
721-
if ( do_clip )
722-
delete geomClipped;
728+
723729

724730
//data defined position / alignment / rotation?
725731
bool dataDefinedPosition = false;
@@ -921,12 +927,11 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
921927
pal::LabelPosition* lp = new LabelPosition( 1, xPos, yPos, labelX, labelY,
922928
( angleOffset * M_PI / 180 ), 0.0, fpart );
923929

930+
// lp->getWidth or lp->getHeight doesn't account for rotation, get bbox instead
924931
double amin[2], amax[2];
925932
lp->getBoundingBox( amin, amax );
926933
QgsRectangle lblrect = QgsRectangle( amin[0], amin[1], amax[0], amax[1] );
927934

928-
// labelW = lp->getWidth();
929-
// labelH = lp->getHeight();
930935
labelW = lblrect.width();
931936
labelH = lblrect.height();
932937
delete fpart;

‎src/core/qgspallabeling.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class CORE_EXPORT QgsPalLayerSettings
123123
int xQuadOffset, yQuadOffset;
124124
double xOffset, yOffset; // offset from point in mm or map units
125125
double angleOffset; // rotation applied to offset labels
126+
bool centroidWhole; // whether centroid calculated from whole or visible polygon
126127
QFont textFont;
127128
QString textNamedStyle;
128129
QColor textColor;

0 commit comments

Comments
 (0)