Skip to content

Commit

Permalink
More fixes, removal of layer-wise obstacle flag, removed pal::Layer u…
Browse files Browse the repository at this point in the history
…sage outside of labeling engine

This code has been funded by Tuscany Region (Italy) - SITA (CIG: 63526840AE) and commissioned to Gis3W s.a.s.
  • Loading branch information
wonder-sk committed Sep 21, 2015
1 parent 959fbb5 commit aa85cf3
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 83 deletions.
9 changes: 7 additions & 2 deletions src/core/pal/layer.cpp
Expand Up @@ -46,7 +46,7 @@
namespace pal
{

Layer::Layer( const QString &lyrName, Arrangement arrangement, double defaultPriority, bool obstacle, bool active, bool toLabel, Pal *pal, bool displayAll )
Layer::Layer( const QString &lyrName, Arrangement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal, bool displayAll )
: mName( lyrName )
, pal( pal )
, mObstacleType( PolygonInterior )
Expand Down Expand Up @@ -263,7 +263,12 @@ namespace pal
bool Layer::registerFeature( QgsLabelFeature* label )
{
QgsPalGeometry* g = label->geometry();
if ( !registerFeature( g->strId(), g, label->size().width(), label->size().height() ) )
if ( !registerFeature( g->strId(), g, label->size().width(), label->size().height(), label->labelText(),
label->fixedPosition().x(), label->fixedPosition().y(), label->hasFixedPosition(),
label->fixedAngle(), label->hasFixedAngle(),
label->quadOffset().x(), label->quadOffset().y(),
label->positionOffset().x(), label->positionOffset().y(),
label->alwaysShow(), label->repeatDistance() ) )
return false;

pal::Feature* pf = getFeature( g->strId() );
Expand Down
3 changes: 1 addition & 2 deletions src/core/pal/layer.h
Expand Up @@ -309,14 +309,13 @@ namespace pal
* @param lyrName layer's name
* @param arrangement Arrangement mode : how to place candidates
* @param defaultPriority layer's prioriry (0 is the best, 1 the worst)
* @param obstacle 'true' will discourage other label to be placed above features of this layer
* @param active is the layer is active (currently displayed)
* @param toLabel the layer will be labeled whether toLablel is true
* @param pal pointer to the pal object
* @param displayAll if true, all features will be labelled even though overlaps occur
*
*/
Layer( const QString& lyrName, Arrangement arrangement, double defaultPriority, bool obstacle, bool active, bool toLabel, Pal *pal, bool displayAll = false );
Layer( const QString& lyrName, Arrangement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal, bool displayAll = false );

/** Add newly created feature part into r tree and to the list */
void addFeaturePart( FeaturePart* fpart, const QString &labelText = QString() );
Expand Down
4 changes: 2 additions & 2 deletions src/core/pal/pal.cpp
Expand Up @@ -138,7 +138,7 @@ namespace pal
//finishGEOS();
}

Layer* Pal::addLayer( const QString &layerName, Arrangement arrangement, double defaultPriority, bool obstacle, bool active, bool toLabel, bool displayAll )
Layer* Pal::addLayer( const QString &layerName, Arrangement arrangement, double defaultPriority, bool active, bool toLabel, bool displayAll )
{
mMutex.lock();

Expand All @@ -151,7 +151,7 @@ namespace pal
return mLayers.value( layerName );
}

Layer* layer = new Layer( layerName, arrangement, defaultPriority, obstacle, active, toLabel, this, displayAll );
Layer* layer = new Layer( layerName, arrangement, defaultPriority, active, toLabel, this, displayAll );
mLayers.insert( layerName, layer );
mMutex.unlock();

Expand Down
3 changes: 1 addition & 2 deletions src/core/pal/pal.h
Expand Up @@ -130,7 +130,6 @@ namespace pal
* @param layerName layer's name
* @param arrangement Howto place candidates
* @param defaultPriority layer's prioriry (0 is the best, 1 the worst)
* @param obstacle 'true' will discourage other label to be placed above features of this layer
* @param active is the layer is active (currently displayed)
* @param toLabel the layer will be labeled only if toLablel is true
* @param displayAll if true, all features will be labelled even though overlaps occur
Expand All @@ -139,7 +138,7 @@ namespace pal
*
* @todo add symbolUnit
*/
Layer* addLayer( const QString& layerName, Arrangement arrangement, double defaultPriority, bool obstacle, bool active, bool toLabel, bool displayAll = false );
Layer* addLayer( const QString& layerName, Arrangement arrangement, double defaultPriority, bool active, bool toLabel, bool displayAll = false );

/**
* \brief Look for a layer
Expand Down
1 change: 0 additions & 1 deletion src/core/qgsdiagramrendererv2.cpp
Expand Up @@ -31,7 +31,6 @@ QgsDiagramLayerSettings::QgsDiagramLayerSettings()
, obstacle( false )
, dist( 0.0 )
, renderer( 0 )
, palLayer( 0 )
, ct( 0 )
, xform( 0 )
, xPosColumn( -1 )
Expand Down
1 change: 0 additions & 1 deletion src/core/qgsdiagramrendererv2.h
Expand Up @@ -73,7 +73,6 @@ class CORE_EXPORT QgsDiagramLayerSettings
QgsDiagramRendererV2* renderer; // if any renderer is assigned, it is owned by this class

//assigned when layer gets prepared
pal::Layer* palLayer;
const QgsCoordinateTransform* ct;
const QgsMapToPixel* xform;
QList<QgsPalGeometry*> geometries;
Expand Down
1 change: 0 additions & 1 deletion src/core/qgslabelingenginev2.cpp
Expand Up @@ -112,7 +112,6 @@ void QgsLabelingEngineV2::run( QgsRenderContext& context )
pal::Layer* l = p.addLayer( provider->id(),
arrangement,
provider->priority(),
flags.testFlag( QgsAbstractLabelProvider::GeometriesAreObstacles ),
true,
flags.testFlag( QgsAbstractLabelProvider::DrawLabels ),
flags.testFlag( QgsAbstractLabelProvider::DrawAllLabels ) );
Expand Down
19 changes: 12 additions & 7 deletions src/core/qgslabelingenginev2.h
Expand Up @@ -104,6 +104,11 @@ class CORE_EXPORT QgsLabelFeature
double obstacleFactor() const { return mObstacleFactor; }
void setObstacleFactor( double factor ) { mObstacleFactor = factor; }

//! Text of the label - used if "merge connected lines to avoid duplicate labels" is enabled
//! to identify which features may be merged
QString labelText() const { return mLabelText; }
void setLabelText( const QString& text ) { mLabelText = text; }

protected:

//! Associated ID unique within the parent label provider
Expand All @@ -125,6 +130,7 @@ class CORE_EXPORT QgsLabelFeature
bool mAlwaysShow;
bool mIsObstacle;
double mObstacleFactor;
QString mLabelText;
};


Expand All @@ -150,13 +156,12 @@ class CORE_EXPORT QgsAbstractLabelProvider

enum Flag
{
GeometriesAreObstacles = 1 << 1, //!< whether the geometries of labels by default act as obstacles (may be overriden on feature level)
DrawLabels = 1 << 2, //!< whether the labels should be rendered
DrawAllLabels = 1 << 3, //!< whether all features will be labelled even though overlaps occur
MergeConnectedLines = 1 << 4, //!< whether adjacent lines (with the same label text) should be merged
CentroidMustBeInside = 1 << 5, //!< whether location of centroid must be inside of polygons
FitInPolygonOnly = 1 << 6, //!< whether labels must fall completely within the polygon
LabelPerFeaturePart = 1 << 7, //!< whether to label each part of multi-part features separately
DrawLabels = 1 << 1, //!< whether the labels should be rendered
DrawAllLabels = 1 << 2, //!< whether all features will be labelled even though overlaps occur
MergeConnectedLines = 1 << 3, //!< whether adjacent lines (with the same label text) should be merged
CentroidMustBeInside = 1 << 4, //!< whether location of centroid must be inside of polygons
FitInPolygonOnly = 1 << 5, //!< whether labels must fall completely within the polygon
LabelPerFeaturePart = 1 << 6, //!< whether to label each part of multi-part features separately
};
Q_DECLARE_FLAGS( Flags, Flag )

Expand Down
82 changes: 21 additions & 61 deletions src/core/qgspallabeling.cpp
Expand Up @@ -80,7 +80,6 @@ using namespace pal;

QgsPalLayerSettings::QgsPalLayerSettings()
: upsidedownLabels( Upright )
, palLayer( NULL )
, mCurFeat( 0 )
, xform( NULL )
, ct( NULL )
Expand Down Expand Up @@ -322,8 +321,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
}

QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
: palLayer( NULL )
, mCurFeat( NULL )
: mCurFeat( NULL )
, fieldIndex( 0 )
, xform( NULL )
, ct( NULL )
Expand Down Expand Up @@ -1471,7 +1469,7 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext& context, QString dxfLayer, QgsLabelFeature** labelFeature )
{
// either used in QgsPalLabeling (palLayer is set) or in QgsLabelingEngineV2 (labelFeature is set)
Q_ASSERT(( palLayer || labelFeature ) && !( palLayer && labelFeature ) );
Q_ASSERT( labelFeature );

if ( !drawLabels )
{
Expand Down Expand Up @@ -1835,7 +1833,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
{
return;
}
mFeatsRegPal = palLayer->featureCount();
mFeatsRegPal = geometries.count();
if ( mFeatsRegPal >= maxNumLabels )
{
return;
Expand Down Expand Up @@ -2185,30 +2183,22 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
}

// feature to the layer
try
{
if ( !palLayer )
*labelFeature = new QgsLabelFeature( lbl->strId(), lbl, QSizeF( labelX, labelY ) );
else
if ( !palLayer->registerFeature( lbl->strId(), lbl, labelX, labelY, labelText,
xPos, yPos, dataDefinedPosition, angle, dataDefinedRotation,
quadOffsetX, quadOffsetY, offsetX, offsetY, alwaysShow, repeatDist ) )
return;
}
catch ( std::exception &e )
{
Q_UNUSED( e );
QgsDebugMsgLevel( QString( "Ignoring feature %1 due PAL exception:" ).arg( f.id() ) + QString::fromLatin1( e.what() ), 4 );
return;
}
*labelFeature = new QgsLabelFeature( lbl->strId(), lbl, QSizeF( labelX, labelY ) );

( *labelFeature )->setHasFixedPosition( dataDefinedPosition );
( *labelFeature )->setFixedPosition( QgsPoint( xPos, yPos ) );
( *labelFeature )->setHasFixedAngle( dataDefinedRotation );
( *labelFeature )->setFixedAngle( angle );
( *labelFeature )->setQuadOffset( QPointF( quadOffsetX, quadOffsetY ) );
( *labelFeature )->setPositionOffset( QgsPoint( offsetX, offsetY ) );
( *labelFeature )->setAlwaysShow( alwaysShow );
( *labelFeature )->setRepeatDistance( repeatDist );
( *labelFeature )->setLabelText( labelText );

// TODO: only for placement which needs character info
pal::Feature* feat = palLayer ? palLayer->getFeature( lbl->strId() ) : 0;
// account for any data defined font metrics adjustments
lbl->calculateInfo( labelFontMetrics, xform, rasterCompressFactor, maxcharanglein, maxcharangleout );
// for labelFeature the LabelInfo is passed to feat when it is registered
if ( feat )
feat->setLabelInfo( lbl->info() );
delete labelFontMetrics;

// TODO: allow layer-wide feature dist in PAL...?
Expand Down Expand Up @@ -2248,18 +2238,12 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
distance *= vectorScaleFactor;
}
double d = qAbs( ptOne.x() - ptZero.x() ) * distance;
if ( feat )
feat->setDistLabel( d );
else
( *labelFeature )->setDistLabel( d );
( *labelFeature )->setDistLabel( d );
}

if ( ddFixedQuad )
{
if ( feat )
feat->setFixedQuadrant( true );
else
( *labelFeature )->setHasFixedQuadrant( true );
( *labelFeature )->setHasFixedQuadrant( true );
}

// data defined priority?
Expand All @@ -2271,10 +2255,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
{
priorityD = qBound( 0.0, priorityD, 10.0 );
priorityD = 1 - priorityD / 10.0; // convert 0..10 --> 1..0
if ( feat )
feat->setPriority( priorityD );
else
( *labelFeature )->setPriority( priorityD );
( *labelFeature )->setPriority( priorityD );
}
}

Expand All @@ -2285,10 +2266,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
isObstacle = exprVal.toBool();
}

if ( feat )
feat->setIsObstacle( isObstacle );
else
( *labelFeature )->setIsObstacle( isObstacle );
( *labelFeature )->setIsObstacle( isObstacle );

double featObstacleFactor = obstacleFactor;
if ( dataDefinedEvaluate( QgsPalLayerSettings::ObstacleFactor, exprVal, &context.expressionContext() ) )
Expand All @@ -2302,10 +2280,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
featObstacleFactor = factorD;
}
}
if ( feat )
feat->setObstacleFactor( featObstacleFactor );
else
( *labelFeature )->setObstacleFactor( featObstacleFactor );
( *labelFeature )->setObstacleFactor( featObstacleFactor );

//add parameters for data defined labeling to QgsPalGeometry
QMap< DataDefinedProperties, QVariant >::const_iterator dIt = dataDefinedValues.constBegin();
Expand Down Expand Up @@ -2358,23 +2333,8 @@ void QgsPalLayerSettings::registerObstacleFeature( QgsFeature& f, const QgsRende
geometries.append( lbl );

// feature to the layer
try
{
if ( obstacleFeature )
{
*obstacleFeature = new QgsLabelFeature( lbl->strId(), lbl, QSizeF( 0, 0 ) );
( *obstacleFeature )->setIsObstacle( true );
}
else
if ( !palLayer->registerFeature( lbl->strId(), lbl, 0, 0 ) )
return;
}
catch ( std::exception &e )
{
Q_UNUSED( e );
QgsDebugMsgLevel( QString( "Ignoring feature %1 due PAL exception:" ).arg( f.id() ) + QString::fromLatin1( e.what() ), 4 );
return;
}
*obstacleFeature = new QgsLabelFeature( lbl->strId(), lbl, QSizeF( 0, 0 ) );
( *obstacleFeature )->setIsObstacle( true );
}

bool QgsPalLayerSettings::dataDefinedValEval( DataDefinedValueType valType,
Expand Down
2 changes: 0 additions & 2 deletions src/core/qgspallabeling.h
Expand Up @@ -555,8 +555,6 @@ class CORE_EXPORT QgsPalLayerSettings
QMap<QgsPalLayerSettings::DataDefinedProperties, QPair<QString, int> > dataDefinedNames() const { return mDataDefinedNames; }

// temporary stuff: set when layer gets prepared or labeled
// NOTE: not in Python binding
pal::Layer* palLayer;
QgsFeature* mCurFeat;
QgsFields mCurFields;
int fieldIndex;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerdiagramprovider.cpp
Expand Up @@ -64,7 +64,6 @@ void QgsVectorLayerDiagramProvider::init()
mPlacement = QgsPalLayerSettings::Placement( mSettings.placement );
mLinePlacementFlags = mSettings.placementFlags;
mFlags |= DrawAllLabels;
if ( mSettings.obstacle ) mFlags |= GeometriesAreObstacles;
}


Expand Down Expand Up @@ -349,6 +348,7 @@ QgsLabelFeature* QgsVectorLayerDiagramProvider::registerDiagram( QgsFeature& fea
lf->setHasFixedAngle( true );
lf->setFixedAngle( 0 );
lf->setAlwaysShow( alwaysShow );
lf->setIsObstacle( mSettings.obstacle );

QgsPoint ptZero = mSettings.xform->toMapCoordinates( 0, 0 );
QgsPoint ptOne = mSettings.xform->toMapCoordinates( 1, 0 );
Expand Down
1 change: 0 additions & 1 deletion src/core/qgsvectorlayerlabelprovider.cpp
Expand Up @@ -91,7 +91,6 @@ void QgsVectorLayerLabelProvider::init()
mPlacement = mSettings.placement;
mLinePlacementFlags = mSettings.placementFlags;
mFlags = Flags();
if ( mSettings.obstacle ) mFlags |= GeometriesAreObstacles;
if ( mSettings.drawLabels ) mFlags |= DrawLabels;
if ( mSettings.displayAll ) mFlags |= DrawAllLabels;
if ( mSettings.mergeLines ) mFlags |= MergeConnectedLines;
Expand Down

0 comments on commit aa85cf3

Please sign in to comment.