Skip to content
Permalink
Browse files

Merge pull request #9103 from m-kuhn/constFeatureInLabeling

Const correctnes for QgsFeature in labeling
  • Loading branch information
m-kuhn committed Feb 6, 2019
2 parents 14db137 + d288560 commit 7f0ab8b9ffb79be228c9ea54552f5a7dd8ff1b75
@@ -386,9 +386,13 @@ Returns the QgsExpression for this label settings. May be None if isExpression i

double zIndex;

void calculateLabelSize( const QFontMetricsF *fm, QString text, double &labelX, double &labelY, QgsFeature *f = 0, QgsRenderContext *context = 0 );
void calculateLabelSize( const QFontMetricsF *fm, const QString &text, double &labelX, double &labelY, const QgsFeature *f = 0, QgsRenderContext *context = 0 );
%Docstring
Calculates the space required to render the provided ``text`` in map units.
Results will be written to ``labelX`` and ``labelY``.
%End

void registerFeature( QgsFeature &f, QgsRenderContext &context );
void registerFeature( const QgsFeature &f, QgsRenderContext &context );

%Docstring
Register a feature for labeling.
@@ -457,7 +461,7 @@ Sets the label text formatting settings, e.g., font settings, buffer settings, e
.. versionadded:: 3.0
%End

QgsFeature *mCurFeat;
const QgsFeature *mCurFeat;
QgsFields mCurFields;
int fieldIndex;
const QgsMapToPixel *xform;
@@ -469,7 +473,6 @@ Sets the label text formatting settings, e.g., font settings, buffer settings, e
int mFeaturesToLabel;
int mFeatsSendingToPal;
int mFeatsRegPal;

};

class QgsLabelCandidate
@@ -34,7 +34,7 @@ void QgsDxfLabelProvider::drawLabel( QgsRenderContext &context, pal::LabelPositi
mDxfExport->drawLabel( layerId(), context, label, mSettings );
}

void QgsDxfLabelProvider::registerDxfFeature( QgsFeature &feature, QgsRenderContext &context, const QString &dxfLayerName )
void QgsDxfLabelProvider::registerDxfFeature( const QgsFeature &feature, QgsRenderContext &context, const QString &dxfLayerName )
{
registerFeature( feature, context );
mDxfExport->registerDxfLayer( layerId(), feature.id(), dxfLayerName );
@@ -54,7 +54,7 @@ class QgsDxfLabelProvider : public QgsVectorLayerLabelProvider
* \param context render context
* \param dxfLayerName name of dxf layer
*/
void registerDxfFeature( QgsFeature &feature, QgsRenderContext &context, const QString &dxfLayerName );
void registerDxfFeature( const QgsFeature &feature, QgsRenderContext &context, const QString &dxfLayerName );

protected:
//! pointer to parent DXF export where this instance is used
@@ -1038,13 +1038,15 @@ bool QgsPalLayerSettings::checkMinimumSizeMM( const QgsRenderContext &ct, const
return QgsPalLabeling::checkMinimumSizeMM( ct, geom, minSize );
}

void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF *fm, QString text, double &labelX, double &labelY, QgsFeature *f, QgsRenderContext *context )
void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF *fm, const QString &text, double &labelX, double &labelY, const QgsFeature *f, QgsRenderContext *context )
{
if ( !fm || !f )
{
return;
}

QString textCopy( text );

//try to keep < 2.12 API - handle no passed render context
std::unique_ptr< QgsRenderContext > scopedRc;
if ( !context )
@@ -1150,29 +1152,25 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF *fm, QString t

if ( placeDirSymb == QgsPalLayerSettings::SymbolLeftRight )
{
text.append( dirSym );
textCopy.append( dirSym );
}
else
{
text.prepend( dirSym + QStringLiteral( "\n" ) ); // SymbolAbove or SymbolBelow
textCopy.prepend( dirSym + QStringLiteral( "\n" ) ); // SymbolAbove or SymbolBelow
}
}

double w = 0.0, h = 0.0;
QStringList multiLineSplit = QgsPalLabeling::splitToLines( text, wrapchr, evalAutoWrapLength, useMaxLineLengthForAutoWrap );
const QStringList multiLineSplit = QgsPalLabeling::splitToLines( textCopy, wrapchr, evalAutoWrapLength, useMaxLineLengthForAutoWrap );
int lines = multiLineSplit.size();

double labelHeight = fm->ascent() + fm->descent(); // ignore +1 for baseline

h += fm->height() + static_cast< double >( ( lines - 1 ) * labelHeight * multilineH );

for ( int i = 0; i < lines; ++i )
for ( const QString &line : multiLineSplit )
{
double width = fm->width( multiLineSplit.at( i ) );
if ( width > w )
{
w = width;
}
w = std::max( w, fm->width( line ) );
}

#if 0 // XXX strk
@@ -1186,7 +1184,7 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF *fm, QString t
#endif
}

void QgsPalLayerSettings::registerFeature( QgsFeature &f, QgsRenderContext &context, QgsLabelFeature **labelFeature, QgsGeometry obstacleGeometry )
void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext &context, QgsLabelFeature **labelFeature, QgsGeometry obstacleGeometry )
{
// either used in QgsPalLabeling (palLayer is set) or in QgsLabelingEngine (labelFeature is set)
Q_ASSERT( labelFeature );
@@ -1995,7 +1993,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature &f, QgsRenderContext &cont
lf->setDataDefinedValues( dataDefinedValues );
}

void QgsPalLayerSettings::registerObstacleFeature( QgsFeature &f, QgsRenderContext &context, QgsLabelFeature **obstacleFeature, const QgsGeometry &obstacleGeometry )
void QgsPalLayerSettings::registerObstacleFeature( const QgsFeature &f, QgsRenderContext &context, QgsLabelFeature **obstacleFeature, const QgsGeometry &obstacleGeometry )
{
mCurFeat = &f;

@@ -750,8 +750,11 @@ class CORE_EXPORT QgsPalLayerSettings
//! Z-Index of label, where labels with a higher z-index are rendered on top of labels with a lower z-index
double zIndex;

// called from register feature hook
void calculateLabelSize( const QFontMetricsF *fm, QString text, double &labelX, double &labelY, QgsFeature *f = nullptr, QgsRenderContext *context = nullptr );
/**
* Calculates the space required to render the provided \a text in map units.
* Results will be written to \a labelX and \a labelY.
*/
void calculateLabelSize( const QFontMetricsF *fm, const QString &text, double &labelX, double &labelY, const QgsFeature *f = nullptr, QgsRenderContext *context = nullptr );

/**
* Register a feature for labeling.
@@ -766,7 +769,7 @@ class CORE_EXPORT QgsPalLayerSettings
* the feature's original geometry will be used as an obstacle for labels. Not available
* in Python bindings.
*/
void registerFeature( QgsFeature &f, QgsRenderContext &context,
void registerFeature( const QgsFeature &f, QgsRenderContext &context,
QgsLabelFeature **labelFeature SIP_PYARGREMOVE = nullptr,
QgsGeometry obstacleGeometry SIP_PYARGREMOVE = QgsGeometry() );

@@ -823,7 +826,7 @@ class CORE_EXPORT QgsPalLayerSettings
void setFormat( const QgsTextFormat &format ) { mFormat = format; }

// temporary stuff: set when layer gets prepared or labeled
QgsFeature *mCurFeat = nullptr;
const QgsFeature *mCurFeat = nullptr;
QgsFields mCurFields;
int fieldIndex;
const QgsMapToPixel *xform = nullptr;
@@ -835,7 +838,6 @@ class CORE_EXPORT QgsPalLayerSettings
int mFeaturesToLabel = 0; // total features that will probably be labeled, may be less (figured before PAL)
int mFeatsSendingToPal = 0; // total features tested for sending into PAL (relative to maxNumLabels)
int mFeatsRegPal = 0; // number of features registered in PAL, when using limitNumLabels

private:

friend class QgsVectorLayer; // to allow calling readFromLayerCustomProperties()
@@ -899,7 +901,7 @@ class CORE_EXPORT QgsPalLayerSettings
/**
* Registers a feature as an obstacle only (no label rendered)
*/
void registerObstacleFeature( QgsFeature &f, QgsRenderContext &context, QgsLabelFeature **obstacleFeature, const QgsGeometry &obstacleGeometry = QgsGeometry() );
void registerObstacleFeature( const QgsFeature &f, QgsRenderContext &context, QgsLabelFeature **obstacleFeature, const QgsGeometry &obstacleGeometry = QgsGeometry() );

QMap<Property, QVariant> dataDefinedValues;

@@ -37,7 +37,7 @@ bool QgsRuleBasedLabelProvider::prepare( const QgsRenderContext &context, QSet<Q
return true;
}

void QgsRuleBasedLabelProvider::registerFeature( QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry )
void QgsRuleBasedLabelProvider::registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry )
{
// will register the feature to relevant sub-providers
mRules->rootRule()->registerFeature( feature, context, mSubProviders, obstacleGeometry );
@@ -316,7 +316,7 @@ void QgsRuleBasedLabeling::Rule::prepare( const QgsRenderContext &context, QSet<
}
}

QgsRuleBasedLabeling::Rule::RegisterResult QgsRuleBasedLabeling::Rule::registerFeature( QgsFeature &feature, QgsRenderContext &context, QgsRuleBasedLabeling::RuleToProviderMap &subProviders, const QgsGeometry &obstacleGeometry )
QgsRuleBasedLabeling::Rule::RegisterResult QgsRuleBasedLabeling::Rule::registerFeature( const QgsFeature &feature, QgsRenderContext &context, QgsRuleBasedLabeling::RuleToProviderMap &subProviders, const QgsGeometry &obstacleGeometry )
{
if ( !isFilterOK( feature, context )
|| !isScaleOK( context.rendererScale() ) )
@@ -363,7 +363,7 @@ QgsRuleBasedLabeling::Rule::RegisterResult QgsRuleBasedLabeling::Rule::registerF
return Filtered;
}

bool QgsRuleBasedLabeling::Rule::isFilterOK( QgsFeature &f, QgsRenderContext &context ) const
bool QgsRuleBasedLabeling::Rule::isFilterOK( const QgsFeature &f, QgsRenderContext &context ) const
{
if ( ! mFilter || mElseRule )
return true;
@@ -282,7 +282,7 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
* register individual features
* \note not available in Python bindings
*/
RegisterResult registerFeature( QgsFeature &feature, QgsRenderContext &context, RuleToProviderMap &subProviders, const QgsGeometry &obstacleGeometry = QgsGeometry() ) SIP_SKIP;
RegisterResult registerFeature( const QgsFeature &feature, QgsRenderContext &context, RuleToProviderMap &subProviders, const QgsGeometry &obstacleGeometry = QgsGeometry() ) SIP_SKIP;

/**
* Returns true if this rule or any of its children requires advanced composition effects
@@ -302,7 +302,7 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
* \param context The context in which the rendering happens
* \returns True if the feature shall be rendered
*/
bool isFilterOK( QgsFeature &f, QgsRenderContext &context ) const;
bool isFilterOK( const QgsFeature &f, QgsRenderContext &context ) const;

/**
* Check if this rule applies for a given \a scale.
@@ -395,7 +395,7 @@ class CORE_EXPORT QgsRuleBasedLabelProvider : public QgsVectorLayerLabelProvider

bool prepare( const QgsRenderContext &context, QSet<QString> &attributeNames ) override;

void registerFeature( QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry = QgsGeometry() ) override;
void registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry = QgsGeometry() ) override;

//! create a label provider
virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings );
@@ -258,9 +258,10 @@ QList<QgsLabelFeature *> QgsVectorLayerLabelProvider::labelFeatures( QgsRenderCo
return mLabels;
}

void QgsVectorLayerLabelProvider::registerFeature( QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry )
void QgsVectorLayerLabelProvider::registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry )
{
QgsLabelFeature *label = nullptr;

mSettings.registerFeature( feature, context, &label, obstacleGeometry );
if ( label )
mLabels << label;
@@ -74,7 +74,7 @@ class CORE_EXPORT QgsVectorLayerLabelProvider : public QgsAbstractLabelProvider
* symbol, the obstacle geometry should represent the bounds of the offset symbol). If not set,
* the feature's original geometry will be used as an obstacle for labels.
*/
virtual void registerFeature( QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry = QgsGeometry() );
virtual void registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry = QgsGeometry() );

/**
* Returns the geometry for a point feature which should be used as an obstacle for labels. This

0 comments on commit 7f0ab8b

Please sign in to comment.
You can’t perform that action at this time.