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
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
namespace pal 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 ) : mName( lyrName )
, pal( pal ) , pal( pal )
, mObstacleType( PolygonInterior ) , mObstacleType( PolygonInterior )
Expand Down Expand Up @@ -263,7 +263,12 @@ namespace pal
bool Layer::registerFeature( QgsLabelFeature* label ) bool Layer::registerFeature( QgsLabelFeature* label )
{ {
QgsPalGeometry* g = label->geometry(); 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; return false;


pal::Feature* pf = getFeature( g->strId() ); pal::Feature* pf = getFeature( g->strId() );
Expand Down
3 changes: 1 addition & 2 deletions src/core/pal/layer.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -309,14 +309,13 @@ namespace pal
* @param lyrName layer's name * @param lyrName layer's name
* @param arrangement Arrangement mode : how to place candidates * @param arrangement Arrangement mode : how to place candidates
* @param defaultPriority layer's prioriry (0 is the best, 1 the worst) * @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 active is the layer is active (currently displayed)
* @param toLabel the layer will be labeled whether toLablel is true * @param toLabel the layer will be labeled whether toLablel is true
* @param pal pointer to the pal object * @param pal pointer to the pal object
* @param displayAll if true, all features will be labelled even though overlaps occur * @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 */ /** Add newly created feature part into r tree and to the list */
void addFeaturePart( FeaturePart* fpart, const QString &labelText = QString() ); void addFeaturePart( FeaturePart* fpart, const QString &labelText = QString() );
Expand Down
4 changes: 2 additions & 2 deletions src/core/pal/pal.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace pal
//finishGEOS(); //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(); mMutex.lock();


Expand All @@ -151,7 +151,7 @@ namespace pal
return mLayers.value( layerName ); 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 ); mLayers.insert( layerName, layer );
mMutex.unlock(); mMutex.unlock();


Expand Down
3 changes: 1 addition & 2 deletions src/core/pal/pal.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ namespace pal
* @param layerName layer's name * @param layerName layer's name
* @param arrangement Howto place candidates * @param arrangement Howto place candidates
* @param defaultPriority layer's prioriry (0 is the best, 1 the worst) * @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 active is the layer is active (currently displayed)
* @param toLabel the layer will be labeled only if toLablel is true * @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 * @param displayAll if true, all features will be labelled even though overlaps occur
Expand All @@ -139,7 +138,7 @@ namespace pal
* *
* @todo add symbolUnit * @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 * \brief Look for a layer
Expand Down
1 change: 0 additions & 1 deletion src/core/qgsdiagramrendererv2.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ QgsDiagramLayerSettings::QgsDiagramLayerSettings()
, obstacle( false ) , obstacle( false )
, dist( 0.0 ) , dist( 0.0 )
, renderer( 0 ) , renderer( 0 )
, palLayer( 0 )
, ct( 0 ) , ct( 0 )
, xform( 0 ) , xform( 0 )
, xPosColumn( -1 ) , xPosColumn( -1 )
Expand Down
1 change: 0 additions & 1 deletion src/core/qgsdiagramrendererv2.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class CORE_EXPORT QgsDiagramLayerSettings
QgsDiagramRendererV2* renderer; // if any renderer is assigned, it is owned by this class QgsDiagramRendererV2* renderer; // if any renderer is assigned, it is owned by this class


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


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




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


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


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


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


QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s ) QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
: palLayer( NULL ) : mCurFeat( NULL )
, mCurFeat( NULL )
, fieldIndex( 0 ) , fieldIndex( 0 )
, xform( NULL ) , xform( NULL )
, ct( 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 ) 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) // either used in QgsPalLabeling (palLayer is set) or in QgsLabelingEngineV2 (labelFeature is set)
Q_ASSERT(( palLayer || labelFeature ) && !( palLayer && labelFeature ) ); Q_ASSERT( labelFeature );


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


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


// TODO: only for placement which needs character info // 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 // account for any data defined font metrics adjustments
lbl->calculateInfo( labelFontMetrics, xform, rasterCompressFactor, maxcharanglein, maxcharangleout ); lbl->calculateInfo( labelFontMetrics, xform, rasterCompressFactor, maxcharanglein, maxcharangleout );
// for labelFeature the LabelInfo is passed to feat when it is registered // for labelFeature the LabelInfo is passed to feat when it is registered
if ( feat )
feat->setLabelInfo( lbl->info() );
delete labelFontMetrics; delete labelFontMetrics;


// TODO: allow layer-wide feature dist in PAL...? // 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; distance *= vectorScaleFactor;
} }
double d = qAbs( ptOne.x() - ptZero.x() ) * distance; double d = qAbs( ptOne.x() - ptZero.x() ) * distance;
if ( feat ) ( *labelFeature )->setDistLabel( d );
feat->setDistLabel( d );
else
( *labelFeature )->setDistLabel( d );
} }


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


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


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


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


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


//add parameters for data defined labeling to QgsPalGeometry //add parameters for data defined labeling to QgsPalGeometry
QMap< DataDefinedProperties, QVariant >::const_iterator dIt = dataDefinedValues.constBegin(); 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 ); geometries.append( lbl );


// feature to the layer // feature to the layer
try *obstacleFeature = new QgsLabelFeature( lbl->strId(), lbl, QSizeF( 0, 0 ) );
{ ( *obstacleFeature )->setIsObstacle( true );
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;
}
} }


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


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




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


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

0 comments on commit aa85cf3

Please sign in to comment.