Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix for #6315, and keeps centroid calc. for whole feature in adv labe…
…ling
  • Loading branch information
dakcarto committed Sep 8, 2012
1 parent 595d2a5 commit f143507
Show file tree
Hide file tree
Showing 4 changed files with 507 additions and 329 deletions.
11 changes: 11 additions & 0 deletions src/app/qgslabelinggui.cpp
Expand Up @@ -99,13 +99,17 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
int distUnitIndex = lyr.distInMapUnits ? 1 : 0;
mXQuadOffset = lyr.xQuadOffset;
mYQuadOffset = lyr.yQuadOffset;
mCentroidRadioWhole->setChecked( lyr.centroidWhole );
mCentroidFrame->setVisible( false );
switch ( lyr.placement )
{
case QgsPalLayerSettings::AroundPoint:
radAroundPoint->setChecked( true );
radAroundCentroid->setChecked( true );
spinDistPoint->setValue( lyr.dist );
mPointDistanceUnitComboBox->setCurrentIndex( distUnitIndex );
mCentroidFrame->setVisible( layer->geometryType() == QGis::Polygon );

//spinAngle->setValue( lyr.angle );
break;
case QgsPalLayerSettings::OverPoint:
Expand All @@ -126,6 +130,7 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
mPointOffsetYOffsetSpinBox->setValue( lyr.yOffset );
mPointOffsetUnitsComboBox->setCurrentIndex( lyr.labelOffsetInMapUnits ? 1 : 0 );
mPointOffsetAngleSpinBox->setValue( lyr.angleOffset );
mCentroidFrame->setVisible( layer->geometryType() == QGis::Polygon );

break;
case QgsPalLayerSettings::Line:
Expand Down Expand Up @@ -290,6 +295,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
lyr.dist = 0;
lyr.placementFlags = 0;

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

void QgsLabelingGui::updateOptions()
{
mCentroidFrame->setVisible( false );
if (( stackedPlacement->currentWidget() == pagePoint && radAroundPoint->isChecked() )
|| ( stackedPlacement->currentWidget() == pagePolygon && radAroundCentroid->isChecked() ) )
{
stackedOptions->setCurrentWidget( pageOptionsPoint );
mCentroidFrame->setVisible( stackedPlacement->currentWidget() == pagePolygon
&& radAroundCentroid->isChecked() );
}
else if (( stackedPlacement->currentWidget() == pagePoint && radOverPoint->isChecked() )
|| ( stackedPlacement->currentWidget() == pagePolygon && radOverCentroid->isChecked() ) )
{
stackedOptions->setCurrentWidget( pageOptionsPointOffset );
mCentroidFrame->setVisible( stackedPlacement->currentWidget() == pagePolygon
&& radOverCentroid->isChecked() );
}
else if (( stackedPlacement->currentWidget() == pageLine && radLineParallel->isChecked() )
|| ( stackedPlacement->currentWidget() == pagePolygon && radPolygonPerimeter->isChecked() )
Expand Down
55 changes: 30 additions & 25 deletions src/core/qgspallabeling.cpp
Expand Up @@ -147,6 +147,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
xOffset = 0;
yOffset = 0;
angleOffset = 0;
centroidWhole = false;
//textFont = QFont();
textNamedStyle = QString( "" );
textColor = Qt::black;
Expand Down Expand Up @@ -193,6 +194,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
xOffset = s.xOffset;
yOffset = s.yOffset;
angleOffset = s.angleOffset;
centroidWhole = s.centroidWhole;
textFont = s.textFont;
textNamedStyle = s.textNamedStyle;
textColor = s.textColor;
Expand Down Expand Up @@ -363,6 +365,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
xOffset = layer->customProperty( "labeling/xOffset", QVariant( 0.0 ) ).toDouble();
yOffset = layer->customProperty( "labeling/yOffset", QVariant( 0.0 ) ).toDouble();
angleOffset = layer->customProperty( "labeling/angleOffset", QVariant( 0.0 ) ).toDouble();
centroidWhole = layer->customProperty( "labeling/centroidWhole", QVariant( false ) ).toBool();
QString fontFamily = layer->customProperty( "labeling/fontFamily" ).toString();
double fontSize = layer->customProperty( "labeling/fontSize" ).toDouble();
int fontWeight = layer->customProperty( "labeling/fontWeight" ).toInt();
Expand Down Expand Up @@ -421,6 +424,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
layer->setCustomProperty( "labeling/xOffset", xOffset );
layer->setCustomProperty( "labeling/yOffset", yOffset );
layer->setCustomProperty( "labeling/angleOffset", angleOffset );
layer->setCustomProperty( "labeling/centroidWhole", centroidWhole );

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

// whether we're going to create a centroid for polygon
bool centroidPoly = (( placement == QgsPalLayerSettings::AroundPoint
|| placement == QgsPalLayerSettings::OverPoint )
&& geom->type() == QGis::Polygon );

// CLIP the geometry if it is bigger than the extent
// don't clip if centroid is requested for whole feature
bool do_clip = false;
if ( !centroidPoly || ( centroidPoly && !centroidWhole ) )
{
do_clip = !extentGeom->contains( geom );
if ( do_clip )
{
geom = geom->intersection( extentGeom ); // creates new geometry
if ( !geom )
{
return;
}
}
}

// convert centroids to points before processing to use GEOS instead of PAL calculation
if (( placement == QgsPalLayerSettings::AroundPoint
|| placement == QgsPalLayerSettings::OverPoint )
&& geom->type() == QGis::Polygon )
if ( centroidPoly )
{
QgsGeometry* centroidpt = geom->centroid();
if ( centroidpt->isGeosValid() && extentGeom->contains( centroidpt ) )
Expand All @@ -692,34 +715,17 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
}
else
{
// invalid geom type, skip registering feature with PAL
// invalid geom type or outside extents
return;
}
}

// CLIP the geometry if it is bigger than the extent
QgsGeometry* geomClipped = NULL;
GEOSGeometry* geos_geom;
bool do_clip = !extentGeom->contains( geom );
if ( do_clip )
{
geomClipped = geom->intersection( extentGeom ); // creates new geometry
if ( !geomClipped )
{
return;
}
geos_geom = geomClipped->asGeos();
}
else
{
geos_geom = geom->asGeos();
}
GEOSGeometry* geos_geom = geom->asGeos();

if ( geos_geom == NULL )
return; // invalid geometry
GEOSGeometry* geos_geom_clone = GEOSGeom_clone( geos_geom );
if ( do_clip )
delete geomClipped;


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

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

// labelW = lp->getWidth();
// labelH = lp->getHeight();
labelW = lblrect.width();
labelH = lblrect.height();
delete fpart;
Expand Down
1 change: 1 addition & 0 deletions src/core/qgspallabeling.h
Expand Up @@ -123,6 +123,7 @@ class CORE_EXPORT QgsPalLayerSettings
int xQuadOffset, yQuadOffset;
double xOffset, yOffset; // offset from point in mm or map units
double angleOffset; // rotation applied to offset labels
bool centroidWhole; // whether centroid calculated from whole or visible polygon
QFont textFont;
QString textNamedStyle;
QColor textColor;
Expand Down

0 comments on commit f143507

Please sign in to comment.