Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[pal] Remove dedicated setters/getters for individual obstacle settin…
…gs, instead

expose whole QgsLabelObstacleSettings to pal
  • Loading branch information
nyalldawson committed Dec 7, 2019
1 parent 64f17aa commit 0443299
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/core/pal/costcalculator.cpp
Expand Up @@ -84,7 +84,7 @@ void CostCalculator::addObstacleCostPenalty( LabelPosition *lp, FeaturePart *obs
}

//scale cost by obstacle's factor
double obstacleCost = obstacle->obstacleFactor() * double( n );
double obstacleCost = obstacle->obstacleSettings().factor() * double( n );
if ( n > 0 )
lp->setConflictsWithObstacle( true );

Expand All @@ -97,7 +97,7 @@ void CostCalculator::addObstacleCostPenalty( LabelPosition *lp, FeaturePart *obs
{
// obstacle factor is from 0 -> 2, label priority is from 1 -> 0. argh!
const double priority = 2 * ( 1 - lp->feature->calculatePriority() );
const double obstaclePriority = obstacle->obstacleFactor();
const double obstaclePriority = obstacle->obstacleSettings().factor();

// if feature priority is < obstaclePriorty, there's a hard conflict...
if ( n > 0 && ( priority < obstaclePriority && !qgsDoubleNear( priority, obstaclePriority, 0.001 ) ) )
Expand Down
8 changes: 2 additions & 6 deletions src/core/pal/feature.h
Expand Up @@ -284,14 +284,10 @@ namespace pal
*/
bool alwaysShow() const { return mLF->alwaysShow(); }

//! Returns TRUE if the feature should act as an obstacle to labels
bool isObstacle() const { return mLF->isObstacle(); }

/**
* Returns the feature's obstacle factor, which represents the penalty
* incurred for a label to overlap the feature
* Returns the feature's obstacle settings.
*/
double obstacleFactor() const { return mLF->obstacleFactor(); }
const QgsLabelObstacleSettings &obstacleSettings() const { return mLF->obstacleSettings(); }

//! Returns the distance between repeating labels for this feature
double repeatDistance() const { return mLF->repeatDistance(); }
Expand Down
4 changes: 2 additions & 2 deletions src/core/pal/layer.cpp
Expand Up @@ -151,7 +151,7 @@ bool Layer::registerFeature( QgsLabelFeature *lf )
// is the feature well defined? TODO Check epsilon
bool labelWellDefined = ( lf->size().width() > 0.0000001 && lf->size().height() > 0.0000001 );

if ( lf->isObstacle() && featureGeomIsObstacleGeom )
if ( lf->obstacleSettings().isObstacle() && featureGeomIsObstacleGeom )
{
//if we are not labeling the layer, only insert it into the obstacle list and avoid an
//unnecessary copy
Expand Down Expand Up @@ -192,7 +192,7 @@ bool Layer::registerFeature( QgsLabelFeature *lf )
addedFeature = true;
}

if ( lf->isObstacle() && !featureGeomIsObstacleGeom )
if ( lf->obstacleSettings().isObstacle() && !featureGeomIsObstacleGeom )
{
//do the same for the obstacle geometry
simpleGeometries.reset( Util::unmulti( lf->obstacleGeometry() ) );
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgslabelfeature.cpp
Expand Up @@ -111,3 +111,13 @@ void QgsLabelFeature::setOverrunSmoothDistance( double overrunSmoothDistance )
{
mOverrunSmoothDistance = overrunSmoothDistance;
}

const QgsLabelObstacleSettings &QgsLabelFeature::obstacleSettings() const
{
return mObstacleSettings;
}

void QgsLabelFeature::setObstacleSettings( const QgsLabelObstacleSettings &settings )
{
mObstacleSettings = settings;
}
49 changes: 21 additions & 28 deletions src/core/qgslabelfeature.h
Expand Up @@ -22,6 +22,7 @@
#include "geos_c.h"
#include "qgsgeos.h"
#include "qgsmargins.h"
#include "qgslabelobstaclesettings.h"
#include "pal.h"

namespace pal
Expand Down Expand Up @@ -304,37 +305,15 @@ class CORE_EXPORT QgsLabelFeature
//! Sets whether label should be always shown (sets very high label priority)
void setAlwaysShow( bool enabled ) { mAlwaysShow = enabled; }



/**
* Returns whether the feature will act as an obstacle for labels.
* \returns TRUE if feature is an obstacle
* \see setIsObstacle
*/
bool isObstacle() const { return mIsObstacle; }

/**
* Sets whether the feature will act as an obstacle for labels.
* \param enabled whether feature will act as an obstacle
* \see isObstacle
*/
void setIsObstacle( bool enabled ) { mIsObstacle = enabled; }

/**
* Returns the obstacle factor for the feature. The factor controls the penalty
* for labels overlapping this feature.
* \see setObstacleFactor
*/
double obstacleFactor() const { return mObstacleFactor; }

/**
* Sets the obstacle factor for the feature. The factor controls the penalty
* for labels overlapping this feature.
* \param factor larger factors ( > 1.0 ) will result in labels
* which are less likely to cover this feature, smaller factors ( < 1.0 ) mean labels
* are more likely to cover this feature (where required)
* \see obstacleFactor
*/
void setObstacleFactor( double factor ) { mObstacleFactor = factor; }

/**
* Returns the feature's arrangement flags.
* \see setArrangementFlags
Expand Down Expand Up @@ -466,6 +445,22 @@ class CORE_EXPORT QgsLabelFeature
*/
void setRotatedSize( QSizeF size ) { mRotatedSize = size; }

/**
* Returns the label's obstacle settings.
*
* \see setObstacleSettings()
* \since QGIS 3.12
*/
const QgsLabelObstacleSettings &obstacleSettings() const;

/**
* Sets the label's obstacle \a settings.
*
* \see pbstacleSettings()
* \since QGIS 3.12
*/
void setObstacleSettings( const QgsLabelObstacleSettings &settings );

protected:
//! Pointer to PAL layer (assigned when registered to PAL)
pal::Layer *mLayer = nullptr;
Expand Down Expand Up @@ -514,10 +509,6 @@ class CORE_EXPORT QgsLabelFeature
double mRepeatDistance;
//! whether to always show label - even in case of collisions
bool mAlwaysShow;
//! whether the feature geometry acts as an obstacle for labels
bool mIsObstacle;
//! how strong is the geometry acting as obstacle
double mObstacleFactor;
//! text of the label
QString mLabelText;
//! extra information for curved labels (may be NULLPTR)
Expand All @@ -544,6 +535,8 @@ class CORE_EXPORT QgsLabelFeature

bool mLabelAllParts = false;

QgsLabelObstacleSettings mObstacleSettings;

};

#endif // QGSLABELFEATURE_H
14 changes: 10 additions & 4 deletions src/core/qgspallabeling.cpp
Expand Up @@ -2478,7 +2478,8 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
}
}

( *labelFeature )->setIsObstacle( isObstacle );
QgsLabelObstacleSettings os = mObstacleSettings;
os.setIsObstacle( isObstacle );

double featObstacleFactor = mObstacleSettings.factor();
if ( isObstacle && mDataDefinedProperties.isActive( QgsPalLayerSettings::ObstacleFactor ) )
Expand All @@ -2497,7 +2498,8 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
}
}
}
( *labelFeature )->setObstacleFactor( featObstacleFactor );
os.setFactor( featObstacleFactor );
lf->setObstacleSettings( os );

QVector< QgsPalLayerSettings::PredefinedPointPosition > positionOrder = predefinedPositionOrder;
if ( positionOrder.isEmpty() )
Expand Down Expand Up @@ -2562,9 +2564,11 @@ void QgsPalLayerSettings::registerObstacleFeature( const QgsFeature &f, QgsRende

// feature to the layer
*obstacleFeature = new QgsLabelFeature( f.id(), std::move( geos_geom_clone ), QSizeF( 0, 0 ) );
( *obstacleFeature )->setIsObstacle( true );
( *obstacleFeature )->setFeature( f );

QgsLabelObstacleSettings os = mObstacleSettings;
os.setIsObstacle( true );

double featObstacleFactor = mObstacleSettings.factor();
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::ObstacleFactor ) )
{
Expand All @@ -2582,7 +2586,9 @@ void QgsPalLayerSettings::registerObstacleFeature( const QgsFeature &f, QgsRende
}
}
}
( *obstacleFeature )->setObstacleFactor( featObstacleFactor );
os.setFactor( featObstacleFactor );

( *obstacleFeature )->setObstacleSettings( os );

mFeatsRegPal++;
}
Expand Down

0 comments on commit 0443299

Please sign in to comment.